FOSSology  4.4.0
Open Source License Compliance by Open Source Software
testOtheFunctions.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 /* cunit includes */
7 #include <CUnit/CUnit.h>
8 #include "finder.h"
9 #include <string.h>
10 
16 extern int CheckMimeTypes (char *Ext);
17 extern int DBCheckFileExtention();
18 extern char * GetFieldValue (char *Sin, char *Field, int FieldMax,
19  char *Value, int ValueMax);
20 extern char *DBConfFile;
21 
22 static PGresult *result = NULL;
23 static long upload_pk = -1;
24 static long pfile_pk = -1;
25 
29 int DBInit()
30 {
31  char *ErrorBuf;
32 
33  pgConn = fo_dbconnect(DBConfFile, &ErrorBuf);
34  if (!pgConn)
35  {
36  LOG_FATAL("Unable to connect to database");
37  exit(-1);
38  }
39  memset(SQL,'\0',MAXCMD);
40  snprintf(SQL,MAXCMD,"BEGIN;");
41  result = PQexec(pgConn, SQL);
42  if (fo_checkPQcommand(pgConn, result, SQL, __FILE__ ,__LINE__))
43  {
44  printf("Perpare pfile information ERROR!\n");
45  return (-1);
46  }
47  PQclear(result);
48  /* insert pfile */
49  memset(SQL,'\0',MAXCMD);
50  snprintf(SQL,MAXCMD,"INSERT INTO pfile (pfile_sha1,pfile_md5,pfile_size) VALUES ('%.40s','%.32s','%s');",
51  "F1D2319DF20ABC4CEB02CA5A3C2021BD87B26810","87972FC55E2CDD2609ED85051BE50BAF","722");
52  result = PQexec(pgConn, SQL);
53  if (fo_checkPQcommand(pgConn, result, SQL, __FILE__ ,__LINE__))
54  {
55  printf("Perpare pfile information ERROR!\n");
56  return (-1);
57  }
58  PQclear(result);
59 
60  /* select pfile_pk */
61  memset(SQL,'\0',MAXCMD);
62  snprintf(SQL,MAXCMD,"SELECT pfile_pk FROM pfile WHERE pfile_sha1 = '%.40s' AND pfile_md5 = '%.32s' AND pfile_size = '%s';",
63  "F1D2319DF20ABC4CEB02CA5A3C2021BD87B26810","87972FC55E2CDD2609ED85051BE50BAF","722");
64  result = PQexec(pgConn, SQL);
65  if (fo_checkPQresult(pgConn, result, SQL, __FILE__, __LINE__))
66  {
67  printf("Get pfile information ERROR!\n");
68  return (-1);
69  }
70  pfile_pk = atoi(PQgetvalue(result, 0, 0));
71  PQclear(result);
72 
73  /* insert upload ununpack.c */
74  memset(SQL,'\0',MAXCMD);
75  snprintf(SQL,MAXCMD,"INSERT INTO upload (upload_filename,upload_mode,upload_ts,pfile_fk) VALUES ('ununpack.c',40,now(),'%ld');", pfile_pk);
76  result = PQexec(pgConn, SQL);
77  if (fo_checkPQcommand(pgConn, result, SQL, __FILE__ ,__LINE__))
78  {
79  printf("Perpare pfile information ERROR!\n");
80  exit(-1);
81  }
82  PQclear(result);
83 
84  /* select upload_pk */
85  memset(SQL,'\0',MAXCMD);
86  snprintf(SQL,MAXCMD,"SELECT upload_pk FROM upload WHERE pfile_fk = '%ld';",
87  pfile_pk);
88  result = PQexec(pgConn, SQL);
89  if (fo_checkPQresult(pgConn, result, SQL, __FILE__, __LINE__))
90  {
91  printf("Get pfile information ERROR!\n");
92  return (-1);
93  }
94  upload_pk = atoi(PQgetvalue(result, 0, 0));
95  PQclear(result);
96 
97  /* insert uploadtree */
98  memset(SQL, '\0', MAXCMD);
99  snprintf(SQL,MAXCMD,"INSERT INTO uploadtree (upload_fk,pfile_fk,lft,rgt,ufile_name) VALUES (%ld,%ld,1,48,'ununpack.c');", upload_pk, pfile_pk);
100  result = PQexec(pgConn, SQL);
101  if (fo_checkPQcommand(pgConn, result, SQL, __FILE__, __LINE__))
102  {
103  PQfinish(pgConn);
104  exit(-1);
105  }
106  PQclear(result);
107 
108  memset(SQL,'\0',MAXCMD);
109  snprintf(SQL,MAXCMD,"COMMIT;");
110  result = PQexec(pgConn, SQL);
111  if (fo_checkPQcommand(pgConn, result, SQL, __FILE__ ,__LINE__))
112  {
113  printf("Perpare pfile information ERROR!\n");
114  return (-1);
115  }
116  PQclear(result);
117  MagicCookie = magic_open(MAGIC_PRESERVE_ATIME|MAGIC_MIME);
118  /* clear all data in mimetype */
119  memset(SQL, '\0', MAXCMD);
120  snprintf(SQL, MAXCMD, "DELETE FROM mimetype;");
121  result = PQexec(pgConn, SQL);
122  if (fo_checkPQcommand(pgConn, result, SQL, __FILE__, __LINE__))
123  {
124  PQfinish(pgConn);
125  exit(-1);
126  }
127  PQclear(result);
128 
129  Akey = pfile_pk;
130  FMimetype = fopen("/etc/mime.types","rb");
131  if (!FMimetype)
132  {
133  LOG_WARNING("Unable to open /etc/mime.types\n");
134  }
135 
136  return 0;
137 }
141 int DBClean()
142 {
143  memset(SQL,'\0',MAXCMD);
144  snprintf(SQL,MAXCMD,"BEGIN;");
145  result = PQexec(pgConn, SQL);
146  if (fo_checkPQcommand(pgConn, result, SQL, __FILE__ ,__LINE__))
147  {
148  printf("Remove pfile database information ERROR!\n");
149  return (-1);
150  }
151  PQclear(result);
152 
153  /* delete uploadtree info */
154  memset(SQL,'\0',MAXCMD);
155  snprintf(SQL,MAXCMD,"DELETE FROM uploadtree WHERE upload_fk = %ld;", upload_pk);
156  result = PQexec(pgConn, SQL);
157  if (fo_checkPQcommand(pgConn, result, SQL, __FILE__ ,__LINE__))
158  {
159  printf("Remove pfile database information ERROR!\n");
160  return (-1);
161  }
162  PQclear(result);
163 
164  /* delete upload info */
165  memset(SQL,'\0',MAXCMD);
166  snprintf(SQL,MAXCMD,"DELETE FROM upload WHERE upload_pk = %ld;", upload_pk);
167  result = PQexec(pgConn, SQL);
168  if (fo_checkPQcommand(pgConn, result, SQL, __FILE__ ,__LINE__))
169  {
170  printf("Remove pfile database information ERROR!\n");
171  return (-1);
172  }
173  PQclear(result);
174 
175  /* delete pfile info */
176  memset(SQL,'\0',MAXCMD);
177  snprintf(SQL,MAXCMD,"DELETE FROM pfile WHERE pfile_pk = %ld;", pfile_pk);
178  result = PQexec(pgConn, SQL);
179  if (fo_checkPQcommand(pgConn, result, SQL, __FILE__ ,__LINE__))
180  {
181  printf("Remove pfile database information ERROR!\n");
182  return (-1);
183  }
184  PQclear(result);
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  if (pgConn) PQfinish(pgConn);
196  if (FMimetype) fclose(FMimetype);
197  Akey = 0;
198  return 0;
199 }
200 
201 
202 /* test functions */
203 
213 {
214  /* for the file, if the extension is bin, the mime type is application/octet-stream */
215  char Ext[] = "bin";
216  MagicCookie = magic_open(MAGIC_PRESERVE_ATIME|MAGIC_MIME);
217  /* extension is bin */
218  /* delete the record the mimetype_name is application/octet-stream in mimetype, before testing */
219  memset(SQL, '\0', MAXCMD);
220  snprintf(SQL, MAXCMD, "DELETE FROM mimetype where mimetype_name = 'application/octet-stream';");
221  result = PQexec(pgConn, SQL);
222  if (fo_checkPQcommand(pgConn, result, SQL, __FILE__, __LINE__))
223  {
224  PQfinish(pgConn);
225  exit(-1);
226  }
227  PQclear(result);
228  /* testing the function CheckMimeTypes */
229  int ret = CheckMimeTypes(Ext);
230  /* justify if the record mimetype_name is application/octet-stream is in mimetype */
231  memset(SQL, '\0', MAXCMD);
232  snprintf(SQL, MAXCMD, "SELECT mimetype_pk from mimetype where mimetype_name = 'application/octet-stream';");
233  result = PQexec(pgConn, SQL);
234  if (fo_checkPQresult(pgConn, result, SQL, __FILE__, __LINE__))
235  {
236  PQfinish(pgConn);
237  exit(-1);
238  }
239  int mimetype_pk = atoi(PQgetvalue(result, 0, 0));
240  PQclear(result);
241  CU_ASSERT_EQUAL(ret, mimetype_pk);
242  /* delete the record the mimetype_name is application/octet-stream in mimetype, after testing */
243  memset(SQL, '\0', MAXCMD);
244  snprintf(SQL, MAXCMD, "DELETE FROM mimetype where mimetype_name = 'application/octet-stream';");
245  result = PQexec(pgConn, SQL);
246  if (fo_checkPQcommand(pgConn, result, SQL, __FILE__, __LINE__))
247  {
248  PQfinish(pgConn);
249  exit(-1);
250  }
251  PQclear(result);
252 #if 0
253 #endif
254  /* mimetype_name is spec */
255  /* for the file, if the extension is spec, the mime type is application/x-rpm-spec */
256  char Ext2[] = "spec";
257  /* delete the record the mimetype_name is application/x-rpm-spec in mimetype, before testing */
258  memset(SQL, '\0', MAXCMD);
259  snprintf(SQL, MAXCMD, "DELETE FROM mimetype where mimetype_name = 'application/x-rpm-spec';");
260  result = PQexec(pgConn, SQL);
261  if (fo_checkPQcommand(pgConn, result, SQL, __FILE__, __LINE__))
262  {
263  PQfinish(pgConn);
264  exit(-1);
265  }
266  PQclear(result);
267  /* testing the function CheckMimeTypes */
268  ret = CheckMimeTypes(Ext2);
269  /* justify if the record mimetype_name is application/x-rpm-spec is in mimetype */
270  memset(SQL, '\0', MAXCMD);
271  snprintf(SQL, MAXCMD, "SELECT mimetype_pk from mimetype where mimetype_name = 'application/x-rpm-spec';");
272  result = PQexec(pgConn, SQL);
273  if (fo_checkPQresult(pgConn, result, SQL, __FILE__, __LINE__))
274  {
275  PQfinish(pgConn);
276  exit(-1);
277  }
278  mimetype_pk = atoi(PQgetvalue(result, 0, 0));
279  PQclear(result);
280  CU_ASSERT_EQUAL(ret, mimetype_pk);
281  /* delete the record the mimetype_name is application/x-rpm-spec in mimetype, after testing */
282  memset(SQL, '\0', MAXCMD);
283  snprintf(SQL, MAXCMD, "DELETE FROM mimetype where mimetype_name = 'application/x-rpm-spec';");
284  result = PQexec(pgConn, SQL);
285  if (fo_checkPQcommand(pgConn, result, SQL, __FILE__, __LINE__))
286  {
287  PQfinish(pgConn);
288  exit(-1);
289  }
290  PQclear(result);
291 }
292 
300 {
301  /* delete the record the mimetype_name is text/x-csrc in mimetype, before testing */
302  memset(SQL, '\0', MAXCMD);
303  snprintf(SQL, MAXCMD, "DELETE FROM mimetype where mimetype_name = 'text/x-csrc';");
304  result = PQexec(pgConn, SQL);
305  if (fo_checkPQcommand(pgConn, result, SQL, __FILE__, __LINE__))
306  {
307  PQfinish(pgConn);
308  exit(-1);
309  }
310  PQclear(result);
311 
312  int mimetype_id = DBCheckFileExtention();
313  memset(SQL, '\0', MAXCMD);
314  snprintf(SQL,sizeof(SQL)-1,"SELECT mimetype_pk FROM mimetype where mimetype_name = 'text/x-csrc';" );
315  result = PQexec(pgConn, SQL);
316  if (fo_checkPQresult(pgConn, result, SQL, __FILE__, __LINE__))
317  {
318  PQfinish(pgConn);
319  exit(-1);
320  }
321  int mimetype_pk = atoi(PQgetvalue(result, 0, 0));
322  PQclear(result);
323  CU_ASSERT_EQUAL(mimetype_id, mimetype_pk);
324  /* delete the record the mimetype_name is text/x-csrc in mimetype, after testing */
325  memset(SQL, '\0', MAXCMD);
326  snprintf(SQL, MAXCMD, "DELETE FROM mimetype where mimetype_name = 'text/x-csrc';");
327  result = PQexec(pgConn, SQL);
328  if (fo_checkPQcommand(pgConn, result, SQL, __FILE__, __LINE__))
329  {
330  PQfinish(pgConn);
331  exit(-1);
332  }
333  PQclear(result);
334 }
335 
344 {
345  char Sin[] = "akey='20' pfile_fk='37331'" ;
346  char Field[256] = {0};
347  char Value[1024] = {0};
348  GetFieldValue(Sin, Field, 256, Value, 1024);
349  CU_ASSERT_STRING_EQUAL(Field, "akey");
350  CU_ASSERT_STRING_EQUAL(Value, "20");
351 }
352 
353 
358 {
359 #if 0
360 #endif
361 {"CheckMimeTypes:C", testCheckMimeTypes},
362  CU_TEST_INFO_NULL
363 };
364 
369 {
370 #if 0
371 #endif
372 {"DBCheckFileExtention:C", testDBCheckFileExtention},
373  CU_TEST_INFO_NULL
374 };
375 
379 CU_TestInfo testcases_Utilities[] =
380 {
381 #if 0
382 #endif
383 {"GetFieldValue:Exist", testGetFieldValue},
384  CU_TEST_INFO_NULL
385 };
386 
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
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
void testGetFieldValue()
for function GetFieldValue()
char * GetFieldValue(char *Sin, char *Field, int FieldMax, char *Value, int ValueMax)
Given a string that contains field='value' pairs, save the items.
Definition: finder.c:414
int CheckMimeTypes(char *Ext)
Given an extension, see if extension exists in the /etc/mime.types.
Definition: finder.c:127
int DBInit()
initialize DB
CU_TestInfo testcases_DBCheckFileExtention[]
testcases for function DBCheckFileExtention
CU_TestInfo testcases_Utilities[]
testcases for function GetFieldValue
char * DBConfFile
DB conf file location.
Definition: testRun.c:21
void testCheckMimeTypes()
for function CheckMimeTypes()
int DBClean()
clean the env
CU_TestInfo testcases_CheckMimeTypes[]
testcases for function CheckMimeTypes
int DBCheckFileExtention()
Given a pfile, identify any filenames and see if any of them have a known extension based on /etc/mim...
Definition: finder.c:185
void testDBCheckFileExtention()
for function DBCheckFileExtention()