FOSSology  3.2.0rc1
Open Source License Compliance by Open Source Software
testOtheFunctions.c
Go to the documentation of this file.
1 /*********************************************************************
2 Copyright (C) 2011 Hewlett-Packard Development Company, L.P.
3 
4 This program is free software; you can redistribute it and/or
5 modify it under the terms of the GNU General Public License
6 version 2 as published by the Free Software Foundation.
7 
8 This program is distributed in the hope that it will be useful,
9 but WITHOUT ANY WARRANTY; without even the implied warranty of
10 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 GNU General Public License for more details.
12 
13 You should have received a copy of the GNU General Public License along
14 with this program; if not, write to the Free Software Foundation, Inc.,
15 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16 *********************************************************************/
17 
18 /* cunit includes */
19 #include <CUnit/CUnit.h>
20 #include "finder.h"
21 #include <string.h>
22 
28 extern int CheckMimeTypes (char *Ext);
29 extern int DBCheckFileExtention();
30 extern char * GetFieldValue (char *Sin, char *Field, int FieldMax,
31  char *Value, int ValueMax);
32 extern char *DBConfFile;
33 
34 static PGresult *result = NULL;
35 static long upload_pk = -1;
36 static long pfile_pk = -1;
37 
41 int DBInit()
42 {
43  char *ErrorBuf;
44 
45  pgConn = fo_dbconnect(DBConfFile, &ErrorBuf);
46  if (!pgConn)
47  {
48  LOG_FATAL("Unable to connect to database");
49  exit(-1);
50  }
51  memset(SQL,'\0',MAXCMD);
52  snprintf(SQL,MAXCMD,"BEGIN;");
53  result = PQexec(pgConn, SQL);
54  if (fo_checkPQcommand(pgConn, result, SQL, __FILE__ ,__LINE__))
55  {
56  printf("Perpare pfile information ERROR!\n");
57  return (-1);
58  }
59  PQclear(result);
60  /* insert pfile */
61  memset(SQL,'\0',MAXCMD);
62  snprintf(SQL,MAXCMD,"INSERT INTO pfile (pfile_sha1,pfile_md5,pfile_size) VALUES ('%.40s','%.32s','%s');",
63  "F1D2319DF20ABC4CEB02CA5A3C2021BD87B26810","87972FC55E2CDD2609ED85051BE50BAF","722");
64  result = PQexec(pgConn, SQL);
65  if (fo_checkPQcommand(pgConn, result, SQL, __FILE__ ,__LINE__))
66  {
67  printf("Perpare pfile information ERROR!\n");
68  return (-1);
69  }
70  PQclear(result);
71 
72  /* select pfile_pk */
73  memset(SQL,'\0',MAXCMD);
74  snprintf(SQL,MAXCMD,"SELECT pfile_pk FROM pfile WHERE pfile_sha1 = '%.40s' AND pfile_md5 = '%.32s' AND pfile_size = '%s';",
75  "F1D2319DF20ABC4CEB02CA5A3C2021BD87B26810","87972FC55E2CDD2609ED85051BE50BAF","722");
76  result = PQexec(pgConn, SQL);
77  if (fo_checkPQresult(pgConn, result, SQL, __FILE__, __LINE__))
78  {
79  printf("Get pfile information ERROR!\n");
80  return (-1);
81  }
82  pfile_pk = atoi(PQgetvalue(result, 0, 0));
83  PQclear(result);
84 
85  /* insert upload ununpack.c */
86  memset(SQL,'\0',MAXCMD);
87  snprintf(SQL,MAXCMD,"INSERT INTO upload (upload_filename,upload_mode,upload_ts,pfile_fk) VALUES ('ununpack.c',40,now(),'%ld');", pfile_pk);
88  result = PQexec(pgConn, SQL);
89  if (fo_checkPQcommand(pgConn, result, SQL, __FILE__ ,__LINE__))
90  {
91  printf("Perpare pfile information ERROR!\n");
92  exit(-1);
93  }
94  PQclear(result);
95 
96  /* select upload_pk */
97  memset(SQL,'\0',MAXCMD);
98  snprintf(SQL,MAXCMD,"SELECT upload_pk FROM upload WHERE pfile_fk = '%ld';",
99  pfile_pk);
100  result = PQexec(pgConn, SQL);
101  if (fo_checkPQresult(pgConn, result, SQL, __FILE__, __LINE__))
102  {
103  printf("Get pfile information ERROR!\n");
104  return (-1);
105  }
106  upload_pk = atoi(PQgetvalue(result, 0, 0));
107  PQclear(result);
108 
109  /* insert uploadtree */
110  memset(SQL, '\0', MAXCMD);
111  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);
112  result = PQexec(pgConn, SQL);
113  if (fo_checkPQcommand(pgConn, result, SQL, __FILE__, __LINE__))
114  {
115  PQfinish(pgConn);
116  exit(-1);
117  }
118  PQclear(result);
119 
120  memset(SQL,'\0',MAXCMD);
121  snprintf(SQL,MAXCMD,"COMMIT;");
122  result = PQexec(pgConn, SQL);
123  if (fo_checkPQcommand(pgConn, result, SQL, __FILE__ ,__LINE__))
124  {
125  printf("Perpare pfile information ERROR!\n");
126  return (-1);
127  }
128  PQclear(result);
129  MagicCookie = magic_open(MAGIC_PRESERVE_ATIME|MAGIC_MIME);
130  /* clear all data in mimetype */
131  memset(SQL, '\0', MAXCMD);
132  snprintf(SQL, MAXCMD, "DELETE FROM mimetype;");
133  result = PQexec(pgConn, SQL);
134  if (fo_checkPQcommand(pgConn, result, SQL, __FILE__, __LINE__))
135  {
136  PQfinish(pgConn);
137  exit(-1);
138  }
139  PQclear(result);
140 
141  Akey = pfile_pk;
142  FMimetype = fopen("/etc/mime.types","rb");
143  if (!FMimetype)
144  {
145  LOG_WARNING("Unable to open /etc/mime.types\n");
146  }
147 
148  return 0;
149 }
153 int DBClean()
154 {
155  memset(SQL,'\0',MAXCMD);
156  snprintf(SQL,MAXCMD,"BEGIN;");
157  result = PQexec(pgConn, SQL);
158  if (fo_checkPQcommand(pgConn, result, SQL, __FILE__ ,__LINE__))
159  {
160  printf("Remove pfile database information ERROR!\n");
161  return (-1);
162  }
163  PQclear(result);
164 
165  /* delete uploadtree info */
166  memset(SQL,'\0',MAXCMD);
167  snprintf(SQL,MAXCMD,"DELETE FROM uploadtree WHERE upload_fk = %ld;", upload_pk);
168  result = PQexec(pgConn, SQL);
169  if (fo_checkPQcommand(pgConn, result, SQL, __FILE__ ,__LINE__))
170  {
171  printf("Remove pfile database information ERROR!\n");
172  return (-1);
173  }
174  PQclear(result);
175 
176  /* delete upload info */
177  memset(SQL,'\0',MAXCMD);
178  snprintf(SQL,MAXCMD,"DELETE FROM upload WHERE upload_pk = %ld;", upload_pk);
179  result = PQexec(pgConn, SQL);
180  if (fo_checkPQcommand(pgConn, result, SQL, __FILE__ ,__LINE__))
181  {
182  printf("Remove pfile database information ERROR!\n");
183  return (-1);
184  }
185  PQclear(result);
186 
187  /* delete pfile info */
188  memset(SQL,'\0',MAXCMD);
189  snprintf(SQL,MAXCMD,"DELETE FROM pfile WHERE pfile_pk = %ld;", pfile_pk);
190  result = PQexec(pgConn, SQL);
191  if (fo_checkPQcommand(pgConn, result, SQL, __FILE__ ,__LINE__))
192  {
193  printf("Remove pfile database information ERROR!\n");
194  return (-1);
195  }
196  PQclear(result);
197 
198  memset(SQL,'\0',MAXCMD);
199  snprintf(SQL,MAXCMD,"COMMIT;");
200  result = PQexec(pgConn, SQL);
201  if (fo_checkPQcommand(pgConn, result, SQL, __FILE__ ,__LINE__))
202  {
203  printf("Perpare pfile information ERROR!\n");
204  return (-1);
205  }
206  PQclear(result);
207  if (pgConn) PQfinish(pgConn);
208  if (FMimetype) fclose(FMimetype);
209  Akey = 0;
210  return 0;
211 }
212 
213 
214 /* test functions */
215 
225 {
226  /* for the file, if the extension is bin, the mime type is application/octet-stream */
227  char Ext[] = "bin";
228  MagicCookie = magic_open(MAGIC_PRESERVE_ATIME|MAGIC_MIME);
229  /* extension is bin */
230  /* delete the record the mimetype_name is application/octet-stream in mimetype, before testing */
231  memset(SQL, '\0', MAXCMD);
232  snprintf(SQL, MAXCMD, "DELETE FROM mimetype where mimetype_name = 'application/octet-stream';");
233  result = PQexec(pgConn, SQL);
234  if (fo_checkPQcommand(pgConn, result, SQL, __FILE__, __LINE__))
235  {
236  PQfinish(pgConn);
237  exit(-1);
238  }
239  PQclear(result);
240  /* testing the function CheckMimeTypes */
241  int ret = CheckMimeTypes(Ext);
242  /* justify if the record mimetype_name is application/octet-stream is in mimetype */
243  memset(SQL, '\0', MAXCMD);
244  snprintf(SQL, MAXCMD, "SELECT mimetype_pk from mimetype where mimetype_name = 'application/octet-stream';");
245  result = PQexec(pgConn, SQL);
246  if (fo_checkPQresult(pgConn, result, SQL, __FILE__, __LINE__))
247  {
248  PQfinish(pgConn);
249  exit(-1);
250  }
251  int mimetype_pk = atoi(PQgetvalue(result, 0, 0));
252  PQclear(result);
253  CU_ASSERT_EQUAL(ret, mimetype_pk);
254  /* delete the record the mimetype_name is application/octet-stream in mimetype, after testing */
255  memset(SQL, '\0', MAXCMD);
256  snprintf(SQL, MAXCMD, "DELETE FROM mimetype where mimetype_name = 'application/octet-stream';");
257  result = PQexec(pgConn, SQL);
258  if (fo_checkPQcommand(pgConn, result, SQL, __FILE__, __LINE__))
259  {
260  PQfinish(pgConn);
261  exit(-1);
262  }
263  PQclear(result);
264 #if 0
265 #endif
266  /* mimetype_name is spec */
267  /* for the file, if the extension is spec, the mime type is application/x-rpm-spec */
268  char Ext2[] = "spec";
269  /* delete the record the mimetype_name is application/x-rpm-spec in mimetype, before testing */
270  memset(SQL, '\0', MAXCMD);
271  snprintf(SQL, MAXCMD, "DELETE FROM mimetype where mimetype_name = 'application/x-rpm-spec';");
272  result = PQexec(pgConn, SQL);
273  if (fo_checkPQcommand(pgConn, result, SQL, __FILE__, __LINE__))
274  {
275  PQfinish(pgConn);
276  exit(-1);
277  }
278  PQclear(result);
279  /* testing the function CheckMimeTypes */
280  ret = CheckMimeTypes(Ext2);
281  /* justify if the record mimetype_name is application/x-rpm-spec is in mimetype */
282  memset(SQL, '\0', MAXCMD);
283  snprintf(SQL, MAXCMD, "SELECT mimetype_pk from mimetype where mimetype_name = 'application/x-rpm-spec';");
284  result = PQexec(pgConn, SQL);
285  if (fo_checkPQresult(pgConn, result, SQL, __FILE__, __LINE__))
286  {
287  PQfinish(pgConn);
288  exit(-1);
289  }
290  mimetype_pk = atoi(PQgetvalue(result, 0, 0));
291  PQclear(result);
292  CU_ASSERT_EQUAL(ret, mimetype_pk);
293  /* delete the record the mimetype_name is application/x-rpm-spec in mimetype, after testing */
294  memset(SQL, '\0', MAXCMD);
295  snprintf(SQL, MAXCMD, "DELETE FROM mimetype where mimetype_name = 'application/x-rpm-spec';");
296  result = PQexec(pgConn, SQL);
297  if (fo_checkPQcommand(pgConn, result, SQL, __FILE__, __LINE__))
298  {
299  PQfinish(pgConn);
300  exit(-1);
301  }
302  PQclear(result);
303 }
304 
312 {
313  /* delete the record the mimetype_name is text/x-csrc in mimetype, before testing */
314  memset(SQL, '\0', MAXCMD);
315  snprintf(SQL, MAXCMD, "DELETE FROM mimetype where mimetype_name = 'text/x-csrc';");
316  result = PQexec(pgConn, SQL);
317  if (fo_checkPQcommand(pgConn, result, SQL, __FILE__, __LINE__))
318  {
319  PQfinish(pgConn);
320  exit(-1);
321  }
322  PQclear(result);
323 
324  int mimetype_id = DBCheckFileExtention();
325  memset(SQL, '\0', MAXCMD);
326  snprintf(SQL,sizeof(SQL)-1,"SELECT mimetype_pk FROM mimetype where mimetype_name = 'text/x-csrc';" );
327  result = PQexec(pgConn, SQL);
328  if (fo_checkPQresult(pgConn, result, SQL, __FILE__, __LINE__))
329  {
330  PQfinish(pgConn);
331  exit(-1);
332  }
333  int mimetype_pk = atoi(PQgetvalue(result, 0, 0));
334  PQclear(result);
335  CU_ASSERT_EQUAL(mimetype_id, mimetype_pk);
336  /* delete the record the mimetype_name is text/x-csrc in mimetype, after testing */
337  memset(SQL, '\0', MAXCMD);
338  snprintf(SQL, MAXCMD, "DELETE FROM mimetype where mimetype_name = 'text/x-csrc';");
339  result = PQexec(pgConn, SQL);
340  if (fo_checkPQcommand(pgConn, result, SQL, __FILE__, __LINE__))
341  {
342  PQfinish(pgConn);
343  exit(-1);
344  }
345  PQclear(result);
346 }
347 
356 {
357  char Sin[] = "akey='20' pfile_fk='37331'" ;
358  char Field[256] = {0};
359  char Value[1024] = {0};
360  GetFieldValue(Sin, Field, 256, Value, 1024);
361  CU_ASSERT_STRING_EQUAL(Field, "akey");
362  CU_ASSERT_STRING_EQUAL(Value, "20");
363 }
364 
365 
370 {
371 #if 0
372 #endif
373 {"CheckMimeTypes:C", testCheckMimeTypes},
374  CU_TEST_INFO_NULL
375 };
376 
381 {
382 #if 0
383 #endif
384 {"DBCheckFileExtention:C", testDBCheckFileExtention},
385  CU_TEST_INFO_NULL
386 };
387 
391 CU_TestInfo testcases_Utilities[] =
392 {
393 #if 0
394 #endif
395 {"GetFieldValue:Exist", testGetFieldValue},
396  CU_TEST_INFO_NULL
397 };
398 
int fo_checkPQresult(PGconn *pgConn, PGresult *result, char *sql, char *FileID, int LineNumb)
Check the result status of a postgres SELECT.
Definition: libfossdb.c:181
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:196
PGconn * pgConn
Database connection.
Definition: adj2nest.c:98
int DBInit()
initialize DB
CU_TestInfo testcases_DBCheckFileExtention[]
testcases for function DBCheckFileExtention
void testCheckMimeTypes()
for function CheckMimeTypes()
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:215
char SQL[256]
SQL query to execute.
Definition: adj2nest.c:90
CU_TestInfo testcases_Utilities[]
testcases for function GetFieldValue
int DBClean()
clean the env
CU_TestInfo testcases_CheckMimeTypes[]
testcases for function CheckMimeTypes
char * GetFieldValue(char *Sin, char *Field, int FieldMax, char *Value, int ValueMax)
Given a string that contains field=&#39;value&#39; pairs, save the items.
Definition: finder.c:425
void testDBCheckFileExtention()
for function DBCheckFileExtention()
void testGetFieldValue()
for function GetFieldValue()
FILE * FMimetype
for /etc/mime.types
Definition: finder.c:32
magic_t MagicCookie
for Magic
Definition: finder.c:34
int CheckMimeTypes(char *Ext)
Given an extension, see if extension exists in the /etc/mime.types.
Definition: finder.c:138
char * DBConfFile
DB conf file location.
Definition: testRun.c:33
const char * upload_pk
Definition: sqlstatements.h:93
PGconn * fo_dbconnect(char *DBConfFile, char **ErrorBuf)
Connect to a database. The default is Db.conf.
Definition: libfossdb.c:40