FOSSology  4.4.0
Open Source License Compliance by Open Source Software
cliTest.php
1 <?php
2 /*
3  SPDX-FileCopyrightText: © 2014-2015 Siemens AG
4 
5  SPDX-License-Identifier: GPL-2.0-only
6 */
7 
10 use PHPUnit\Runner\Version as PHPUnitVersion;
11 
12 class cliTest extends \PHPUnit\Framework\TestCase
13 {
15  private $testDb;
17  private $dbManager;
19  private $testDataDir;
20 
21  public static function providerWhetherToUseStandalone()
22  {
23  return array(
24  array(FALSE), // not standalone
25  array(TRUE) // standalone
26  );
27  }
28 
29  protected function setUp() : void
30  {
31  $this->testDataDir = dirname(__DIR__)."/testlicenses";
32  $this->testDb = new TestPgDb("monkCli".time());
33  $this->dbManager = $this->testDb->getDbManager();
34  }
35 
36  protected function tearDown() : void
37  {
38  $this->testDb->fullDestruct();
39  $this->testDb = null;
40  $this->dbManager = null;
41  }
42 
43  private function runMonk($args="", $files=array(), $standalone=FALSE)
44  {
45  if($standalone) {
46  $temporaryKB = tempnam("/tmp", "monk.knowledgebase");
47  list($output,$retCode) = $this->runMonk("-s $temporaryKB");
48  $this->assertEquals(0, $retCode, "monk failed to save the knowledgebase to $temporaryKB: ".$output);
49  $result = $this->runMonk("-k $temporaryKB $args", $files);
50  unlink($temporaryKB);
51  return $result;
52  }
53  $sysConf = $this->testDb->getFossSysConf();
54 
55  $confFile = $sysConf."/fossology.conf";
56  system("touch ".$confFile);
57  $config = "[FOSSOLOGY]\ndepth = 0\npath = $sysConf/repo\n";
58  file_put_contents($confFile, $config);
59 
60  $agentName = "monk";
61 
62  $agentDir = dirname(__DIR__,4).'/build/src/monk';
63  $execDir = $agentDir.'/agent';
64  system("install -D $agentDir/VERSION-monk $sysConf/mods-enabled/$agentName/VERSION");
65 
66  foreach ($files as $file) {
67  $args .= " ".escapeshellarg($file);
68  }
69 
70  $cmd = "$execDir/$agentName -c $sysConf $args";
71  $pipeFd = popen($cmd, "r");
72  $this->assertTrue($pipeFd !== false, 'running monk failed');
73 
74  $output = "";
75  while (($buffer = fgets($pipeFd, 4096)) !== false) {
76  $output .= $buffer;
77  }
78  $retCode = pclose($pipeFd);
79 
80 
81  unlink("$sysConf/mods-enabled/$agentName/VERSION");
82  rmdir("$sysConf/mods-enabled/$agentName");
83  rmdir("$sysConf/mods-enabled");
84  unlink($confFile);
85 
86  return array($output,$retCode);
87  }
88 
89  private function setUpTables()
90  {
91  $this->testDb->createPlainTables(array('license_ref'),false);
92  $this->testDb->createSequences(array('license_ref_rf_pk_seq'),false);
93  $this->testDb->alterTables(array('license_ref'),false);
94 
95  $this->testDb->insertData_license_ref(1<<10);
96  }
97 
101  public function testRunMonkScan($standalone)
102  {
103  $this->setUpTables();
104 
105  list($output,$retCode) = $this->runMonk("", array($this->testDataDir."/expectedFull/Apache-2.0"), $standalone);
106 
107  $this->assertEquals(0, $retCode, 'monk failed: '.$output);
108 
109  $pattern = "/found full match between \".*expectedFull\\/Apache-2.0\" and \"Apache-2\\.0\" \\(rf_pk=[0-9]*\\); matched: 0\\+10456\n/";
110  if (intval(explode('.', PHPUnitVersion::id())[0]) >= 9) {
111  $this->assertMatchesRegularExpression($pattern, $output);
112  } else {
113  $this->assertRegExp($pattern, $output);
114  }
115  }
116 
117  private function extractSortedLines($output) {
118  $lines = explode("\n", $output);
119 
120  sort($lines, SORT_STRING);
121  foreach($lines as $key => $val) {
122  if (empty($val))
123  {
124  unset($lines[$key]);
125  }
126  }
127  return array_values($lines);
128  }
129 
130  private function assertLinesRegex($regexFmt, $lines, $testFiles) {
131  for ($i = 0; $i < count($lines); $i++)
132  {
133  $line = $lines[$i];
134 
135  $file = $testFiles[$i];
136  $licenseName = preg_quote(preg_replace('/.*\/([^,]*),?[^,]*/','${1}', $file), "/");
137 
138  $fileName = preg_quote($file, "/");
139 
140  $regex = $regexFmt;
141  $regex = preg_replace("/\\\$fileName/", $fileName, $regex);
142  $regex = preg_replace("/\\\$licenseName/", $licenseName, $regex);
143 
144  if (intval(explode('.', PHPUnitVersion::id())[0]) >= 9) {
145  $this->assertMatchesRegularExpression($regex, $line);
146  } else {
147  $this->assertRegExp($regex, $line);
148  }
149  }
150  }
151 
155  public function testRunMultipleMonkScansFulls($standalone)
156  {
157  $this->setUpTables();
158 
159  $testFiles = glob($this->testDataDir."/expectedFull/*");
160 
161  list($output,$retCode) = $this->runMonk("", $testFiles, $standalone);
162 
163  $this->assertEquals(0, $retCode, 'monk failed: '.$output);
164 
165  sort($testFiles, SORT_STRING);
166  $lines = $this->extractSortedLines($output);
167 
168  $this->assertEquals(count($testFiles), count($lines),
169  "scanned\n".implode("\n",$testFiles)."\n---\noutput\n".implode("\n",$lines)."\n---\n");
170 
171 
172  $this->assertLinesRegex('/found full match between "$fileName" and "$licenseName" \‍(rf_pk=[0-9]+\‍); matched: [0-9]+\+[0-9]+/',
173  $lines, $testFiles);
174  }
175 
179  public function testRunMultipleMonkScansDiff($standalone)
180  {
181  $this->setUpTables();
182 
183  $testFiles = glob($this->testDataDir."/expectedDiff/*");
184 
185  list($output,$retCode) = $this->runMonk("", $testFiles, $standalone);
186 
187  $this->assertEquals(0, $retCode, 'monk failed: '.$output);
188 
189  sort($testFiles, SORT_STRING);
190  $lines = $this->extractSortedLines($output);
191 
192  $this->assertEquals(count($testFiles), count($lines),
193  "scanned\n".implode("\n",$testFiles)."\n---\noutput\n".implode("\n",$lines)."\n---\n");
194 
195  $this->assertLinesRegex('/found diff match between "$fileName" and "$licenseName" \‍(rf_pk=[0-9]+\‍); rank [0-9]{1,3}; diffs: \{[stMR+\[\]0-9, -]+\}/',
196  $lines, $testFiles);
197  }
198 
202  public function testRunMonkHelpMode($standalone)
203  {
204  $this->setUpTables();
205 
206  list($output,$retCode) = $this->runMonk("-h", array(), $standalone);
207 
208  $this->assertEquals(0, $retCode, 'monk failed: '.$output);
209 
210  $expectedOutputRgx = '/Usage:.*/';
211 
212  if (intval(explode('.', PHPUnitVersion::id())[0]) >= 9) {
213  $this->assertMatchesRegularExpression($expectedOutputRgx, $output);
214  } else {
215  $this->assertRegExp($expectedOutputRgx, $output);
216  }
217  }
218 
222  public function testRunMonkScansWithNegativeMatch($standalone)
223  {
224  $this->setUpTables();
225 
226  $fileName = tempnam(".", "monkCli");
227 
228  list($output,$retCode) = $this->runMonk("", array($fileName), $standalone);
229 
230  unlink($fileName);
231 
232  $this->assertEquals(0, $retCode, 'monk failed: '.$output);
233 
234  $this->assertEquals("",$output);
235  }
236 
240  public function testRunMonkScansWithNegativeMatchVerbose($standalone)
241  {
242  $this->setUpTables();
243 
244  $fileName = tempnam(".", "monkCli");
245  $testFiles = array($fileName);
246 
247  list($output,$retCode) = $this->runMonk("-v", $testFiles, $standalone);
248 
249  unlink($fileName);
250 
251  $this->assertEquals(0, $retCode, 'monk failed: '.$output);
252 
253  sort($testFiles, SORT_STRING);
254  $lines = $this->extractSortedLines($output);
255 
256  $this->assertEquals(count($testFiles), count($lines),
257  "scanned\n".implode("\n",$testFiles)."\n---\noutput\n".implode("\n",$lines)."\n---\n");
258 
259  $this->assertLinesRegex('/$fileName contains license\‍(s\‍) No_license_found/',
260  $lines, $testFiles);
261  }
262 }
testRunMultipleMonkScansFulls($standalone)
Definition: cliTest.php:155
testRunMultipleMonkScansDiff($standalone)
Definition: cliTest.php:179
testRunMonkScan($standalone)
Definition: cliTest.php:101
testRunMonkHelpMode($standalone)
Definition: cliTest.php:202
testRunMonkScansWithNegativeMatchVerbose($standalone)
Definition: cliTest.php:240
testRunMonkScansWithNegativeMatch($standalone)
Definition: cliTest.php:222
fo_dbManager * dbManager
fo_dbManager object
Definition: process.c:16