FOSSology  4.4.0
Open Source License Compliance by Open Source Software
bulkTest.php
1 <?php
2 /*
3  SPDX-FileCopyrightText: © 2014-2015 Siemens AG
4 
5  SPDX-License-Identifier: GPL-2.0-only
6 */
7 
18 use Monolog\Logger;
19 
20 class bulkTest extends \PHPUnit\Framework\TestCase
21 {
23  private $testDb;
25  private $dbManager;
27  private $licenseDao;
29  private $clearingDao;
31  private $uploadDao;
33  private $uploadPermDao;
35  private $highlightDao;
36 
37  protected function setUp() : void
38  {
39  $this->testDb = new TestPgDb("monkBulk");
40  $this->dbManager = $this->testDb->getDbManager();
41 
42  $this->licenseDao = new LicenseDao($this->dbManager);
43  $logger = new Logger("MonkBulkTest");
44  $this->uploadPermDao = \Mockery::mock(UploadPermissionDao::class);
45  $this->uploadDao = new UploadDao($this->dbManager, $logger, $this->uploadPermDao);
46  $this->highlightDao = new HighlightDao($this->dbManager);
47  $this->clearingDao = new ClearingDao($this->dbManager, $this->uploadDao);
48  }
49 
50  protected function tearDown() : void
51  {
52  $this->testDb->fullDestruct();
53  $this->testDb = null;
54  $this->dbManager = null;
55  $this->licenseDao = null;
56  $this->highlightDao = null;
57  $this->clearingDao = null;
58  }
59 
60  private function runBulkMonk($userId = 2, $groupId = 2, $jobId = 1, $bulkId = 3)
61  {
62  $sysConf = $this->testDb->getFossSysConf();
63 
64  $agentName = "monkbulk";
65 
66  $agentDir = dirname(__DIR__,4).'/build/src/monk';
67  $execDir = $agentDir.'/agent';
68  system("install -D $agentDir/VERSION-monkbulk $sysConf/mods-enabled/$agentName/VERSION");
69 
70  $pipeFd = popen("echo '0\n$bulkId\n0' | $execDir/$agentName -c $sysConf --userID=$userId --groupID=$groupId --jobId=$jobId --scheduler_start", "r");
71  $this->assertTrue($pipeFd !== false, 'running monk bulk failed');
72 
73  $output = "";
74  while (($buffer = fgets($pipeFd, 4096)) !== false) {
75  $output .= $buffer;
76  }
77  $retCode = pclose($pipeFd);
78 
79  unlink("$sysConf/mods-enabled/$agentName/VERSION");
80  rmdir("$sysConf/mods-enabled/$agentName");
81  rmdir("$sysConf/mods-enabled");
82 
83  return array($output,$retCode);
84  }
85 
86  private function setUpRepo()
87  {
88  $sysConf = $this->testDb->getFossSysConf();
89 
90  $confFile = $sysConf."/fossology.conf";
91  system("touch ".$confFile);
92  $config = "[FOSSOLOGY]\ndepth = 0\npath = $sysConf/repo\n";
93  file_put_contents($confFile, $config);
94 
95  $testRepoDir = dirname(dirname(dirname(__DIR__)))."/lib/php/Test/";
96  system("cp -a $testRepoDir/repo $sysConf/");
97  }
98 
99  private function rmRepo()
100  {
101  $sysConf = $this->testDb->getFossSysConf();
102  system("rm $sysConf/repo -rf");
103  unlink($sysConf."/fossology.conf");
104  }
105 
106  private function setUpTables()
107  {
108  $this->testDb->createPlainTables(array('upload','uploadtree','license_ref','license_ref_bulk', 'license_set_bulk',
109  'clearing_event','clearing_decision','report_info','clearing_decision_event','license_file','highlight','highlight_bulk','agent','pfile','ars_master','users'),false);
110  $this->testDb->createSequences(array('agent_agent_pk_seq','pfile_pfile_pk_seq','upload_upload_pk_seq','nomos_ars_ars_pk_seq','license_file_fl_pk_seq','license_ref_rf_pk_seq','license_ref_bulk_lrb_pk_seq','clearing_event_clearing_event_pk_seq'),false);
111  $this->testDb->createViews(array('license_file_ref'),false);
112  $this->testDb->createConstraints(array('agent_pkey','pfile_pkey','upload_pkey_idx','FileLicense_pkey','clearing_event_pkey', 'license_ref_bulk_pkey', 'license_set_bulk_fkey'),false);
113  $this->testDb->alterTables(array('agent','pfile','upload','ars_master','license_ref_bulk','license_ref','license_set_bulk','clearing_event','license_file','highlight'),false);
114  $this->testDb->createInheritedTables();
115  $this->testDb->insertData(array('pfile','upload','uploadtree_a','users'), false);
116  $this->testDb->insertData_license_ref();
117  }
118 
119  private function getHeartCount($output)
120  {
121  $matches = array();
122  if (preg_match("/.*HEART: ([0-9]*).*/", $output, $matches)) {
123  return intval($matches[1]);
124  }
125  else {
126  return 0;
127  }
128  }
129 
132  {
133  $this->setUpTables();
134  $this->setUpRepo();
135 
136  $userId = 2;
137  $groupId = 2;
138  $uploadTreeId = 1;
139 
140  $licenseId = 225;
141  $removing = false;
142  $refText = "The GNU General Public License is a free, copyleft license for software and other kinds of works.";
143 
144  $bulkId = $this->licenseDao->insertBulkLicense($userId, $groupId, $uploadTreeId,
145  array($licenseId => array($removing,"","","")), $refText);
146 
147  $this->assertGreaterThan($expected=0, $bulkId);
148 
149  $jobId = 64;
150  list($output,$retCode) = $this->runBulkMonk($userId, $groupId, $jobId, $bulkId);
151 
152  $this->assertEquals($retCode, 0, 'monk bulk failed: '.$output);
153  $bounds6 = new ItemTreeBounds(6, 'uploadtree_a', 1, 17, 18);
154  $bounds7 = new ItemTreeBounds(7, 'uploadtree_a', 1, 15, 16);
155  $relevantDecisionsItem6 = $this->clearingDao->getRelevantClearingEvents($bounds6, $groupId);
156  $relevantDecisionsItem7 = $this->clearingDao->getRelevantClearingEvents($bounds7, $groupId);
157 
158  assertThat(count($relevantDecisionsItem6),is(equalTo(1)));
159  assertThat(count($relevantDecisionsItem7),is(equalTo(1)));
160  assertThat($relevantDecisionsItem6,hasKeyInArray($licenseId));
161 
162  $refSecondText = "Our General Public Licenses are designed to make sure that you " .
163  "have the freedom to distribute copies of free software";
164  $licenseSecondId = 215;
165  $bulkSecondId = $this->licenseDao->insertBulkLicense($userId, $groupId, $uploadTreeId,
166  array($licenseSecondId => array($removing,"","","")), $refSecondText);
167 
168  $jobId++;
169  list($output,$retCode) = $this->runBulkMonk($userId, $groupId, $jobId, $bulkSecondId);
170 
171  $this->assertEquals($retCode, 0, 'monk bulk failed: '.$output);
172  $relevantDecisionsItemPfile3 = $this->clearingDao->getRelevantClearingEvents($bounds6, $groupId);
173  $relevantDecisionsItemPfile4 = $this->clearingDao->getRelevantClearingEvents($bounds7, $groupId);
174  assertThat(count($relevantDecisionsItemPfile3), is(equalTo(1)));
175 
176  assertThat(count($relevantDecisionsItemPfile4), is(equalTo(2)));
177  assertThat($relevantDecisionsItemPfile4, hasKeyInArray($licenseSecondId));
178 
179  $this->rmRepo();
180  }
181 
183  public function testRunMonkBulkScan()
184  {
185  $this->setUpTables();
186  $this->setUpRepo();
187 
188  $userId = 2;
189  $groupId = 2;
190  $uploadTreeId = 1;
191 
192  $licenseId1 = 225;
193  $removing1 = false;
194  $licenseId2 = 213;
195  $removing2 = false;
196  $refText = "The GNU General Public License is a free, copyleft license for software and other kinds of works.";
197 
198  $bulkId = $this->licenseDao->insertBulkLicense($userId, $groupId, $uploadTreeId,
199  array($licenseId1 => array($removing1,"","",""),
200  $licenseId2 => array($removing2,"","","")), $refText);
201 
202  $this->assertGreaterThan($expected=0, $bulkId);
203 
204  $jobId = 64;
205  list($output,$retCode) = $this->runBulkMonk($userId, $groupId, $jobId, $bulkId);
206  $this->assertEquals(6, $this->getHeartCount($output));
207  $this->rmRepo();
208 
209  $this->assertEquals($retCode, 0, 'monk bulk failed: '.$output);
210  $bounds6 = new ItemTreeBounds(6, 'uploadtree_a', 1, 17, 18);
211  $bounds7 = new ItemTreeBounds(7, 'uploadtree_a', 1, 15, 16);
212  $relevantDecisionsItem6 = $this->clearingDao->getRelevantClearingEvents($bounds6, $groupId);
213  $relevantDecisionsItem7 = $this->clearingDao->getRelevantClearingEvents($bounds7, $groupId);
214 
215  assertThat(count($relevantDecisionsItem6),is(equalTo(2)));
216  assertThat(count($relevantDecisionsItem7),is(equalTo(2)));
217  $rfForACE = 225;
218  assertThat($relevantDecisionsItem6,hasKeyInArray($rfForACE));
220  $clearingEvent = $relevantDecisionsItem6[$rfForACE];
221  $eventId = $clearingEvent->getEventId();
222  $bulkHighlights = $this->highlightDao->getHighlightBulk(6, $eventId);
223 
224  assertThat(count($bulkHighlights), is(1));
225 
227  $bulkHighlight1 = $bulkHighlights[0];
228  assertThat($bulkHighlight1->getLicenseId(), is(equalTo($licenseId1)));
229  assertThat($bulkHighlight1->getType(), is(equalTo(Highlight::BULK)));
230  assertThat($bulkHighlight1->getStart(), is(3));
231  assertThat($bulkHighlight1->getEnd(), is(103));
232 
233  $rfForACE = 213;
234  assertThat($relevantDecisionsItem6,hasKeyInArray($rfForACE));
236  $clearingEvent = $relevantDecisionsItem6[$rfForACE];
237  $eventId = $clearingEvent->getEventId();
238  $bulkHighlights = $this->highlightDao->getHighlightBulk(6, $eventId);
239 
240  assertThat(count($bulkHighlights), is(1));
241 
243  $bulkHighlight2 = $bulkHighlights[0];
244  assertThat($bulkHighlight2->getLicenseId(), is(equalTo($licenseId2)));
245  assertThat($bulkHighlight2->getType(), is(equalTo(Highlight::BULK)));
246  assertThat($bulkHighlight2->getStart(), is(3));
247  assertThat($bulkHighlight2->getEnd(), is(103));
248 
249  $bulkHighlights = $this->highlightDao->getHighlightBulk(6);
250 
251  assertThat(count($bulkHighlights), is(equalTo(2)));
252  assertThat($bulkHighlights, containsInAnyOrder($bulkHighlight1, $bulkHighlight2));
253  }
254 
256  public function testRunMonkBulkScanWithMultipleLicenses()
257  {
258  $this->setUpTables();
259  $this->setUpRepo();
260 
261  $userId = 2;
262  $groupId = 2;
263  $uploadTreeId = 1;
264 
265  $licenseId = 225;
266  $removing = false;
267  $refText = "The GNU General Public License is a free, copyleft license for software and other kinds of works.";
268 
269  $bulkId = $this->licenseDao->insertBulkLicense($userId, $groupId, $uploadTreeId,
270  array($licenseId => array($removing,"","","")), $refText);
271 
272  $this->assertGreaterThan($expected = 0, $bulkId);
273 
274  $jobId = 64;
275  list($output, $retCode) = $this->runBulkMonk($userId, $groupId, $jobId, $bulkId);
276  $this->assertEquals(6, $this->getHeartCount($output));
277  $this->rmRepo();
278 
279  $this->assertEquals($retCode, 0, 'monk bulk failed: ' . $output);
280  $bounds6 = new ItemTreeBounds(6, 'uploadtree_a', 1, 17, 18);
281  $bounds7 = new ItemTreeBounds(7, 'uploadtree_a', 1, 15, 16);
282  $relevantDecisionsItem6 = $this->clearingDao->getRelevantClearingEvents($bounds6, $groupId);
283  $relevantDecisionsItem7 = $this->clearingDao->getRelevantClearingEvents($bounds7, $groupId);
284 
285  assertThat(count($relevantDecisionsItem6), is(equalTo(1)));
286  assertThat(count($relevantDecisionsItem7), is(equalTo(1)));
287  $rfForACE = 225;
288  assertThat($relevantDecisionsItem6, hasKeyInArray($rfForACE));
290  $clearingEvent = $relevantDecisionsItem6[$rfForACE];
291  $eventId = $clearingEvent->getEventId();
292  $bulkHighlights = $this->highlightDao->getHighlightBulk(6, $eventId);
293 
294  $this->assertEquals(1, count($bulkHighlights));
295 
297  $bulkHighlight = $bulkHighlights[0];
298  $this->assertEquals($licenseId, $bulkHighlight->getLicenseId());
299  $this->assertEquals(Highlight::BULK, $bulkHighlight->getType());
300  $this->assertEquals(3, $bulkHighlight->getStart());
301  $this->assertEquals(103, $bulkHighlight->getEnd());
302 
303  $bulkHighlights = $this->highlightDao->getHighlightBulk(6);
304 
305  $this->assertEquals(1, count($bulkHighlights));
306  $this->assertEquals($bulkHighlight, $bulkHighlights[0]);
307  }
308 
311  {
312  $this->setUpTables();
313  $this->setUpRepo();
314 
315  $userId = 2;
316  $groupId = 2;
317  $uploadTreeId = 1;
318 
319  $licenseId = 225;
320  $removing = false;
321  $refText = "The GNU General Public License is copyleft license for software and other kinds of works.";
322 
323  $jobId = 64;
324 
325  $bulkId = $this->licenseDao->insertBulkLicense($userId, $groupId, $uploadTreeId,
326  array($licenseId => array($removing,"","","")), $refText);
327 
328  $this->assertGreaterThan($expected=0, $bulkId);
329 
330  list($output,$retCode) = $this->runBulkMonk($userId, $groupId, $jobId, $bulkId);
331 
332  $this->rmRepo();
333 
334  $this->assertEquals($retCode, 0, "monk bulk failed: $output");
335  $bounds6 = new ItemTreeBounds(6, 'uploadtree_a', 1, 17, 18);
336  $bounds7 = new ItemTreeBounds(7, 'uploadtree_a', 1, 15, 16);
337  $relevantDecisionsItem6 = $this->clearingDao->getRelevantClearingEvents($bounds6, $groupId);
338  $relevantDecisionsItem7 = $this->clearingDao->getRelevantClearingEvents($bounds7, $groupId);
339 
340  $this->assertEquals($expected=0, count($relevantDecisionsItem6));
341  $this->assertEquals($expected=0, count($relevantDecisionsItem7));
342  }
343 
345  public function testRunMonkBulkScanWithAShortSearch()
346  {
347  $this->setUpTables();
348  $this->setUpRepo();
349 
350  $userId = 2;
351  $groupId = 2;
352  $uploadTreeId = 1;
353 
354  $licenseId = 225;
355  $removing = false;
356  $refText = "The GNU";
357 
358  $bulkId = $this->licenseDao->insertBulkLicense($userId, $groupId, $uploadTreeId,
359  array($licenseId => array($removing,"","","")), $refText);
360 
361  $this->assertGreaterThan($expected=0, $bulkId);
362 
363  $jobId = 64;
364  list($output,$retCode) = $this->runBulkMonk($userId, $groupId, $jobId, $bulkId);
365 
366  $this->rmRepo();
367 
368  $this->assertEquals($retCode, 0, 'monk bulk failed: '.$output);
369  $bounds6 = new ItemTreeBounds(6, 'uploadtree_a', 1, 17, 18);
370  $bounds7 = new ItemTreeBounds(7, 'uploadtree_a', 1, 15, 16);
371  $relevantDecisionsItem6 = $this->clearingDao->getRelevantClearingEvents($bounds6, $groupId);
372  $relevantDecisionsItem7 = $this->clearingDao->getRelevantClearingEvents($bounds7, $groupId);
373 
374  assertThat(count($relevantDecisionsItem6),is(equalTo(1)));
375  assertThat(count($relevantDecisionsItem7),is(equalTo(1)));
376  $rfForACE = 225;
377  assertThat($relevantDecisionsItem6,hasKeyInArray($rfForACE));
379  $clearingEvent = $relevantDecisionsItem6[$rfForACE];
380  $eventId = $clearingEvent->getEventId();
381  $bulkHighlights = $this->highlightDao->getHighlightBulk(6, $eventId);
382 
383  $this->assertEquals(1, count($bulkHighlights));
384 
386  $bulkHighlight = $bulkHighlights[0];
387  $this->assertEquals($licenseId, $bulkHighlight->getLicenseId());
388  $this->assertEquals(Highlight::BULK, $bulkHighlight->getType());
389  $this->assertEquals(3, $bulkHighlight->getStart());
390  $this->assertEquals(10, $bulkHighlight->getEnd());
391 
392  $bulkHighlights = $this->highlightDao->getHighlightBulk(6);
393 
394  $this->assertEquals(1, count($bulkHighlights));
395  $this->assertEquals($bulkHighlight, $bulkHighlights[0]);
396  }
397 
400  {
401  $this->setUpTables();
402  $this->setUpRepo();
403 
404  $userId = 2;
405  $groupId = 2;
406  $uploadTreeId = 1;
407 
408  $licenseId = 225;
409  $removing = false;
410  $refText = "";
411 
412  $bulkId = $this->licenseDao->insertBulkLicense($userId, $groupId, $uploadTreeId,
413  array($licenseId => array($removing,"","","")), $refText);
414 
415  $this->assertGreaterThan($expected=0, $bulkId);
416 
417  $jobId = 64;
418  list($output,$retCode) = $this->runBulkMonk($userId, $groupId, $jobId, $bulkId);
419 
420  $this->rmRepo();
421 
422  $this->assertEquals($retCode, 0, 'monk bulk failed: '.$output);
423  $bounds6 = new ItemTreeBounds(6, 'uploadtree_a', 1, 17, 18);
424  $bounds7 = new ItemTreeBounds(7, 'uploadtree_a', 1, 15, 16);
425  $relevantDecisionsItem6 = $this->clearingDao->getRelevantClearingEvents($bounds6, $groupId);
426  $relevantDecisionsItem7 = $this->clearingDao->getRelevantClearingEvents($bounds7, $groupId);
427 
428  assertThat(count($relevantDecisionsItem6),is(equalTo(0)));
429  assertThat(count($relevantDecisionsItem7),is(equalTo(0)));
430 
431  $bulkHighlights = $this->highlightDao->getHighlightBulk(6);
432 
433  assertThat(count($bulkHighlights),is(equalTo(0)));
434  }
435 }
testRunTwoIndependentMonkBulkScans()
Definition: bulkTest.php:131
testRunMonkBulkScanWithBadSearchForDiff()
Definition: bulkTest.php:310
testRunMonkBulkScanWithAnEmptySearchText()
Definition: bulkTest.php:399
fo_dbManager * dbManager
fo_dbManager object
Definition: process.c:16