FOSSology  3.2.0rc1
Open Source License Compliance by Open Source Software
testDBCheckMime.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 static PGresult *result = NULL;
29 static long upload_pk = -1;
30 static long pfile_pk = -1;
31 extern char *DBConfFile;
32 
37 {
38  char *ErrorBuf;
39 
40  pgConn = fo_dbconnect(DBConfFile, &ErrorBuf);
41 
42  if (!pgConn)
43  {
44  LOG_FATAL("Unable to connect to database");
45  exit(-1);
46  }
47  MagicCookie = magic_open(MAGIC_PRESERVE_ATIME|MAGIC_MIME);
48  memset(SQL,'\0',MAXCMD);
49  snprintf(SQL,MAXCMD,"BEGIN;");
50  result = PQexec(pgConn, SQL);
51  if (fo_checkPQcommand(pgConn, result, SQL, __FILE__ ,__LINE__))
52  {
53  printf("Perpare pfile information ERROR!\n");
54  return (-1);
55  }
56  PQclear(result);
57  /* insert pfile */
58  memset(SQL,'\0',MAXCMD);
59  snprintf(SQL,MAXCMD,"INSERT INTO pfile (pfile_sha1,pfile_md5,pfile_size) VALUES ('%.40s','%.32s','%s');",
60  "F1D2319DF20ABC4CEB02CA5A3C2021BD87B26810","87972FC55E2CDD2609ED85051BE50BAF","722");
61  result = PQexec(pgConn, SQL);
62  if (fo_checkPQcommand(pgConn, result, SQL, __FILE__ ,__LINE__))
63  {
64  printf("Perpare pfile information ERROR!\n");
65  return (-1);
66  }
67  PQclear(result);
68 
69  /* select pfile_pk */
70  memset(SQL,'\0',MAXCMD);
71  snprintf(SQL,MAXCMD,"SELECT pfile_pk FROM pfile WHERE pfile_sha1 = '%.40s' AND pfile_md5 = '%.32s' AND pfile_size = '%s';",
72  "F1D2319DF20ABC4CEB02CA5A3C2021BD87B26810","87972FC55E2CDD2609ED85051BE50BAF","722");
73  result = PQexec(pgConn, SQL);
74  if (fo_checkPQresult(pgConn, result, SQL, __FILE__, __LINE__))
75  {
76  printf("Get pfile information ERROR!\n");
77  return (-1);
78  }
79  pfile_pk = atoi(PQgetvalue(result, 0, 0));
80  PQclear(result);
81 
82  /* insert upload a executable file */
83  memset(SQL,'\0',MAXCMD);
84  snprintf(SQL,MAXCMD,"INSERT INTO upload (upload_filename,upload_mode,upload_ts,pfile_fk) VALUES ('mimetype',40,now(),'%ld');", pfile_pk);
85  result = PQexec(pgConn, SQL);
86  if (fo_checkPQcommand(pgConn, result, SQL, __FILE__ ,__LINE__))
87  {
88  printf("Perpare pfile information ERROR!\n");
89  exit(-1);
90  }
91  PQclear(result);
92 
93  /* select upload_pk */
94  memset(SQL,'\0',MAXCMD);
95  snprintf(SQL,MAXCMD,"SELECT upload_pk FROM upload WHERE pfile_fk = '%ld';",
96  pfile_pk);
97  result = PQexec(pgConn, SQL);
98  if (fo_checkPQresult(pgConn, result, SQL, __FILE__, __LINE__))
99  {
100  printf("Get pfile information ERROR!\n");
101  return (-1);
102  }
103  upload_pk = atoi(PQgetvalue(result, 0, 0));
104  PQclear(result);
105 
106  /* insert uploadtree */
107  memset(SQL, '\0', MAXCMD);
108  snprintf(SQL,MAXCMD,"INSERT INTO uploadtree (upload_fk,pfile_fk,lft,rgt,ufile_name) VALUES (%ld,%ld,1,48,'mimetype');", upload_pk, pfile_pk);
109  result = PQexec(pgConn, SQL);
110  if (fo_checkPQcommand(pgConn, result, SQL, __FILE__, __LINE__))
111  {
112  PQfinish(pgConn);
113  exit(-1);
114  }
115  PQclear(result);
116 
117  memset(SQL,'\0',MAXCMD);
118  snprintf(SQL,MAXCMD,"COMMIT;");
119  result = PQexec(pgConn, SQL);
120  if (fo_checkPQcommand(pgConn, result, SQL, __FILE__ ,__LINE__))
121  {
122  printf("Perpare pfile information ERROR!\n");
123  return (-1);
124  }
125  PQclear(result);
126  MagicCookie = magic_open(MAGIC_PRESERVE_ATIME|MAGIC_MIME);
127  /* clear all data in mimetype */
128  memset(SQL, '\0', MAXCMD);
129  snprintf(SQL, MAXCMD, "DELETE FROM mimetype;");
130  result = PQexec(pgConn, SQL);
131  if (fo_checkPQcommand(pgConn, result, SQL, __FILE__, __LINE__))
132  {
133  PQfinish(pgConn);
134  exit(-1);
135  }
136  PQclear(result);
137 
138  Akey = pfile_pk;
139  FMimetype = fopen("/etc/mime.types","rb");
140  if (!FMimetype)
141  {
142  LOG_WARNING("Unable to open /etc/mime.types\n");
143  }
144 
145  return 0;
146 }
151 {
152  memset(SQL,'\0',MAXCMD);
153  snprintf(SQL,MAXCMD,"BEGIN;");
154  result = PQexec(pgConn, SQL);
155  if (fo_checkPQcommand(pgConn, result, SQL, __FILE__ ,__LINE__))
156  {
157  printf("Remove pfile database information ERROR!\n");
158  return (-1);
159  }
160  PQclear(result);
161 
162  /* delete uploadtree info */
163  memset(SQL,'\0',MAXCMD);
164  snprintf(SQL,MAXCMD,"DELETE FROM uploadtree WHERE upload_fk = %ld;", upload_pk);
165  result = PQexec(pgConn, SQL);
166  if (fo_checkPQcommand(pgConn, result, SQL, __FILE__ ,__LINE__))
167  {
168  printf("Remove pfile database information ERROR!\n");
169  return (-1);
170  }
171  PQclear(result);
172 
173  /* delete upload info */
174  memset(SQL,'\0',MAXCMD);
175  snprintf(SQL,MAXCMD,"DELETE FROM upload WHERE upload_pk = %ld;", upload_pk);
176  result = PQexec(pgConn, SQL);
177  if (fo_checkPQcommand(pgConn, result, SQL, __FILE__ ,__LINE__))
178  {
179  printf("Remove pfile database information ERROR!\n");
180  return (-1);
181  }
182  PQclear(result);
183 
184  /* delete pfile info */
185  memset(SQL,'\0',MAXCMD);
186  snprintf(SQL,MAXCMD,"DELETE FROM pfile WHERE pfile_pk = %ld;", pfile_pk);
187  result = PQexec(pgConn, SQL);
188  if (fo_checkPQcommand(pgConn, result, SQL, __FILE__ ,__LINE__))
189  {
190  printf("Remove pfile database information ERROR!\n");
191  return (-1);
192  }
193  PQclear(result);
194 
195 
196  memset(SQL,'\0',MAXCMD);
197  snprintf(SQL,MAXCMD,"COMMIT;");
198  result = PQexec(pgConn, SQL);
199  if (fo_checkPQcommand(pgConn, result, SQL, __FILE__ ,__LINE__))
200  {
201  printf("Perpare pfile information ERROR!\n");
202  return (-1);
203  }
204  PQclear(result);
205 
206  /* delete the record the mimetype_name is application/octet-stream in mimetype, after testing */
207  memset(SQL, '\0', MAXCMD);
208  snprintf(SQL, MAXCMD, "DELETE FROM mimetype where mimetype_name = 'application/octet-stream';");
209  result = PQexec(pgConn, SQL);
210  if (fo_checkPQcommand(pgConn, result, SQL, __FILE__, __LINE__))
211  {
212  PQfinish(pgConn);
213  exit(-1);
214  }
215  PQclear(result);
216  if (pgConn) PQfinish(pgConn);
217  if (FMimetype) fclose(FMimetype);
218  Akey = 0;
219  return 0;
220 }
221 
222 /* test functions */
223 
232 {
233  char SQL[MAXCMD] = {0};
234  PGresult *result = NULL;
235  char file_path[MAXCMD] = "../../agent/mimetype";
236  char mimetype_name[] = "application/octet-stream";
237  int pfile_mimetypefk = 0;
238  int mimetype_id = -1;
239 
240  DBCheckMime(file_path);
241  /* get mimetype_pk from table mimetype */
242  memset(SQL,'\0',sizeof(SQL));
243  snprintf(SQL,sizeof(SQL)-1,"SELECT mimetype_pk FROM mimetype WHERE mimetype_name= '%s';", mimetype_name);
244  result = PQexec(pgConn, SQL);
245  if (fo_checkPQresult(pgConn, result, SQL, __FILE__, __LINE__))
246  {
247  PQfinish(pgConn);
248  exit(-1);
249  }
250  mimetype_id = atoi(PQgetvalue(result, 0, 0));
251  PQclear(result);
252 
253  /* get mimetype id from pfile */
254  memset(SQL,'\0',sizeof(SQL));
255  snprintf(SQL,sizeof(SQL)-1,"SELECT pfile_mimetypefk FROM pfile WHERE pfile_pk= %ld;", pfile_pk);
256  result = PQexec(pgConn, SQL);
257  if (fo_checkPQresult(pgConn, result, SQL, __FILE__, __LINE__))
258  {
259  PQfinish(pgConn);
260  exit(-1);
261  }
262  pfile_mimetypefk = atoi(PQgetvalue(result, 0, 0));
263  PQclear(result);
264 
265  CU_ASSERT_EQUAL(pfile_mimetypefk, mimetype_id);
266  /* delete the record the mimetype_name is application/octet-stream in mimetype, after testing */
267  memset(SQL, '\0', MAXCMD);
268  snprintf(SQL, MAXCMD, "DELETE FROM mimetype where mimetype_name = 'text/x-csrc';");
269  result = PQexec(pgConn, SQL);
270  if (fo_checkPQcommand(pgConn, result, SQL, __FILE__, __LINE__))
271  {
272  PQfinish(pgConn);
273  exit(-1);
274  }
275  PQclear(result);
276 }
277 
281 CU_TestInfo testcases_DBCheckMime[] =
282 {
283 #if 0
284 #endif
285 {"DBCheckMime:C", testDBCheckMime},
286  CU_TEST_INFO_NULL
287 };
288 
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
char * DBConfFile
DB conf file location.
Definition: testRun.c:33
int DBCheckMimeClean()
Clean the env.
PGconn * pgConn
Database connection.
Definition: adj2nest.c:98
void testDBCheckMime()
For function DBCheckMime()
int DBCheckMimeInit()
Initialize DB.
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
void DBCheckMime(char *Filename)
Given a file, check if it has a mime type in the DB.
Definition: finder.c:292
char SQL[256]
SQL query to execute.
Definition: adj2nest.c:90
CU_TestInfo testcases_DBCheckMime[]
testcases for function DBCheckMime
FILE * FMimetype
for /etc/mime.types
Definition: finder.c:32
magic_t MagicCookie
for Magic
Definition: finder.c:34
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