FOSSology  3.2.0rc1
Open Source License Compliance by Open Source Software
TreeDaoTest.php
1 <?php
2 /*
3 Copyright (C) 2014-2015, Siemens AG
4 Copyright (C) 2017 TNG Technology Consulting GmbH
5 Author: Steffen Weber, Johannes Najjar, Maximilian Huber
6 
7 This program is free software; you can redistribute it and/or
8 modify it under the terms of the GNU General Public License
9 version 2 as published by the Free Software Foundation.
10 
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15 
16 You should have received a copy of the GNU General Public License along
17 with this program; if not, write to the Free Software Foundation, Inc.,
18 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19 */
20 
21 namespace Fossology\Lib\Dao;
22 
25 
26 class TreeDaoTest extends \PHPUnit\Framework\TestCase
27 {
29  private $testDb;
31  private $dbManager;
33  private $treeDao;
34 
35  protected function setUp()
36  {
37  $this->testDb = new TestPgDb();
38  $this->dbManager = &$this->testDb->getDbManager();
39 
40  $this->testDb->createPlainTables(array('upload','uploadtree'));
41 
42  $this->dbManager->prepare($stmt = 'insert.upload',
43  "INSERT INTO upload (upload_pk, uploadtree_tablename) VALUES ($1, $2)");
44  $uploadArray = array(array(1, 'uploadtree'), array(32, 'uploadtree'));
45  foreach ($uploadArray as $uploadEntry) {
46  $this->dbManager->freeResult($this->dbManager->execute($stmt, $uploadEntry));
47  }
48  $this->treeDao = new TreeDao($this->dbManager);
49  $this->assertCountBefore = \Hamcrest\MatcherAssert::getCount();
50  }
51 
52  protected function tearDown()
53  {
54  $this->addToAssertionCount(\Hamcrest\MatcherAssert::getCount()-$this->assertCountBefore);
55 
56  $this->testDb = null;
57  $this->dbManager = null;
58  }
59 
60  public function testGetMinimalCoveringItem()
61  {
62  $this->prepareModularTable(array());
63  $coverContainer = $this->treeDao->getMinimalCoveringItem(1, "uploadtree");
64  assertThat($coverContainer,equalTo(5));
65 
66  $this->prepareUploadTree(array(array($item=99,null,$upload=32,88,0x0,1,2,'plainFile',null)));
67  $coverSelf = $this->treeDao->getMinimalCoveringItem($upload, "uploadtree");
68  assertThat($coverSelf,equalTo($item));
69  }
70 
71  public function testGetFullPathFromSingleFolderUpload()
72  {
73  $this->prepareModularTable(array(array(6,5,1,0,0,5,6,$fName="file",5)));
74  $cover = $this->treeDao->getMinimalCoveringItem(1, "uploadtree");
75  assertThat($cover,equalTo(5));
76 
77  $path = $this->treeDao->getFullPath(6, "uploadtree", $cover);
78  assertThat($path,equalTo($fName));
79 
80  $path = $this->treeDao->getFullPath(6, "uploadtree", $cover, true);
81  assertThat($path,equalTo($fName));
82  }
83 
84  public function testGetFullPathFromSingleFolderUploadWithDropArtifact()
85  {
86  $this->prepareModularTable(array(array(6,5,1,0,0,5,6,$fName="file",5)));
87  $cover = $this->treeDao->getMinimalCoveringItem(1, "uploadtree", true);
88  assertThat($cover,equalTo(5));
89  $path = $this->treeDao->getFullPath(6, "uploadtree", $cover);
90  assertThat($path,equalTo($fName));
91  }
92 
93  public function testGetFullPathFromSingleFolderUploadWithAFileOutside()
94  {
95  $this->prepareModularTable(array(
96  array(6,5,1,0,0,5,6,"file",5),
97  array(11,4,1,0,0,11,12,"file2",3)
98  ));
99  $cover = $this->treeDao->getMinimalCoveringItem(1, "uploadtree");
100  assertThat($cover,equalTo(4));
101 
102  $pathInsideArchive = $this->treeDao->getFullPath(6, "uploadtree", $cover);
103  assertThat($pathInsideArchive,equalTo("archive/file"));
104 
105  $pathInsideArchive = $this->treeDao->getFullPath(6, "uploadtree", $cover, true);
106  assertThat($pathInsideArchive,equalTo("archive/file"));
107 
108  $pathOutsideArchive = $this->treeDao->getFullPath(11, "uploadtree", $cover);
109  assertThat($pathOutsideArchive,equalTo("file2"));
110 
111  $pathOutsideArchive = $this->treeDao->getFullPath(11, "uploadtree", $cover, true);
112  assertThat($pathOutsideArchive,equalTo("file2"));
113  }
114 
119  protected function prepareUploadTree($uploadTreeArray = array())
120  {
121  $this->dbManager->prepare($stmt = 'insert.uploadtree',
122  "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)");
123  foreach ($uploadTreeArray as $uploadTreeEntry) {
124  $this->dbManager->freeResult($this->dbManager->execute($stmt, $uploadTreeEntry));
125  }
126  }
127 
128 
133  protected function prepareModularTable($subentries = array())
134  {
135  $right_base = 5 + count($subentries) * 2;
136 
137  $uploadTreeArray = array_merge(
138  array(
139  array(1, null, 1, 1, 0x20008400, 1, $right_base + 5, 'archive.tar.gz', null),
140  array(2, 1, 1, 0, 0x30004400, 2, $right_base + 4, 'artifact.dir', 1),
141  array(3, 2, 1, 2, 0x20008000, 3, $right_base + 3, 'archive.tar', 1),
142  array(4, 3, 1, 0, 0x30004400, 4, $right_base + 2, 'artifact.dir', 3),
143  array(5, 4, 1, 0, 0x20004400, 5, $right_base + 1, 'archive', 3)),
144  $subentries);
145  $this->prepareUploadTree($uploadTreeArray);
146  }
147 
189  protected function getTestFileStructure()
190  {
191  return array(
192  array(3675, 3674, 32, 3299, 33188, 23, 24, 'N3',3674),
193  array(3674, 3652, 32, 0, 0x20004400, 22, 37, 'N',3652),
194  array(3673, 3652, 32, 3298, 33188, 38, 39, 'O',3652),
195  array(3671, 3669, 32, 3296, 33188, 9, 10, 'J',3669),
196  array(3669, 3652, 32, 0, 0x20004400, 4, 11, 'H',3652),
197  array(3668, 3652, 32, 3294, 33188, 42, 43, 'C',3652),
198  array(3662, 3661, 32, 0, 0x20004400, 45, 48, 'L3',3661),
199  array(3666, 3661, 32, 0, 0x20004400, 49, 52, 'L1',3661),
200  array(3665, 3664, 32, 3292, 33188, 54, 55, 'L2a',3664),
201  array(3664, 3661, 32, 0, 0x20004400, 53, 56, 'L2',3661),
202  array(3661, 3652, 32, 0, 0x20004400, 44, 57, 'L',3652),
203  array(3658, 3657, 32, 3288, 33188, 60, 61, 'P2a',3657),
204  array(3657, 3656, 32, 0, 0x20004400, 59, 62, 'P2',3656),
205  array(3660, 3656, 32, 3290, 33188, 65, 66, 'P3',6356),
206  array(3656, 3652, 32, 0, 0x20004400, 58, 67, 'P',3652),
207  array(3655, 3654, 32, 0, 0x20004400, 69, 70, 'M1',3654),
208  array(3654, 3652, 32, 0, 0x20004400, 68, 71, 'M',3652),
209  array(3653, 3652, 32, 3287, 33188, 72, 73, 'A',3652),
210  array(3652, 3651, 32, 0, 0x20004400, 3, 74, 'uploadDaoTest',3650),
211  array(3651, 3650, 32, 0, 0x30004400, 2, 75, 'artifact.dir',3650),
212  array(3650, NULL, 32, 3286, 0x20008400, 1, 76, 'uploadDaoTest.tar',null),
213  array(3686, 3652, 32, 3306, 33188, 12, 13, 'R',3652),
214  array(3683, 3682, 32, 3303, 33188, 15, 16, 'E',3682),
215  array(3685, 3682, 32, 3305, 33188, 17, 18, 'G',3682),
216  array(3682, 3652, 32, 0, 0x20004400, 14, 21, 'D',3652),
217  array(3677, 3674, 32, 3300, 33188, 25, 26, 'N5',3674),
218  array(3676, 3674, 32, 3293, 33188, 27, 28, 'N1',3674),
219  array(3678, 3674, 32, 0, 0x20004400, 29, 32, 'N2',3674),
220  array(3681, 3680, 32, 3302, 33188, 34, 35, 'N4a',3680),
221  array(3680, 3674, 32, 0, 0x20004400, 33, 36, 'N4',3674),
222  );
223  }
224 
225  public function testGetFullPathWithComlpexStructureFromFolder()
226  {
227  $this->prepareUploadTree($this->getTestFileStructure());
228  $cover = $this->treeDao->getMinimalCoveringItem(32, "uploadtree");
229  assertThat($cover,equalTo(3652));
230  assertThat($this->treeDao->getFullPath(3666, "uploadtree", $cover), equalTo("L/L1"));
231  assertThat($this->treeDao->getFullPath(3666, "uploadtree", $cover,true), equalTo("L/L1"));
232  }
233 
234  public function testGetFullPathWithComlpexStructureFromFile()
235  {
236  $this->prepareUploadTree($this->getTestFileStructure());
237  $cover = $this->treeDao->getMinimalCoveringItem(32, "uploadtree");
238  assertThat($cover,equalTo(3652));
239 
240  assertThat($this->treeDao->getFullPath(3665, "uploadtree", $cover), equalTo("L/L2/L2a"));
241  assertThat($this->treeDao->getFullPath(3665, "uploadtree", $cover, true), equalTo("L/L2/L2a"));
242 
243  assertThat($this->treeDao->getFullPath(3665, "uploadtree"), equalTo("uploadDaoTest.tar/uploadDaoTest/L/L2/L2a"));
244  assertThat($this->treeDao->getFullPath(3665, "uploadtree",0,true), equalTo( "uploadDaoTest/L/L2/L2a"));
245  }
246 
247  public function testGetFullPathWithComlpexStructureFromFileAndOtherUpload()
248  {
249  $this->prepareUploadTree($this->getTestFileStructure());
250  $this->prepareModularTable(array(array(6,5,1,0,0,5,6,"file",6)));
251  $cover = $this->treeDao->getMinimalCoveringItem(32, "uploadtree");
252  assertThat($cover,equalTo(3652));
253  assertThat($this->treeDao->getFullPath(3665, "uploadtree", $cover), equalTo("L/L2/L2a"));
254  assertThat($this->treeDao->getFullPath(3665, "uploadtree", $cover,true), equalTo("L/L2/L2a"));
255  assertThat($this->treeDao->getFullPath(3665, "uploadtree"), equalTo("uploadDaoTest.tar/uploadDaoTest/L/L2/L2a"));
256  assertThat($this->treeDao->getFullPath(3665, "uploadtree",0,true), equalTo( "uploadDaoTest/L/L2/L2a"));
257  }
258 
259  public function testGetUploadHashes()
260  {
261  $this->testDb->createPlainTables(array('pfile'));
262  $this->dbManager->queryOnce('ALTER TABLE uploadtree RENAME TO uploadtree_a');
263  $this->testDb->insertData(array('uploadtree_a','pfile'));
264  // (pfile_pk, pfile_md5, pfile_sha1, pfile_sha256, pfile_size) := (762, 'C655F0AD3E4D3F90D8EE0541DD636E2E', 'D62DCD25DC68180758FD1C064ADC91AB70A78CB1', '8F39AC8ADD8CD0C0C2BF8924CE3B24F4B2760B8723BFD4205A49FB142490A355', 34);
265  $hashes = $this->treeDao->getItemHashes(463,'uploadtree_a');
266  assertThat($hashes,equalTo(array('md5'=>'C655F0AD3E4D3F90D8EE0541DD636E2E','sha1'=>'D62DCD25DC68180758FD1C064ADC91AB70A78CB1','sha256'=>'8F39AC8ADD8CD0C0C2BF8924CE3B24F4B2760B8723BFD4205A49FB142490A355')));
267  }
268 
269  protected function getNestedTestFileStructure()
270  {
271  /*
272  * example.zip
273  * |
274  * \- file
275  * \~ subExample.tar.gz
276  * |
277  * \- subEample.tar
278  * |
279  * \- innerFile
280  * \~ someFolder
281  * |
282  * \- someFileInFolder
283  */
284  return array(
285  array(14, 13, 2, 8, 32768, 5, 6, 'someFileInFolder', 13, ),
286  array(13, 5, 2, 0, 536888320, 4, 7, 'someFolder', 5, ),
287  array(12, 11, 2, 7, 33188, 13, 14, 'innerFile', 11, ),
288  array(11, 10, 2, 0, 536888320, 12, 15, 'subExample', 9, ),
289  array(10, 9, 2, 0, 805323776, 11, 16, 'artifact.dir', 9, ),
290  array(9, 8, 2, 6, 536903680, 10, 17, 'subExample.tar', 7, ),
291  array(8, 7, 2, 0, 805323776, 9, 18, 'artifact.dir', 7, ),
292  array(7, 5, 2, 5, 536903680, 8, 19, 'subExample.tar.gz', 5, ),
293  array(6, 5, 2, 4, 32768, 20, 21, 'file', 5, ),
294  array(5, 4, 2, 0, 536888320, 3, 22, 'example', 2, ),
295  array(4, 2, 2, 0, 805323776, 2, 23, 'artifact.dir', 2, ),
296  array(3, 2, 2, 3, 268469248, 24, 25, 'artifact.meta', 2, ),
297  array(2, NULL, 2, 2, 536904704, 1, 26, 'example.zip', NULL, ),
298  );
299  }
300 
301  public function testGetFullPathWithNestedStructure()
302  {
303  $this->prepareUploadTree($this->getNestedTestFileStructure());
304  assertThat($this->treeDao->getFullPath(2, "uploadtree", 0), equalTo('example.zip'));
305  assertThat($this->treeDao->getFullPath(2, "uploadtree", 0,true), equalTo('example.zip'));
306 
307  assertThat($this->treeDao->getFullPath(6, "uploadtree", 0), equalTo('example.zip/example/file'));
308  assertThat($this->treeDao->getFullPath(6, "uploadtree", 0,true), equalTo( 'example/file'));
309 
310  assertThat($this->treeDao->getFullPath(12, "uploadtree", 0), equalTo('example.zip/example/subExample.tar.gz/subExample.tar/subExample/innerFile'));
311  assertThat($this->treeDao->getFullPath(12, "uploadtree", 0,true), equalTo( 'example/subExample.tar.gz/subExample.tar/subExample/innerFile'));
312 
313  assertThat($this->treeDao->getFullPath(12, "uploadtree", 5), equalTo('subExample.tar.gz/subExample.tar/subExample/innerFile'));
314  assertThat($this->treeDao->getFullPath(12, "uploadtree", 5,true), equalTo( 'subExample/innerFile'));
315 
316  assertThat($this->treeDao->getFullPath(12, "uploadtree", 7), equalTo('subExample.tar/subExample/innerFile'));
317  assertThat($this->treeDao->getFullPath(12, "uploadtree", 7,true), equalTo( 'subExample/innerFile'));
318  }
319 }
prepareModularTable($subentries=array())
fo_dbManager * dbManager
fo_dbManager object
Definition: process.c:28
prepareUploadTree($uploadTreeArray=array())