29 #include <cppunit/TestFixture.h>
30 #include <cppunit/extensions/HelperMacros.h>
33 #include "ReuserState.hpp"
34 #include "ReuserTypes.hpp"
40 int groupId,
int userId)
42 auto reusedUploads =
db.getReusedUploads(uploadId, groupId);
43 for (
const auto& triple : reusedUploads)
46 if (!
db.getParentItemBounds(triple.reusedUploadId, bounds))
49 if (triple.reuseMode & REUSE_ENHANCED)
51 if (!
db.processEnhancedUploadReuse(uploadId, triple.reusedUploadId,
52 groupId, triple.reusedGroupId, userId))
57 if (!
db.processUploadReuse(uploadId, triple.reusedUploadId,
58 groupId, triple.reusedGroupId, userId))
62 if (triple.reuseMode & REUSE_MAIN)
63 db.reuseMainLicense(uploadId, groupId, triple.reusedUploadId,
64 triple.reusedGroupId);
66 if (triple.reuseMode & REUSE_CONF)
67 db.reuseConfSettings(uploadId, triple.reusedUploadId);
69 if (triple.reuseMode & REUSE_COPYRIGHT)
70 db.reuseCopyrights(uploadId, triple.reusedUploadId, userId);
103 CPPUNIT_TEST_SUITE_END();
117 bool processUploadCalled =
false;
118 db.onProcessUploadReuse =
119 [&](int, int, int, int, int) ->
bool
120 { processUploadCalled =
true;
return true; };
123 bool ok = runProcess(
db, 1, 3, 2);
125 CPPUNIT_ASSERT_MESSAGE(
"should succeed with no reuse links", ok);
126 CPPUNIT_ASSERT_MESSAGE(
"processUploadReuse must not be called",
127 !processUploadCalled);
141 db.onGetReusedUploads = [](int, int) -> std::vector<ReuseTriple>
142 {
return {{42, 3, 0}}; };
145 { out = {100,
"uploadtree_a", 42, 1, 100};
return true; };
147 int processCalls = 0;
149 db.onProcessUploadReuse =
150 [&](int, int, int, int, int) ->
bool
151 { ++processCalls;
return true; };
153 bool ok = runProcess(
db, 3, 3, 2);
155 CPPUNIT_ASSERT_MESSAGE(
"should succeed even if source has no decisions", ok);
156 CPPUNIT_ASSERT_EQUAL_MESSAGE(
157 "processUploadReuse must be called exactly once", 1, processCalls);
170 const int uploadId = 3;
171 const int reusedUpload = 2;
172 const int groupId = 3;
173 const int reusedGroupId = 3;
174 const int userId = 2;
176 db.onGetReusedUploads =
177 [&](
int uid,
int gid) -> std::vector<ReuseTriple>
179 CPPUNIT_ASSERT_EQUAL(uploadId, uid);
180 CPPUNIT_ASSERT_EQUAL(groupId, gid);
181 return {{reusedUpload, reusedGroupId, 0 }};
185 { out = {1,
"uploadtree_a", 2, 1, 100};
return true; };
187 int capturedUpload = -1;
188 int capturedReused = -1;
189 int capturedGroup = -1;
190 int capturedReusedGroup = -1;
191 int capturedUser = -1;
193 db.onProcessUploadReuse =
194 [&](
int u,
int r,
int g,
int rg,
int usr) ->
bool
199 capturedReusedGroup = rg;
204 bool ok = runProcess(
db, uploadId, groupId, userId);
207 CPPUNIT_ASSERT_EQUAL(uploadId, capturedUpload);
208 CPPUNIT_ASSERT_EQUAL(reusedUpload, capturedReused);
209 CPPUNIT_ASSERT_EQUAL(groupId, capturedGroup);
210 CPPUNIT_ASSERT_EQUAL(reusedGroupId, capturedReusedGroup);
211 CPPUNIT_ASSERT_EQUAL(userId, capturedUser);
225 bool standardCalled =
false;
226 bool enhancedCalled =
false;
229 db.onGetReusedUploads = [](int, int) -> std::vector<ReuseTriple>
230 {
return {{2, 3, 0}}; };
233 { out = {1,
"uploadtree_a", 2, 1, 100};
return true; };
235 db.onProcessUploadReuse =
236 [&](int, int, int, int, int) ->
bool
237 { standardCalled =
true;
return true; };
239 db.onProcessEnhancedUploadReuse =
240 [&](int, int, int, int, int) ->
bool
241 { enhancedCalled =
true;
return true; };
243 bool ok = runProcess(
db, 3, 3, 2);
246 CPPUNIT_ASSERT_MESSAGE(
"standard path for repo-scoped clearing",
248 CPPUNIT_ASSERT_MESSAGE(
"enhanced path must not be used", !enhancedCalled);
261 bool standardCalled =
false;
262 bool enhancedCalled =
false;
264 db.onGetReusedUploads = [](int, int) -> std::vector<ReuseTriple>
265 {
return {{2, 3, REUSE_ENHANCED}}; };
268 { out = {1,
"uploadtree_a", 2, 1, 100};
return true; };
270 db.onProcessUploadReuse =
271 [&](int, int, int, int, int) ->
bool
272 { standardCalled =
true;
return true; };
274 db.onProcessEnhancedUploadReuse =
275 [&](int, int, int, int, int) ->
bool
276 { enhancedCalled =
true;
return true; };
278 bool ok = runProcess(
db, 3, 3, 2);
281 CPPUNIT_ASSERT_MESSAGE(
"enhanced path must be taken", enhancedCalled);
282 CPPUNIT_ASSERT_MESSAGE(
"standard path must not be taken", !standardCalled);
292 db.onGetReusedUploads = [](int, int) -> std::vector<ReuseTriple>
293 {
return {{2, 3, REUSE_ENHANCED}}; };
296 { out = {1,
"uploadtree_a", 2, 1, 100};
return true; };
298 db.onProcessEnhancedUploadReuse =
299 [](int, int, int, int, int) ->
bool {
return false; };
301 bool ok = runProcess(
db, 3, 3, 2);
303 CPPUNIT_ASSERT_MESSAGE(
"failure of enhanced reuse must propagate", !ok);
316 db.onGetReusedUploads = [](int, int) -> std::vector<ReuseTriple>
317 {
return {{10, 3, 0}, {20, 5, 0}}; };
320 { out = {1,
"uploadtree_a", 0, 1, 100};
return true; };
322 std::vector<int> calledWithReused;
323 db.onProcessUploadReuse =
324 [&](int,
int reused, int, int, int) ->
bool
325 { calledWithReused.push_back(reused);
return true; };
327 bool ok = runProcess(
db, 1, 3, 2);
330 CPPUNIT_ASSERT_EQUAL_MESSAGE(
"both reuse links must be processed",
331 2u,
static_cast<unsigned>(calledWithReused.size()));
332 CPPUNIT_ASSERT_EQUAL(10, calledWithReused[0]);
333 CPPUNIT_ASSERT_EQUAL(20, calledWithReused[1]);
345 bool mainCalled =
false;
346 bool confCalled =
false;
347 bool copyrightCalled =
false;
349 db.onGetReusedUploads = [](int, int) -> std::vector<ReuseTriple>
350 {
return {{2, 3, REUSE_MAIN}}; };
353 { out = {1,
"uploadtree_a", 2, 1, 100};
return true; };
355 db.onProcessUploadReuse =
356 [](int, int, int, int, int) ->
bool {
return true; };
358 db.onReuseMainLicense =
359 [&](int, int, int, int) ->
bool { mainCalled =
true;
return true; };
361 db.onReuseConfSettings =
362 [&](int, int) ->
bool { confCalled =
true;
return true; };
364 db.onReuseCopyrights =
365 [&](int, int, int) ->
bool { copyrightCalled =
true;
return true; };
367 bool ok = runProcess(
db, 3, 3, 2);
370 CPPUNIT_ASSERT_MESSAGE(
"reuseMainLicense must be called", mainCalled);
371 CPPUNIT_ASSERT_MESSAGE(
"reuseConfSettings must not be called", !confCalled);
372 CPPUNIT_ASSERT_MESSAGE(
"reuseCopyrights must not be called",
385 bool mainCalled =
false;
386 bool confCalled =
false;
387 bool copyrightCalled =
false;
389 db.onGetReusedUploads = [](int, int) -> std::vector<ReuseTriple>
390 {
return {{2, 3, REUSE_CONF}}; };
393 { out = {1,
"uploadtree_a", 2, 1, 100};
return true; };
395 db.onProcessUploadReuse =
396 [](int, int, int, int, int) ->
bool {
return true; };
398 db.onReuseMainLicense =
399 [&](int, int, int, int) ->
bool { mainCalled =
true;
return true; };
401 db.onReuseConfSettings =
402 [&](int, int) ->
bool { confCalled =
true;
return true; };
404 db.onReuseCopyrights =
405 [&](int, int, int) ->
bool { copyrightCalled =
true;
return true; };
407 bool ok = runProcess(
db, 3, 3, 2);
410 CPPUNIT_ASSERT_MESSAGE(
"reuseMainLicense must not be called", !mainCalled);
411 CPPUNIT_ASSERT_MESSAGE(
"reuseConfSettings must be called", confCalled);
412 CPPUNIT_ASSERT_MESSAGE(
"reuseCopyrights must not be called",
425 bool mainCalled =
false;
426 bool confCalled =
false;
427 bool copyrightCalled =
false;
429 db.onGetReusedUploads = [](int, int) -> std::vector<ReuseTriple>
430 {
return {{2, 3, REUSE_COPYRIGHT}}; };
433 { out = {1,
"uploadtree_a", 2, 1, 100};
return true; };
435 db.onProcessUploadReuse =
436 [](int, int, int, int, int) ->
bool {
return true; };
438 db.onReuseMainLicense =
439 [&](int, int, int, int) ->
bool { mainCalled =
true;
return true; };
441 db.onReuseConfSettings =
442 [&](int, int) ->
bool { confCalled =
true;
return true; };
444 db.onReuseCopyrights =
445 [&](int, int, int) ->
bool { copyrightCalled =
true;
return true; };
447 bool ok = runProcess(
db, 3, 3, 2);
450 CPPUNIT_ASSERT_MESSAGE(
"reuseMainLicense must not be called", !mainCalled);
451 CPPUNIT_ASSERT_MESSAGE(
"reuseConfSettings must not be called", !confCalled);
452 CPPUNIT_ASSERT_MESSAGE(
"reuseCopyrights must be called", copyrightCalled);
465 bool mainCalled =
false;
466 bool confCalled =
false;
467 bool copyrightCalled =
false;
468 bool standardCalled =
false;
470 db.onGetReusedUploads = [](int, int) -> std::vector<ReuseTriple>
471 {
return {{2, 3, 0 }}; };
474 { out = {1,
"uploadtree_a", 2, 1, 100};
return true; };
476 db.onProcessUploadReuse =
477 [&](int, int, int, int, int) ->
bool
478 { standardCalled =
true;
return true; };
480 db.onReuseMainLicense =
481 [&](int, int, int, int) ->
bool { mainCalled =
true;
return true; };
483 db.onReuseConfSettings =
484 [&](int, int) ->
bool { confCalled =
true;
return true; };
486 db.onReuseCopyrights =
487 [&](int, int, int) ->
bool { copyrightCalled =
true;
return true; };
489 bool ok = runProcess(
db, 3, 3, 2);
492 CPPUNIT_ASSERT_MESSAGE(
"standard reuse must be called", standardCalled);
493 CPPUNIT_ASSERT_MESSAGE(
"reuseMainLicense must not be called", !mainCalled);
494 CPPUNIT_ASSERT_MESSAGE(
"reuseConfSettings must not be called", !confCalled);
495 CPPUNIT_ASSERT_MESSAGE(
"reuseCopyrights must not be called",
Mock ReuserDatabaseHandler for unit tests.
Test double for ReuserDatabaseHandler.
Database handler for the reuser agent.
void testMultipleReuseLinksAllProcessed()
All upload_reuse rows for an upload are processed in order.
void testCorrectArgumentsForwardedToProcessUploadReuse()
All five arguments are forwarded correctly to processUploadReuse.
void testOptionalFlagsNotCalledWithoutBits()
reuseMode == 0 -> only processUploadReuse is called, no optional extras.
void testEnhancedReuseUsesEnhancedPath()
REUSE_ENHANCED bit routes to processEnhancedUploadReuse.
void testNoReuseLinkSucceedsWithoutProcessing()
No upload_reuse row exists for this upload.
void testEnhancedReuseFailurePropagates()
A false return from processEnhancedUploadReuse propagates to the caller.
void testReuseMainLicenseFlagOnly()
REUSE_MAIN bit causes only reuseMainLicense to be called.
void testReuseExistsButSourceHasNoClearings()
upload_reuse row exists but the source upload has no clearing decisions.
void testRepoClearingUsesStandardReusePath()
reuseMode == 0 always takes the standard reuse path.
void testReuseConfFlagOnly()
REUSE_CONF bit causes only reuseConfSettings to be called.
void testReuseCopyrightFlagOnly()
REUSE_COPYRIGHT bit causes only reuseCopyrights to be called.
Bounds of an item within an uploadtree table.