FOSSology  4.4.0
Open Source License Compliance by Open Source Software
TreeDaoTest.php
1 <?php
2 /*
3  SPDX-FileCopyrightText: © 2014-2015 Siemens AG
4  SPDX-FileCopyrightText: © 2017 TNG Technology Consulting GmbH
5  Author: Steffen Weber, Johannes Najjar, Maximilian Huber
6 
7  SPDX-License-Identifier: GPL-2.0-only
8 */
9 
10 namespace Fossology\Lib\Dao;
11 
14 
15 class TreeDaoTest extends \PHPUnit\Framework\TestCase
16 {
18  private $testDb;
20  private $dbManager;
22  private $treeDao;
23 
24  protected function setUp() : void
25  {
26  $this->testDb = new TestPgDb();
27  $this->dbManager = &$this->testDb->getDbManager();
28 
29  $this->testDb->createPlainTables(array('upload','uploadtree'));
30 
31  $this->dbManager->prepare($stmt = 'insert.upload',
32  "INSERT INTO upload (upload_pk, uploadtree_tablename) VALUES ($1, $2)");
33  $uploadArray = array(array(1, 'uploadtree'), array(32, 'uploadtree'));
34  foreach ($uploadArray as $uploadEntry) {
35  $this->dbManager->freeResult($this->dbManager->execute($stmt, $uploadEntry));
36  }
37  $this->treeDao = new TreeDao($this->dbManager);
38  $this->assertCountBefore = \Hamcrest\MatcherAssert::getCount();
39  }
40 
41  protected function tearDown() : void
42  {
43  $this->addToAssertionCount(\Hamcrest\MatcherAssert::getCount()-$this->assertCountBefore);
44 
45  $this->testDb = null;
46  $this->dbManager = null;
47  }
48 
49  public function testGetMinimalCoveringItem()
50  {
51  $this->prepareModularTable(array());
52  $coverContainer = $this->treeDao->getMinimalCoveringItem(1, "uploadtree");
53  assertThat($coverContainer,equalTo(5));
54 
55  $this->prepareUploadTree(array(array($item=99,null,$upload=32,88,0x0,1,2,'plainFile',null)));
56  $coverSelf = $this->treeDao->getMinimalCoveringItem($upload, "uploadtree");
57  assertThat($coverSelf,equalTo($item));
58  }
59 
60  public function testGetFullPathFromSingleFolderUpload()
61  {
62  $this->prepareModularTable(array(array(6,5,1,0,0,5,6,$fName="file",5)));
63  $cover = $this->treeDao->getMinimalCoveringItem(1, "uploadtree");
64  assertThat($cover,equalTo(5));
65 
66  $path = $this->treeDao->getFullPath(6, "uploadtree", $cover);
67  assertThat($path,equalTo($fName));
68 
69  $path = $this->treeDao->getFullPath(6, "uploadtree", $cover, true);
70  assertThat($path,equalTo($fName));
71  }
72 
73  public function testGetFullPathFromSingleFolderUploadWithDropArtifact()
74  {
75  $this->prepareModularTable(array(array(6,5,1,0,0,5,6,$fName="file",5)));
76  $cover = $this->treeDao->getMinimalCoveringItem(1, "uploadtree", true);
77  assertThat($cover,equalTo(5));
78  $path = $this->treeDao->getFullPath(6, "uploadtree", $cover);
79  assertThat($path,equalTo($fName));
80  }
81 
82  public function testGetFullPathFromSingleFolderUploadWithAFileOutside()
83  {
84  $this->prepareModularTable(array(
85  array(6,5,1,0,0,5,6,"file",5),
86  array(11,4,1,0,0,11,12,"file2",3)
87  ));
88  $cover = $this->treeDao->getMinimalCoveringItem(1, "uploadtree");
89  assertThat($cover,equalTo(4));
90 
91  $pathInsideArchive = $this->treeDao->getFullPath(6, "uploadtree", $cover);
92  assertThat($pathInsideArchive,equalTo("archive/file"));
93 
94  $pathInsideArchive = $this->treeDao->getFullPath(6, "uploadtree", $cover, true);
95  assertThat($pathInsideArchive,equalTo("archive/file"));
96 
97  $pathOutsideArchive = $this->treeDao->getFullPath(11, "uploadtree", $cover);
98  assertThat($pathOutsideArchive,equalTo("file2"));
99 
100  $pathOutsideArchive = $this->treeDao->getFullPath(11, "uploadtree", $cover, true);
101  assertThat($pathOutsideArchive,equalTo("file2"));
102  }
103 
108  protected function prepareUploadTree($uploadTreeArray = array())
109  {
110  $this->dbManager->prepare($stmt = 'insert.uploadtree',
111  "INSERT INTO uploadtree (uploadtree_pk, parent, upload_fk, pfile_fk, ufile_mode, lft, rgt, ufile_name, realparent) VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9)");
112  foreach ($uploadTreeArray as $uploadTreeEntry) {
113  $this->dbManager->freeResult($this->dbManager->execute($stmt, $uploadTreeEntry));
114  }
115  }
116 
117 
122  protected function prepareModularTable($subentries = array())
123  {
124  $right_base = 5 + count($subentries) * 2;
125 
126  $uploadTreeArray = array_merge(
127  array(
128  array(1, null, 1, 1, 0x20008400, 1, $right_base + 5, 'archive.tar.gz', null),
129  array(2, 1, 1, 0, 0x30004400, 2, $right_base + 4, 'artifact.dir', 1),
130  array(3, 2, 1, 2, 0x20008000, 3, $right_base + 3, 'archive.tar', 1),
131  array(4, 3, 1, 0, 0x30004400, 4, $right_base + 2, 'artifact.dir', 3),
132  array(5, 4, 1, 0, 0x20004400, 5, $right_base + 1, 'archive', 3)),
133  $subentries);
134  $this->prepareUploadTree($uploadTreeArray);
135  }
136 
178  protected function getTestFileStructure()
179  {
180  return array(
181  array(3675, 3674, 32, 3299, 33188, 23, 24, 'N3',3674),
182  array(3674, 3652, 32, 0, 0x20004400, 22, 37, 'N',3652),
183  array(3673, 3652, 32, 3298, 33188, 38, 39, 'O',3652),
184  array(3671, 3669, 32, 3296, 33188, 9, 10, 'J',3669),
185  array(3669, 3652, 32, 0, 0x20004400, 4, 11, 'H',3652),
186  array(3668, 3652, 32, 3294, 33188, 42, 43, 'C',3652),
187  array(3662, 3661, 32, 0, 0x20004400, 45, 48, 'L3',3661),
188  array(3666, 3661, 32, 0, 0x20004400, 49, 52, 'L1',3661),
189  array(3665, 3664, 32, 3292, 33188, 54, 55, 'L2a',3664),
190  array(3664, 3661, 32, 0, 0x20004400, 53, 56, 'L2',3661),
191  array(3661, 3652, 32, 0, 0x20004400, 44, 57, 'L',3652),
192  array(3658, 3657, 32, 3288, 33188, 60, 61, 'P2a',3657),
193  array(3657, 3656, 32, 0, 0x20004400, 59, 62, 'P2',3656),
194  array(3660, 3656, 32, 3290, 33188, 65, 66, 'P3',6356),
195  array(3656, 3652, 32, 0, 0x20004400, 58, 67, 'P',3652),
196  array(3655, 3654, 32, 0, 0x20004400, 69, 70, 'M1',3654),
197  array(3654, 3652, 32, 0, 0x20004400, 68, 71, 'M',3652),
198  array(3653, 3652, 32, 3287, 33188, 72, 73, 'A',3652),
199  array(3652, 3651, 32, 0, 0x20004400, 3, 74, 'uploadDaoTest',3650),
200  array(3651, 3650, 32, 0, 0x30004400, 2, 75, 'artifact.dir',3650),
201  array(3650, NULL, 32, 3286, 0x20008400, 1, 76, 'uploadDaoTest.tar',null),
202  array(3686, 3652, 32, 3306, 33188, 12, 13, 'R',3652),
203  array(3683, 3682, 32, 3303, 33188, 15, 16, 'E',3682),
204  array(3685, 3682, 32, 3305, 33188, 17, 18, 'G',3682),
205  array(3682, 3652, 32, 0, 0x20004400, 14, 21, 'D',3652),
206  array(3677, 3674, 32, 3300, 33188, 25, 26, 'N5',3674),
207  array(3676, 3674, 32, 3293, 33188, 27, 28, 'N1',3674),
208  array(3678, 3674, 32, 0, 0x20004400, 29, 32, 'N2',3674),
209  array(3681, 3680, 32, 3302, 33188, 34, 35, 'N4a',3680),
210  array(3680, 3674, 32, 0, 0x20004400, 33, 36, 'N4',3674),
211  );
212  }
213 
214  public function testGetFullPathWithComlpexStructureFromFolder()
215  {
216  $this->prepareUploadTree($this->getTestFileStructure());
217  $cover = $this->treeDao->getMinimalCoveringItem(32, "uploadtree");
218  assertThat($cover,equalTo(3652));
219  assertThat($this->treeDao->getFullPath(3666, "uploadtree", $cover), equalTo("L/L1"));
220  assertThat($this->treeDao->getFullPath(3666, "uploadtree", $cover,true), equalTo("L/L1"));
221  }
222 
223  public function testGetFullPathWithComlpexStructureFromFile()
224  {
225  $this->prepareUploadTree($this->getTestFileStructure());
226  $cover = $this->treeDao->getMinimalCoveringItem(32, "uploadtree");
227  assertThat($cover,equalTo(3652));
228 
229  assertThat($this->treeDao->getFullPath(3665, "uploadtree", $cover), equalTo("L/L2/L2a"));
230  assertThat($this->treeDao->getFullPath(3665, "uploadtree", $cover, true), equalTo("L/L2/L2a"));
231 
232  assertThat($this->treeDao->getFullPath(3665, "uploadtree"), equalTo("uploadDaoTest.tar/uploadDaoTest/L/L2/L2a"));
233  assertThat($this->treeDao->getFullPath(3665, "uploadtree",0,true), equalTo( "uploadDaoTest/L/L2/L2a"));
234  }
235 
236  public function testGetFullPathWithComlpexStructureFromFileAndOtherUpload()
237  {
238  $this->prepareUploadTree($this->getTestFileStructure());
239  $this->prepareModularTable(array(array(6,5,1,0,0,5,6,"file",6)));
240  $cover = $this->treeDao->getMinimalCoveringItem(32, "uploadtree");
241  assertThat($cover,equalTo(3652));
242  assertThat($this->treeDao->getFullPath(3665, "uploadtree", $cover), equalTo("L/L2/L2a"));
243  assertThat($this->treeDao->getFullPath(3665, "uploadtree", $cover,true), equalTo("L/L2/L2a"));
244  assertThat($this->treeDao->getFullPath(3665, "uploadtree"), equalTo("uploadDaoTest.tar/uploadDaoTest/L/L2/L2a"));
245  assertThat($this->treeDao->getFullPath(3665, "uploadtree",0,true), equalTo( "uploadDaoTest/L/L2/L2a"));
246  }
247 
248  public function testGetUploadHashes()
249  {
250  $this->testDb->createPlainTables(array('pfile'));
251  $this->dbManager->queryOnce('ALTER TABLE uploadtree RENAME TO uploadtree_a');
252  $this->testDb->insertData(array('uploadtree_a','pfile'));
253  // (pfile_pk, pfile_md5, pfile_sha1, pfile_sha256, pfile_size) := (762, 'C655F0AD3E4D3F90D8EE0541DD636E2E', 'D62DCD25DC68180758FD1C064ADC91AB70A78CB1', '8F39AC8ADD8CD0C0C2BF8924CE3B24F4B2760B8723BFD4205A49FB142490A355', 34);
254  $hashes = $this->treeDao->getItemHashes(463,'uploadtree_a');
255  assertThat($hashes,equalTo(array('md5'=>'C655F0AD3E4D3F90D8EE0541DD636E2E','sha1'=>'D62DCD25DC68180758FD1C064ADC91AB70A78CB1','sha256'=>'8F39AC8ADD8CD0C0C2BF8924CE3B24F4B2760B8723BFD4205A49FB142490A355')));
256  }
257 
258  protected function getNestedTestFileStructure()
259  {
260  /*
261  * example.zip
262  * |
263  * \- file
264  * \~ subExample.tar.gz
265  * |
266  * \- subEample.tar
267  * |
268  * \- innerFile
269  * \~ someFolder
270  * |
271  * \- someFileInFolder
272  */
273  return array(
274  array(14, 13, 2, 8, 32768, 5, 6, 'someFileInFolder', 13, ),
275  array(13, 5, 2, 0, 536888320, 4, 7, 'someFolder', 5, ),
276  array(12, 11, 2, 7, 33188, 13, 14, 'innerFile', 11, ),
277  array(11, 10, 2, 0, 536888320, 12, 15, 'subExample', 9, ),
278  array(10, 9, 2, 0, 805323776, 11, 16, 'artifact.dir', 9, ),
279  array(9, 8, 2, 6, 536903680, 10, 17, 'subExample.tar', 7, ),
280  array(8, 7, 2, 0, 805323776, 9, 18, 'artifact.dir', 7, ),
281  array(7, 5, 2, 5, 536903680, 8, 19, 'subExample.tar.gz', 5, ),
282  array(6, 5, 2, 4, 32768, 20, 21, 'file', 5, ),
283  array(5, 4, 2, 0, 536888320, 3, 22, 'example', 2, ),
284  array(4, 2, 2, 0, 805323776, 2, 23, 'artifact.dir', 2, ),
285  array(3, 2, 2, 3, 268469248, 24, 25, 'artifact.meta', 2, ),
286  array(2, NULL, 2, 2, 536904704, 1, 26, 'example.zip', NULL, ),
287  );
288  }
289 
290  public function testGetFullPathWithNestedStructure()
291  {
292  $this->prepareUploadTree($this->getNestedTestFileStructure());
293  assertThat($this->treeDao->getFullPath(2, "uploadtree", 0), equalTo('example.zip'));
294  assertThat($this->treeDao->getFullPath(2, "uploadtree", 0,true), equalTo('example.zip'));
295 
296  assertThat($this->treeDao->getFullPath(6, "uploadtree", 0), equalTo('example.zip/example/file'));
297  assertThat($this->treeDao->getFullPath(6, "uploadtree", 0,true), equalTo( 'example/file'));
298 
299  assertThat($this->treeDao->getFullPath(12, "uploadtree", 0), equalTo('example.zip/example/subExample.tar.gz/subExample.tar/subExample/innerFile'));
300  assertThat($this->treeDao->getFullPath(12, "uploadtree", 0,true), equalTo( 'example/subExample.tar.gz/subExample.tar/subExample/innerFile'));
301 
302  assertThat($this->treeDao->getFullPath(12, "uploadtree", 5), equalTo('subExample.tar.gz/subExample.tar/subExample/innerFile'));
303  assertThat($this->treeDao->getFullPath(12, "uploadtree", 5,true), equalTo( 'subExample/innerFile'));
304 
305  assertThat($this->treeDao->getFullPath(12, "uploadtree", 7), equalTo('subExample.tar/subExample/innerFile'));
306  assertThat($this->treeDao->getFullPath(12, "uploadtree", 7,true), equalTo( 'subExample/innerFile'));
307  }
308 }
prepareUploadTree($uploadTreeArray=array())
prepareModularTable($subentries=array())
fo_dbManager * dbManager
fo_dbManager object
Definition: process.c:16