FOSSology  4.4.0
Open Source License Compliance by Open Source Software
testDBCheckMime.c
Go to the documentation of this file.
1 /*
2  SPDX-FileCopyrightText: © 2011 Hewlett-Packard Development Company, L.P.
3 
4  SPDX-License-Identifier: GPL-2.0-only
5 */
6 
7 
8 /* cunit includes */
9 #include <CUnit/CUnit.h>
10 #include "finder.h"
11 #include <string.h>
12 
18 static PGresult *result = NULL;
19 static long upload_pk = -1;
20 static long pfile_pk = -1;
21 extern char *DBConfFile;
22 
27 {
28  char *ErrorBuf;
29 
30  pgConn = fo_dbconnect(DBConfFile, &ErrorBuf);
31 
32  if (!pgConn)
33  {
34  LOG_FATAL("Unable to connect to database");
35  exit(-1);
36  }
37  MagicCookie = magic_open(MAGIC_PRESERVE_ATIME|MAGIC_MIME);
38  memset(SQL,'\0',MAXCMD);
39  snprintf(SQL,MAXCMD,"BEGIN;");
40  result = PQexec(pgConn, SQL);
41  if (fo_checkPQcommand(pgConn, result, SQL, __FILE__ ,__LINE__))
42  {
43  printf("Perpare pfile information ERROR!\n");
44  return (-1);
45  }
46  PQclear(result);
47  /* insert pfile */
48  memset(SQL,'\0',MAXCMD);
49  snprintf(SQL,MAXCMD,"INSERT INTO pfile (pfile_sha1,pfile_md5,pfile_size) VALUES ('%.40s','%.32s','%s');",
50  "F1D2319DF20ABC4CEB02CA5A3C2021BD87B26810","87972FC55E2CDD2609ED85051BE50BAF","722");
51  result = PQexec(pgConn, SQL);
52  if (fo_checkPQcommand(pgConn, result, SQL, __FILE__ ,__LINE__))
53  {
54  printf("Perpare pfile information ERROR!\n");
55  return (-1);
56  }
57  PQclear(result);
58 
59  /* select pfile_pk */
60  memset(SQL,'\0',MAXCMD);
61  snprintf(SQL,MAXCMD,"SELECT pfile_pk FROM pfile WHERE pfile_sha1 = '%.40s' AND pfile_md5 = '%.32s' AND pfile_size = '%s';",
62  "F1D2319DF20ABC4CEB02CA5A3C2021BD87B26810","87972FC55E2CDD2609ED85051BE50BAF","722");
63  result = PQexec(pgConn, SQL);
64  if (fo_checkPQresult(pgConn, result, SQL, __FILE__, __LINE__))
65  {
66  printf("Get pfile information ERROR!\n");
67  return (-1);
68  }
69  pfile_pk = atoi(PQgetvalue(result, 0, 0));
70  PQclear(result);
71 
72  /* insert upload a executable file */
73  memset(SQL,'\0',MAXCMD);
74  snprintf(SQL,MAXCMD,"INSERT INTO upload (upload_filename,upload_mode,upload_ts,pfile_fk) VALUES ('mimetype',40,now(),'%ld');", pfile_pk);
75  result = PQexec(pgConn, SQL);
76  if (fo_checkPQcommand(pgConn, result, SQL, __FILE__ ,__LINE__))
77  {
78  printf("Perpare pfile information ERROR!\n");
79  exit(-1);
80  }
81  PQclear(result);
82 
83  /* select upload_pk */
84  memset(SQL,'\0',MAXCMD);
85  snprintf(SQL,MAXCMD,"SELECT upload_pk FROM upload WHERE pfile_fk = '%ld';",
86  pfile_pk);
87  result = PQexec(pgConn, SQL);
88  if (fo_checkPQresult(pgConn, result, SQL, __FILE__, __LINE__))
89  {
90  printf("Get pfile information ERROR!\n");
91  return (-1);
92  }
93  upload_pk = atoi(PQgetvalue(result, 0, 0));
94  PQclear(result);
95 
96  /* insert uploadtree */
97  memset(SQL, '\0', MAXCMD);
98  snprintf(SQL,MAXCMD,"INSERT INTO uploadtree (upload_fk,pfile_fk,lft,rgt,ufile_name) VALUES (%ld,%ld,1,48,'mimetype');", upload_pk, pfile_pk);
99  result = PQexec(pgConn, SQL);
100  if (fo_checkPQcommand(pgConn, result, SQL, __FILE__, __LINE__))
101  {
102  PQfinish(pgConn);
103  exit(-1);
104  }
105  PQclear(result);
106 
107  memset(SQL,'\0',MAXCMD);
108  snprintf(SQL,MAXCMD,"COMMIT;");
109  result = PQexec(pgConn, SQL);
110  if (fo_checkPQcommand(pgConn, result, SQL, __FILE__ ,__LINE__))
111  {
112  printf("Perpare pfile information ERROR!\n");
113  return (-1);
114  }
115  PQclear(result);
116  MagicCookie = magic_open(MAGIC_PRESERVE_ATIME|MAGIC_MIME);
117  /* clear all data in mimetype */
118  memset(SQL, '\0', MAXCMD);
119  snprintf(SQL, MAXCMD, "DELETE FROM mimetype;");
120  result = PQexec(pgConn, SQL);
121  if (fo_checkPQcommand(pgConn, result, SQL, __FILE__, __LINE__))
122  {
123  PQfinish(pgConn);
124  exit(-1);
125  }
126  PQclear(result);
127 
128  Akey = pfile_pk;
129  FMimetype = fopen("/etc/mime.types","rb");
130  if (!FMimetype)
131  {
132  LOG_WARNING("Unable to open /etc/mime.types\n");
133  }
134 
135  return 0;
136 }
141 {
142  memset(SQL,'\0',MAXCMD);
143  snprintf(SQL,MAXCMD,"BEGIN;");
144  result = PQexec(pgConn, SQL);
145  if (fo_checkPQcommand(pgConn, result, SQL, __FILE__ ,__LINE__))
146  {
147  printf("Remove pfile database information ERROR!\n");
148  return (-1);
149  }
150  PQclear(result);
151 
152  /* delete uploadtree info */
153  memset(SQL,'\0',MAXCMD);
154  snprintf(SQL,MAXCMD,"DELETE FROM uploadtree WHERE upload_fk = %ld;", upload_pk);
155  result = PQexec(pgConn, SQL);
156  if (fo_checkPQcommand(pgConn, result, SQL, __FILE__ ,__LINE__))
157  {
158  printf("Remove pfile database information ERROR!\n");
159  return (-1);
160  }
161  PQclear(result);
162 
163  /* delete upload info */
164  memset(SQL,'\0',MAXCMD);
165  snprintf(SQL,MAXCMD,"DELETE FROM upload WHERE upload_pk = %ld;", upload_pk);
166  result = PQexec(pgConn, SQL);
167  if (fo_checkPQcommand(pgConn, result, SQL, __FILE__ ,__LINE__))
168  {
169  printf("Remove pfile database information ERROR!\n");
170  return (-1);
171  }
172  PQclear(result);
173 
174  /* delete pfile info */
175  memset(SQL,'\0',MAXCMD);
176  snprintf(SQL,MAXCMD,"DELETE FROM pfile WHERE pfile_pk = %ld;", pfile_pk);
177  result = PQexec(pgConn, SQL);
178  if (fo_checkPQcommand(pgConn, result, SQL, __FILE__ ,__LINE__))
179  {
180  printf("Remove pfile database information ERROR!\n");
181  return (-1);
182  }
183  PQclear(result);
184 
185 
186  memset(SQL,'\0',MAXCMD);
187  snprintf(SQL,MAXCMD,"COMMIT;");
188  result = PQexec(pgConn, SQL);
189  if (fo_checkPQcommand(pgConn, result, SQL, __FILE__ ,__LINE__))
190  {
191  printf("Perpare pfile information ERROR!\n");
192  return (-1);
193  }
194  PQclear(result);
195 
196  /* delete the record the mimetype_name is application/octet-stream in mimetype, after testing */
197  memset(SQL, '\0', MAXCMD);
198  snprintf(SQL, MAXCMD, "DELETE FROM mimetype where mimetype_name = 'application/octet-stream';");
199  result = PQexec(pgConn, SQL);
200  if (fo_checkPQcommand(pgConn, result, SQL, __FILE__, __LINE__))
201  {
202  PQfinish(pgConn);
203  exit(-1);
204  }
205  PQclear(result);
206  if (pgConn) PQfinish(pgConn);
207  if (FMimetype) fclose(FMimetype);
208  Akey = 0;
209  return 0;
210 }
211 
212 /* test functions */
213 
222 {
223  char SQL[MAXCMD] = {0};
224  PGresult *result = NULL;
225  char file_path[MAXCMD] = "../../agent/mimetype";
226  char mimetype_name[] = "application/octet-stream";
227  int pfile_mimetypefk = 0;
228  int mimetype_id = -1;
229 
230  DBCheckMime(file_path);
231  /* get mimetype_pk from table mimetype */
232  memset(SQL,'\0',sizeof(SQL));
233  snprintf(SQL,sizeof(SQL)-1,"SELECT mimetype_pk FROM mimetype WHERE mimetype_name= '%s';", mimetype_name);
234  result = PQexec(pgConn, SQL);
235  if (fo_checkPQresult(pgConn, result, SQL, __FILE__, __LINE__))
236  {
237  PQfinish(pgConn);
238  exit(-1);
239  }
240  mimetype_id = atoi(PQgetvalue(result, 0, 0));
241  PQclear(result);
242 
243  /* get mimetype id from pfile */
244  memset(SQL,'\0',sizeof(SQL));
245  snprintf(SQL,sizeof(SQL)-1,"SELECT pfile_mimetypefk FROM pfile WHERE pfile_pk= %ld;", pfile_pk);
246  result = PQexec(pgConn, SQL);
247  if (fo_checkPQresult(pgConn, result, SQL, __FILE__, __LINE__))
248  {
249  PQfinish(pgConn);
250  exit(-1);
251  }
252  pfile_mimetypefk = atoi(PQgetvalue(result, 0, 0));
253  PQclear(result);
254 
255  CU_ASSERT_EQUAL(pfile_mimetypefk, mimetype_id);
256  /* delete the record the mimetype_name is application/octet-stream in mimetype, after testing */
257  memset(SQL, '\0', MAXCMD);
258  snprintf(SQL, MAXCMD, "DELETE FROM mimetype where mimetype_name = 'text/x-csrc';");
259  result = PQexec(pgConn, SQL);
260  if (fo_checkPQcommand(pgConn, result, SQL, __FILE__, __LINE__))
261  {
262  PQfinish(pgConn);
263  exit(-1);
264  }
265  PQclear(result);
266 }
267 
271 CU_TestInfo testcases_DBCheckMime[] =
272 {
273 #if 0
274 #endif
275 {"DBCheckMime:C", testDBCheckMime},
276  CU_TEST_INFO_NULL
277 };
278 
char SQL[256]
SQL query to execute.
Definition: adj2nest.c:78
PGconn * pgConn
Database connection.
Definition: adj2nest.c:86
FILE * FMimetype
for /etc/mime.types
Definition: finder.c:21
magic_t MagicCookie
for Magic
Definition: finder.c:23
void DBCheckMime(char *Filename)
Given a file, check if it has a mime type in the DB.
Definition: finder.c:281
PGconn * fo_dbconnect(char *DBConfFile, char **ErrorBuf)
Connect to a database. The default is Db.conf.
Definition: libfossdb.c:29
int fo_checkPQresult(PGconn *pgConn, PGresult *result, char *sql, char *FileID, int LineNumb)
Check the result status of a postgres SELECT.
Definition: libfossdb.c:170
int fo_checkPQcommand(PGconn *pgConn, PGresult *result, char *sql, char *FileID, int LineNumb)
Check the result status of a postgres commands (not select) If an error occured, write the error to s...
Definition: libfossdb.c:204
const char * upload_pk
Definition: sqlstatements.h:82
int DBCheckMimeClean()
Clean the env.
char * DBConfFile
DB conf file location.
Definition: testRun.c:21
int DBCheckMimeInit()
Initialize DB.
CU_TestInfo testcases_DBCheckMime[]
testcases for function DBCheckMime
void testDBCheckMime()
For function DBCheckMime()