FOSSology  3.2.0rc1
Open Source License Compliance by Open Source Software
cliParamsTest4Mimetype.php
Go to the documentation of this file.
1 <?php
2 
3 /*
4  Copyright (C) 2011-2012 Hewlett-Packard Development Company, L.P.
5 
6  Copyright (C) 2018 Siemens AG
7 
8  This program is free software; you can redistribute it and/or
9  modify it under the terms of the GNU General Public License
10  version 2 as published by the Free Software Foundation.
11 
12  This program is distributed in the hope that it will be useful,
13  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  GNU General Public License for more details.
16 
17  You should have received a copy of the GNU General Public License along
18  with this program; if not, write to the Free Software Foundation, Inc.,
19  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20  */
21 
30 require_once (__DIR__ . "/../../../testing/db/createEmptyTestEnvironment.php");
31 
36 class cliParamsTest4Mimetype extends \PHPUnit\Framework\TestCase {
37 
38  public $EXE_PATH = "";
39  public $PG_CONN;
40  public $DB_COMMAND = "";
41  public $DB_NAME = "";
42  public $DB_CONF = "";
43 
48  protected function setUp() {
49  global $EXE_PATH;
50  global $PG_CONN;
51  global $DB_COMMAND;
52  global $DB_NAME;
53  global $DB_CONF;
54 
55  $cwd = getcwd();
56  list($test_name, $DB_CONF, $DB_NAME, $PG_CONN) = setupTestEnv($cwd, "mimetype");
57 
58  $sql = "CREATE TABLE mimetype (mimetype_pk SERIAL, mimetype_name text);";
59  $result = pg_query($PG_CONN, $sql);
60  pg_free_result($result);
61  $sql = "INSERT INTO public.mimetype (mimetype_pk, mimetype_name) VALUES (2, 'application/gzip'),"
62  . " (3, 'application/x-gzip'), (4, 'application/x-compress'), (5, 'application/x-bzip'), (6, 'application/x-bzip2'),"
63  . " (7, 'application/x-upx'), (8, 'application/pdf'), (9, 'application/x-pdf'), (10, 'application/x-zip'),"
64  . " (11, 'application/zip'), (12, 'application/x-tar'), (13, 'application/x-gtar'), (14, 'application/x-cpio'),"
65  . " (15, 'application/x-rar'), (16, 'application/x-cab'), (17, 'application/x-7z-compressed'),"
66  . " (18, 'application/x-7z-w-compressed'), (19, 'application/x-rpm'), (20, 'application/x-archive'),"
67  . " (21, 'application/x-debian-package'), (22, 'application/x-iso'), (23, 'application/x-iso9660-image'),"
68  . " (24, 'application/x-fat'), (25, 'application/x-ntfs'), (26, 'application/x-ext2'), (27, 'application/x-ext3'),"
69  . " (28, 'application/x-x86_boot'), (29, 'application/x-debian-source'), (30, 'application/x-xz'),"
70  . " (31, 'application/jar'), (32, 'application/java-archive'), (33, 'application/x-dosexec'),"
71  . " (34, 'text/plain');";
72  $result = pg_query($PG_CONN, $sql);
73  pg_free_result($result);
74 
75  $EXE_PATH = '../../agent/mimetype';
76  $usage= "";
77  if(file_exists($EXE_PATH))
78  {
79  $usage = 'Usage: ../../agent/mimetype [options] [file [file [...]]';
80  }
81  else
82  {
83  $this->assertFileExists($EXE_PATH,
84  $message = 'FATAL: cannot find executable file, stop testing\n');
85  }
86  // run it
87  $EXE_PATH = $EXE_PATH." -C -c $DB_CONF";
88  $last = exec("$EXE_PATH -h 2>&1", $out, $rtn);
89  $this->assertEquals($usage, $out[0]); // check if executable file mimetype is exited
90  }
91 
101  global $EXE_PATH;
102  global $PG_CONN;
103 
104  $mimeType1 = "application/x-executable";
105  /* delete test data pre testing */
106  $sql = "DELETE FROM mimetype where mimetype_name in ('$mimeType1');";
107  $result = pg_query($PG_CONN, $sql);
108  pg_free_result($result);
109 
110  /* the file is one executable file */
111  $filePath = "../../agent/mimetype";
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 = "../../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 = pg_query($PG_CONN, $sql);
126  pg_free_result($result);
127  }
128 
129 
136  function testMimetypeInDB(){
137  global $EXE_PATH;
138  global $PG_CONN;
139 
140  $mimeType = "text/x-makefile";
141  /* delete test data pre testing */
142  $sql = "DELETE FROM mimetype where mimetype_name in ('$mimeType');";
143  $result = pg_query($PG_CONN, $sql);
144  pg_free_result($result);
145  /* insert on record */
146  $sql = "INSERT INTO mimetype(mimetype_pk, mimetype_name) VALUES(10000, '$mimeType');";
147  $result = pg_query($PG_CONN, $sql);
148  pg_free_result($result);
149  /* the file is one c source file */
150  $filePath = "./Makefile";
151  $command = "$EXE_PATH $filePath";
152  exec($command, $out, $rtn);
153  $expected_string = "text/x-makefile : mimetype_pk=10000";
154  $this->assertStringStartsWith($expected_string, $out[0]);
155 
156  /* delete test data post testing */
157  $sql = "DELETE FROM mimetype where mimetype_name in ('$mimeType');";
158  $result = pg_query($PG_CONN, $sql);
159  pg_free_result($result);
160  }
161 
165  protected function tearDown() {
166  global $PG_CONN;
167  global $DB_COMMAND;
168  global $DB_NAME;
169  global $DB_CONF;
170 
171  pg_close($PG_CONN);
172  exec("$DB_COMMAND -d $DB_NAME");
173  exec("rm -rf $DB_CONF");
174  }
175 }
176 
177 
Test mimetype agent from cli.
testMimetypeNotInDB()
Test mimetype name is not in table mimetype.
list_t type structure used to keep various lists. (e.g. there are multiple lists).
Definition: nomos.h:321
testMimetypeInDB()
The mimetype name is in table mimetype.