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