FOSSology  4.7.1
Open Source License Compliance by Open Source Software
test_worker.cc
Go to the documentation of this file.
1 /*
2  SPDX-License-Identifier: GPL-2.0-only
3  Author: Dietmar Helmut Leher <helmut.leher.ext@vaillant-group.com>
4  SPDX-FileCopyrightText: © 2026 Vaillant GmbH
5 */
14 #include <cppunit/TestFixture.h>
15 #include <cppunit/extensions/HelperMacros.h>
16 
18 #include "ReuserState.hpp"
19 #include "ReuserTypes.hpp"
20 
28 static bool runProcessUpload(ReuserDatabaseHandler& db, int uploadId,
29  int groupId, int userId)
30 {
31  auto reusedUploads = db.getReusedUploads(uploadId, groupId);
32  for (const auto& triple : reusedUploads)
33  {
34  ItemTreeBounds bounds;
35  if (!db.getParentItemBounds(triple.reusedUploadId, bounds))
36  continue;
37 
38  if (triple.reuseMode & REUSE_ENHANCED)
39  {
40  if (!db.processEnhancedUploadReuse(uploadId, triple.reusedUploadId,
41  groupId, triple.reusedGroupId, userId))
42  return false;
43  }
44  else
45  {
46  if (!db.processUploadReuse(uploadId, triple.reusedUploadId,
47  groupId, triple.reusedGroupId, userId))
48  return false;
49  }
50 
51  if (triple.reuseMode & REUSE_MAIN)
52  db.reuseMainLicense(uploadId, groupId, triple.reusedUploadId,
53  triple.reusedGroupId);
54 
55  if (triple.reuseMode & REUSE_CONF)
56  db.reuseConfSettings(uploadId, triple.reusedUploadId);
57 
58  if (triple.reuseMode & REUSE_COPYRIGHT)
59  db.reuseCopyrights(uploadId, triple.reusedUploadId, userId);
60  }
61  return true;
62 }
63 
64 class ReuserWorkerTest : public CPPUNIT_NS::TestFixture
65 {
66  CPPUNIT_TEST_SUITE(ReuserWorkerTest);
67  CPPUNIT_TEST(testStandardReuseDispatch);
68  CPPUNIT_TEST(testEnhancedReuseDispatch);
69  CPPUNIT_TEST(testAllFlagsDispatch);
70  CPPUNIT_TEST(testNoReusedUploadsSucceeds);
73  CPPUNIT_TEST_SUITE_END();
74 
75 protected:
83  {
85 
86  bool standardCalled = false;
87  bool enhancedCalled = false;
88 
89  handler.onGetReusedUploads = [](int, int) -> std::vector<ReuseTriple>
90  { return {{10, 1, 0 /* no flags -> standard */}}; };
91 
92  handler.onGetParentItemBounds = [](int, ItemTreeBounds& out) -> bool
93  { out = {1, "uploadtree_a", 10, 1, 100}; return true; };
94 
95  handler.onProcessUploadReuse =
96  [&](int, int, int, int, int) -> bool
97  { standardCalled = true; return true; };
98 
99  handler.onProcessEnhancedUploadReuse =
100  [&](int, int, int, int, int) -> bool
101  { enhancedCalled = true; return true; };
102 
103  bool ok = runProcessUpload(handler, 1, 1, 1);
104 
105  CPPUNIT_ASSERT(ok);
106  CPPUNIT_ASSERT(standardCalled);
107  CPPUNIT_ASSERT(!enhancedCalled);
108  }
109 
117  {
119 
120  bool standardCalled = false;
121  bool enhancedCalled = false;
122 
123  handler.onGetReusedUploads = [](int, int) -> std::vector<ReuseTriple>
124  { return {{10, 1, REUSE_ENHANCED}}; };
125 
126  handler.onGetParentItemBounds = [](int, ItemTreeBounds& out) -> bool
127  { out = {1, "uploadtree_a", 10, 1, 100}; return true; };
128 
129  handler.onProcessUploadReuse =
130  [&](int, int, int, int, int) -> bool
131  { standardCalled = true; return true; };
132 
133  handler.onProcessEnhancedUploadReuse =
134  [&](int, int, int, int, int) -> bool
135  { enhancedCalled = true; return true; };
136 
137  bool ok = runProcessUpload(handler, 1, 1, 1);
138 
139  CPPUNIT_ASSERT(ok);
140  CPPUNIT_ASSERT(!standardCalled);
141  CPPUNIT_ASSERT(enhancedCalled);
142  }
143 
152  {
154 
155  bool mainCalled = false;
156  bool confCalled = false;
157  bool copyrightCalled = false;
158 
159  int allFlags = REUSE_ENHANCED | REUSE_MAIN | REUSE_CONF | REUSE_COPYRIGHT;
160  handler.onGetReusedUploads = [allFlags](int, int) -> std::vector<ReuseTriple>
161  { return {{10, 1, allFlags}}; };
162 
163  handler.onGetParentItemBounds = [](int, ItemTreeBounds& out) -> bool
164  { out = {1, "uploadtree_a", 10, 1, 100}; return true; };
165 
166  handler.onReuseMainLicense =
167  [&](int, int, int, int) -> bool
168  { mainCalled = true; return true; };
169 
170  handler.onReuseConfSettings =
171  [&](int, int) -> bool
172  { confCalled = true; return true; };
173 
174  handler.onReuseCopyrights =
175  [&](int, int, int) -> bool
176  { copyrightCalled = true; return true; };
177 
178  bool ok = runProcessUpload(handler, 1, 1, 1);
179 
180  CPPUNIT_ASSERT(ok);
181  CPPUNIT_ASSERT(mainCalled);
182  CPPUNIT_ASSERT(confCalled);
183  CPPUNIT_ASSERT(copyrightCalled);
184  }
185 
193  {
195  // Default: onGetReusedUploads not set -> returns empty vector.
196  bool ok = runProcessUpload(handler, 1, 1, 1);
197  CPPUNIT_ASSERT(ok);
198  }
199 
208  {
210 
211  int processCalledCount = 0;
212 
213  handler.onGetReusedUploads = [](int, int) -> std::vector<ReuseTriple>
214  { return {{10, 1, 0}, {20, 1, 0}}; };
215 
216  // First upload: bounds not found -> skip.
217  // Second upload: bounds found -> process.
218  handler.onGetParentItemBounds =
219  [](int uploadId, ItemTreeBounds& out) -> bool
220  {
221  if (uploadId == 10) return false;
222  out = {1, "uploadtree", uploadId, 1, 10};
223  return true;
224  };
225 
226  handler.onProcessUploadReuse =
227  [&](int, int, int, int, int) -> bool
228  { ++processCalledCount; return true; };
229 
230  bool ok = runProcessUpload(handler, 1, 1, 1);
231 
232  CPPUNIT_ASSERT(ok);
233  CPPUNIT_ASSERT_EQUAL(1, processCalledCount); // only second upload processed
234  }
235 
243  {
245 
246  handler.onGetReusedUploads = [](int, int) -> std::vector<ReuseTriple>
247  { return {{10, 1, 0}}; };
248 
249  handler.onGetParentItemBounds = [](int, ItemTreeBounds& out) -> bool
250  { out = {1, "uploadtree_a", 10, 1, 100}; return true; };
251 
252  handler.onProcessUploadReuse =
253  [](int, int, int, int, int) -> bool
254  { return false; }; // simulate DB error
255 
256  bool ok = runProcessUpload(handler, 1, 1, 1);
257 
258  CPPUNIT_ASSERT(!ok);
259  }
260 };
261 
262 CPPUNIT_TEST_SUITE_REGISTRATION(ReuserWorkerTest);
Mock ReuserDatabaseHandler for unit tests.
Test double for ReuserDatabaseHandler.
Database handler for the reuser agent.
void testProcessUploadReuseFailurePropagates()
A false return from processUploadReuse propagates to the caller.
Definition: test_worker.cc:242
void testStandardReuseDispatch()
reuseMode == 0 routes to processUploadReuse (standard path).
Definition: test_worker.cc:82
void testMissingParentBoundsSkipsUpload()
A reuse link whose parent bounds cannot be determined is skipped.
Definition: test_worker.cc:207
void testEnhancedReuseDispatch()
REUSE_ENHANCED bit routes to processEnhancedUploadReuse.
Definition: test_worker.cc:116
void testAllFlagsDispatch()
All optional flag bits together cause all optional methods to be called.
Definition: test_worker.cc:151
void testNoReusedUploadsSucceeds()
An empty reuse list results in immediate success without any processing.
Definition: test_worker.cc:192
Definition: db.php:28
Bounds of an item within an uploadtree table.
Definition: ReuserTypes.hpp:14
static bool runProcessUpload(ReuserDatabaseHandler &db, int uploadId, int groupId, int userId)
Minimal replica of processUploadId for unit-testing.
Definition: test_worker.cc:28