]> git.ipfire.org Git - thirdparty/squid.git/blob - src/tests/testStore.cc
Source Format Enforcement (#1234)
[thirdparty/squid.git] / src / tests / testStore.cc
1 /*
2 * Copyright (C) 1996-2023 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 "Store.h"
11 #include "store/SwapMeta.h"
12 #include "testStore.h"
13 #include "unitTestMain.h"
14
15 #include <limits>
16
17 CPPUNIT_TEST_SUITE_REGISTRATION( testStore );
18
19 int
20 TestStore::callback()
21 {
22 return 1;
23 }
24
25 StoreEntry*
26 TestStore::get(const cache_key*)
27 {
28 return nullptr;
29 }
30
31 void
32 TestStore::get(String, void (*)(StoreEntry*, void*), void*)
33 {}
34
35 void
36 TestStore::init()
37 {}
38
39 uint64_t
40 TestStore::maxSize() const
41 {
42 return 3;
43 }
44
45 uint64_t
46 TestStore::minSize() const
47 {
48 return 1;
49 }
50
51 uint64_t
52 TestStore::currentSize() const
53 {
54 return 2;
55 }
56
57 uint64_t
58 TestStore::currentCount() const
59 {
60 return 2;
61 }
62
63 int64_t
64 TestStore::maxObjectSize() const
65 {
66 return 1;
67 }
68
69 void
70 TestStore::getStats(StoreInfoStats &) const
71 {
72 }
73
74 void
75 TestStore::stat(StoreEntry &) const
76 {
77 const_cast<TestStore *>(this)->statsCalled = true;
78 }
79
80 StoreSearch *
81 TestStore::search()
82 {
83 return nullptr;
84 }
85
86 void
87 testStore::testSetRoot()
88 {
89 Store::Controller *aStore(new TestStore);
90 Store::Init(aStore);
91
92 CPPUNIT_ASSERT_EQUAL(&Store::Root(), aStore);
93 Store::FreeMemory();
94 }
95
96 void
97 testStore::testUnsetRoot()
98 {
99 Store::Controller *aStore(new TestStore);
100 Store::Controller *aStore2(new TestStore);
101 Store::Init(aStore);
102 Store::FreeMemory();
103 Store::Init(aStore2);
104 CPPUNIT_ASSERT_EQUAL(&Store::Root(),aStore2);
105 Store::FreeMemory();
106 }
107
108 void
109 testStore::testStats()
110 {
111 TestStore *aStore(new TestStore);
112 Store::Init(aStore);
113 CPPUNIT_ASSERT_EQUAL(false, aStore->statsCalled);
114 StoreEntry entry;
115 Store::Stats(&entry);
116 CPPUNIT_ASSERT_EQUAL(true, aStore->statsCalled);
117 Store::FreeMemory();
118 }
119
120 void
121 testStore::testMaxSize()
122 {
123 Store::Controller *aStore(new TestStore);
124 Store::Init(aStore);
125 CPPUNIT_ASSERT_EQUAL(static_cast<uint64_t>(3), aStore->maxSize());
126 Store::FreeMemory();
127 }
128
129 namespace Store {
130
131 /// check rawType that may be ignored
132 static void
133 checkIgnorableSwapMetaRawType(const RawSwapMetaType rawType)
134 {
135 if (IgnoredSwapMetaType(rawType)) {
136 // an ignored raw type is either deprecated or reserved
137 CPPUNIT_ASSERT(DeprecatedSwapMetaType(rawType) || ReservedSwapMetaType(rawType));
138 CPPUNIT_ASSERT(!(DeprecatedSwapMetaType(rawType) && ReservedSwapMetaType(rawType)));
139 } else {
140 // all other raw types are neither deprecated nor reserved
141 CPPUNIT_ASSERT(!DeprecatedSwapMetaType(rawType) && !ReservedSwapMetaType(rawType));
142 }
143 }
144
145 /// check a raw swap meta field type below SwapMetaType range or STORE_META_VOID
146 static void
147 checkTooSmallSwapMetaRawType(const RawSwapMetaType rawType)
148 {
149 // RawSwapMetaTypeBottom and smaller values are unrelated to any named
150 // SwapMetaDataType values, including past, current, and future ones
151 CPPUNIT_ASSERT(!HonoredSwapMetaType(rawType)); // current
152 CPPUNIT_ASSERT(!IgnoredSwapMetaType(rawType)); // past and future
153 CPPUNIT_ASSERT(!DeprecatedSwapMetaType(rawType)); // past
154 CPPUNIT_ASSERT(!ReservedSwapMetaType(rawType)); // future
155 }
156
157 /// check a raw swap meta field type within SwapMetaType range, excluding STORE_META_VOID
158 static void
159 checkKnownSwapMetaRawType(const RawSwapMetaType rawType)
160 {
161 // an in-range rawType other than STORE_META_VOID is either honored or ignored
162 CPPUNIT_ASSERT(HonoredSwapMetaType(rawType) || IgnoredSwapMetaType(rawType));
163 CPPUNIT_ASSERT(!(HonoredSwapMetaType(rawType) && IgnoredSwapMetaType(rawType)));
164 checkIgnorableSwapMetaRawType(rawType);
165 }
166
167 /// check a raw swap meta field type exceeding RawSwapMetaTypeTop()
168 static void
169 checkTooBigSwapMetaRawType(const RawSwapMetaType rawType)
170 {
171 // values beyond RawSwapMetaTypeTop() cannot be honored but may be ignored
172 CPPUNIT_ASSERT(!HonoredSwapMetaType(rawType));
173 checkIgnorableSwapMetaRawType(rawType);
174 }
175
176 /// check a given raw swap meta field type
177 static void
178 checkSwapMetaRawType(const RawSwapMetaType rawType)
179 {
180 if (rawType <= RawSwapMetaTypeBottom)
181 checkTooSmallSwapMetaRawType(rawType);
182 else if (rawType > RawSwapMetaTypeTop())
183 checkTooBigSwapMetaRawType(rawType);
184 else
185 checkKnownSwapMetaRawType(rawType);
186 }
187
188 } // namespace Store
189
190 void
191 testStore::testSwapMetaTypeClassification()
192 {
193 using limits = std::numeric_limits<Store::RawSwapMetaType>;
194 for (auto rawType = limits::min(); true; ++rawType) {
195
196 Store::checkSwapMetaRawType(rawType);
197
198 if (rawType == limits::max())
199 break;
200 }
201
202 // Store::RawSwapMetaTypeTop() is documented as an honored type value
203 CPPUNIT_ASSERT(Store::HonoredSwapMetaType(Store::RawSwapMetaTypeTop()));
204 }
205