FOSSology  4.4.0
Open Source License Compliance by Open Source Software
testRecordMetadataDEB.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 #include "pkgagent.h"
7 
8 #include <stdio.h>
9 #include "CUnit/CUnit.h"
10 
11 #define MAXSQL 4096
12 extern char *DBConfFile;
27 {
28  struct debpkginfo *pi;
29  int data_size, i, j;
30  char SQL[MAXSQL];
31  PGresult *result;
32  char Fuid[1024];
33  //char *DBConfFile = NULL; /* use default Db.conf */
34  char *ErrorBuf;
35 
36  for(i=0; i<20; i++) { sprintf(Fuid+0+i*2,"%02X",'s'); }
37  Fuid[40]='.';
38  for(i=0; i<16; i++) { sprintf(Fuid+41+i*2,"%02X",'m'); }
39  Fuid[73]='.';
40  snprintf(Fuid+74,sizeof(Fuid)-74,"%Lu",(long long unsigned int)100);
41 
42  pi = (struct debpkginfo *)malloc(sizeof(struct debpkginfo));
43  memset(pi, 0, sizeof(struct debpkginfo));
44  int predictValue = 0;
45 
46  /* perpare testing data in database */
47  db_conn = fo_dbconnect(DBConfFile, &ErrorBuf);
48  snprintf(SQL,MAXSQL,"INSERT INTO pfile (pfile_sha1,pfile_md5,pfile_size) VALUES ('%.40s','%.32s','%s');",
49  Fuid,Fuid+41,Fuid+74);
50  result = PQexec(db_conn, SQL);
51  if (fo_checkPQcommand(db_conn, result, SQL, __FILE__ ,__LINE__))
52  {
53  printf("Perpare pfile information ERROR!\n");
54  free(pi);
55  exit(-1);
56  }
57  PQclear(result);
58  memset(SQL,'\0',MAXSQL);
59  snprintf(SQL,MAXSQL,"SELECT pfile_pk FROM pfile WHERE pfile_sha1 = '%.40s' AND pfile_md5 = '%.32s' AND pfile_size = '%s';",
60  Fuid,Fuid+41,Fuid+74);
61  result = PQexec(db_conn, SQL);
62  if (fo_checkPQresult(db_conn, result, SQL, __FILE__, __LINE__))
63  {
64  printf("Get pfile information ERROR!\n");
65  exit(-1);
66  }
67  pi->pFileFk = atoi(PQgetvalue(result, 0, 0));
68  PQclear(result);
69  strncpy(pi->pkgName, "Test Pkg", sizeof(pi->pkgName));
70  strncpy(pi->pkgArch, "Test Arch", sizeof(pi->pkgArch));
71  strncpy(pi->version, "Test version", sizeof(pi->version));
72  strncpy(pi->maintainer, "Test maintainer", sizeof(pi->maintainer));
73  strncpy(pi->description, "Test description", sizeof(pi->description));
74  strncpy(pi->section, "Test section", sizeof(pi->section));
75  strncpy(pi->priority, "Test priority", sizeof(pi->priority));
76  strncpy(pi->homepage, "Test homepage", sizeof(pi->homepage));
77  strncpy(pi->source, "Test source", sizeof(pi->source));
78  strncpy(pi->summary, "Test summary", sizeof(pi->summary));
79  strncpy(pi->format, "Test format", sizeof(pi->format));
80  strncpy(pi->uploaders, "Test uploaders", sizeof(pi->uploaders));
81  strncpy(pi->standardsVersion, "Test standard", sizeof(pi->standardsVersion));
82  pi->installedSize = 0;
83 
84  data_size = 2;
85  pi->depends = calloc(data_size, sizeof(char *));
86  for (j=0; j<data_size;j++){
87  pi->depends[j] = malloc(MAXCMD);
88  strcpy(pi->depends[j],"Test depends");
89  }
90  pi->dep_size = data_size;
91 
92  /* Test RecordMetadataRPM function */
93  int Result = RecordMetadataDEB(pi);
94  printf("RecordMetadataDEB Result is:%d\n", Result);
95 
96  /* Check data correction */
97  memset(SQL,'\0',MAXSQL);
98  snprintf(SQL,MAXSQL,"SELECT pkg_pk, pkg_name, pkg_arch, version, maintainer, description FROM pkg_deb INNER JOIN pfile ON pfile_fk = '%ld' AND pfile_fk = pfile_pk;", pi->pFileFk);
99  result = PQexec(db_conn, SQL);
100  if (fo_checkPQresult(db_conn, result, SQL, __FILE__, __LINE__))
101  {
102  printf("Get pkg information ERROR!\n");
103  PQclear(result);
104  free(pi);
105  exit(-1);
106  }
107  CU_ASSERT_STRING_EQUAL(PQgetvalue(result, 0, 1), "Test Pkg");
108  CU_ASSERT_STRING_EQUAL(PQgetvalue(result, 0, 2), "Test Arch");
109  CU_ASSERT_STRING_EQUAL(PQgetvalue(result, 0, 3), "Test version");
110  CU_ASSERT_STRING_EQUAL(PQgetvalue(result, 0, 4), "Test maintainer");
111  CU_ASSERT_STRING_EQUAL(PQgetvalue(result, 0, 5), "Test description");
112  PQclear(result);
113 
114 
115  /* Clear testing data in database */
116  memset(SQL,'\0',MAXSQL);
117  snprintf(SQL,MAXSQL,"DELETE FROM pkg_deb_req WHERE pkg_fk IN (SELECT pkg_pk FROM pkg_deb WHERE pfile_fk = '%ld');", pi->pFileFk);
118  result = PQexec(db_conn, SQL);
119  if (fo_checkPQcommand(db_conn, result, SQL, __FILE__ ,__LINE__))
120  {
121  printf("Clear pkg_deb_req test data ERROR!\n");
122  PQclear(result);
123  free(pi);
124  exit(-1);
125  }
126  PQclear(result);
127  memset(SQL,'\0',MAXSQL);
128  snprintf(SQL,MAXSQL,"DELETE FROM pkg_deb WHERE pfile_fk = '%ld';", pi->pFileFk);
129  result = PQexec(db_conn, SQL);
130  if (fo_checkPQcommand(db_conn, result, SQL, __FILE__ ,__LINE__))
131  {
132  printf("Clear pkg_deb test data ERROR!\n");
133  PQclear(result);
134  free(pi);
135  exit(-1);
136  }
137  PQclear(result);
138  memset(SQL,'\0',MAXSQL);
139  snprintf(SQL,MAXSQL,"DELETE FROM pfile WHERE pfile_pk = '%ld'", pi->pFileFk);
140  result = PQexec(db_conn, SQL);
141  if (fo_checkPQcommand(db_conn, result, SQL, __FILE__ ,__LINE__))
142  {
143  printf("Clear pfile test data ERROR!\n");
144  PQclear(result);
145  free(pi);
146  exit(-1);
147  }
148  PQclear(result);
149 
150  PQfinish(db_conn);
151  int k;
152  for(k=0; k< pi->dep_size;k++)
153  free(pi->depends[k]);
154  free(pi->depends);
155  memset(pi,0,sizeof(struct debpkginfo));
156  free(pi);
157  CU_ASSERT_EQUAL(Result, predictValue);
158 }
159 
164  {"Testing the function RecordMetadataDEB", test_RecordMetadataDEB},
165  CU_TEST_INFO_NULL
166 };
167 
char SQL[256]
SQL query to execute.
Definition: adj2nest.c:78
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
PGconn * db_conn
The connection to Database.
Definition: pkgagent.c:22
int RecordMetadataDEB(struct debpkginfo *pi)
Store debian package info into database.
Definition: pkgagent.c:818
pkgagent header
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
char standardsVersion[MAXCMD]
Package standards version.
Definition: pkgagent.h:83
long pFileFk
Package pfile in FOSSology.
Definition: pkgagent.h:77
char ** depends
Package dependency list.
Definition: pkgagent.h:79
char summary[MAXCMD]
Package summary.
Definition: pkgagent.h:75
char version[MAXCMD]
Package version.
Definition: pkgagent.h:68
char format[MAXCMD]
Package format.
Definition: pkgagent.h:82
char uploaders[MAXCMD]
Package contributors.
Definition: pkgagent.h:81
char maintainer[MAXCMD]
Package maintainer.
Definition: pkgagent.h:73
int installedSize
Size of package after install.
Definition: pkgagent.h:72
char pkgArch[MAXCMD]
Package architecture.
Definition: pkgagent.h:71
char description[MAXCMD]
Package description.
Definition: pkgagent.h:76
int dep_size
Package dependency list size.
Definition: pkgagent.h:80
char source[MAXCMD]
Package source.
Definition: pkgagent.h:67
char pkgName[MAXCMD]
Package name.
Definition: pkgagent.h:66
char homepage[MAXCMD]
Package link.
Definition: pkgagent.h:74
CU_TestInfo testcases_RecordMetadataDEB[]
testcases for function RecordMetadataDEB
char * DBConfFile
DB conf file location.
Definition: testRun.c:21
void test_RecordMetadataDEB()
Test pkgagent.c function RecordMetadataDEB()
static int Result
Result of calls.
Definition: test_CopyFile.c:17