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