FOSSology  4.7.1
Open Source License Compliance by Open Source Software
UploadDaoTest.php
1 <?php
2 /*
3  SPDX-FileCopyrightText: © 2014-2015 Siemens AG
4  Author: Steffen Weber, Johannes Najjar
5 
6  SPDX-License-Identifier: GPL-2.0-only
7 */
8 
9 namespace Fossology\Lib\Dao;
10 
11 use DateTime;
12 use Exception;
19 use Mockery as M;
20 
21 class UploadDaoTest extends \PHPUnit\Framework\TestCase
22 {
23  public $assertCountBefore;
24 
26  private $testDb;
28  private $dbManager;
30  private $uploadDao;
31 
32  protected function setUp() : void
33  {
34  $this->testDb = new TestPgDb();
35  $this->dbManager = &$this->testDb->getDbManager();
36 
37  $this->testDb->createPlainTables(array('upload', 'uploadtree',
38  'report_info', 'upload_events', 'users'));
39 
40  $this->dbManager->prepare($stmt = 'insert.upload',
41  "INSERT INTO upload (upload_pk, uploadtree_tablename) VALUES ($1, $2)");
42  $uploadArray = array(array(1, 'uploadtree'), array(2, 'uploadtree_a'));
43  foreach ($uploadArray as $uploadEntry) {
44  $this->dbManager->freeResult($this->dbManager->execute($stmt, $uploadEntry));
45  }
46  $logger = M::mock('Monolog\Logger'); // new Logger("UploadDaoTest");
47  $logger->shouldReceive('debug');
48  $uploadPermissionDao = M::mock('Fossology\Lib\Dao\UploadPermissionDao');
49  $this->uploadDao = new UploadDao($this->dbManager, $logger, $uploadPermissionDao);
50 
51  $this->assertCountBefore = \Hamcrest\MatcherAssert::getCount();
52  }
53 
54  protected function tearDown() : void
55  {
56  $this->addToAssertionCount(\Hamcrest\MatcherAssert::getCount()-$this->assertCountBefore);
57  $this->testDb = null;
58  $this->dbManager = null;
59  }
60 
61  public function testGetFileTreeBounds()
62  {
63  $uploadTreeId = 103;
64  $left = 1;
65  $uploadId = 101;
66  $this->dbManager->queryOnce("INSERT INTO uploadtree (uploadtree_pk, parent, upload_fk, pfile_fk, ufile_mode, lft, rgt, ufile_name)"
67  . " VALUES ($uploadTreeId, NULL, $uploadId, 1, 33792, $left, 2, 'WXwindows.txt');",
68  __METHOD__ . '.insert.data');
70  $itemTreeBounds = $this->uploadDao->getItemTreeBounds($uploadTreeId);
71  assertThat($itemTreeBounds, anInstanceOf('Fossology\Lib\Data\Tree\ItemTreeBounds'));
72 
73  assertThat($uploadId, equalTo($itemTreeBounds->getUploadId()));
74  assertThat($left, equalTo($itemTreeBounds->getLeft()));
75  }
76 
77  public function testGetNextItemWithEmptyArchive()
78  {
79  $this->prepareModularTable();
80 
81  $nextItem = $this->uploadDao->getNextItem(1, 1);
82  assertThat($nextItem, is(nullValue()));
83  }
84 
85  public function testGetPreviousItemWithEmptyArchive()
86  {
87  $this->prepareModularTable();
88 
89  $nextItem = $this->uploadDao->getPreviousItem(1, 1);
90  assertThat($nextItem, is(nullValue()));
91  }
92 
93  public function testGetNextItemWithSingleFile()
94  {
95  $subentries = $this->getSubentriesForSingleFile();
96  $this->prepareModularTable($subentries);
97 
98  $nextItem = $this->uploadDao->getNextItem(1, 1);
99  assertThat($nextItem->getId(), is(6));
100  }
101 
102  public function testGetPreviousItemWithSingleFile()
103  {
104  $subentries = $this->getSubentriesForSingleFile();
105  $this->prepareModularTable($subentries);
106 
107  $nextItem = $this->uploadDao->getPreviousItem(1, 1);
108  assertThat($nextItem, is(nullValue()));
109  }
110 
111  public function testGetNextItemWithNestedFile()
112  {
113  $subentries = $this->getSubentriesForNestedFile();
114  $this->prepareModularTable($subentries);
115 
116  $nextItem = $this->uploadDao->getNextItem(1, 1);
117  assertThat($nextItem->getId(), is(8));
118  }
119 
120  public function testGetPreviousItemWithNestedFile()
121  {
122  $subentries = $this->getSubentriesForNestedFile();
123  $this->prepareModularTable($subentries);
124 
125  $nextItem = $this->uploadDao->getPreviousItem(1, 1);
126  assertThat($nextItem, is(nullValue()));
127  }
128 
129  public function testGetNextItemWithFileAfterEmptyDirectory()
130  {
131  $subentries = $this->getSubentriesForFileAfterEmptyDirectory();
132  $this->prepareModularTable($subentries);
133 
134  $nextItem = $this->uploadDao->getNextItem(1, 1);
135  assertThat($nextItem->getId(), is(8));
136  }
137 
138  public function testGetPreviousItemWithFileAfterEmptyDirectory()
139  {
140  $subentries = $this->getSubentriesForFileAfterEmptyDirectory();
141  $this->prepareModularTable($subentries);
142 
143  $nextItem = $this->uploadDao->getPreviousItem(1, 1);
144  assertThat($nextItem, is(nullValue()));
145  }
146 
147  public function testGetNextItemWithMultipleFiles()
148  {
149  $subentries = $this->getSubentriesForMultipleFiles();
150  $this->prepareModularTable($subentries);
151 
152  $nextItem = $this->uploadDao->getNextItem(1, 6);
153  assertThat($nextItem->getId(), is(7));
154  }
155 
156  public function testGetPreviousItemWithMultipleFiles()
157  {
158  $subentries = $this->getSubentriesForMultipleFiles();
159  $this->prepareModularTable($subentries);
160 
161  $nextItem = $this->uploadDao->getPreviousItem(1, 6);
162  assertThat($nextItem, anInstanceOf(Item::class));
163  assertThat($nextItem->getId(), is(8));
164  }
165 
170  protected function prepareUploadTree($uploadTreeArray = array())
171  {
172  $this->dbManager->prepare($stmt = 'insert.uploadtree',
173  "INSERT INTO uploadtree (uploadtree_pk, parent, upload_fk, pfile_fk, ufile_mode, lft, rgt, ufile_name) VALUES ($1, $2, $3, $4, $5, $6, $7, $8)");
174  foreach ($uploadTreeArray as $uploadTreeEntry) {
175  $this->dbManager->freeResult($this->dbManager->execute($stmt, $uploadTreeEntry));
176  }
177  }
178 
179 
183  protected function prepareModularTable($subentries = array())
184  {
185  $right_base = 5 + count($subentries) * 2;
186 
187  $uploadTreeArray = array_merge(
188  array(
189  array(1, null, 1, 1, 536904704, 1, $right_base + 5, 'archive.tar.gz'),
190  array(2, 1, 1, 0, 805323776, 2, $right_base + 4, 'artifact.dir'),
191  array(3, 2, 1, 2, 536903680, 3, $right_base + 3, 'archive.tar'),
192  array(4, 3, 1, 0, 805323776, 4, $right_base + 2, 'artifact.dir'),
193  array(5, 4, 1, 0, 536888320, 5, $right_base + 1, 'archive')),
194  $subentries);
195  $this->prepareUploadTree($uploadTreeArray);
196  }
197 
201  protected function getSubentriesForSingleFile()
202  {
203  return array(array(6, 5, 1, 3, 33188, 6, 10, 'README'));
204  }
205 
209  protected function getSubentriesForNestedFile()
210  {
211  return array(
212  array(6, 5, 1, 0, 536888320, 7, 12, 'docs'),
213  array(7, 6, 1, 0, 536888320, 8, 11, 'txt'),
214  array(8, 7, 1, 3, 33188, 9, 10, 'README')
215  );
216  }
217 
222  {
229  return array(
230  array(6, 5, 1, 0, 536888320, 7, 10, 'docs'),
231  array(7, 6, 1, 0, 536888320, 8, 9, 'txt'),
232  array(8, 5, 1, 3, 33188, 11, 12, 'README')
233  );
234  }
235 
239  protected function getSubentriesForMultipleFiles()
240  {
241  return array(
242  array(6, 5, 1, 3, 33188, 9, 10, 'INSTALL'),
243  array(7, 5, 1, 4, 33188, 11, 12, 'README'),
244  array(8, 5, 1, 5, 33188, 7, 8, 'COPYING')
245  );
246  }
247 
290  private $entries = array(
291  3653, 3668, 3683, 3685, 3671, 3665, 3676, 3675, 3681, 3677, 3673, 3658, 3660, 3686,
292  );
293 
294  protected function getTestFileStructure()
295  {
296  $isFile = 33188;
297  $isContainer = 536888320;
298  return array(
299  array(3650, NULL, 32, 3286, 536904704, 1, 76, 'uploadDaoTest.tar'),
300  array(3651, 3650, 32, 0, 805323776, 2, 75, 'artifact.dir'),
301  array(3652, 3651, 32, 0, 536888320, 3, 74, 'uploadDaoTest'),
302 
303  array(3653, 3652, 32, 3287, $isFile, 4, 5, 'A'),
304  array(3663, 3652, 32, 3287, $isContainer, 6, 7, 'B'),
305  array(3668, 3652, 32, 3294, $isFile, 8, 9, 'C'),
306  array(3682, 3652, 32, 0, $isContainer, 10, 16, 'D'),
307  array(3683, 3682, 32, 3303, $isFile, 11, 12, 'E'),
308  // * D/F_NoLic 13:14
309  array(3685, 3682, 32, 3305, $isFile, 14, 15, 'G'),
310  array(3669, 3652, 32, 0, $isContainer, 16, 23, 'H'),
311  // * H/I_NoLic
312  array(3671, 3669, 32, 3296, $isFile, 19, 20, 'J'),
313  // * H/K_NoLic 21:22
314  array(3661, 3652, 32, 0, $isContainer, 24, 37, 'L'),
315  array(3666, 3661, 32, 0, $isContainer, 25, 28, 'L1'),
316  array(3667, 3666, 32, 0, $isContainer, 26, 27, 'L1a'), // * L/L1/L1a_NoLic 26:27
317  array(3664, 3661, 32, 0, $isContainer, 29, 32, 'L2'),
318  array(3665, 3664, 32, 3292, $isFile, 30, 31, 'L2a'),
319  array(3662, 3661, 32, 0, $isContainer, 33, 36, 'L3'),
320  // * L/L3/L3a_NoLic 34:35
321  array(3654, 3652, 32, 0, $isContainer, 38, 41, 'M'),
322  array(3655, 3654, 32, 0, $isContainer, 39, 40, 'M1'),
323  array(3674, 3652, 32, 0, $isContainer, 42, 57, 'N'),
324  array(3676, 3674, 32, 3293, $isFile, 43, 44, 'N1'),
325  array(3678, 3674, 32, 0, $isContainer, 45, 48, 'N2'),
326  // * N/N2/N2a_NoLic 46:47
327  array(3675, 3674, 32, 3299, $isFile, 49, 50, 'N3'),
328  array(3680, 3674, 32, 0, $isContainer, 51, 54, 'N4'),
329  array(3681, 3680, 32, 3302, $isFile, 52, 53, 'N4a'),
330  array(3677, 3674, 32, 3300, $isFile, 55, 56, 'N5'),
331  array(3673, 3652, 32, 3298, $isFile, 58, 59, 'O'),
332  array(3656, 3652, 32, 0, $isContainer, 60, 69, 'P'),
333  // * P/P1_NoLic 61:62
334  array(3657, 3656, 32, 0, $isContainer, 63, 66, 'P2'),
335  array(3658, 3657, 32, 3288, $isFile, 64, 65, 'P2a'),
336  array(3660, 3656, 32, 3290, 33188, 67, 68, 'P3'),
337  array(3686, 3652, 32, 3306, 33188, 70, 71, 'R'),
338  // * S_NoLic 72:73
339  );
340  }
341 
342  public function testGetNextItemUsesRecursiveAndRegularSearchAsFallback()
343  {
344  $this->prepareUploadTree($this->getTestFileStructure());
345 
346  // L1 -> N1
347  $nextItem = $this->uploadDao->getNextItem(32, 3666);
348  assertThat($nextItem->getId(), is(3665));
349  }
350 
351  public function testGetPrevItemUsesRecursiveAndRegularSearchAsFallback()
352  {
353  $this->prepareUploadTree($this->getTestFileStructure());
354 
355  $nextItem = $this->uploadDao->getPreviousItem(32, 3666);
356  assertThat($nextItem->getId(), is(3671));
357  }
358 
359  public function testGetNextItemUsesRecursiveOnly()
360  {
361  $this->prepareUploadTree($this->getTestFileStructure());
362 
363  $nextItem = $this->uploadDao->getNextItem(32, 3674);
364  assertThat($nextItem->getId(), is(3676));
365  }
366 
367  public function testGetPrevItemUsesRecursiveOnly()
368  {
369  $this->prepareUploadTree($this->getTestFileStructure());
370 
371  $nextItem = $this->uploadDao->getPreviousItem(32, 3674);
372  assertThat($nextItem->getId(), is(3665));
373  }
374 
375  public function testGetNextFull()
376  {
377  $this->prepareUploadTree($this->getTestFileStructure());
378 
379  $previousId = 3650;
380  foreach ($this->entries as $entry) {
381  $nextItem = $this->uploadDao->getNextItem(32, $previousId);
382  assertThat($nextItem->getId(), is($entry));
383  $previousId = $entry;
384  }
385 
386  $nextItem = $this->uploadDao->getNextItem(32, $previousId);
387  assertThat($nextItem, is(nullValue()));
388  }
389 
390  public function testGetPreviousFull()
391  {
392  $this->prepareUploadTree($this->getTestFileStructure());
393 
394  $entries = array_reverse($this->entries);
395 
396  $previousId = $entries[0];
397  foreach (array_slice($entries, 1) as $entry) {
398  $previousItem = $this->uploadDao->getPreviousItem(32, $previousId);
399  assertThat($previousItem->getId(), is($entry));
400  $previousId = $entry;
401  }
402 
403  $previousItem = $this->uploadDao->getPreviousItem(32, $previousId);
404  assertThat($previousItem, is(nullValue()));
405  }
406 
407 
408  public function testCountNonArtifactDescendants()
409  {
410  $this->dbManager->queryOnce('ALTER TABLE uploadtree RENAME TO uploadtree_a');
411  $this->testDb->insertData(array('uploadtree_a'));
412 
413  $artifact = new ItemTreeBounds(2,'uploadtree_a', 1, 2, 3);
414  $artifactDescendants = $this->uploadDao->countNonArtifactDescendants($artifact);
415  assertThat($artifactDescendants, is(0));
416 
417  $zip = new ItemTreeBounds(1,'uploadtree_a', 1, 1, 24);
418  $zipDescendants = $this->uploadDao->countNonArtifactDescendants($zip);
419  assertThat($zipDescendants, is(count(array(6,7,8,10,11,12)) ) );
420  }
421 
422 
423  public function testGetUploadParent()
424  {
425  $this->prepareUploadTree($this->getTestFileStructure());
426  $topId = $this->uploadDao->getUploadParent(32);
427  assertThat($topId,equalTo(3650));
428  }
429 
430  public function testGetUploadParentFromBrokenTree()
431  {
432  $this->expectException(Exception::class);
433  $this->expectExceptionMessage("Missing upload tree parent for upload");
434  $this->prepareUploadTree(array(array(4651, 3650, 33, 0, 805323776, 2, 75, 'artifact.dir')));
435  $this->uploadDao->getUploadParent(33);
436  }
437 
438  public function testGetUploadParentFromNonExistingTree()
439  {
440  $this->expectException(Exception::class);
441  $this->expectExceptionMessage("Missing upload tree parent for upload");
442  $this->uploadDao->getUploadParent(34);
443  }
444 
445  public function testGetUploadHashes()
446  {
447  $this->testDb->createPlainTables(array('pfile'));
448  $this->dbManager->queryOnce('TRUNCATE upload');
449  $this->testDb->insertData(array('upload','pfile'));
450  // (pfile_pk, pfile_md5, pfile_sha1, pfile_sha256, pfile_size) := (755, 'E7295A5773D0EA17D53CBE6293924DD4', '93247C8DB814F0A224B75B522C1FA4DC92DC3078', 'E29ABC32DB8B6241D598BC7C76681A7623D176D85F99E738A56C0CB684C367E1', 10240)
451  $hashes = $this->uploadDao->getUploadHashes(44);
452  assertThat($hashes,equalTo(array('md5'=>'E7295A5773D0EA17D53CBE6293924DD4','sha1'=>'93247C8DB814F0A224B75B522C1FA4DC92DC3078','sha256'=>'E29ABC32DB8B6241D598BC7C76681A7623D176D85F99E738A56C0CB684C367E1')));
453  }
454 
455  public function testGetFatItem()
456  {
457  $this->prepareUploadTree($this->getTestFileStructure());
458  $isContainer = 536888320;
459  $itemM1a = 13655;
460  $this->prepareUploadTree(array(array($itemM1a, 3655, 32, 0, $isContainer, 39+0, 40-0, 'M1a')));
461  $this->dbManager->queryOnce('UPDATE uploadtree SET realparent=parent WHERE ufile_mode&(1<<28)=0',__METHOD__.'.fixRealparent');
462 
463  $fatA = $this->uploadDao->getFatItemId($itemA=3653, 32, 'uploadtree');
464  assertThat($fatA,equalTo($itemA));
465  $fatB = $this->uploadDao->getFatItemId($itemBEmpty=3663, 32, 'uploadtree');
466  assertThat($fatB, equalTo($itemBEmpty));
467  $fatD = $this->uploadDao->getFatItemId($itemDFolder=3682, 32, 'uploadtree');
468  assertThat($fatD, equalTo($itemDFolder));
469  $fatL1 = $this->uploadDao->getFatItemId($itemL1ToFolder=3666, 32, 'uploadtree');
470  assertThat($fatL1, equalTo(3667));
471  $fatL2 = $this->uploadDao->getFatItemId($itemL2ToItem=3664, 32, 'uploadtree');
472  assertThat($fatL2, equalTo(3665));
473 
474  $fatM = $this->uploadDao->getFatItemId(3654, 32, 'uploadtree');
475  assertThat($fatM, equalTo($itemM1a));
476  }
477 
487  public function testGetAssigneeDate()
488  {
489  $time_now = new DateTime();
490  $time_1_day = DateTime::createFromFormat("U",
491  strtotime($time_now->format(DATE_ATOM) . " +1 day"));
492  $this->dbManager->insertTableRow("upload_events", [
493  "upload_fk" => 2,
494  "event_ts" => $time_now->format(DATE_ATOM),
495  "event_type" => UploadEvents::ASSIGNEE_EVENT
496  ]);
497  $this->dbManager->insertTableRow("upload_events", [
498  "upload_fk" => 2,
499  "event_ts" => $time_1_day->format(DATE_ATOM),
500  "event_type" => UploadEvents::UPLOAD_CLOSED_EVENT
501  ]);
502  $assigneeDate = $this->uploadDao->getAssigneeDate(2);
503  assertThat(strtotime($assigneeDate), equalTo($time_now->getTimestamp()));
504  self::assertThat($this->uploadDao->getAssigneeDate(3), self::equalTo(null));
505  }
506 
516  public function testGetClosedDate()
517  {
518  $time_now = new DateTime();
519  $time_1_day = DateTime::createFromFormat("U",
520  strtotime($time_now->format(DATE_ATOM) . " +1 day"));
521  $this->dbManager->insertTableRow("upload_events", [
522  "upload_fk" => 2,
523  "event_ts" => $time_now->format(DATE_ATOM),
524  "event_type" => UploadEvents::ASSIGNEE_EVENT
525  ]);
526  $this->dbManager->insertTableRow("upload_events", [
527  "upload_fk" => 2,
528  "event_ts" => $time_1_day->format(DATE_ATOM),
529  "event_type" => UploadEvents::UPLOAD_CLOSED_EVENT
530  ]);
531  $assigneeDate = $this->uploadDao->getClosedDate(2);
532  assertThat(strtotime($assigneeDate), equalTo($time_1_day->getTimestamp()));
533  self::assertThat($this->uploadDao->getClosedDate(3), self::equalTo(null));
534  }
535 
540  private function setUploadOwnerCyclonedxDefaults($userSettings)
541  {
542  $this->dbManager->insertTableRow('users', ['user_pk' => 7,
543  'user_name' => 'cdx', 'cyclonedx_settings' => $userSettings]);
544  $this->dbManager->getSingleRow("UPDATE upload SET user_fk = 7 WHERE upload_pk = 2",
545  [], __METHOD__);
546  }
547 
548  public function testGetCyclonedxSettingsFallsBackToOwnerDefaults()
549  {
550  $this->setUploadOwnerCyclonedxDefaults('checked,unchecked,checked,acme:,1.5');
551  $this->dbManager->insertTableRow('report_info', ['upload_fk' => 2]);
552 
553  $settings = $this->uploadDao->getCyclonedxSettings(2);
554  $this->assertTrue($settings->isOsselotExport());
555  $this->assertFalse($settings->isLicenseCommentChecked());
556  $this->assertTrue($settings->isIgnoreFilesWithoutInfo());
557  $this->assertSame('acme:', $settings->getTagNamespace());
558  $this->assertSame('1.5', $settings->getSpecVersion());
559  }
560 
561  public function testGetCyclonedxSettingsPrefersStoredSettings()
562  {
563  $this->setUploadOwnerCyclonedxDefaults('checked,checked,checked,acme:,1.5');
564  $stored = new CycloneDxSettings(false, false, false, 'own:', '1.6');
565  $this->dbManager->insertTableRow('report_info', ['upload_fk' => 2,
566  'ri_cyclonedx_selection' => $stored->toReportInfo()]);
567 
568  $this->assertEquals($stored, $this->uploadDao->getCyclonedxSettings(2));
569  }
570 
571  public function testNewReportInfoRowIsSeededFromOwnerDefaults()
572  {
573  $this->setUploadOwnerCyclonedxDefaults('unchecked,checked,unchecked,acme:,1.4');
574 
575  $this->assertEquals(0, $this->uploadDao->getGlobalDecisionSettingsFromInfo(2, 0));
576  $row = $this->dbManager->getSingleRow(
577  "SELECT ri_cyclonedx_selection FROM report_info WHERE upload_fk = 2", [], __METHOD__);
578  $this->assertEquals(new CycloneDxSettings(true, false, false, 'acme:', '1.4'),
579  CycloneDxSettings::fromReportInfo($row['ri_cyclonedx_selection']));
580  }
581 
582  public function testUpdateReportInfoMergesCyclonedxSettings()
583  {
584  $this->setUploadOwnerCyclonedxDefaults('checked,checked,unchecked,acme:,1.5');
585  $this->dbManager->insertTableRow('report_info', ['upload_fk' => 2]);
586 
587  $this->uploadDao->updateReportInfo(2, 'ri_cyclonedx_selection',
588  '{"specVersion":"1.4","osselotExport":"false"}');
589 
590  $this->assertEquals(new CycloneDxSettings(true, false, false, 'acme:', '1.4'),
591  $this->uploadDao->getCyclonedxSettings(2));
592  }
593 
594  public static function provideInvalidCyclonedxUpdates()
595  {
596  return [
597  'not an object' => ['"1.4"'],
598  'unknown key' => ['{"spec":"1.4"}'],
599  'comma in namespace' => ['{"tagNamespace":"a,b:"}'],
600  'unsupported version' => ['{"specVersion":"2.0"}'],
601  'not a boolean' => ['{"licenseComment":"maybe"}'],
602  ];
603  }
604 
609  {
610  $this->dbManager->insertTableRow('report_info', ['upload_fk' => 2]);
611  $this->expectException(\UnexpectedValueException::class);
612  $this->uploadDao->updateReportInfo(2, 'ri_cyclonedx_selection', $value);
613  }
614 }
setUploadOwnerCyclonedxDefaults($userSettings)
testUpdateReportInfoRejectsInvalidCyclonedxSettings($value)
prepareModularTable($subentries=array())
prepareUploadTree($uploadTreeArray=array())
CycloneDX export settings of a user (defaults) or of an upload.
This class contains the events for the upload_events table.
fo_dbManager * dbManager
fo_dbManager object
Definition: process.c:16
Bounds of an item within an uploadtree table.
Definition: ReuserTypes.hpp:14