FOSSology  4.4.0
Open Source License Compliance by Open Source Software
PfileDaoTest.php
1 <?php
2 /*
3  SPDX-FileCopyrightText: © 2020 Siemens AG
4  Author: Gaurav Mishra <mishra.gaurav@siemens.com>
5 
6  SPDX-License-Identifier: GPL-2.0-only
7 */
8 
9 namespace Fossology\Lib\Dao;
10 
13 use Monolog\Logger;
14 
19 define("SPDX_TEST_DATA", dirname(dirname(dirname(dirname(__DIR__)))) .
20  "/spdx2/agent_tests/Functional/fo_report.sql");
21 
26 class PfileDaoTest extends \PHPUnit\Framework\TestCase
27 {
30  private $testDb;
33  private $dbManager;
36  private $logger;
39  private $pfileDao;
43 
48  protected function setUp() : void
49  {
50  $this->testDb = new TestPgDb("pfiledao");
51  $this->dbManager = $this->testDb->getDbManager();
52  $this->logger = new Logger("test");
53  $this->pfileDao = new PfileDao($this->dbManager, $this->logger);
54  $this->assertCountBefore = \Hamcrest\MatcherAssert::getCount();
55  }
56 
61  protected function tearDown() : void
62  {
63  $this->addToAssertionCount(\Hamcrest\MatcherAssert::getCount() -
64  $this->assertCountBefore);
65  $this->testDb->fullDestruct();
66  $this->testDb = null;
67  $this->dbManager = null;
68  }
69 
77  public function testGetPfile()
78  {
79  $this->testDb->createPlainTables(['pfile']);
80  $this->testDb->insertData(['pfile']);
81 
82  $pfileSha1 = [
83  'pfile_pk' => 755,
84  'pfile_md5' => 'E7295A5773D0EA17D53CBE6293924DD4',
85  'pfile_sha1' => '93247C8DB814F0A224B75B522C1FA4DC92DC3078',
86  'pfile_sha256' => 'E29ABC32DB8B6241D598BC7C76681A7623D176D85F99E738A56C0CB684C367E1',
87  'pfile_size' => 10240,
88  'pfile_mimetypefk' => 12
89  ];
90  $pfileMd5 = [
91  'pfile_pk' => 3,
92  'pfile_md5' => 'E35086757EE4F4B35B0B1C63107DE47F',
93  'pfile_sha1' => '678C8AE6FC318346D61D540D30AA4ACE024BBB8B',
94  'pfile_sha256' => null,
95  'pfile_size' => 4483,
96  'pfile_mimetypefk' => 30
97  ];
98  $pfileSha1Sha256 = [
99  'pfile_pk' => 758,
100  'pfile_md5' => '95405535F6607638FC8B344F369D8CA9',
101  'pfile_sha1' => 'FBE3181FD0ADDBD6E64B1FF6CAE1223A7DACB836',
102  'pfile_sha256' => '77949BC4E251CF4F0BAD9C5DC8C44C5803BC50A7E73F3B5DCAF985DEBE0E93B4',
103  'pfile_size' => 64,
104  'pfile_mimetypefk' => null
105  ];
106  $notFoundPfile = null;
107 
108  $validPfileSha1 = $this->pfileDao
109  ->getPfile("93247C8DB814F0A224B75B522C1FA4DC92DC3078");
110  $validPfileMd5 = $this->pfileDao
111  ->getPfile(null, "e35086757ee4f4b35b0b1c63107de47f");
112  $validPfileSha1Sha256 = $this->pfileDao
113  ->getPfile("FBE3181FD0ADDBD6E64B1FF6CAE1223A7DACB836", null,
114  "77949bc4e251cf4f0bad9c5dc8c44c5803bc50a7e73f3b5dcaf985debe0e93b4");
115  $invalidPfileSha1 = $this->pfileDao
116  ->getPfile("E47AA02935A589FADA86489705E0E0F0");
117 
118  $this->assertEquals($pfileSha1, $validPfileSha1);
119  $this->assertEquals($pfileMd5, $validPfileMd5);
120  $this->assertEquals($pfileSha1Sha256, $validPfileSha1Sha256);
121  $this->assertEquals($notFoundPfile, $invalidPfileSha1);
122  }
123 
132  public function testGetScannerFindings()
133  {
134  $this->testDb->createPlainTables(['license_ref', 'license_file']);
135  $this->testDb->insertData(['license_ref', 'license_file']);
136 
137  $expectedFirstFinding = [
138  'GPL-2.0-only',
139  'LGPL-2.1-or-later'
140  ];
141  $expectedSecondFinding = [
142  'Classpath-exception-2.0',
143  'GPL-2.0-only'
144  ];
145  $expectedThirdFinding = [
146  'MIT'
147  ];
148  $expectedNoFinding = [];
149 
150  $actualFirstFinding = $this->pfileDao->getScannerFindings(11);
151  $actualSecondFinding = $this->pfileDao->getScannerFindings(14);
152  $actualThirdFinding = $this->pfileDao->getScannerFindings(15);
153  $actualNoFinding = $this->pfileDao->getScannerFindings(12);
154 
155  $this->assertEquals($expectedFirstFinding, $actualFirstFinding);
156  $this->assertEquals($expectedSecondFinding, $actualSecondFinding);
157  $this->assertEquals($expectedThirdFinding, $actualThirdFinding);
158  $this->assertEquals($expectedNoFinding, $actualNoFinding);
159  }
160 
167  public function testGetUploadForPackage()
168  {
169  $this->testDb->createPlainTables(['upload']);
170  $this->testDb->insertData(['upload']);
171 
172  $expectedSingleUpload = [1];
173  $expectedMultipleUploads = [2, 3];
174  $expectedNoUpload = null;
175 
176  $actualSingleUpload = $this->pfileDao->getUploadForPackage(1);
177  $actualMultipleUploads = $this->pfileDao->getUploadForPackage(9);
178  $actualNoUpload = $this->pfileDao->getUploadForPackage(99);
179 
180  $this->assertEquals($expectedSingleUpload, $actualSingleUpload);
181  $this->assertEquals($expectedMultipleUploads, $actualMultipleUploads);
182  $this->assertEquals($expectedNoUpload, $actualNoUpload);
183  }
184 
193  public function testGetConclusions()
194  {
195  $this->testDb->createPlainTables(['clearing_decision',
196  'clearing_decision_event', 'clearing_event', 'license_ref']);
197  $this->testDb->insertData(['clearing_decision', 'clearing_decision_event',
198  'clearing_event', 'license_ref'], false, SPDX_TEST_DATA);
199 
200  $expectedLocalConclusion = ['lic A'];
201  $expectedGlobalConclusion = ['lic B'];
202  $expectedAllRemoval = ["NONE"];
203  $expectedNoConclusion = ["NOASSERTION"];
204 
205  $actualLocalConclusion = $this->pfileDao->getConclusions(2, 2);
206  $actualGlobalConclusion = $this->pfileDao->getConclusions(4, 3);
207  $actualNoConclusions = $this->pfileDao->getConclusions(2, 7);
208  $actualAllRemoval = $this->pfileDao->getConclusions(2, 6);
209 
210  $this->assertEquals($expectedLocalConclusion, $actualLocalConclusion);
211  $this->assertEquals($expectedGlobalConclusion, $actualGlobalConclusion);
212  $this->assertEquals($expectedNoConclusion, $actualNoConclusions);
213  $this->assertEquals($expectedAllRemoval, $actualAllRemoval);
214  }
215 
224  public function testGetCopyright()
225  {
226  $this->testDb->createPlainTables(['copyright', 'copyright_decision']);
227  $this->testDb->insertData(['copyright', 'copyright_decision']);
228 
229  $expectedFirstFinding = [
230  'copyright (c) 2048',
231  'copyright (c) manual'
232  ];
233  $expectedSecondFinding = [
234  'copyright (c) 2002 lawrence e. rosen. all rights
235 reserved. permission is hereby granted to copy and distribute this
236 ',
237  'copyright grant to the software. the
238  bsd and apache licenses are vague and incomplete in that respect.
239 ',
240  'copyright to the license will control changes. the apache
241 '
242  ];
243  $expectedThirdFinding = [
244  'copyright (c) independent manual'
245  ];
246  $expectedNoFinding = [];
247 
248  $actualFirstFinding = $this->pfileDao->getCopyright(9);
249  $actualSecondFinding = $this->pfileDao->getCopyright(6);
250  $actualThirdFinding = $this->pfileDao->getCopyright(14);
251  $actualNoFinding = $this->pfileDao->getCopyright(15);
252 
253  $this->assertEquals($expectedFirstFinding, $actualFirstFinding);
254  $this->assertEquals($expectedSecondFinding, $actualSecondFinding);
255  $this->assertEquals($expectedThirdFinding, $actualThirdFinding);
256  $this->assertEquals($expectedNoFinding, $actualNoFinding);
257  }
258 }
Test cases for PfileDao.
fo_dbManager * dbManager
fo_dbManager object
Definition: process.c:16