FOSSology  4.4.0
Open Source License Compliance by Open Source Software
cliParamsTest4Mimetype.php
Go to the documentation of this file.
1 <?php
2 /*
3  SPDX-FileCopyrightText: © 2011-2012 Hewlett-Packard Development Company, L.P.
4  SPDX-FileCopyrightText: © 2018 Siemens AG
5 
6  SPDX-License-Identifier: GPL-2.0-only
7 */
8 
20 
25 class cliParamsTest4Mimetype extends \PHPUnit\Framework\TestCase {
26 
27  public $EXE_PATH = "";
28  public $cwd;
29  public $DB_CONF = "";
30 
32  private $testDb;
33 
35  private $dbManager;
36 
38  private $testInstaller;
39 
44  protected function setUp() : void {
45  global $EXE_PATH;
46  global $cwd;
47  global $DB_CONF;
48 
49  $cwd = dirname(__DIR__, 4).'/build/src/mimetype/agent_tests';
50  $this->testDb = new TestPgDb("fossmimetypetest");
51  $tables = array('mimetype', 'agent');
52  $this->testDb->createPlainTables($tables);
53  $this->testDb->createSequences(['mimetype_mimetype_pk_seq', 'agent_agent_pk_seq']);
54  $this->testDb->createConstraints(['mimetype_pk', 'dirmodemask','agent_pkey']);
55  $this->testDb->alterTables($tables);
56  $this->dbManager = $this->testDb->getDbManager();
57  $DB_CONF = $this->testDb->getFossSysConf();
58  $this->testInstaller = new TestInstaller($DB_CONF);
59  $this->testInstaller->init();
60  $this->testInstaller->cpRepo();
61  $this->testInstaller->install($cwd.'/..');
62 
63  $sql = "INSERT INTO public.mimetype (mimetype_pk, mimetype_name) VALUES (2, 'application/gzip'),"
64  . " (3, 'application/x-gzip'), (4, 'application/x-compress'), (5, 'application/x-bzip'), (6, 'application/x-bzip2'),"
65  . " (7, 'application/x-upx'), (8, 'application/pdf'), (9, 'application/x-pdf'), (10, 'application/x-zip'),"
66  . " (11, 'application/zip'), (12, 'application/x-tar'), (13, 'application/x-gtar'), (14, 'application/x-cpio'),"
67  . " (15, 'application/x-rar'), (16, 'application/x-cab'), (17, 'application/x-7z-compressed'),"
68  . " (18, 'application/x-7z-w-compressed'), (19, 'application/x-rpm'), (20, 'application/x-archive'),"
69  . " (21, 'application/x-debian-package'), (22, 'application/x-iso'), (23, 'application/x-iso9660-image'),"
70  . " (24, 'application/x-fat'), (25, 'application/x-ntfs'), (26, 'application/x-ext2'), (27, 'application/x-ext3'),"
71  . " (28, 'application/x-x86_boot'), (29, 'application/x-debian-source'), (30, 'application/x-xz'),"
72  . " (31, 'application/jar'), (32, 'application/java-archive'), (33, 'application/x-dosexec'),"
73  . " (34, 'text/plain');";
74  $result = $this->dbManager->getSingleRow($sql, [], __METHOD__ . "insert.mimetype");
75 
76  $EXE_PATH = $cwd . '/../agent/mimetype';
77  $usage= "";
78  if(file_exists($EXE_PATH))
79  {
80  $usage = 'Usage: '.$EXE_PATH.' [options] [file [file [...]]';
81  }
82  else
83  {
84  $this->assertFileExists($EXE_PATH,
85  $message = 'FATAL: cannot find executable file, stop testing\n');
86  }
87  // run it
88  $EXE_PATH .= " -C -c $DB_CONF";
89  $last = exec("$EXE_PATH -h 2>&1", $out, $rtn);
90  $this->assertEquals($usage, $out[0]); // check if executable file mimetype is exited
91  }
92 
102  global $EXE_PATH;
103 
104  $mimeType1 = "application/x-msi";
105  /* delete test data pre testing */
106  $sql = "DELETE FROM mimetype where mimetype_name in ('$mimeType1');";
107  $result = $this->dbManager->getSingleRow($sql, [], __METHOD__ . "delete.mimetype");
108 
109  /* the file is one executable file */
110  // HACK: Hot fix to use different binary
111  $filePath = dirname(__DIR__, 4).'/build/src/ununpack/agent_tests/testdata/test.msi';
112  $command = "$EXE_PATH $filePath";
113  exec($command, $out, $rtn);
114  $this->assertStringStartsWith($mimeType1, $out[0]);
115 
116  /* the file is one text file */
117  $filePath = dirname(__DIR__, 2)."/mimetype.conf";
118  $command = "$EXE_PATH $filePath";
119  $out = "";
120  exec($command, $out, $rtn);
121  $mimeType2 = "text/plain";
122  $this->assertStringStartsWith($mimeType2, $out[0]);
123  /* delete test data post testing */
124  $sql = "DELETE FROM mimetype where mimetype_name in ('$mimeType1');";
125  $result = $this->dbManager->getSingleRow($sql, [], __METHOD__ . "delete.mimetype");
126  }
127 
128 
135  function testMimetypeInDB(){
136  global $EXE_PATH;
137 
138  $mimeType = "text/plain";
139  /* delete test data pre testing */
140  $sql = "DELETE FROM mimetype where mimetype_name in ('$mimeType');";
141  $result = $this->dbManager->getSingleRow($sql, [], __METHOD__ . "delete.mimetype");
142  /* insert on record */
143  $sql = "INSERT INTO mimetype(mimetype_pk, mimetype_name) VALUES(10000, '$mimeType');";
144  $result = $this->dbManager->getSingleRow($sql, [], __METHOD__ . "insert.mimetype");
145  /* the file is one c source file */
146  $filePath = dirname(__DIR__)."/CMakeLists.txt";
147  $command = "$EXE_PATH $filePath";
148  exec($command, $out, $rtn);
149  $expected_string = "text/plain : mimetype_pk=10000";
150  $this->assertStringStartsWith($expected_string, $out[0]);
151 
152  /* delete test data post testing */
153  $sql = "DELETE FROM mimetype where mimetype_name in ('$mimeType');";
154  $result = $this->dbManager->getSingleRow($sql, [], __METHOD__ . "delete.mimetype");
155  }
156 
160  protected function tearDown() : void {
161  global $cwd;
162  if (!is_callable('cwdect')) {
163  return;
164  }
165  $this->testDb->fullDestruct();
166  $this->testDb = null;
167  $this->dbManager = null;
168  $this->testInstaller->uninstall($cwd.'/..');
169  }
170 }
171 
172 
Test mimetype agent from cli.
testMimetypeInDB()
The mimetype name is in table mimetype.
testMimetypeNotInDB()
Test mimetype name is not in table mimetype.
fo_dbManager * dbManager
fo_dbManager object
Definition: process.c:16