FOSSology  3.2.0rc1
Open Source License Compliance by Open Source Software
testGetMetadataDebBinary.c
Go to the documentation of this file.
1 /*********************************************************************
2 Copyright (C) 2011-2014 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 #include "pkgagent.h"
18 
19 #include <stdio.h>
20 #include "CUnit/CUnit.h"
21 
22 #define MAXSQL 4096
23 extern char *DBConfFile;
35 long prepare_Database(PGconn *db_conn, struct debpkginfo *pi)
36 {
37  long upload_pk;
38  char SQL[MAXSQL];
39  PGresult *result;
40  long control_pfilepk;
41 
42  memset(SQL,'\0',MAXSQL);
43  snprintf(SQL,MAXSQL,"BEGIN;");
44  result = PQexec(db_conn, SQL);
45  if (fo_checkPQcommand(db_conn, result, SQL, __FILE__ ,__LINE__))
46  {
47  printf("Perpare pfile information ERROR!\n");
48  PQclear(result);
49  return (-1);
50  }
51  PQclear(result);
52 
53  /* insert mimetype */
54  memset(SQL,'\0',MAXSQL);
55  snprintf(SQL,MAXSQL,"INSERT INTO mimetype (mimetype_name) VALUES ('application/x-rpm');");
56  result = PQexec(db_conn, SQL);
57  if (fo_checkPQcommand(db_conn, result, SQL, __FILE__ ,__LINE__))
58  {
59  printf("Perpare mimetype information ERROR!\n");
60  PQclear(result);
61  return (-1);
62  }
63  PQclear(result);
64  memset(SQL,'\0',MAXSQL);
65  snprintf(SQL,MAXSQL,"INSERT INTO mimetype (mimetype_name) VALUES ('application/x-debian-package');");
66  result = PQexec(db_conn, SQL);
67  if (fo_checkPQcommand(db_conn, result, SQL, __FILE__ ,__LINE__))
68  {
69  printf("Perpare mimetype information ERROR!\n");
70  PQclear(result);
71  return (-1);
72  }
73  PQclear(result);
74  memset(SQL,'\0',MAXSQL);
75  snprintf(SQL,MAXSQL,"INSERT INTO mimetype (mimetype_name) VALUES ('application/x-debian-source');");
76  result = PQexec(db_conn, SQL);
77  if (fo_checkPQcommand(db_conn, result, SQL, __FILE__ ,__LINE__))
78  {
79  printf("Perpare mimetype information ERROR!\n");
80  PQclear(result);
81  return (-1);
82  }
83  PQclear(result);
84 
85  /* insert pfile: fossology-web_1.4.1_all.deb */
86  memset(SQL,'\0',MAXSQL);
87  snprintf(SQL,MAXSQL,"INSERT INTO pfile (pfile_sha1,pfile_md5,pfile_size) VALUES ('%.40s','%.32s','%s');",
88  "AF1DF2C4B32E4115DB5F272D9EFD0E674CF2A0BC","2239AA7DAC291B6F8D0A56396B1B8530","4560");
89  result = PQexec(db_conn, SQL);
90  if (fo_checkPQcommand(db_conn, result, SQL, __FILE__ ,__LINE__))
91  {
92  printf("Perpare pfile information ERROR!\n");
93  PQclear(result);
94  return (-1);
95  }
96  PQclear(result);
97  /* insert pfile: control */
98  memset(SQL,'\0',MAXSQL);
99  snprintf(SQL,MAXSQL,"INSERT INTO pfile (pfile_sha1,pfile_md5,pfile_size) VALUES ('%.40s','%.32s','%s');",
100  "F1D2319DF20ABC4CEB02CA5A3C2021BD87B26810","87972FC55E2CDD2609ED85051BE50BAF","722");
101  result = PQexec(db_conn, SQL);
102  if (fo_checkPQcommand(db_conn, result, SQL, __FILE__ ,__LINE__))
103  {
104  printf("Perpare pfile information ERROR!\n");
105  PQclear(result);
106  return (-1);
107  }
108  PQclear(result);
109 
110  /* select pfile_pk: fossology-web_1.4.1_all.deb */
111  memset(SQL,'\0',MAXSQL);
112  snprintf(SQL,MAXSQL,"SELECT pfile_pk FROM pfile WHERE pfile_sha1 = '%.40s' AND pfile_md5 = '%.32s' AND pfile_size = '%s';",
113  "AF1DF2C4B32E4115DB5F272D9EFD0E674CF2A0BC","2239AA7DAC291B6F8D0A56396B1B8530","4560");
114  result = PQexec(db_conn, SQL);
115  if (fo_checkPQresult(db_conn, result, SQL, __FILE__, __LINE__))
116  {
117  printf("Get pfile information ERROR!\n");
118  PQclear(result);
119  return (-1);
120  }
121  pi->pFileFk = atoi(PQgetvalue(result, 0, 0));
122  PQclear(result);
123 
124  /* select pfile_pk: control */
125  memset(SQL,'\0',MAXSQL);
126  snprintf(SQL,MAXSQL,"SELECT pfile_pk FROM pfile WHERE pfile_sha1 = '%.40s' AND pfile_md5 = '%.32s' AND pfile_size = '%s';",
127  "F1D2319DF20ABC4CEB02CA5A3C2021BD87B26810","87972FC55E2CDD2609ED85051BE50BAF","722");
128  result = PQexec(db_conn, SQL);
129  if (fo_checkPQresult(db_conn, result, SQL, __FILE__, __LINE__))
130  {
131  printf("Get pfile information ERROR!\n");
132  PQclear(result);
133  return (-1);
134  }
135  control_pfilepk = atoi(PQgetvalue(result, 0, 0));
136  PQclear(result);
137 
138  /* insert upload: fossology-web_1.4.1_all.deb */
139  memset(SQL,'\0',MAXSQL);
140  snprintf(SQL,MAXSQL,"INSERT INTO upload (upload_filename,upload_mode,upload_ts,pfile_fk) VALUES ('%s',40,now(),%ld);",
141  "fossology-web_1.4.1_all.deb", pi->pFileFk);
142  result = PQexec(db_conn, SQL);
143  if (fo_checkPQcommand(db_conn, result, SQL, __FILE__ ,__LINE__))
144  {
145  printf("Perpare pfile information ERROR!\n");
146  PQclear(result);
147  exit(-1);
148  }
149  PQclear(result);
150  memset(SQL,'\0',MAXSQL);
151  snprintf(SQL,MAXSQL,"SELECT upload_pk FROM upload WHERE pfile_fk = '%ld';",
152  pi->pFileFk);
153  result = PQexec(db_conn, SQL);
154  if (fo_checkPQresult(db_conn, result, SQL, __FILE__, __LINE__))
155  {
156  printf("Get pfile information ERROR!\n");
157  PQclear(result);
158  return (-1);
159  }
160  upload_pk = atoi(PQgetvalue(result, 0, 0));
161  PQclear(result);
162 
163  /* insert uploadtree_a: fossology-web_1.4.1_all.deb */
164  memset(SQL,'\0',MAXSQL);
165  snprintf(SQL,MAXSQL,"INSERT INTO uploadtree_a (upload_fk,pfile_fk,lft,rgt,ufile_name) VALUES (%ld,%ld,1,48,'fossology-web_1.4.1_all.deb');",
166  upload_pk, pi->pFileFk);
167  result = PQexec(db_conn, SQL);
168  if (fo_checkPQcommand(db_conn, result, SQL, __FILE__ ,__LINE__))
169  {
170  printf("Perpare pfile information ERROR!\n");
171  PQclear(result);
172  return (-1);
173  }
174  PQclear(result);
175  /* insert uploadtree_a: control */
176  memset(SQL,'\0',MAXSQL);
177  snprintf(SQL,MAXSQL,"INSERT INTO uploadtree_a (upload_fk,pfile_fk,lft,rgt,ufile_name) VALUES (%ld,%ld,9,10,'control');",
178  upload_pk, control_pfilepk);
179  result = PQexec(db_conn, SQL);
180  if (fo_checkPQcommand(db_conn, result, SQL, __FILE__ ,__LINE__))
181  {
182  printf("Perpare pfile information ERROR!\n");
183  PQclear(result);
184  return (-1);
185  }
186  PQclear(result);
187 
188  memset(SQL,'\0',MAXSQL);
189  snprintf(SQL,MAXSQL,"COMMIT;");
190  result = PQexec(db_conn, SQL);
191  if (fo_checkPQcommand(db_conn, result, SQL, __FILE__ ,__LINE__))
192  {
193  printf("Perpare pfile information ERROR!\n");
194  PQclear(result);
195  return (-1);
196  }
197  PQclear(result);
198 
199  return upload_pk;
200 }
201 
207 {
208  char *Source = "../testdata/control";
209  char *Pfile = "F1D2319DF20ABC4CEB02CA5A3C2021BD87B26810.87972FC55E2CDD2609ED85051BE50BAF.722";
210  if (!fo_RepExist("files",Pfile))
211  {
212  if (fo_RepImport(Source, "files", Pfile, 1) != 0)
213  {
214  printf("Failed to import %s\n", Source);
215  return (-1);
216  }
217  }
218  return (0);
219 }
220 
228 int remove_Database(PGconn *db_conn, struct debpkginfo *pi, long upload_pk)
229 {
230  char SQL[MAXSQL];
231  PGresult *result;
232 
233  memset(SQL,'\0',MAXSQL);
234  snprintf(SQL,MAXSQL,"BEGIN;");
235  result = PQexec(db_conn, SQL);
236  if (fo_checkPQcommand(db_conn, result, SQL, __FILE__ ,__LINE__))
237  {
238  printf("Remove pfile database information ERROR!\n");
239  PQclear(result);
240  return (-1);
241  }
242  PQclear(result);
243 
244  memset(SQL,'\0',MAXSQL);
245  snprintf(SQL,MAXSQL,"DELETE FROM mimetype;");
246  result = PQexec(db_conn, SQL);
247  if (fo_checkPQcommand(db_conn, result, SQL, __FILE__ ,__LINE__))
248  {
249  printf("Remove mimetype information ERROR!\n");
250  PQclear(result);
251  return (-1);
252  }
253  PQclear(result);
254 
255  memset(SQL,'\0',MAXSQL);
256  snprintf(SQL,MAXSQL,"DELETE FROM uploadtree_a WHERE upload_fk = %ld;", upload_pk);
257  result = PQexec(db_conn, SQL);
258  if (fo_checkPQcommand(db_conn, result, SQL, __FILE__ ,__LINE__))
259  {
260  printf("Remove pfile database information ERROR!\n");
261  PQclear(result);
262  return (-1);
263  }
264  PQclear(result);
265 
266  memset(SQL,'\0',MAXSQL);
267  snprintf(SQL,MAXSQL,"DELETE FROM upload WHERE upload_pk = %ld;", upload_pk);
268  result = PQexec(db_conn, SQL);
269  if (fo_checkPQcommand(db_conn, result, SQL, __FILE__ ,__LINE__))
270  {
271  printf("Remove pfile database information ERROR!\n");
272  PQclear(result);
273  return (-1);
274  }
275  PQclear(result);
276 
277  memset(SQL,'\0',MAXSQL);
278  snprintf(SQL,MAXSQL,"DELETE FROM pfile WHERE pfile_pk = %ld;", pi->pFileFk);
279  result = PQexec(db_conn, SQL);
280  if (fo_checkPQcommand(db_conn, result, SQL, __FILE__ ,__LINE__))
281  {
282  printf("Remove pfile database information ERROR!\n");
283  PQclear(result);
284  return (-1);
285  }
286  PQclear(result);
287 
288  memset(SQL,'\0',MAXSQL);
289  snprintf(SQL,MAXSQL,"DELETE FROM pfile WHERE pfile_sha1 = '%.40s' AND pfile_md5 = '%.32s' AND pfile_size = '%s';", "F1D2319DF20ABC4CEB02CA5A3C2021BD87B26810","87972FC55E2CDD2609ED85051BE50BAF","722");
290  result = PQexec(db_conn, SQL);
291  if (fo_checkPQcommand(db_conn, result, SQL, __FILE__ ,__LINE__))
292  {
293  printf("Remove pfile database 'control' ERROR!\n");
294  PQclear(result);
295  return (-1);
296  }
297  PQclear(result);
298 
299  memset(SQL,'\0',MAXSQL);
300  snprintf(SQL,MAXSQL,"COMMIT;");
301  result = PQexec(db_conn, SQL);
302  if (fo_checkPQcommand(db_conn, result, SQL, __FILE__ ,__LINE__))
303  {
304  printf("Perpare pfile information ERROR!\n");
305  PQclear(result);
306  return (-1);
307  }
308  PQclear(result);
309 
310  return (0);
311 }
312 
318 {
319  char *Pfile = "F1D2319DF20ABC4CEB02CA5A3C2021BD87B26810.87972FC55E2CDD2609ED85051BE50BAF.722";
320  if (fo_RepExist("files",Pfile))
321  {
322  if (fo_RepRemove("files", Pfile) != 0)
323  {
324  printf("Failed to remove %s\n", Pfile);
325  return (-1);
326  }
327  }
328  return (0);
329 }
330 
340 {
341  struct debpkginfo *pi;
342  long upload_pk;
343  //char *DBConfFile = NULL; /* use default Db.conf */
344  char *ErrorBuf;
345 
346  pi = (struct debpkginfo *)malloc(sizeof(struct debpkginfo));
347  memset(pi, 0, sizeof(struct debpkginfo));
348  int predictValue = 0;
349 
350  /* perpare testing data in database */
351  db_conn = fo_dbconnect(DBConfFile, &ErrorBuf);
352 
353  upload_pk = prepare_Database(db_conn, pi);
354  if (upload_pk == -1)
355  CU_FAIL_FATAL("Prepare database data ERROR!");
356 
357  if (prepare_Repository() == -1)
358  {
359  remove_Database(db_conn, pi, upload_pk);
360  CU_FAIL_FATAL("Prepare repository data ERROR!");
361  }
362 
363  /* Test GetMetadataDebBinary function */
364  int Result = GetMetadataDebBinary(upload_pk, pi);
365  //printf("GetMetadataDebBinary Result is:%d\n", Result);
366 
367  /* Check data correction */
368  CU_ASSERT_STRING_EQUAL(pi->pkgName, "fossology-web");
369  CU_ASSERT_STRING_EQUAL(pi->pkgArch, "all");
370  CU_ASSERT_STRING_EQUAL(pi->version, "1.4.1");
371  CU_ASSERT_STRING_EQUAL(pi->section, "utils");
372  CU_ASSERT_STRING_EQUAL(pi->priority, "extra");
373  CU_ASSERT_STRING_EQUAL(pi->maintainer, "Matt Taggart <taggart@debian.org>");
374  CU_ASSERT_STRING_EQUAL(pi->homepage, "http://fossology.org");
375  CU_ASSERT_EQUAL(pi->dep_size, 3);
376 
377  CU_ASSERT_EQUAL(Result, predictValue);
378 
379  /* Clear testing data in database */
380  if (remove_Database(db_conn, pi, upload_pk) == -1)
381  CU_FAIL_FATAL("Remove database data ERROR!");
382  if (remove_Repository() == -1)
383  CU_FAIL_FATAL("Remove repository data ERROR!");
384 
385  PQfinish(db_conn);
386  int i;
387  for(i=0; i< pi->dep_size;i++)
388  free(pi->depends[i]);
389  free(pi->depends);
390  memset(pi, 0, sizeof(struct debpkginfo));
391  free(pi);
392 }
393 
402 {
403  struct debpkginfo *pi;
404  long upload_pk = 0;
405  char *ErrorBuf;
406 
407  pi = (struct debpkginfo *)malloc(sizeof(struct debpkginfo));
408  memset(pi, 0, sizeof(struct debpkginfo));
409  int predictValue = -1;
410 
411  /* perpare testing data in database */
412  db_conn = fo_dbconnect(DBConfFile, &ErrorBuf);
413 
414  /* Test GetMetadataDebBinary function */
415  int Result = GetMetadataDebBinary(upload_pk, pi);
416  CU_ASSERT_EQUAL(Result, predictValue);
417 
418  PQfinish(db_conn);
419  int i;
420  for(i=0; i< pi->dep_size;i++)
421  free(pi->depends[i]);
422  free(pi->depends);
423  memset(pi, 0, sizeof(struct debpkginfo));
424  free(pi);
425 }
426 
438 {
439  struct debpkginfo *pi;
440  long upload_pk;
441  char *ErrorBuf;
442 
443  int predictValue = 0;
444  pi = (struct debpkginfo *)malloc(sizeof(struct debpkginfo));
445  memset(pi, 0, sizeof(struct debpkginfo));
446 
447  /* perpare testing data in database */
448  db_conn = fo_dbconnect(DBConfFile, &ErrorBuf);
449 
450  upload_pk = prepare_Database(db_conn, pi);
451  if (upload_pk == -1)
452  CU_FAIL_FATAL("Prepare database data ERROR!");
453 
454  if (prepare_Repository() == -1)
455  {
456  remove_Database(db_conn, pi, upload_pk);
457  CU_FAIL_FATAL("Prepare repository data ERROR!");
458  }
459 
460  /* Test ProcessUpload function */
461  int Result = ProcessUpload(upload_pk);
462  printf("ProcessUpload Result is:%d\n", Result);
463 
464  CU_ASSERT_EQUAL(Result, predictValue);
465 
466  /* Clear testing data in database */
467  if (remove_Database(db_conn, pi, upload_pk) == -1)
468  CU_FAIL_FATAL("Remove database data ERROR!");
469  if (remove_Repository() == -1)
470  CU_FAIL_FATAL("Remove repository data ERROR!");
471 
472  PQfinish(db_conn);
473  memset(pi, 0, sizeof(struct debpkginfo));
474  free(pi);
475 }
480  {"Testing the function GetMetadataDebBinary", test_GetMetadataDebBinary},
481  {"Testing the function GetMetadataDebBinary with no uploadpk", test_GetMetadataDebBinary_no_uploadpk},
482  {"Testing the function ProcessUpload for debian binary package", test_ProcessUpload},
483  CU_TEST_INFO_NULL
484 };
485 
char ** depends
Package dependency list.
Definition: pkgagent.h:91
char maintainer[MAXCMD]
Package maintainer.
Definition: pkgagent.h:85
static int Result
Result of calls.
Definition: test_CopyFile.c:28
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
FUNCTION int ProcessUpload(int upload_pk, int agent_fk)
Process a single upload - read the first 32 bytes in each file.
Definition: process.c:73
char version[MAXCMD]
Package version.
Definition: pkgagent.h:80
void test_GetMetadataDebBinary_no_uploadpk()
Test pkgagent.c GetMetadataDebBinary function with no upload_pk in database.
long prepare_Database(PGconn *db_conn, struct debpkginfo *pi)
Prepare database.
char homepage[MAXCMD]
Package link.
Definition: pkgagent.h:86
long pFileFk
Package pfile in FOSSology.
Definition: pkgagent.h:89
char * Pfile
Pfile name (SHA1.MD5.Size)
void test_GetMetadataDebBinary()
Test pkgagent.c GetMetadataDebBinary function get Debian binary package info.
int remove_Repository()
remove repository
int dep_size
Package dependency list size.
Definition: pkgagent.h:92
char pkgArch[MAXCMD]
Package architecture.
Definition: pkgagent.h:83
Holds meta info of Debian packages.
Definition: pkgagent.h:76
char pkgName[MAXCMD]
Package name.
Definition: pkgagent.h:78
pkgagent header
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_GetMetadataDebBinary[]
testcases for function GetMetadataDebBinary and ProcessUpload
PGconn * db_conn
The connection to Database.
Definition: pkgagent.c:34
int prepare_Repository()
Prepare repository.
int GetMetadataDebBinary(long upload_pk, struct debpkginfo *pi)
Get debian binary package info.
Definition: pkgagent.c:682
char priority[MAXCMD]
Package priority.
Definition: pkgagent.h:82
char * DBConfFile
DB conf file location.
Definition: testRun.c:33
int remove_Database(PGconn *db_conn, struct debpkginfo *pi, long upload_pk)
remove database
char section[MAXCMD]
Package section.
Definition: pkgagent.h:81
int fo_RepImport(char *Source, char *Type, char *Filename, int Link)
Import a file into the repository.
Definition: libfossrepo.c:824
void test_ProcessUpload()
Test pkgagent.c ProcessUpload function.
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
int fo_RepRemove(char *Type, char *Filename)
Delete a repository file.
Definition: libfossrepo.c:580
int fo_RepExist(char *Type, char *Filename)
Determine if a file exists.
Definition: libfossrepo.c:498