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