FOSSology  3.2.0rc1
Open Source License Compliance by Open Source Software
testRecordMetadataRPM.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;
39 {
40  struct rpmpkginfo *pi;
41  int data_size, i, j;
42  char SQL[MAXSQL];
43  PGresult *result;
44  char Fuid[1024];
45  //char *DBConfFile = NULL; /* use default Db.conf */
46  char *ErrorBuf;
47 
48  for(i=0; i<20; i++) { sprintf(Fuid+0+i*2,"%02X",'s'); }
49  Fuid[40]='.';
50  for(i=0; i<16; i++) { sprintf(Fuid+41+i*2,"%02X",'m'); }
51  Fuid[73]='.';
52  snprintf(Fuid+74,sizeof(Fuid)-74,"%Lu",(long long unsigned int)100);
53 
54  pi = (struct rpmpkginfo *)malloc(sizeof(struct rpmpkginfo));
55  memset(pi, 0, sizeof(struct rpmpkginfo));
56  int predictValue = 0;
57 
58  /* perpare testing data in database */
59  db_conn = fo_dbconnect(DBConfFile, &ErrorBuf);
60  snprintf(SQL,MAXSQL,"INSERT INTO pfile (pfile_sha1,pfile_md5,pfile_size) VALUES ('%.40s','%.32s','%s');",
61  Fuid,Fuid+41,Fuid+74);
62  result = PQexec(db_conn, SQL);
63  if (fo_checkPQcommand(db_conn, result, SQL, __FILE__ ,__LINE__))
64  {
65  printf("Perpare pfile information ERROR!\n");
66  PQclear(result);
67  free(pi);
68  exit(-1);
69  }
70  PQclear(result);
71  memset(SQL,'\0',MAXSQL);
72  snprintf(SQL,MAXSQL,"SELECT pfile_pk FROM pfile WHERE pfile_sha1 = '%.40s' AND pfile_md5 = '%.32s' AND pfile_size = '%s';",
73  Fuid,Fuid+41,Fuid+74);
74  result = PQexec(db_conn, SQL);
75  if (fo_checkPQresult(db_conn, result, SQL, __FILE__, __LINE__))
76  {
77  printf("Get pfile information ERROR!\n");
78  PQclear(result);
79  free(pi);
80  exit(-1);
81  }
82  pi->pFileFk = atoi(PQgetvalue(result, 0, 0));
83  PQclear(result);
84  strncpy(pi->pkgName, "Test Pkg", sizeof(pi->pkgName));
85  strncpy(pi->pkgArch, "Test Arch", sizeof(pi->pkgArch));
86  strncpy(pi->version, "Test version", sizeof(pi->version));
87  strncpy(pi->license, "Test license", sizeof(pi->license));
88  strncpy(pi->packager, "Test packager", sizeof(pi->packager));
89  strncpy(pi->release, "Test release", sizeof(pi->release));
90  strncpy(pi->buildDate, "Test buildDate", sizeof(pi->buildDate));
91  strncpy(pi->vendor, "Test vendor", sizeof(pi->vendor));
92  strncpy(pi->pkgAlias, "Test Alias", sizeof(pi->pkgAlias));
93  strncpy(pi->rpmFilename, "Test rpmfile", sizeof(pi->rpmFilename));
94  strncpy(pi->group, "Test group", sizeof(pi->group));
95  strncpy(pi->url, "Test url", sizeof(pi->url));
96  strncpy(pi->sourceRPM, "Test sourceRPM", sizeof(pi->sourceRPM));
97  strncpy(pi->summary, "Test summary", sizeof(pi->summary));
98  strncpy(pi->description, "Test description", sizeof(pi->description));
99 
100  data_size = 2;
101  pi->requires = calloc(data_size, sizeof(char *));
102  for (j=0; j<data_size;j++){
103  pi->requires[j] = malloc(MAXCMD);
104  strcpy(pi->requires[j],"Test requires");
105  }
106  pi->req_size = data_size;
107 
108  /* Test RecordMetadataRPM function */
109  int Result = RecordMetadataRPM(pi);
110  printf("RecordMetadataRPM Result is:%d\n", Result);
111 
112  /* Check data correction */
113  memset(SQL,'\0',MAXSQL);
114  snprintf(SQL,MAXSQL,"SELECT pkg_pk, pkg_name, pkg_arch, version, license, packager, release, vendor FROM pkg_rpm INNER JOIN pfile ON pfile_fk = '%ld' AND pfile_fk = pfile_pk;", pi->pFileFk);
115  result = PQexec(db_conn, SQL);
116  if (fo_checkPQresult(db_conn, result, SQL, __FILE__, __LINE__))
117  {
118  printf("Get pkg information ERROR!\n");
119  PQclear(result);
120  free(pi);
121  exit(-1);
122  }
123  CU_ASSERT_STRING_EQUAL(PQgetvalue(result, 0, 1), "Test Pkg");
124  CU_ASSERT_STRING_EQUAL(PQgetvalue(result, 0, 2), "Test Arch");
125  CU_ASSERT_STRING_EQUAL(PQgetvalue(result, 0, 3), "Test version");
126  CU_ASSERT_STRING_EQUAL(PQgetvalue(result, 0, 4), "Test license");
127  CU_ASSERT_STRING_EQUAL(PQgetvalue(result, 0, 5), "Test packager");
128  CU_ASSERT_STRING_EQUAL(PQgetvalue(result, 0, 6), "Test release");
129  CU_ASSERT_STRING_EQUAL(PQgetvalue(result, 0, 7), "Test vendor");
130  PQclear(result);
131 
132  /* Clear testing data in database */
133  memset(SQL,'\0',MAXSQL);
134  snprintf(SQL,MAXSQL,"DELETE FROM pkg_rpm_req WHERE pkg_fk IN (SELECT pkg_pk FROM pkg_rpm WHERE pfile_fk = '%ld');", pi->pFileFk);
135  result = PQexec(db_conn, SQL);
136  if (fo_checkPQcommand(db_conn, result, SQL, __FILE__ ,__LINE__))
137  {
138  printf("Clear pkg_rpm_req test data ERROR!\n");
139  PQclear(result);
140  free(pi);
141  exit(-1);
142  }
143  PQclear(result);
144  memset(SQL,'\0',MAXSQL);
145  snprintf(SQL,MAXSQL,"DELETE FROM pkg_rpm WHERE pfile_fk = '%ld';", pi->pFileFk);
146  result = PQexec(db_conn, SQL);
147  if (fo_checkPQcommand(db_conn, result, SQL, __FILE__ ,__LINE__))
148  {
149  printf("Clear pkg_rpm test data ERROR!\n");
150  exit(-1);
151  }
152  PQclear(result);
153  memset(SQL,'\0',MAXSQL);
154  snprintf(SQL,MAXSQL,"DELETE FROM pfile WHERE pfile_pk = '%ld'", pi->pFileFk);
155  result = PQexec(db_conn, SQL);
156  if (fo_checkPQcommand(db_conn, result, SQL, __FILE__ ,__LINE__))
157  {
158  printf("Clear pfile test data ERROR!\n");
159  PQclear(result);
160  free(pi);
161  exit(-1);
162  }
163  PQclear(result);
164 
165  PQfinish(db_conn);
166  int k;
167  for(k=0; k< pi->req_size;k++)
168  free(pi->requires[k]);
169  free(pi->requires);
170  memset(pi, 0, sizeof(struct rpmpkginfo));
171  free(pi);
172  CU_ASSERT_EQUAL(Result, predictValue);
173 }
174 
179  {"Testing the function RecordMetadataRPM", test_RecordMetadataRPM},
180  CU_TEST_INFO_NULL
181 };
182 
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
void test_RecordMetadataRPM()
Test pkgagent.c function RecordMetadataRPM()
char license[512]
RPM licenses.
Definition: pkgagent.h:56
char description[MAXCMD]
Package description.
Definition: pkgagent.h:65
int RecordMetadataRPM(struct rpmpkginfo *pi)
Store rpm package info into database.
Definition: pkgagent.c:575
char packager[1024]
Packager.
Definition: pkgagent.h:58
char buildDate[128]
Package build date.
Definition: pkgagent.h:60
char sourceRPM[256]
Package source.
Definition: pkgagent.h:63
char pkgName[256]
RPM package name.
Definition: pkgagent.h:51
Holds meta info of rpm packages.
Definition: pkgagent.h:49
char version[64]
Package version.
Definition: pkgagent.h:54
CU_TestInfo testcases_RecordMetadataRPM[]
testcases for function RecordMetadataRPM
char ** requires
Package dependency list.
Definition: pkgagent.h:68
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
char summary[MAXCMD]
Package summary.
Definition: pkgagent.h:64
char * DBConfFile
DB conf file location.
Definition: testRun.c:33
char pkgAlias[256]
Package alias.
Definition: pkgagent.h:52
char pkgArch[64]
Package architecture.
Definition: pkgagent.h:53
PGconn * db_conn
The connection to Database.
Definition: pkgagent.c:34
char rpmFilename[256]
RPM file name.
Definition: pkgagent.h:55
char vendor[128]
Package vendor.
Definition: pkgagent.h:61
char group[128]
Package group.
Definition: pkgagent.h:57
char release[64]
Package release.
Definition: pkgagent.h:59
char url[256]
Package link.
Definition: pkgagent.h:62
int req_size
Package dependency list size.
Definition: pkgagent.h:69
long pFileFk
Package pfile in FOSSology.
Definition: pkgagent.h:66
PGconn * fo_dbconnect(char *DBConfFile, char **ErrorBuf)
Connect to a database. The default is Db.conf.
Definition: libfossdb.c:40