]> git.ipfire.org Git - thirdparty/squid.git/blob - src/tests/testUfs.cc
Make packerClean() the destructor actions for Packer class
[thirdparty/squid.git] / src / tests / testUfs.cc
1 /*
2 * Copyright (C) 1996-2015 The Squid Software Foundation and contributors
3 *
4 * Squid software is distributed under GPLv2+ license and includes
5 * contributions from numerous individuals and organizations.
6 * Please see the COPYING and CONTRIBUTORS files for details.
7 */
8
9 #include "squid.h"
10 #include "DiskIO/DiskIOModule.h"
11 #include "fs/ufs/UFSSwapDir.h"
12 #include "globals.h"
13 #include "HttpHeader.h"
14 #include "HttpReply.h"
15 #include "MemObject.h"
16 #include "RequestFlags.h"
17 #include "SquidConfig.h"
18 #include "Store.h"
19 #include "SwapDir.h"
20 #include "testStoreSupport.h"
21 #include "testUfs.h"
22 #include "unitTestMain.h"
23
24 #include <stdexcept>
25
26 #define TESTDIR "testUfs_Store"
27
28 CPPUNIT_TEST_SUITE_REGISTRATION( testUfs );
29
30 typedef RefCount<Fs::Ufs::UFSSwapDir> SwapDirPointer;
31 extern REMOVALPOLICYCREATE createRemovalPolicy_lru; /* XXX fails with --enable-removal-policies=heap */
32
33 static void
34 addSwapDir(SwapDirPointer aStore)
35 {
36 allocate_new_swapdir(&Config.cacheSwap);
37 Config.cacheSwap.swapDirs[Config.cacheSwap.n_configured] = aStore.getRaw();
38 ++Config.cacheSwap.n_configured;
39 }
40
41 /* TODO make this a cbdata class */
42
43 static bool cbcalled;
44
45 static void
46 searchCallback(void *cbdata)
47 {
48 cbcalled = true;
49 }
50
51 void
52 testUfs::commonInit()
53 {
54 static bool inited = false;
55
56 if (inited)
57 return;
58
59 Config.Store.avgObjectSize = 1024;
60 Config.Store.objectsPerBucket = 20;
61 Config.Store.maxObjectSize = 2048;
62
63 Config.store_dir_select_algorithm = xstrdup("round-robin");
64
65 Config.replPolicy = new RemovalPolicySettings;
66 Config.replPolicy->type = xstrdup("lru");
67
68 /* garh garh */
69 storeReplAdd("lru", createRemovalPolicy_lru);
70
71 Mem::Init();
72
73 comm_init();
74
75 httpHeaderInitModule(); /* must go before any header processing (e.g. the one in errorInitialize) */
76
77 httpReplyInitModule(); /* must go before accepting replies */
78
79 inited = true;
80 }
81
82 void
83 testUfs::testUfsSearch()
84 {
85 /* test sequence
86 * make a valid working ufs swapdir
87 * put two entries in it and sync logs
88 * search the ufs dir
89 * check the entries we find are what we want
90 */
91
92 if (0 > system ("rm -rf " TESTDIR))
93 throw std::runtime_error("Failed to clean test work directory");
94
95 Store::Root(new StoreController);
96
97 SwapDirPointer aStore (new Fs::Ufs::UFSSwapDir("ufs", "Blocking"));
98
99 aStore->IO = new Fs::Ufs::UFSStrategy(DiskIOModule::Find("Blocking")->createStrategy());
100
101 addSwapDir(aStore);
102
103 commonInit();
104 mem_policy = createRemovalPolicy(Config.replPolicy);
105
106 char *path=xstrdup(TESTDIR);
107
108 char *config_line=xstrdup("100 1 1");
109
110 visible_appname_string = xstrdup(PACKAGE "/" VERSION);
111
112 ConfigParser::SetCfgLine(config_line);
113
114 aStore->parse(0, path);
115 store_maxobjsize = 1024*1024*2;
116
117 safe_free(path);
118
119 safe_free(config_line);
120
121 /* ok, ready to create */
122 aStore->create();
123
124 /* ok, ready to use - inits store & hash too */
125 Store::Root().init();
126
127 /* our swapdir must be scheduled to rebuild */
128 CPPUNIT_ASSERT_EQUAL(2, StoreController::store_dirs_rebuilding);
129
130 /* rebuild is a scheduled event */
131 StockEventLoop loop;
132
133 while (StoreController::store_dirs_rebuilding)
134 loop.runOnce();
135
136 /* cannot use loop.run(); as the loop will never idle: the store-dir
137 * clean() scheduled event prevents it
138 */
139
140 /* nothing left to rebuild */
141 CPPUNIT_ASSERT_EQUAL(0, StoreController::store_dirs_rebuilding);
142
143 /* add an entry */
144 {
145 /* Create "vary" base object */
146 RequestFlags flags;
147 flags.cachable = true;
148 StoreEntry *pe = storeCreateEntry("dummy url", "dummy log url", flags, Http::METHOD_GET);
149 HttpReply *rep = (HttpReply *) pe->getReply(); // bypass const
150 rep->setHeaders(Http::scOkay, "dummy test object", "x-squid-internal/test", 0, -1, squid_curtime + 100000);
151
152 pe->setPublicKey();
153
154 pe->buffer();
155 /* TODO: remove this when the metadata is separated */
156 {
157 Packer p;
158 packerToStoreInit(&p, pe);
159 pe->getReply()->packHeadersInto(&p);
160 }
161
162 pe->flush();
163 pe->timestampsSet();
164 pe->complete();
165 pe->swapOut();
166 CPPUNIT_ASSERT_EQUAL(0, pe->swap_dirn);
167 CPPUNIT_ASSERT_EQUAL(0, pe->swap_filen);
168 pe->unlock("testUfs::testUfsSearch vary");
169 }
170
171 storeDirWriteCleanLogs(0);
172
173 /* here we cheat: we know that UFSSwapDirs search off disk. If we did an init call to a new
174 * swapdir instance, we'd not be testing a clean build.
175 */
176 StoreSearchPointer search = aStore->search (NULL, NULL); /* search for everything in the store */
177
178 /* nothing should be immediately available */
179 #if 0
180
181 CPPUNIT_ASSERT_EQUAL(false, search->next());
182 #endif
183
184 CPPUNIT_ASSERT_EQUAL(false, search->error());
185 CPPUNIT_ASSERT_EQUAL(false, search->isDone());
186 CPPUNIT_ASSERT_EQUAL(static_cast<StoreEntry *>(NULL), search->currentItem());
187
188 /* trigger a callback */
189 cbcalled = false;
190 search->next(searchCallback, NULL);
191 CPPUNIT_ASSERT_EQUAL(true, cbcalled);
192
193 /* we should have access to a entry now, that matches the entry we had before */
194 //CPPUNIT_ASSERT_EQUAL(false, search->next());
195 CPPUNIT_ASSERT_EQUAL(false, search->error());
196 CPPUNIT_ASSERT_EQUAL(false, search->isDone());
197 CPPUNIT_ASSERT(search->currentItem() != NULL);
198
199 /* trigger another callback */
200 cbcalled = false;
201 search->next(searchCallback, NULL);
202 CPPUNIT_ASSERT_EQUAL(true, cbcalled);
203
204 /* now we should have no error, we should have finished and have no current item */
205 //CPPUNIT_ASSERT_EQUAL(false, search->next());
206 CPPUNIT_ASSERT_EQUAL(false, search->error());
207 CPPUNIT_ASSERT_EQUAL(true, search->isDone());
208 CPPUNIT_ASSERT_EQUAL(static_cast<StoreEntry *>(NULL), search->currentItem());
209
210 Store::Root(NULL);
211
212 free_cachedir(&Config.cacheSwap);
213
214 /* todo: here we should test a dirty rebuild */
215
216 safe_free(Config.replPolicy->type);
217 delete Config.replPolicy;
218
219 if (0 > system ("rm -rf " TESTDIR))
220 throw std::runtime_error("Failed to clean test work directory");
221 }
222
223 /* The UFS store should always configure an IO engine even if none is
224 * supplied on the configuration line.
225 */
226 void
227 testUfs::testUfsDefaultEngine()
228 {
229 /* boring common test boilerplate */
230 if (0 > system ("rm -rf " TESTDIR))
231 throw std::runtime_error("Failed to clean test work directory");
232
233 // This assertion may fail if previous test cases fail.
234 // Apparently, CPPUNIT_ASSERT* failure may prevent destructors of local
235 // objects such as "StorePointer aRoot" from being called.
236 CPPUNIT_ASSERT(!store_table); // or StoreHashIndex ctor will abort below
237
238 Store::Root(new StoreController);
239 SwapDirPointer aStore (new Fs::Ufs::UFSSwapDir("ufs", "Blocking"));
240 addSwapDir(aStore);
241 commonInit();
242 Config.replPolicy = new RemovalPolicySettings;
243 Config.replPolicy->type = xstrdup("lru");
244 mem_policy = createRemovalPolicy(Config.replPolicy);
245
246 char *path=xstrdup(TESTDIR);
247 char *config_line=xstrdup("100 1 1");
248 ConfigParser::SetCfgLine(config_line);
249 aStore->parse(0, path);
250 safe_free(path);
251 safe_free(config_line);
252 CPPUNIT_ASSERT(aStore->IO->io != NULL);
253
254 Store::Root(NULL);
255 free_cachedir(&Config.cacheSwap);
256 safe_free(Config.replPolicy->type);
257 delete Config.replPolicy;
258
259 if (0 > system ("rm -rf " TESTDIR))
260 throw std::runtime_error("Failed to clean test work directory");
261 }
262