Squid Web Cache master
Loading...
Searching...
No Matches
testUfs.cc
Go to the documentation of this file.
1/*
2 * Copyright (C) 1996-2025 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 "compat/cppunit.h"
11#include "DiskIO/DiskIOModule.h"
12#include "fde.h"
13#include "fs/ufs/UFSSwapDir.h"
14#include "globals.h"
15#include "HttpHeader.h"
16#include "HttpReply.h"
17#include "MemObject.h"
18#include "RequestFlags.h"
19#include "SquidConfig.h"
20#include "Store.h"
21#include "store/Disks.h"
22#include "testStoreSupport.h"
23#include "unitTestMain.h"
24
25#include <stdexcept>
26
27#define TESTDIR "TestUfs_Store"
28
29/*
30 * test the store framework
31 */
32
33class TestUfs : public CPPUNIT_NS::TestFixture
34{
39
40public:
41protected:
42 void commonInit();
43 void testUfsSearch();
45};
47
49extern REMOVALPOLICYCREATE createRemovalPolicy_lru; /* XXX fails with --enable-removal-policies=heap */
50
51static void
58
59static bool cbcalled;
60
61static void
63{
64 cbcalled = true;
65}
66
67void
69{
70 static bool inited = false;
71
72 if (inited)
73 return;
74
78
80
82 Config.replPolicy->type = xstrdup("lru");
83
85
86 /* garh garh */
88
89 Mem::Init();
90
91 fde::Init();
92
93 comm_init();
94
95 httpHeaderInitModule(); /* must go before any header processing (e.g. the one in errorInitialize) */
96
97 inited = true;
98}
99
100void
102{
103 /* test sequence
104 * make a valid working ufs swapdir
105 * put two entries in it and sync logs
106 * search the ufs dir
107 * check the entries we find are what we want
108 */
109
110 if (0 > system ("rm -rf " TESTDIR))
111 throw std::runtime_error("Failed to clean test work directory");
112
113 MySwapDirPointer aStore (new Fs::Ufs::UFSSwapDir("ufs", "Blocking"));
114
115 aStore->IO = new Fs::Ufs::UFSStrategy(DiskIOModule::Find("Blocking")->createStrategy());
116
117 addSwapDir(aStore);
118
119 commonInit();
121
122 char *path=xstrdup(TESTDIR);
123
124 char *config_line=xstrdup("100 1 1");
125
127
128 ConfigParser::SetCfgLine(config_line);
129
130 aStore->parse(0, path);
131 store_maxobjsize = 1024*1024*2;
132
133 safe_free(path);
134
135 safe_free(config_line);
136
137 /* ok, ready to create */
138 aStore->create();
139
140 /* ok, ready to use - inits store & hash too */
141 Store::Root().init();
142
143 /* our swapdir must be scheduled to rebuild */
144 CPPUNIT_ASSERT_EQUAL(2, StoreController::store_dirs_rebuilding);
145
146 /* rebuild is a scheduled event */
147 StockEventLoop loop;
148
150 loop.runOnce();
151
152 /* cannot use loop.run(); as the loop will never idle: the store-dir
153 * clean() scheduled event prevents it
154 */
155
156 /* nothing left to rebuild */
157 CPPUNIT_ASSERT_EQUAL(0, StoreController::store_dirs_rebuilding);
158
159 /* add an entry */
160 {
161 /* Create "vary" base object */
162 RequestFlags flags;
163 flags.cachable.support();
164 StoreEntry *pe = storeCreateEntry("dummy url", "dummy log url", flags, Http::METHOD_GET);
165 auto &reply = pe->mem().adjustableBaseReply();
166 reply.setHeaders(Http::scOkay, "dummy test object", "x-squid-internal/test", 0, -1, squid_curtime + 100000);
167
168 pe->setPublicKey();
169
170 pe->buffer();
172 pe->flush();
173 pe->timestampsSet();
174 pe->complete();
175 pe->swapOut();
176 CPPUNIT_ASSERT_EQUAL(0, pe->swap_dirn);
177 CPPUNIT_ASSERT_EQUAL(0, pe->swap_filen);
178 pe->unlock("TestUfs::testUfsSearch vary");
179 }
180
182
183 /* here we cheat: we know that UFSSwapDirs search off disk. If we did an init call to a new
184 * swapdir instance, we'd not be testing a clean build.
185 */
186 StoreSearchPointer search = Store::Root().search(); /* search for everything in the store */
187
188 CPPUNIT_ASSERT_EQUAL(false, search->error());
189 CPPUNIT_ASSERT_EQUAL(false, search->isDone());
190 CPPUNIT_ASSERT_EQUAL(static_cast<StoreEntry *>(nullptr), search->currentItem());
191
192 /* trigger a callback */
193 cbcalled = false;
194 search->next(searchCallback, nullptr);
195 CPPUNIT_ASSERT_EQUAL(true, cbcalled);
196
197 /* we should have access to a entry now, that matches the entry we had before */
198 //CPPUNIT_ASSERT_EQUAL(false, search->next());
199 CPPUNIT_ASSERT_EQUAL(false, search->error());
200 CPPUNIT_ASSERT_EQUAL(false, search->isDone());
201 CPPUNIT_ASSERT(search->currentItem() != nullptr);
202
203 /* trigger another callback */
204 cbcalled = false;
205 search->next(searchCallback, nullptr);
206 CPPUNIT_ASSERT_EQUAL(true, cbcalled);
207
208 /* now we should have no error, we should have finished and have no current item */
209 //CPPUNIT_ASSERT_EQUAL(false, search->next());
210 CPPUNIT_ASSERT_EQUAL(false, search->error());
211 CPPUNIT_ASSERT_EQUAL(true, search->isDone());
212 CPPUNIT_ASSERT_EQUAL(static_cast<StoreEntry *>(nullptr), search->currentItem());
213
215
216 // TODO: here we should test a dirty rebuild
217
219 delete Config.replPolicy;
220
221 if (0 > system ("rm -rf " TESTDIR))
222 throw std::runtime_error("Failed to clean test work directory");
223}
224
225/* The UFS store should always configure an IO engine even if none is
226 * supplied on the configuration line.
227 */
228void
230{
231 /* boring common test boilerplate */
232 if (0 > system ("rm -rf " TESTDIR))
233 throw std::runtime_error("Failed to clean test work directory");
234
235 MySwapDirPointer aStore (new Fs::Ufs::UFSSwapDir("ufs", "Blocking"));
236 addSwapDir(aStore);
237 commonInit();
239 Config.replPolicy->type = xstrdup("lru");
241
242 char *path=xstrdup(TESTDIR);
243 char *config_line=xstrdup("100 1 1");
244 ConfigParser::SetCfgLine(config_line);
245 aStore->parse(0, path);
246 safe_free(path);
247 safe_free(config_line);
248 CPPUNIT_ASSERT(aStore->IO->io != nullptr);
249
252 delete Config.replPolicy;
253
254 if (0 > system ("rm -rf " TESTDIR))
255 throw std::runtime_error("Failed to clean test work directory");
256}
257
258int
259main(int argc, char *argv[])
260{
261 return TestProgram().run(argc, argv);
262}
263
void free_cachedir(Store::DiskConfig *swap)
Definition Disks.cc:800
void allocate_new_swapdir(Store::DiskConfig &swap)
Definition Disks.cc:781
int storeDirWriteCleanLogs(int reopen)
Definition Disks.cc:695
void httpHeaderInitModule(void)
RemovalPolicy * mem_policy
Definition MemObject.cc:44
time_t squid_curtime
RemovalPolicy * REMOVALPOLICYCREATE(wordlist *args)
RemovalPolicy * createRemovalPolicy(RemovalPolicySettings *settings)
Definition store.cc:1671
class SquidConfig Config
#define VERSION
Definition autoconf.h:1683
#define PACKAGE
Definition autoconf.h:1380
static void SetCfgLine(char *line)
Set the configuration file line to parse.
static DiskIOModule * Find(char const *type)
bool runOnce()
Definition EventLoop.cc:89
DiskIOStrategy * io
Definition UFSStrategy.h:51
void parse(int index, char *path) override
void create() override
create system resources needed for this store to operate in the future
Fs::Ufs::UFSStrategy * IO
Definition UFSSwapDir.h:83
void setHeaders(Http::StatusCode status, const char *reason, const char *ctype, int64_t clen, time_t lmt, time_t expires)
Definition HttpReply.cc:170
void packHeadersUsingSlowPacker(Packable &p) const
same as packHeadersUsingFastPacker() but assumes that p cannot quickly process small additions
Definition HttpReply.cc:95
const HttpReply & freshestReply() const
Definition MemObject.h:68
HttpReply & adjustableBaseReply()
Definition MemObject.cc:121
C * getRaw() const
Definition RefCount.h:89
SupportOrVeto cachable
whether the response may be stored in the cache
int objectsPerBucket
int64_t avgObjectSize
RemovalPolicySettings * replPolicy
Definition SquidConfig.h:99
Store::DiskConfig cacheSwap
int64_t maxObjectSize
char * store_dir_select_algorithm
struct SquidConfig::@88 Store
YesNoNone memShared
whether the memory cache is shared among workers
Definition SquidConfig.h:89
MemObject & mem()
Definition Store.h:47
sdirno swap_dirn
Definition Store.h:237
int unlock(const char *context)
Definition store.cc:469
void complete()
Definition store.cc:1031
void flush() override
Definition store.cc:1612
bool timestampsSet()
Definition store.cc:1387
sfileno swap_filen
unique ID inside a cache_dir for swapped out entries; -1 for others
Definition Store.h:235
void buffer() override
Definition store.cc:1601
bool setPublicKey(const KeyScope keyScope=ksDefault)
Definition store.cc:575
virtual bool error() const =0
virtual void next(void(callback)(void *cbdata), void *cbdata)=0
virtual StoreEntry * currentItem()=0
virtual bool isDone() const =0
static int store_dirs_rebuilding
the number of cache_dirs being rebuilt; TODO: move to Disks::Rebuilding
Definition Controller.h:133
StoreSearch * search()
void init() override
Definition Controller.cc:53
RefCount< SwapDir > * swapDirs
Definition SquidConfig.h:68
implements test program's main() function while enabling customization
int run(int argc, char *argv[])
void testUfsSearch()
Definition testUfs.cc:101
void commonInit()
Definition testUfs.cc:68
CPPUNIT_TEST_SUITE(TestUfs)
void testUfsDefaultEngine()
Definition testUfs.cc:229
CPPUNIT_TEST_SUITE_END()
CPPUNIT_TEST(testUfsDefaultEngine)
CPPUNIT_TEST(testUfsSearch)
void defaultTo(bool beSet)
enables or disables the option; updating to 'implicit' state
Definition YesNoNone.h:59
static void Init()
Definition fde.cc:141
void comm_init(void)
Definition comm.cc:1141
char const * visible_appname_string
int64_t store_maxobjsize
int main()
@ scOkay
Definition StatusCode.h:27
@ METHOD_GET
Definition MethodType.h:25
void Init()
Definition old_api.cc:281
Controller & Root()
safely access controller singleton
#define xstrdup
StoreEntry * storeCreateEntry(const char *url, const char *logUrl, const RequestFlags &flags, const HttpRequestMethod &method)
Definition store.cc:759
void storeReplAdd(const char *type, REMOVALPOLICYCREATE *create)
Definition store.cc:1645
REMOVALPOLICYCREATE createRemovalPolicy_lru
#define TESTDIR
Definition testUfs.cc:27
RefCount< Fs::Ufs::UFSSwapDir > MySwapDirPointer
Definition testUfs.cc:48
static void searchCallback(void *)
Definition testUfs.cc:62
static void addSwapDir(MySwapDirPointer aStore)
Definition testUfs.cc:52
CPPUNIT_TEST_SUITE_REGISTRATION(TestUfs)
static bool cbcalled
Definition testUfs.cc:59
#define safe_free(x)
Definition xalloc.h:73