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