]> git.ipfire.org Git - thirdparty/squid.git/blob - src/tests/testStoreController.cc
Simplified MSNT basic auth helper
[thirdparty/squid.git] / src / tests / testStoreController.cc
1 /*
2 * Copyright (C) 1996-2014 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 "MemObject.h"
11 #include "SquidConfig.h"
12 #include "SquidTime.h"
13 #include "Store.h"
14 #include "StoreSearch.h"
15 #include "SwapDir.h"
16 #include "testStoreController.h"
17 #include "TestSwapDir.h"
18
19 CPPUNIT_TEST_SUITE_REGISTRATION( testStoreController );
20
21 static void
22 addSwapDir(TestSwapDirPointer aStore)
23 {
24 allocate_new_swapdir(&Config.cacheSwap);
25 Config.cacheSwap.swapDirs[Config.cacheSwap.n_configured] = aStore.getRaw();
26 ++Config.cacheSwap.n_configured;
27 }
28
29 void
30 testStoreController::testStats()
31 {
32 StoreEntry *logEntry = new StoreEntry;
33 logEntry->makeMemObject();
34 logEntry->mem_obj->setUris("dummy_storeId", NULL, HttpRequestMethod());
35 logEntry->store_status = STORE_PENDING;
36 StorePointer aRoot (new StoreController);
37 Store::Root(aRoot);
38 TestSwapDirPointer aStore (new TestSwapDir);
39 TestSwapDirPointer aStore2 (new TestSwapDir);
40 addSwapDir(aStore);
41 addSwapDir(aStore2);
42 CPPUNIT_ASSERT_EQUAL(false, aStore->statsCalled);
43 CPPUNIT_ASSERT_EQUAL(false, aStore2->statsCalled);
44 Store::Stats(logEntry);
45 free_cachedir(&Config.cacheSwap);
46 CPPUNIT_ASSERT_EQUAL(true, aStore->statsCalled);
47 CPPUNIT_ASSERT_EQUAL(true, aStore2->statsCalled);
48 Store::Root(NULL);
49 }
50
51 static void
52 commonInit()
53 {
54 static bool inited = false;
55
56 if (inited)
57 return;
58
59 Mem::Init();
60
61 Config.Store.avgObjectSize = 1024;
62
63 Config.Store.objectsPerBucket = 20;
64
65 Config.Store.maxObjectSize = 2048;
66
67 Config.store_dir_select_algorithm = xstrdup("round-robin");
68 }
69
70 void
71 testStoreController::testMaxSize()
72 {
73 commonInit();
74 StoreEntry *logEntry = new StoreEntry;
75 logEntry->makeMemObject();
76 logEntry->mem_obj->setUris("dummy_storeId", NULL, HttpRequestMethod());
77 logEntry->store_status = STORE_PENDING;
78 StorePointer aRoot (new StoreController);
79 Store::Root(aRoot);
80 TestSwapDirPointer aStore (new TestSwapDir);
81 TestSwapDirPointer aStore2 (new TestSwapDir);
82 addSwapDir(aStore);
83 addSwapDir(aStore2);
84 CPPUNIT_ASSERT_EQUAL(static_cast<uint64_t>(6), Store::Root().maxSize());
85 free_cachedir(&Config.cacheSwap);
86 Store::Root(NULL);
87 }
88
89 static StoreEntry *
90 addedEntry(StorePointer hashStore,
91 StorePointer aStore,
92 String name,
93 String varySpec,
94 String varyKey
95
96 )
97 {
98 StoreEntry *e = new StoreEntry();
99 e->store_status = STORE_OK;
100 e->setMemStatus(NOT_IN_MEMORY);
101 e->swap_status = SWAPOUT_DONE; /* bogus haha */
102 e->swap_filen = 0; /* garh - lower level*/
103 e->swap_dirn = -1;
104
105 for (int i=0; i < Config.cacheSwap.n_configured; ++i) {
106 if (INDEXSD (i) == aStore.getRaw())
107 e->swap_dirn = i;
108 }
109
110 CPPUNIT_ASSERT (e->swap_dirn != -1);
111 e->swap_file_sz = 0; /* garh lower level */
112 e->lastref = squid_curtime;
113 e->timestamp = squid_curtime;
114 e->expires = squid_curtime;
115 e->lastmod = squid_curtime;
116 e->refcount = 1;
117 EBIT_CLR(e->flags, RELEASE_REQUEST);
118 EBIT_CLR(e->flags, KEY_PRIVATE);
119 e->ping_status = PING_NONE;
120 EBIT_CLR(e->flags, ENTRY_VALIDATED);
121 e->hashInsert((const cache_key *)name.termedBuf()); /* do it after we clear KEY_PRIVATE */
122 return e;
123 }
124
125 /* TODO make this a cbdata class */
126
127 static bool cbcalled;
128
129 static void
130 searchCallback(void *cbdata)
131 {
132 cbcalled = true;
133 }
134
135 void
136 testStoreController::testSearch()
137 {
138 commonInit();
139 StorePointer aRoot (new StoreController());
140 Store::Root(aRoot);
141 TestSwapDirPointer aStore (new TestSwapDir);
142 TestSwapDirPointer aStore2 (new TestSwapDir);
143 addSwapDir(aStore);
144 addSwapDir(aStore2);
145 Store::Root().init();
146 StoreEntry * entry1 = addedEntry (&Store::Root(), aStore.getRaw(), "name", NULL, NULL);
147 StoreEntry * entry2 = addedEntry (&Store::Root(), aStore2.getRaw(), "name2", NULL, NULL);
148 StoreSearchPointer search = aRoot->search (NULL, NULL); /* search for everything in the store */
149
150 /* nothing should be immediately available */
151 CPPUNIT_ASSERT_EQUAL(false, search->error());
152 CPPUNIT_ASSERT_EQUAL(false, search->isDone());
153 CPPUNIT_ASSERT_EQUAL(static_cast<StoreEntry *>(NULL), search->currentItem());
154 #if 0
155
156 CPPUNIT_ASSERT_EQUAL(false, search->next());
157 #endif
158
159 /* trigger a callback */
160 cbcalled = false;
161 search->next(searchCallback, NULL);
162 CPPUNIT_ASSERT_EQUAL(true, cbcalled);
163
164 /* we should have access to a entry now, that matches the entry we had before */
165 CPPUNIT_ASSERT_EQUAL(false, search->error());
166 CPPUNIT_ASSERT_EQUAL(false, search->isDone());
167 /* note the hash order is random - the test happens to be in a nice order */
168 CPPUNIT_ASSERT_EQUAL(entry1, search->currentItem());
169 //CPPUNIT_ASSERT_EQUAL(false, search->next());
170
171 /* trigger another callback */
172 cbcalled = false;
173 search->next(searchCallback, NULL);
174 CPPUNIT_ASSERT_EQUAL(true, cbcalled);
175
176 /* we should have access to a entry now, that matches the entry we had before */
177 CPPUNIT_ASSERT_EQUAL(false, search->error());
178 CPPUNIT_ASSERT_EQUAL(false, search->isDone());
179 CPPUNIT_ASSERT_EQUAL(entry2, search->currentItem());
180 //CPPUNIT_ASSERT_EQUAL(false, search->next());
181
182 /* trigger another callback */
183 cbcalled = false;
184 search->next(searchCallback, NULL);
185 CPPUNIT_ASSERT_EQUAL(true, cbcalled);
186
187 /* now we should have no error, we should have finished and have no current item */
188 CPPUNIT_ASSERT_EQUAL(false, search->error());
189 CPPUNIT_ASSERT_EQUAL(true, search->isDone());
190 CPPUNIT_ASSERT_EQUAL(static_cast<StoreEntry *>(NULL), search->currentItem());
191 //CPPUNIT_ASSERT_EQUAL(false, search->next());
192
193 Store::Root(NULL);
194 }
195