]> git.ipfire.org Git - thirdparty/squid.git/blame - src/tests/testUfs.cc
Docs: Copyright updates for 2018 (#114)
[thirdparty/squid.git] / src / tests / testUfs.cc
CommitLineData
4e0938ef 1/*
5b74111a 2 * Copyright (C) 1996-2018 The Squid Software Foundation and contributors
4e0938ef
AJ
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
f7f3304a 9#include "squid.h"
c8f4eac4 10#include "DiskIO/DiskIOModule.h"
582c2af2
FC
11#include "fs/ufs/UFSSwapDir.h"
12#include "globals.h"
c8f4eac4 13#include "HttpHeader.h"
14#include "HttpReply.h"
58373ff8 15#include "MemObject.h"
f206b652 16#include "RequestFlags.h"
4d5904f7 17#include "SquidConfig.h"
58373ff8 18#include "Store.h"
2745fea5 19#include "store/Disks.h"
582c2af2
FC
20#include "testStoreSupport.h"
21#include "testUfs.h"
7f861c77
AJ
22#include "unitTestMain.h"
23
27e059d4 24#include <stdexcept>
27e059d4 25
0795b38d 26#define TESTDIR "testUfs_Store"
2e050051 27
c8f4eac4 28CPPUNIT_TEST_SUITE_REGISTRATION( testUfs );
29
2745fea5 30typedef RefCount<Fs::Ufs::UFSSwapDir> MySwapDirPointer;
f53969cc 31extern REMOVALPOLICYCREATE createRemovalPolicy_lru; /* XXX fails with --enable-removal-policies=heap */
c8f4eac4 32
33static void
2745fea5 34addSwapDir(MySwapDirPointer aStore)
c8f4eac4 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
43static bool cbcalled;
44
45static void
46searchCallback(void *cbdata)
47{
48 cbcalled = true;
49}
50
51void
b7717b61 52testUfs::commonInit()
c8f4eac4 53{
b7717b61 54 static bool inited = false;
c8f4eac4 55
b7717b61 56 if (inited)
57 return;
c8f4eac4 58
59 Config.Store.avgObjectSize = 1024;
c8f4eac4 60 Config.Store.objectsPerBucket = 20;
c8f4eac4 61 Config.Store.maxObjectSize = 2048;
62
63 Config.store_dir_select_algorithm = xstrdup("round-robin");
64
65 Config.replPolicy = new RemovalPolicySettings;
86c63190 66 Config.replPolicy->type = xstrdup("lru");
c8f4eac4 67
c50b35b5
AJ
68 Config.memShared.defaultTo(false);
69
c8f4eac4 70 /* garh garh */
71 storeReplAdd("lru", createRemovalPolicy_lru);
72
73 Mem::Init();
74
c8f4eac4 75 comm_init();
76
f53969cc 77 httpHeaderInitModule(); /* must go before any header processing (e.g. the one in errorInitialize) */
c8f4eac4 78
b7717b61 79 inited = true;
80}
81
82void
83testUfs::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
2745fea5 95 Store::Init();
b7717b61 96
2745fea5 97 MySwapDirPointer aStore (new Fs::Ufs::UFSSwapDir("ufs", "Blocking"));
b7717b61 98
58373ff8 99 aStore->IO = new Fs::Ufs::UFSStrategy(DiskIOModule::Find("Blocking")->createStrategy());
b7717b61 100
101 addSwapDir(aStore);
102
103 commonInit();
c8f4eac4 104 mem_policy = createRemovalPolicy(Config.replPolicy);
105
2e050051 106 char *path=xstrdup(TESTDIR);
c8f4eac4 107
2eceb328 108 char *config_line=xstrdup("100 1 1");
c8f4eac4 109
474dfa84 110 visible_appname_string = xstrdup(PACKAGE "/" VERSION);
111
2eceb328 112 ConfigParser::SetCfgLine(config_line);
c8f4eac4 113
114 aStore->parse(0, path);
b51ec8c8 115 store_maxobjsize = 1024*1024*2;
c8f4eac4 116
117 safe_free(path);
118
119 safe_free(config_line);
120
121 /* ok, ready to create */
122 aStore->create();
123
8ff3fa2e 124 /* ok, ready to use - inits store & hash too */
c8f4eac4 125 Store::Root().init();
126
bef81ea5 127 /* our swapdir must be scheduled to rebuild */
1f8dc0fb 128 CPPUNIT_ASSERT_EQUAL(2, StoreController::store_dirs_rebuilding);
bef81ea5 129
8ff3fa2e 130 /* rebuild is a scheduled event */
131 StockEventLoop loop;
132
02c267b0 133 while (StoreController::store_dirs_rebuilding)
bef81ea5 134 loop.runOnce();
c8f4eac4 135
bef81ea5 136 /* cannot use loop.run(); as the loop will never idle: the store-dir
26ac0430 137 * clean() scheduled event prevents it
bef81ea5 138 */
c8f4eac4 139
bef81ea5 140 /* nothing left to rebuild */
02c267b0 141 CPPUNIT_ASSERT_EQUAL(0, StoreController::store_dirs_rebuilding);
c8f4eac4 142
143 /* add an entry */
144 {
145 /* Create "vary" base object */
f206b652 146 RequestFlags flags;
e857372a 147 flags.cachable = true;
c2a7cefd 148 StoreEntry *pe = storeCreateEntry("dummy url", "dummy log url", flags, Http::METHOD_GET);
f53969cc 149 HttpReply *rep = (HttpReply *) pe->getReply(); // bypass const
955394ce 150 rep->setHeaders(Http::scOkay, "dummy test object", "x-squid-internal/test", 0, -1, squid_curtime + 100000);
c8f4eac4 151
d88e3c49 152 pe->setPublicKey();
c8f4eac4 153
3900307b 154 pe->buffer();
15179984 155 pe->getReply()->packHeadersInto(pe);
3900307b 156 pe->flush();
157 pe->timestampsSet();
c8f4eac4 158 pe->complete();
c07cbbf4 159 pe->swapOut();
5d1dff27
FC
160 CPPUNIT_ASSERT_EQUAL(0, pe->swap_dirn);
161 CPPUNIT_ASSERT_EQUAL(0, pe->swap_filen);
acc5dc4c 162 pe->unlock("testUfs::testUfsSearch vary");
c8f4eac4 163 }
164
165 storeDirWriteCleanLogs(0);
166
167 /* here we cheat: we know that UFSSwapDirs search off disk. If we did an init call to a new
168 * swapdir instance, we'd not be testing a clean build.
169 */
2745fea5 170 StoreSearchPointer search = Store::Root().search(); /* search for everything in the store */
c8f4eac4 171
172 /* nothing should be immediately available */
173#if 0
174
5d1dff27 175 CPPUNIT_ASSERT_EQUAL(false, search->next());
c8f4eac4 176#endif
177
5d1dff27
FC
178 CPPUNIT_ASSERT_EQUAL(false, search->error());
179 CPPUNIT_ASSERT_EQUAL(false, search->isDone());
180 CPPUNIT_ASSERT_EQUAL(static_cast<StoreEntry *>(NULL), search->currentItem());
c8f4eac4 181
182 /* trigger a callback */
183 cbcalled = false;
184 search->next(searchCallback, NULL);
5d1dff27 185 CPPUNIT_ASSERT_EQUAL(true, cbcalled);
c8f4eac4 186
187 /* we should have access to a entry now, that matches the entry we had before */
5d1dff27
FC
188 //CPPUNIT_ASSERT_EQUAL(false, search->next());
189 CPPUNIT_ASSERT_EQUAL(false, search->error());
190 CPPUNIT_ASSERT_EQUAL(false, search->isDone());
c8f4eac4 191 CPPUNIT_ASSERT(search->currentItem() != NULL);
192
193 /* trigger another callback */
194 cbcalled = false;
195 search->next(searchCallback, NULL);
5d1dff27 196 CPPUNIT_ASSERT_EQUAL(true, cbcalled);
c8f4eac4 197
198 /* now we should have no error, we should have finished and have no current item */
5d1dff27
FC
199 //CPPUNIT_ASSERT_EQUAL(false, search->next());
200 CPPUNIT_ASSERT_EQUAL(false, search->error());
201 CPPUNIT_ASSERT_EQUAL(true, search->isDone());
202 CPPUNIT_ASSERT_EQUAL(static_cast<StoreEntry *>(NULL), search->currentItem());
c8f4eac4 203
2745fea5 204 Store::FreeMemory();
d5d5493b 205
c8f4eac4 206 free_cachedir(&Config.cacheSwap);
207
208 /* todo: here we should test a dirty rebuild */
209
b7717b61 210 safe_free(Config.replPolicy->type);
211 delete Config.replPolicy;
212
213 if (0 > system ("rm -rf " TESTDIR))
214 throw std::runtime_error("Failed to clean test work directory");
215}
216
26ac0430 217/* The UFS store should always configure an IO engine even if none is
b7717b61 218 * supplied on the configuration line.
219 */
220void
221testUfs::testUfsDefaultEngine()
222{
223 /* boring common test boilerplate */
224 if (0 > system ("rm -rf " TESTDIR))
225 throw std::runtime_error("Failed to clean test work directory");
226
fe36168c 227 // This assertion may fail if previous test cases fail.
228 // Apparently, CPPUNIT_ASSERT* failure may prevent destructors of local
229 // objects such as "StorePointer aRoot" from being called.
230 CPPUNIT_ASSERT(!store_table); // or StoreHashIndex ctor will abort below
231
2745fea5
AR
232 Store::Init();
233 MySwapDirPointer aStore (new Fs::Ufs::UFSSwapDir("ufs", "Blocking"));
b7717b61 234 addSwapDir(aStore);
235 commonInit();
236 Config.replPolicy = new RemovalPolicySettings;
86c63190 237 Config.replPolicy->type = xstrdup("lru");
b7717b61 238 mem_policy = createRemovalPolicy(Config.replPolicy);
239
240 char *path=xstrdup(TESTDIR);
2eceb328
CT
241 char *config_line=xstrdup("100 1 1");
242 ConfigParser::SetCfgLine(config_line);
b7717b61 243 aStore->parse(0, path);
244 safe_free(path);
245 safe_free(config_line);
246 CPPUNIT_ASSERT(aStore->IO->io != NULL);
247
2745fea5 248 Store::FreeMemory();
d5d5493b 249 free_cachedir(&Config.cacheSwap);
c8f4eac4 250 safe_free(Config.replPolicy->type);
251 delete Config.replPolicy;
252
2e050051 253 if (0 > system ("rm -rf " TESTDIR))
c8f4eac4 254 throw std::runtime_error("Failed to clean test work directory");
255}
f53969cc 256