]> git.ipfire.org Git - thirdparty/squid.git/blob - src/tests/testNull.cc
5f4ce2ab8f11d48aaa42f7190ab1fb4f8c54ef19
[thirdparty/squid.git] / src / tests / testNull.cc
1 #define SQUID_UNIT_TEST 1
2 #include "squid.h"
3 #include "testNull.h"
4 #include "Store.h"
5 #include "SwapDir.h"
6 #include "DiskIO/DiskIOModule.h"
7 #include "fs/null/store_null.h"
8 #include "Mem.h"
9 #include "MemObject.h"
10 #include "HttpHeader.h"
11 #include "HttpReply.h"
12 #include "StoreFileSystem.h"
13 #include "testStoreSupport.h"
14
15 #if HAVE_STDEXCEPT
16 #include <stdexcept>
17 #endif
18
19 #define TESTDIR "testNull__testNullSearch"
20
21 CPPUNIT_TEST_SUITE_REGISTRATION( testNull );
22
23 typedef RefCount<NullSwapDir> SwapDirPointer;
24 extern REMOVALPOLICYCREATE createRemovalPolicy_lru;
25
26 static void
27 addSwapDir(SwapDirPointer aStore)
28 {
29 allocate_new_swapdir(&Config.cacheSwap);
30 Config.cacheSwap.swapDirs[Config.cacheSwap.n_configured] = aStore.getRaw();
31 ++Config.cacheSwap.n_configured;
32 }
33
34 void
35 testNull::commonInit()
36 {
37 static bool inited = false;
38
39 if (inited)
40 return;
41
42 StoreFileSystem::SetupAllFs();
43
44 Config.Store.avgObjectSize = 1024;
45
46 Config.Store.objectsPerBucket = 20;
47
48 Config.Store.maxObjectSize = 2048;
49
50 Config.store_dir_select_algorithm = xstrdup("round-robin");
51
52 Config.replPolicy = new RemovalPolicySettings;
53
54 Config.replPolicy->type = xstrdup ("lru");
55
56 Config.replPolicy->args = NULL;
57
58 /* garh garh */
59 storeReplAdd("lru", createRemovalPolicy_lru);
60
61 visible_appname_string = xstrdup(APP_FULLNAME);
62
63 Mem::Init();
64
65 comm_init();
66
67 httpHeaderInitModule(); /* must go before any header processing (e.g. the one in errorInitialize) */
68
69 httpReplyInitModule(); /* must go before accepting replies */
70
71 mem_policy = createRemovalPolicy(Config.replPolicy);
72
73 inited = true;
74 }
75
76 void
77 testNull::testNullCreate()
78 {
79 StorePointer aRoot (new StoreController);
80 Store::Root(aRoot);
81 SwapDirPointer aStore (new NullSwapDir());
82 addSwapDir(aStore);
83
84 commonInit();
85
86 char *path=xstrdup(TESTDIR);
87 char *config_line=xstrdup("foo");
88 strtok(config_line, w_space);
89 aStore->parse(0, path);
90 safe_free(path);
91 safe_free(config_line);
92
93 /* ok, ready to create */
94 aStore->create();
95
96 free_cachedir(&Config.cacheSwap);
97 Store::Root(NULL);
98
99 /* todo: here we should test a dirty rebuild */
100
101 // safe_free(Config.replPolicy->type);
102 // delete Config.replPolicy;
103 }
104
105 /* TODO make this a cbdata class */
106
107 static bool cbcalled;
108
109 static void
110 searchCallback(void *cbdata)
111 {
112 cbcalled = true;
113 }
114
115 void
116 testNull::testNullSearch()
117 {
118 /* test sequence
119 * make a valid working ufs swapdir
120 * put two entries in it and sync logs
121 * search the ufs dir
122 * check the entries we find are what we want
123 */
124 StorePointer aRoot (new StoreController);
125 Store::Root(aRoot);
126 SwapDirPointer aStore (new NullSwapDir());
127 addSwapDir(aStore);
128
129 commonInit();
130
131 char *path=xstrdup(TESTDIR);
132 char *config_line=xstrdup("foo");
133 strtok(config_line, w_space);
134 aStore->parse(0, path);
135 safe_free(path);
136 safe_free(config_line);
137
138 /* ok, ready to create */
139 aStore->create();
140
141 /* ok, ready to use */
142 Store::Root().init();
143
144 /* rebuild is a scheduled event */
145 StockEventLoop loop;
146
147 /* our swapdir must be scheduled to rebuild - though it does not
148 * make sense to rebuild Null stores at all. store_dirs_rebuilding
149 * is initialized to _1_ and adding our swapdir makes it 2.
150 */
151 CPPUNIT_ASSERT_EQUAL(2, StoreController::store_dirs_rebuilding);
152
153 loop.run();
154
155 /* nothing left to rebuild */
156 CPPUNIT_ASSERT_EQUAL(1, StoreController::store_dirs_rebuilding);
157
158 /* add an entry */
159 {
160 /* Create "vary" base object */
161 request_flags flags;
162 flags.cachable = 1;
163 StoreEntry *pe = storeCreateEntry("dummy url", "dummy log url", flags, METHOD_GET);
164 /* We are allowed to do this typecast */
165 HttpReply *rep = (HttpReply *) pe->getReply(); // bypass const
166 rep->setHeaders(HTTP_OK, "dummy test object", "x-squid-internal/test", -1, -1, squid_curtime + 100000);
167
168 pe->setPublicKey();
169
170 pe->buffer();
171 /* TODO: remove this when the metadata is separated */
172 {
173 Packer p;
174 packerToStoreInit(&p, pe);
175 pe->getReply()->packHeadersInto(&p);
176 packerClean(&p);
177 }
178
179 pe->flush();
180 pe->timestampsSet();
181 pe->complete();
182 pe->swapOut();
183 /* Null does not accept store entries */
184 CPPUNIT_ASSERT(pe->swap_dirn == -1);
185 pe->unlock();
186 }
187
188 storeDirWriteCleanLogs(0);
189
190 /* here we cheat: we know that UFSSwapDirs search off disk. If we did an init call to a new
191 * swapdir instance, we'd not be testing a clean build.
192 */
193 StoreSearchPointer search = aStore->search (NULL, NULL); /* search for everything in the store */
194
195 /* nothing should be available */
196 CPPUNIT_ASSERT(search->error() == false);
197 CPPUNIT_ASSERT(search->isDone() == true);
198 CPPUNIT_ASSERT(search->currentItem() == NULL);
199 CPPUNIT_ASSERT(search->next() == false);
200
201 /* trigger a callback */
202 cbcalled = false;
203 search->next(searchCallback, NULL);
204 CPPUNIT_ASSERT(cbcalled == true);
205
206 /* still nothing */
207 CPPUNIT_ASSERT(search->error() == false);
208 CPPUNIT_ASSERT(search->isDone() == true);
209 CPPUNIT_ASSERT(search->currentItem() == NULL);
210 CPPUNIT_ASSERT(search->next() == false);
211
212 free_cachedir(&Config.cacheSwap);
213 Store::Root(NULL);
214
215 //TODO: do this once, or each time. safe_free(Config.replPolicy->type);
216 // delete Config.replPolicy;
217 }