FOSSology  3.2.0rc1
Open Source License Compliance by Open Source Software
test_common_license_file.php
Go to the documentation of this file.
1 <?php
2 /*
3  Copyright (C) 2011-2013 Hewlett-Packard Development Company, L.P.
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 
26 
27 require_once(dirname(__FILE__) . '/../common-license-file.php');
28 require_once(dirname(__FILE__) . '/../common-db.php');
29 require_once(dirname(__FILE__) . '/../common-dir.php');
30 require_once(dirname(__FILE__) . '/../common-ui.php');
31 
35 class test_common_license_file extends \PHPUnit\Framework\TestCase
36 {
37  public $upload_pk = 0;
38  public $uploadtree_pk_parent = 0;
39  public $uploadtree_pk_child = 0;
40  public $agent_pk = 0;
41  public $uploadtree_tablename = 'uploadtree';
42 
43  public $DB_COMMAND = "";
44  public $DB_NAME = "";
45 
47  private $testDb;
49  private $dbManager;
50 
51  private $logFileName;
52 
56  protected function setUp()
57  {
58  global $PG_CONN;
59  global $upload_pk;
60  global $pfile_pk_parent;
61  global $pfile_pk_child;
62  global $agent_pk;
63 
64  global $DB_COMMAND;
65  global $DB_NAME;
66 
67  $DB_COMMAND = dirname(dirname(dirname(dirname(__FILE__))))."/testing/db/createTestDB.php";
68  print "*** path to test db creation command: " . $DB_COMMAND;
69  exec($DB_COMMAND, $dbout, $rc);
70  if (!empty($rc)) {
71  throw new Exception(implode("\n", $dbout));
72  }
73  preg_match("/(\d+)/", $dbout[0], $matches);
74  $test_name = $matches[1];
75  $db_conf = $dbout[0];
76  $DB_NAME = "fosstest".$test_name;
77 
78  $PG_CONN = DBconnect($db_conf);
79 
80  $logger = new Monolog\Logger('default');
81  $this->logFileName = dirname(dirname(dirname(dirname(dirname(__FILE__))))) . '/db.sqlite.log';
82  $logger->pushHandler(new Monolog\Handler\StreamHandler($this->logFileName, Monolog\Logger::ERROR));
83  $this->dbManager = new ModernDbManager($logger);
84  $this->dbManager->setDriver(new Fossology\Lib\Db\Driver\Postgres($PG_CONN));
85 
87  $upload_filename = "license_file_test"; /* upload file name */
88 
89  $this->dbManager->prepare($stmt='pfile.insert',
90  $sql = "INSERT INTO pfile (pfile_sha1,pfile_md5,pfile_size) VALUES ($1,$2,$3)");
91  $this->dbManager->freeResult($this->dbManager->execute($stmt,array(
92  'AF1DF2C4B32E4115DB5F272D9EFD0E674CF2A0BC', '2239AA7DAC291B6F8D0A56396B1B8530', '4560')));
93  $this->dbManager->freeResult($this->dbManager->execute($stmt,array(
94  'B1938B14B9A573D59ABCBD3BF0F9200CE6E79FB6', '55EFE7F9B9D106047718F1CE9173B869', '1892')));
95 
97  $this->dbManager->queryOnce($sql="INSERT INTO agent (agent_name) VALUES('nomos')");
98 
100  $this->dbManager->prepare($stmt='license_ref.insert',
101  $sql="INSERT INTO license_ref"
102  . " (rf_pk, rf_shortname, rf_text, marydone, rf_active, rf_text_updatable, rf_detector_type)"
103  . " VALUES ($1,$2,$3,$4,$5,$6,$7)");
104  $this->dbManager->freeResult($this->dbManager->execute($stmt,
105  array(1, 'test_ref', 'test_ref', 'false', 'true', 'false', 1)));
106 
108  $this->dbManager->prepare($stmt='license_ref.select',
109  $sql = "SELECT pfile_pk from pfile where pfile_sha1"
110  . " IN ('AF1DF2C4B32E4115DB5F272D9EFD0E674CF2A0BC', 'B1938B14B9A573D59ABCBD3BF0F9200CE6E79FB6')");
111  $result = $this->dbManager->execute($stmt);
112  $row = $this->dbManager->fetchArray($result);
113  $pfile_pk_parent = $row['pfile_pk'];
114  $row = $this->dbManager->fetchArray($result);
115  $pfile_pk_child= $row['pfile_pk'];
116  $this->dbManager->freeResult($result);
117 
119  $agent_nomos = $this->dbManager->getSingleRow("SELECT agent_pk from agent where agent_name = 'nomos'",array(),__METHOD__.'.agent.select');
120  $agent_pk = $agent_nomos['agent_pk'];
121  $this->dbManager->prepare($stmt='license_file.insert',
122  $sql = "INSERT INTO license_file(rf_fk, agent_fk, pfile_fk) VALUES ($1,$2,$3)");
123  $this->dbManager->freeResult($this->dbManager->execute($stmt, array(1, $agent_pk, $pfile_pk_parent)));
124  $this->dbManager->freeResult($this->dbManager->execute($stmt, array(2, $agent_pk, $pfile_pk_child)));
125 
126  $this->dbManager->queryOnce("INSERT INTO upload (upload_filename,upload_mode,upload_ts, pfile_fk, uploadtree_tablename)"
127  . " VALUES ('$upload_filename',40,now(), '$pfile_pk_parent', '$this->uploadtree_tablename')");
128  $row = $this->dbManager->getSingleRow("SELECT upload_pk from upload where upload_filename = '$upload_filename'",array(),__METHOD__.'.upload.select');
129  $upload_pk= $row['upload_pk'];
130 
131  $this->dbManager->prepare($stmtIn=__METHOD__.'.uploadtree.insert',
132  "INSERT INTO uploadtree (parent, upload_fk, pfile_fk, ufile_mode, lft, rgt, ufile_name) VALUES ($1,$2,$3,$4,$5,$6,$7)");
133  $this->dbManager->freeResult(
134  $this->dbManager->execute($stmtIn,array(NULL, $upload_pk, $pfile_pk_parent, 33188, 1, 2, 'license_test.file.parent')));
135 
136  $this->dbManager->prepare($stmtOut=__METHOD__.'uploadtree.select',
137  "SELECT uploadtree_pk from uploadtree where pfile_fk=$1");
138  $res = $this->dbManager->execute($stmtOut,array($pfile_pk_parent));
139  $row = $this->dbManager->fetchArray($res);
140  $this->dbManager->freeResult($res);
141  $this->uploadtree_pk_parent = $row['uploadtree_pk'];
142 
144  $this->dbManager->freeResult(
145  $this->dbManager->execute($stmtIn,array($this->uploadtree_pk_parent, $upload_pk, $pfile_pk_child, 33188, 1, 2, 'license_test.file.child')));
146 
147  $res = $this->dbManager->execute($stmtOut,array($pfile_pk_child));
148  $row = $this->dbManager->fetchArray($res);
149  $this->dbManager->freeResult($res);
150  $this->uploadtree_pk_child = $row['uploadtree_pk'];
151 
152  $this->uploadtree_tablename = GetUploadtreeTableName($upload_pk);
153  print('.');
154  }
155 
161  {
162  global $PG_CONN;
163  global $agent_pk;
164 
165  $license_array = GetFileLicenses($agent_pk, '' , $this->uploadtree_pk_parent, $this->uploadtree_tablename);
167  $sql = "SELECT rf_shortname from license_ref where rf_pk = 1;";
168  $result = pg_query($PG_CONN, $sql);
169  DBCheckResult($result, $sql, __FILE__, __LINE__);
170  $row = pg_fetch_assoc($result);
171  $license_value_expected = $row['rf_shortname'];
172  pg_free_result($result);
173  $count = count($license_array);
174 
175  $this->assertEquals($license_value_expected, $license_array[1]);
176  $this->assertEquals(1, $count);
177  }
178 
184  {
185  global $PG_CONN;
186  global $pfile_pk_parent;
187  global $agent_pk;
188  $sql = "INSERT INTO license_file(rf_fk, agent_fk, pfile_fk) VALUES(1, $agent_pk, $pfile_pk_parent);";
189  $result = pg_query($PG_CONN, $sql);
190  DBCheckResult($result, $sql, __FILE__, __LINE__);
191  pg_free_result($result);
192 
193  $license_array = GetFileLicenses($agent_pk, '' , $this->uploadtree_pk_parent, $this->uploadtree_tablename, "yes");
195  $sql = "SELECT rf_shortname from license_ref where rf_pk = 1;";
196  $result = pg_query($PG_CONN, $sql);
197  DBCheckResult($result, $sql, __FILE__, __LINE__);
198  $row = pg_fetch_assoc($result);
199  $license_value_expected = $row['rf_shortname'];
200  pg_free_result($result);
201 
202  $count = count($license_array);
203  $this->assertEquals(2, $count);
204  $this->assertEquals($license_value_expected, $license_array[1]);
205  $this->assertEquals($license_value_expected, $license_array[3]);
206  }
207 
213  {
214  global $PG_CONN;
215  global $agent_pk;
216 
217  $license_string = GetFileLicenses_string($agent_pk, '', $this->uploadtree_pk_parent, $this->uploadtree_tablename);
219  $sql = "SELECT rf_shortname from license_ref where rf_pk = 1;";
220  $result = pg_query($PG_CONN, $sql);
221  DBCheckResult($result, $sql, __FILE__, __LINE__);
222  $row = pg_fetch_assoc($result);
223  $license_value_expected = $row['rf_shortname'];
224  pg_free_result($result);
225 
226  $this->assertEquals($license_value_expected, $license_string);
227  }
228 
233  {
234  global $PG_CONN;
235  global $pfile_pk_parent;
236  global $agent_pk;
237 
239  $sql = "SELECT rf_shortname from license_ref where rf_pk = 1;";
240  $result = pg_query($PG_CONN, $sql);
241  DBCheckResult($result, $sql, __FILE__, __LINE__);
242  $row = pg_fetch_assoc($result);
243  $rf_shortname = $row['rf_shortname'];
244  pg_free_result($result);
245 
246  $files_result = GetFilesWithLicense($agent_pk, $rf_shortname, $this->uploadtree_pk_parent, false, 0, "ALL", "", null, $this->uploadtree_tablename);
247  $row = pg_fetch_assoc($files_result);
248  $pfile_id_actual = $row['pfile_fk'];
249  pg_free_result($files_result);
250  $this->assertEquals($pfile_pk_parent, $pfile_id_actual);
251  }
252 
257  {
258  global $PG_CONN;
259  global $agent_pk;
260 
262  $sql = "SELECT rf_shortname from license_ref where rf_pk = 1;";
263  $result = pg_query($PG_CONN, $sql);
264  DBCheckResult($result, $sql, __FILE__, __LINE__);
265  $row = pg_fetch_assoc($result);
266  $rf_shortname = $row['rf_shortname'];
267  pg_free_result($result);
268 
269  $file_name = Level1WithLicense($agent_pk, $rf_shortname, $this->uploadtree_pk_parent, false, $this->uploadtree_tablename);
270  $this->assertEquals("license_test.file.child", $file_name[$this->uploadtree_pk_child]);
271  }
272 
273 
277  protected function tearDown()
278  {
279  if (!is_callable('pg_connect')) {
280  return;
281  }
282  global $PG_CONN;
283  global $pfile_pk_parent;
284  global $pfile_pk_child;
285  global $upload_pk;
286  global $DB_COMMAND;
287  global $DB_NAME;
288 
290  $sql = "DELETE FROM uploadtree where upload_fk = $upload_pk;";
291  $result = pg_query($PG_CONN, $sql);
292  DBCheckResult($result, $sql, __FILE__, __LINE__);
293  pg_free_result($result);
294 
296  $sql = "DELETE FROM license_file where pfile_fk IN ($pfile_pk_parent, $pfile_pk_child);";
297  $result = pg_query($PG_CONN, $sql);
298  DBCheckResult($result, $sql, __FILE__, __LINE__);
299  pg_free_result($result);
300 
302  $sql = "DELETE FROM upload where upload_pk = $upload_pk;";
303  $result = pg_query($PG_CONN, $sql);
304  DBCheckResult($result, $sql, __FILE__, __LINE__);
305  pg_free_result($result);
306 
308  $sql = "DELETE FROM pfile where pfile_pk IN ($pfile_pk_parent, $pfile_pk_child);";
309  $result = pg_query($PG_CONN, $sql);
310  DBCheckResult($result, $sql, __FILE__, __LINE__);
311  pg_free_result($result);
312 
314  $sql = "DELETE FROM agent where agent_name = 'nomos';";
315  $result = pg_query($PG_CONN, $sql);
316  DBCheckResult($result, $sql, __FILE__, __LINE__);
317  pg_free_result($result);
318 
319  pg_close($PG_CONN);
320  exec("$DB_COMMAND -d $DB_NAME");
321  }
322 }
FUNCTION char * GetUploadtreeTableName(PGconn *pgConn, int upload_pk)
Get the uploadtree table name for this upload_pk If upload_pk does not exist, return "uploadtree"...
Definition: libfossagent.c:421
#define ERROR(...)
Definition: logging.h:90
testGetFilesWithLicense()
testing for GetFilesWithLicense
DBconnect($sysconfdir, $options="", $exitOnFail=true)
Connect to database engine. This is a no-op if $PG_CONN already has a value.
Definition: common-db.php:44
testGetFileLicensesDul()
testing from GetFileLicenses in this test case, this pfile have 2 same license
testGetFileLicenses_string()
testing from GetFileLicenses_tring in this test case, this pfile have only one license ...
GetFileLicenses_string($agent_pk, $pfile_pk, $uploadtree_pk, $uploadtree_tablename='uploadtree')
Same as GetFileLicenses() but returns license list as a single string.
testGetFileLicenses()
testing from GetFileLicenses in this test case, this pfile have only one license
testLevel1WithLicense()
testing for Level1WithLicense
Namespace used by reuser agent.
char * uploadtree_tablename
upload.uploadtree_tablename
Definition: adj2nest.c:112
fo_dbManager * dbManager
fo_dbManager object
Definition: process.c:28
Level1WithLicense($agent_pk, $rf_shortname, $uploadtree_pk, $PkgsOnly=false, $uploadtree_tablename)
Given an uploadtree_pk, find all the non-artifact, immediate children (uploadtree_pk&#39;s) that have lic...
foreach($Options as $Option=> $OptVal) if(0==$reference_flag &&0==$nomos_flag) $PG_CONN
DBCheckResult($result, $sql, $filenm, $lineno)
Check the postgres result for unexpected errors. If found, treat them as fatal.
Definition: common-db.php:198
GetFileLicenses($agent, $pfile_pk, $uploadtree_pk, $uploadtree_tablename='uploadtree', $duplicate="")
get all the licenses for a single file or uploadtree
GetFilesWithLicense($agent_pk, $rf_shortname, $uploadtree_pk, $PkgsOnly=false, $offset=0, $limit="ALL", $order="", $tag_pk=null, $uploadtree_tablename)
Get files with a given license (shortname).