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  echo dirname(dirname(dirname(dirname(__DIR__)))) .
135  "/spdx2/agent_tests/Functional/fo_report.sql";
136  $this->testDb->createPlainTables(['license_ref', 'license_file']);
137  $this->testDb->insertData(['license_ref', 'license_file']);
138 
139  $expectedFirstFinding = [
140  'GPL-2.0-only',
141  'LGPL-2.1-or-later'
142  ];
143  $expectedSecondFinding = [
144  'Classpath-exception-2.0',
145  'GPL-2.0-only'
146  ];
147  $expectedThirdFinding = [
148  'MIT'
149  ];
150  $expectedNoFinding = [];
151 
152  $actualFirstFinding = $this->pfileDao->getScannerFindings(11);
153  $actualSecondFinding = $this->pfileDao->getScannerFindings(14);
154  $actualThirdFinding = $this->pfileDao->getScannerFindings(15);
155  $actualNoFinding = $this->pfileDao->getScannerFindings(12);
156 
157  $this->assertEquals($expectedFirstFinding, $actualFirstFinding);
158  $this->assertEquals($expectedSecondFinding, $actualSecondFinding);
159  $this->assertEquals($expectedThirdFinding, $actualThirdFinding);
160  $this->assertEquals($expectedNoFinding, $actualNoFinding);
161  }
162 
169  public function testGetUploadForPackage()
170  {
171  $this->testDb->createPlainTables(['upload']);
172  $this->testDb->insertData(['upload']);
173 
174  $expectedSingleUpload = [1];
175  $expectedMultipleUploads = [2, 3];
176  $expectedNoUpload = null;
177 
178  $actualSingleUpload = $this->pfileDao->getUploadForPackage(1);
179  $actualMultipleUploads = $this->pfileDao->getUploadForPackage(9);
180  $actualNoUpload = $this->pfileDao->getUploadForPackage(99);
181 
182  $this->assertEquals($expectedSingleUpload, $actualSingleUpload);
183  $this->assertEquals($expectedMultipleUploads, $actualMultipleUploads);
184  $this->assertEquals($expectedNoUpload, $actualNoUpload);
185  }
186 
195  public function testGetConclusions()
196  {
197  $this->testDb->createPlainTables(['clearing_decision',
198  'clearing_decision_event', 'clearing_event', 'license_ref']);
199  $this->testDb->insertData(['clearing_decision', 'clearing_decision_event',
200  'clearing_event', 'license_ref'], false, SPDX_TEST_DATA);
201 
202  $expectedLocalConclusion = ['lic A'];
203  $expectedGlobalConclusion = ['lic B'];
204  $expectedAllRemoval = ["NONE"];
205  $expectedNoConclusion = ["NOASSERTION"];
206 
207  $actualLocalConclusion = $this->pfileDao->getConclusions(2, 2);
208  $actualGlobalConclusion = $this->pfileDao->getConclusions(4, 3);
209  $actualNoConclusions = $this->pfileDao->getConclusions(2, 7);
210  $actualAllRemoval = $this->pfileDao->getConclusions(2, 6);
211 
212  $this->assertEquals($expectedLocalConclusion, $actualLocalConclusion);
213  $this->assertEquals($expectedGlobalConclusion, $actualGlobalConclusion);
214  $this->assertEquals($expectedNoConclusion, $actualNoConclusions);
215  $this->assertEquals($expectedAllRemoval, $actualAllRemoval);
216  }
217 
226  public function testGetCopyright()
227  {
228  $this->testDb->createPlainTables(['copyright', 'copyright_decision']);
229  $this->testDb->insertData(['copyright', 'copyright_decision']);
230 
231  $expectedFirstFinding = [
232  'copyright (c) 2048',
233  'copyright (c) manual'
234  ];
235  $expectedSecondFinding = [
236  'copyright (c) 2002 lawrence e. rosen. all rights
237 reserved. permission is hereby granted to copy and distribute this
238 ',
239  'copyright grant to the software. the
240  bsd and apache licenses are vague and incomplete in that respect.
241 ',
242  'copyright to the license will control changes. the apache
243 '
244  ];
245  $expectedThirdFinding = [
246  'copyright (c) independent manual'
247  ];
248  $expectedNoFinding = [];
249 
250  $actualFirstFinding = $this->pfileDao->getCopyright(9);
251  $actualSecondFinding = $this->pfileDao->getCopyright(6);
252  $actualThirdFinding = $this->pfileDao->getCopyright(14);
253  $actualNoFinding = $this->pfileDao->getCopyright(15);
254 
255  $this->assertEquals($expectedFirstFinding, $actualFirstFinding);
256  $this->assertEquals($expectedSecondFinding, $actualSecondFinding);
257  $this->assertEquals($expectedThirdFinding, $actualThirdFinding);
258  $this->assertEquals($expectedNoFinding, $actualNoFinding);
259  }
266  public function testHaveConclusionsTrue()
267  {
268  $this->testDb->createPlainTables(['clearing_decision']);
269  $this->testDb->insertData(['clearing_decision'], false, SPDX_TEST_DATA);
270  $result = $this->pfileDao->haveConclusions(2, 2);
271  $this->assertTrue($result);
272  }
279  public function testHaveConclusionsFalse()
280  {
281  $this->testDb->createPlainTables(['clearing_decision']);
282  $this->testDb->insertData(['clearing_decision'], false, SPDX_TEST_DATA);
283  $result = $this->pfileDao->haveConclusions(100, 100);
284  $this->assertFalse($result);
285  }
286 }
Test cases for PfileDao.
fo_dbManager * dbManager
fo_dbManager object
Definition: process.c:16