FOSSology  3.2.0rc1
Open Source License Compliance by Open Source Software
cliTest.php
1 <?php
2 /*
3 Copyright (C) 2014-2015, Siemens AG
4 
5 This program is free software; you can redistribute it and/or
6 modify it under the terms of the GNU General Public License
7 version 2 as published by the Free Software Foundation.
8 
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
13 
14 You should have received a copy of the GNU General Public License along
15 with this program; if not, write to the Free Software Foundation, Inc.,
16 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17 */
18 
21 
22 class MonkCliTest extends \PHPUnit\Framework\TestCase
23 {
25  private $testDb;
27  private $dbManager;
29  private $testDataDir;
30 
31  public static function providerWhetherToUseStandalone()
32  {
33  return array(
34  array(FALSE), // not standalone
35  array(TRUE) // standalone
36  );
37  }
38 
39  protected function setUp()
40  {
41  $this->testDataDir = dirname(__DIR__)."/testlicenses";
42  $this->testDb = new TestPgDb("monkCli".time());
43  $this->dbManager = $this->testDb->getDbManager();
44  }
45 
46  protected function tearDown()
47  {
48  $this->testDb->fullDestruct();
49  $this->testDb = null;
50  $this->dbManager = null;
51  }
52 
53  private function runMonk($args="", $files=array(), $standalone=FALSE)
54  {
55  if($standalone) {
56  $temporaryKB = tempnam("/tmp", "monk.knowledgebase");
57  list($output,$retCode) = $this->runMonk("-s $temporaryKB");
58  $this->assertEquals(0, $retCode, "monk failed to save the knowledgebase to $temporaryKB: ".$output);
59  $result = $this->runMonk("-k $temporaryKB $args", $files);
60  unlink($temporaryKB);
61  return $result;
62  }
63  $sysConf = $this->testDb->getFossSysConf();
64 
65  $confFile = $sysConf."/fossology.conf";
66  system("touch ".$confFile);
67  $config = "[FOSSOLOGY]\ndepth = 0\npath = $sysConf/repo\n";
68  file_put_contents($confFile, $config);
69 
70  $agentName = "monk";
71 
72  $agentDir = dirname(dirname(__DIR__));
73  $execDir = __DIR__;
74  system("install -D $agentDir/VERSION-monk $sysConf/mods-enabled/$agentName/VERSION");
75 
76  foreach ($files as $file) {
77  $args .= " ".escapeshellarg($file);
78  }
79 
80  $cmd = "$execDir/$agentName -c $sysConf $args";
81  $pipeFd = popen($cmd, "r");
82  $this->assertTrue($pipeFd !== false, 'running monk failed');
83 
84  $output = "";
85  while (($buffer = fgets($pipeFd, 4096)) !== false) {
86  $output .= $buffer;
87  }
88  $retCode = pclose($pipeFd);
89 
90 
91  unlink("$sysConf/mods-enabled/$agentName/VERSION");
92  rmdir("$sysConf/mods-enabled/$agentName");
93  rmdir("$sysConf/mods-enabled");
94  unlink($confFile);
95 
96  return array($output,$retCode);
97  }
98 
99  private function setUpTables()
100  {
101  $this->testDb->createPlainTables(array('license_ref'),false);
102  $this->testDb->createSequences(array('license_ref_rf_pk_seq'),false);
103 
104  $this->testDb->insertData_license_ref(1<<10);
105  }
106 
110  public function testRunMonkScan($standalone)
111  {
112  $this->setUpTables();
113 
114  list($output,$retCode) = $this->runMonk("", array($this->testDataDir."/expectedFull/Apache-2.0"), $standalone);
115 
116  $this->assertEquals(0, $retCode, 'monk failed: '.$output);
117 
118  $this->assertRegExp("/found full match between \".*expectedFull\\/Apache-2.0\" and \"Apache-2\\.0\" \\(rf_pk=[0-9]*\\); matched: 0\\+10456\n/", $output);
119  }
120 
121  private function extractSortedLines($output) {
122  $lines = explode("\n", $output);
123 
124  sort($lines, SORT_STRING);
125  foreach($lines as $key => $val) {
126  if (empty($val))
127  {
128  unset($lines[$key]);
129  }
130  }
131  return array_values($lines);
132  }
133 
134  private function assertLinesRegex($regexFmt, $lines, $testFiles) {
135  for ($i = 0; $i < count($lines); $i++)
136  {
137  $line = $lines[$i];
138 
139  $file = $testFiles[$i];
140  $licenseName = preg_quote(preg_replace('/.*\/([^,]*),?[^,]*/','${1}', $file), "/");
141 
142  $fileName = preg_quote($file, "/");
143 
144  $regex = $regexFmt;
145  $regex = preg_replace("/\\\$fileName/", $fileName, $regex);
146  $regex = preg_replace("/\\\$licenseName/", $licenseName, $regex);
147 
148  $this->assertRegExp($regex, $line);
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  $this->assertRegExp($expectedOutputRgx,$output);
213  }
214 
218  public function testRunMonkScansWithNegativeMatch($standalone)
219  {
220  $this->setUpTables();
221 
222  $fileName = tempnam(".", "monkCli");
223 
224  list($output,$retCode) = $this->runMonk("", array($fileName), $standalone);
225 
226  unlink($fileName);
227 
228  $this->assertEquals(0, $retCode, 'monk failed: '.$output);
229 
230  $this->assertEquals("",$output);
231  }
232 
236  public function testRunMonkScansWithNegativeMatchVerbose($standalone)
237  {
238  $this->setUpTables();
239 
240  $fileName = tempnam(".", "monkCli");
241  $testFiles = array($fileName);
242 
243  list($output,$retCode) = $this->runMonk("-v", $testFiles, $standalone);
244 
245  unlink($fileName);
246 
247  $this->assertEquals(0, $retCode, 'monk failed: '.$output);
248 
249  sort($testFiles, SORT_STRING);
250  $lines = $this->extractSortedLines($output);
251 
252  $this->assertEquals(count($testFiles), count($lines),
253  "scanned\n".implode("\n",$testFiles)."\n---\noutput\n".implode("\n",$lines)."\n---\n");
254 
255  $this->assertLinesRegex('/$fileName contains license\(s\) No_license_found/',
256  $lines, $testFiles);
257  }
258 }
testRunMultipleMonkScansFulls($standalone)
Definition: cliTest.php:155
testRunMonkHelpMode($standalone)
Definition: cliTest.php:202
testRunMultipleMonkScansDiff($standalone)
Definition: cliTest.php:179
testRunMonkScansWithNegativeMatchVerbose($standalone)
Definition: cliTest.php:236
testRunMonkScan($standalone)
Definition: cliTest.php:110
fo_dbManager * dbManager
fo_dbManager object
Definition: process.c:28
testRunMonkScansWithNegativeMatch($standalone)
Definition: cliTest.php:218