FOSSology  4.4.0
Open Source License Compliance by Open Source Software
testGetMetadataDebBinary.c
Go to the documentation of this file.
1 /*
2  SPDX-FileCopyrightText: © 2011-2014 Hewlett-Packard Development Company, L.P.
3 
4  SPDX-License-Identifier: GPL-2.0-only
5 */
6 #include "pkgagent.h"
7 
8 #include <stdio.h>
9 #include "CUnit/CUnit.h"
10 
11 #define MAXSQL 4096
12 extern char *DBConfFile;
24 long prepare_Database(PGconn *db_conn, struct debpkginfo *pi)
25 {
26  long upload_pk;
27  char SQL[MAXSQL];
28  PGresult *result;
29  long control_pfilepk;
30 
31  memset(SQL,'\0',MAXSQL);
32  snprintf(SQL,MAXSQL,"BEGIN;");
33  result = PQexec(db_conn, SQL);
34  if (fo_checkPQcommand(db_conn, result, SQL, __FILE__ ,__LINE__))
35  {
36  printf("Perpare pfile information ERROR!\n");
37  PQclear(result);
38  return (-1);
39  }
40  PQclear(result);
41 
42  /* insert mimetype */
43  memset(SQL,'\0',MAXSQL);
44  snprintf(SQL,MAXSQL,"INSERT INTO mimetype (mimetype_name) VALUES ('application/x-rpm');");
45  result = PQexec(db_conn, SQL);
46  if (fo_checkPQcommand(db_conn, result, SQL, __FILE__ ,__LINE__))
47  {
48  printf("Perpare mimetype information ERROR!\n");
49  PQclear(result);
50  return (-1);
51  }
52  PQclear(result);
53  memset(SQL,'\0',MAXSQL);
54  snprintf(SQL,MAXSQL,"INSERT INTO mimetype (mimetype_name) VALUES ('application/x-debian-package');");
55  result = PQexec(db_conn, SQL);
56  if (fo_checkPQcommand(db_conn, result, SQL, __FILE__ ,__LINE__))
57  {
58  printf("Perpare mimetype information ERROR!\n");
59  PQclear(result);
60  return (-1);
61  }
62  PQclear(result);
63  memset(SQL,'\0',MAXSQL);
64  snprintf(SQL,MAXSQL,"INSERT INTO mimetype (mimetype_name) VALUES ('application/x-debian-source');");
65  result = PQexec(db_conn, SQL);
66  if (fo_checkPQcommand(db_conn, result, SQL, __FILE__ ,__LINE__))
67  {
68  printf("Perpare mimetype information ERROR!\n");
69  PQclear(result);
70  return (-1);
71  }
72  PQclear(result);
73 
74  /* insert pfile: fossology-web_1.4.1_all.deb */
75  memset(SQL,'\0',MAXSQL);
76  snprintf(SQL,MAXSQL,"INSERT INTO pfile (pfile_sha1,pfile_md5,pfile_size) VALUES ('%.40s','%.32s','%s');",
77  "AF1DF2C4B32E4115DB5F272D9EFD0E674CF2A0BC","2239AA7DAC291B6F8D0A56396B1B8530","4560");
78  result = PQexec(db_conn, SQL);
79  if (fo_checkPQcommand(db_conn, result, SQL, __FILE__ ,__LINE__))
80  {
81  printf("Perpare pfile information ERROR!\n");
82  PQclear(result);
83  return (-1);
84  }
85  PQclear(result);
86  /* insert pfile: control */
87  memset(SQL,'\0',MAXSQL);
88  snprintf(SQL,MAXSQL,"INSERT INTO pfile (pfile_sha1,pfile_md5,pfile_size) VALUES ('%.40s','%.32s','%s');",
89  "F1D2319DF20ABC4CEB02CA5A3C2021BD87B26810","87972FC55E2CDD2609ED85051BE50BAF","722");
90  result = PQexec(db_conn, SQL);
91  if (fo_checkPQcommand(db_conn, result, SQL, __FILE__ ,__LINE__))
92  {
93  printf("Perpare pfile information ERROR!\n");
94  PQclear(result);
95  return (-1);
96  }
97  PQclear(result);
98 
99  /* select pfile_pk: fossology-web_1.4.1_all.deb */
100  memset(SQL,'\0',MAXSQL);
101  snprintf(SQL,MAXSQL,"SELECT pfile_pk FROM pfile WHERE pfile_sha1 = '%.40s' AND pfile_md5 = '%.32s' AND pfile_size = '%s';",
102  "AF1DF2C4B32E4115DB5F272D9EFD0E674CF2A0BC","2239AA7DAC291B6F8D0A56396B1B8530","4560");
103  result = PQexec(db_conn, SQL);
104  if (fo_checkPQresult(db_conn, result, SQL, __FILE__, __LINE__))
105  {
106  printf("Get pfile information ERROR!\n");
107  PQclear(result);
108  return (-1);
109  }
110  pi->pFileFk = atoi(PQgetvalue(result, 0, 0));
111  PQclear(result);
112 
113  /* select pfile_pk: control */
114  memset(SQL,'\0',MAXSQL);
115  snprintf(SQL,MAXSQL,"SELECT pfile_pk FROM pfile WHERE pfile_sha1 = '%.40s' AND pfile_md5 = '%.32s' AND pfile_size = '%s';",
116  "F1D2319DF20ABC4CEB02CA5A3C2021BD87B26810","87972FC55E2CDD2609ED85051BE50BAF","722");
117  result = PQexec(db_conn, SQL);
118  if (fo_checkPQresult(db_conn, result, SQL, __FILE__, __LINE__))
119  {
120  printf("Get pfile information ERROR!\n");
121  PQclear(result);
122  return (-1);
123  }
124  control_pfilepk = atoi(PQgetvalue(result, 0, 0));
125  PQclear(result);
126 
127  /* insert upload: fossology-web_1.4.1_all.deb */
128  memset(SQL,'\0',MAXSQL);
129  snprintf(SQL,MAXSQL,"INSERT INTO upload (upload_filename,upload_mode,upload_ts,pfile_fk) VALUES ('%s',40,now(),%ld);",
130  "fossology-web_1.4.1_all.deb", pi->pFileFk);
131  result = PQexec(db_conn, SQL);
132  if (fo_checkPQcommand(db_conn, result, SQL, __FILE__ ,__LINE__))
133  {
134  printf("Perpare pfile information ERROR!\n");
135  PQclear(result);
136  exit(-1);
137  }
138  PQclear(result);
139  memset(SQL,'\0',MAXSQL);
140  snprintf(SQL,MAXSQL,"SELECT upload_pk FROM upload WHERE pfile_fk = '%ld';",
141  pi->pFileFk);
142  result = PQexec(db_conn, SQL);
143  if (fo_checkPQresult(db_conn, result, SQL, __FILE__, __LINE__))
144  {
145  printf("Get pfile information ERROR!\n");
146  PQclear(result);
147  return (-1);
148  }
149  upload_pk = atoi(PQgetvalue(result, 0, 0));
150  PQclear(result);
151 
152  /* insert uploadtree_a: fossology-web_1.4.1_all.deb */
153  memset(SQL,'\0',MAXSQL);
154  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');",
155  upload_pk, pi->pFileFk);
156  result = PQexec(db_conn, SQL);
157  if (fo_checkPQcommand(db_conn, result, SQL, __FILE__ ,__LINE__))
158  {
159  printf("Perpare pfile information ERROR!\n");
160  PQclear(result);
161  return (-1);
162  }
163  PQclear(result);
164  /* insert uploadtree_a: control */
165  memset(SQL,'\0',MAXSQL);
166  snprintf(SQL,MAXSQL,"INSERT INTO uploadtree_a (upload_fk,pfile_fk,lft,rgt,ufile_name) VALUES (%ld,%ld,9,10,'control');",
167  upload_pk, control_pfilepk);
168  result = PQexec(db_conn, SQL);
169  if (fo_checkPQcommand(db_conn, result, SQL, __FILE__ ,__LINE__))
170  {
171  printf("Perpare pfile information ERROR!\n");
172  PQclear(result);
173  return (-1);
174  }
175  PQclear(result);
176 
177  memset(SQL,'\0',MAXSQL);
178  snprintf(SQL,MAXSQL,"COMMIT;");
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  return upload_pk;
189 }
190 
196 {
197  char *Source = "./testdata/control";
198  char *Pfile = "F1D2319DF20ABC4CEB02CA5A3C2021BD87B26810.87972FC55E2CDD2609ED85051BE50BAF.722";
199  if (!fo_RepExist("files",Pfile))
200  {
201  if (fo_RepImport(Source, "files", Pfile, 1) != 0)
202  {
203  printf("Failed to import %s\n", Source);
204  return (-1);
205  }
206  }
207  return (0);
208 }
209 
217 int remove_Database(PGconn *db_conn, struct debpkginfo *pi, long upload_pk)
218 {
219  char SQL[MAXSQL];
220  PGresult *result;
221 
222  memset(SQL,'\0',MAXSQL);
223  snprintf(SQL,MAXSQL,"BEGIN;");
224  result = PQexec(db_conn, SQL);
225  if (fo_checkPQcommand(db_conn, result, SQL, __FILE__ ,__LINE__))
226  {
227  printf("Remove pfile database information ERROR!\n");
228  PQclear(result);
229  return (-1);
230  }
231  PQclear(result);
232 
233  memset(SQL,'\0',MAXSQL);
234  snprintf(SQL,MAXSQL,"DELETE FROM mimetype;");
235  result = PQexec(db_conn, SQL);
236  if (fo_checkPQcommand(db_conn, result, SQL, __FILE__ ,__LINE__))
237  {
238  printf("Remove mimetype 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 uploadtree_a WHERE upload_fk = %ld;", upload_pk);
246  result = PQexec(db_conn, SQL);
247  if (fo_checkPQcommand(db_conn, result, SQL, __FILE__ ,__LINE__))
248  {
249  printf("Remove pfile database 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 upload WHERE upload_pk = %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 pfile WHERE pfile_pk = %ld;", pi->pFileFk);
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_sha1 = '%.40s' AND pfile_md5 = '%.32s' AND pfile_size = '%s';", "F1D2319DF20ABC4CEB02CA5A3C2021BD87B26810","87972FC55E2CDD2609ED85051BE50BAF","722");
279  result = PQexec(db_conn, SQL);
280  if (fo_checkPQcommand(db_conn, result, SQL, __FILE__ ,__LINE__))
281  {
282  printf("Remove pfile database 'control' ERROR!\n");
283  PQclear(result);
284  return (-1);
285  }
286  PQclear(result);
287 
288  memset(SQL,'\0',MAXSQL);
289  snprintf(SQL,MAXSQL,"COMMIT;");
290  result = PQexec(db_conn, SQL);
291  if (fo_checkPQcommand(db_conn, result, SQL, __FILE__ ,__LINE__))
292  {
293  printf("Perpare pfile information ERROR!\n");
294  PQclear(result);
295  return (-1);
296  }
297  PQclear(result);
298 
299  return (0);
300 }
301 
307 {
308  char *Pfile = "F1D2319DF20ABC4CEB02CA5A3C2021BD87B26810.87972FC55E2CDD2609ED85051BE50BAF.722";
309  if (fo_RepExist("files",Pfile))
310  {
311  if (fo_RepRemove("files", Pfile) != 0)
312  {
313  printf("Failed to remove %s\n", Pfile);
314  return (-1);
315  }
316  }
317  return (0);
318 }
319 
329 {
330  struct debpkginfo *pi;
331  long upload_pk;
332  //char *DBConfFile = NULL; /* use default Db.conf */
333  char *ErrorBuf;
334 
335  pi = (struct debpkginfo *)malloc(sizeof(struct debpkginfo));
336  memset(pi, 0, sizeof(struct debpkginfo));
337  int predictValue = 0;
338 
339  /* perpare testing data in database */
340  db_conn = fo_dbconnect(DBConfFile, &ErrorBuf);
341 
343  if (upload_pk == -1)
344  CU_FAIL_FATAL("Prepare database data ERROR!");
345 
346  if (prepare_Repository() == -1)
347  {
349  CU_FAIL_FATAL("Prepare repository data ERROR!");
350  }
351 
352  /* Test GetMetadataDebBinary function */
354  //printf("GetMetadataDebBinary Result is:%d\n", Result);
355 
356  /* Check data correction */
357  CU_ASSERT_STRING_EQUAL(pi->pkgName, "fossology-web");
358  CU_ASSERT_STRING_EQUAL(pi->pkgArch, "all");
359  CU_ASSERT_STRING_EQUAL(pi->version, "1.4.1");
360  CU_ASSERT_STRING_EQUAL(pi->section, "utils");
361  CU_ASSERT_STRING_EQUAL(pi->priority, "extra");
362  CU_ASSERT_STRING_EQUAL(pi->maintainer, "Matt Taggart <taggart@debian.org>");
363  CU_ASSERT_STRING_EQUAL(pi->homepage, "http://fossology.org");
364  CU_ASSERT_EQUAL(pi->dep_size, 3);
365 
366  CU_ASSERT_EQUAL(Result, predictValue);
367 
368  /* Clear testing data in database */
369  if (remove_Database(db_conn, pi, upload_pk) == -1)
370  CU_FAIL_FATAL("Remove database data ERROR!");
371  if (remove_Repository() == -1)
372  CU_FAIL_FATAL("Remove repository data ERROR!");
373 
374  PQfinish(db_conn);
375  int i;
376  for(i=0; i< pi->dep_size;i++)
377  free(pi->depends[i]);
378  free(pi->depends);
379  memset(pi, 0, sizeof(struct debpkginfo));
380  free(pi);
381 }
382 
391 {
392  struct debpkginfo *pi;
393  long upload_pk = 0;
394  char *ErrorBuf;
395 
396  pi = (struct debpkginfo *)malloc(sizeof(struct debpkginfo));
397  memset(pi, 0, sizeof(struct debpkginfo));
398  int predictValue = -1;
399 
400  /* perpare testing data in database */
401  db_conn = fo_dbconnect(DBConfFile, &ErrorBuf);
402 
403  /* Test GetMetadataDebBinary function */
405  CU_ASSERT_EQUAL(Result, predictValue);
406 
407  PQfinish(db_conn);
408  int i;
409  for(i=0; i< pi->dep_size;i++)
410  free(pi->depends[i]);
411  free(pi->depends);
412  memset(pi, 0, sizeof(struct debpkginfo));
413  free(pi);
414 }
415 
427 {
428  struct debpkginfo *pi;
429  long upload_pk;
430  char *ErrorBuf;
431 
432  int predictValue = 0;
433  pi = (struct debpkginfo *)malloc(sizeof(struct debpkginfo));
434  memset(pi, 0, sizeof(struct debpkginfo));
435 
436  /* perpare testing data in database */
437  db_conn = fo_dbconnect(DBConfFile, &ErrorBuf);
438 
440  if (upload_pk == -1)
441  CU_FAIL_FATAL("Prepare database data ERROR!");
442 
443  if (prepare_Repository() == -1)
444  {
446  CU_FAIL_FATAL("Prepare repository data ERROR!");
447  }
448 
449  /* Test ProcessUpload function */
451  printf("ProcessUpload Result is:%d\n", Result);
452 
453  CU_ASSERT_EQUAL(Result, predictValue);
454 
455  /* Clear testing data in database */
456  if (remove_Database(db_conn, pi, upload_pk) == -1)
457  CU_FAIL_FATAL("Remove database data ERROR!");
458  if (remove_Repository() == -1)
459  CU_FAIL_FATAL("Remove repository data ERROR!");
460 
461  PQfinish(db_conn);
462  memset(pi, 0, sizeof(struct debpkginfo));
463  free(pi);
464 }
469  {"Testing the function GetMetadataDebBinary", test_GetMetadataDebBinary},
470  {"Testing the function GetMetadataDebBinary with no uploadpk", test_GetMetadataDebBinary_no_uploadpk},
471  {"Testing the function ProcessUpload for debian binary package", test_ProcessUpload},
472  CU_TEST_INFO_NULL
473 };
474 
char SQL[256]
SQL query to execute.
Definition: adj2nest.c:78
FUNCTION int ProcessUpload(int upload_pk, int agent_fk)
Process a single upload - read the first 32 bytes in each file.
Definition: process.c:61
char * Pfile
Pfile name (SHA1.MD5.Size)
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
int fo_RepImport(char *Source, char *Type, char *Filename, int Link)
Import a file into the repository.
Definition: libfossrepo.c:812
int fo_RepExist(char *Type, char *Filename)
Determine if a file exists.
Definition: libfossrepo.c:486
int fo_RepRemove(char *Type, char *Filename)
Delete a repository file.
Definition: libfossrepo.c:568
PGconn * db_conn
The connection to Database.
Definition: pkgagent.c:22
int GetMetadataDebBinary(long upload_pk, struct debpkginfo *pi)
Get debian binary package info.
Definition: pkgagent.c:670
pkgagent header
const char * upload_pk
Definition: sqlstatements.h:82
Holds meta info of Debian packages.
Definition: pkgagent.h:65
char section[MAXCMD]
Package section.
Definition: pkgagent.h:69
char priority[MAXCMD]
Package priority.
Definition: pkgagent.h:70
long pFileFk
Package pfile in FOSSology.
Definition: pkgagent.h:77
char ** depends
Package dependency list.
Definition: pkgagent.h:79
char version[MAXCMD]
Package version.
Definition: pkgagent.h:68
char maintainer[MAXCMD]
Package maintainer.
Definition: pkgagent.h:73
char pkgArch[MAXCMD]
Package architecture.
Definition: pkgagent.h:71
int dep_size
Package dependency list size.
Definition: pkgagent.h:80
char pkgName[MAXCMD]
Package name.
Definition: pkgagent.h:66
char homepage[MAXCMD]
Package link.
Definition: pkgagent.h:74
long prepare_Database(PGconn *db_conn, struct debpkginfo *pi)
Prepare database.
char * DBConfFile
DB conf file location.
Definition: testRun.c:21
int prepare_Repository()
Prepare repository.
int remove_Repository()
remove repository
void test_ProcessUpload()
Test pkgagent.c ProcessUpload function.
CU_TestInfo testcases_GetMetadataDebBinary[]
testcases for function GetMetadataDebBinary and ProcessUpload
void test_GetMetadataDebBinary()
Test pkgagent.c GetMetadataDebBinary function get Debian binary package info.
int remove_Database(PGconn *db_conn, struct debpkginfo *pi, long upload_pk)
remove database
void test_GetMetadataDebBinary_no_uploadpk()
Test pkgagent.c GetMetadataDebBinary function with no upload_pk in database.
static int Result
Result of calls.
Definition: test_CopyFile.c:17