FOSSology  4.4.0
Open Source License Compliance by Open Source Software
test_DBInsert.c
Go to the documentation of this file.
1 /*
2  SPDX-FileCopyrightText: © 2012 Hewlett-Packard Development Company, L.P.
3 
4  SPDX-License-Identifier: GPL-2.0-only
5 */
6 #include "run_tests.h"
7 #include "../agent/externs.h"
13 static PGresult *result = NULL;
14 static long upload_pk = -1;
15 static long pfile_pk = -1;
16 extern char *DBConfFile;
17 
22 {
23  char *ErrorBuf;
24  char *upload_filename = "test_1.orig.tar.gz";
25  int upload_mode = 104;
26  char *upload_origin = "test_1.orig.tar.gz";
27  char *tmp;
28 
29  pgConn = fo_dbconnect(DBConfFile, &ErrorBuf);
30  if (!pgConn)
31  {
32  LOG_FATAL("Unable to connect to database");
33  exit(-1);
34  }
35 
36  /* insert upload info */
37  memset(SQL,'\0',MAXSQL);
38  snprintf(SQL,MAXSQL,"INSERT INTO upload (upload_filename,upload_mode,upload_origin) VALUES ('%s', %d, '%s');",
39  upload_filename, upload_mode, upload_origin);
40  result = PQexec(pgConn, SQL);
41  if (fo_checkPQcommand(pgConn, result, SQL, __FILE__ ,__LINE__))
42  {
43  printf("Insert upload information ERROR!\n");
44  return (-1);
45  }
46  PQclear(result);
47 
48  /* select the upload pk */
49  memset(SQL,'\0',MAXSQL);
50  snprintf(SQL,MAXSQL,"SELECT upload_pk FROM upload WHERE upload_filename = '%s';",
51  upload_filename);
52  result = PQexec(pgConn, SQL); /* SELECT */
53  if (fo_checkPQresult(pgConn, result, SQL, __FILE__, __LINE__)) return(-1);
54 
55  tmp = PQgetvalue(result,0,0);
56  if(tmp)
57  {
58  Upload_Pk = tmp;
59  upload_pk = atol(tmp);
60  }
61  PQclear(result);
62  return 0;
63 }
64 
69 {
70  memset(SQL,'\0',MAXSQL);
71  snprintf(SQL,MAXSQL,"BEGIN;");
72  result = PQexec(pgConn, SQL);
73  if (fo_checkPQcommand(pgConn, result, SQL, __FILE__ ,__LINE__))
74  {
75  printf("Remove pfile database information ERROR!\n");
76  return (-1);
77  }
78  PQclear(result);
79 
80  /* delete uploadtree info */
81  memset(SQL,'\0',MAXSQL);
82  snprintf(SQL,MAXSQL,"DELETE FROM uploadtree WHERE upload_fk = %ld;", upload_pk);
83  result = PQexec(pgConn, SQL);
84  if (fo_checkPQcommand(pgConn, result, SQL, __FILE__ ,__LINE__))
85  {
86  printf("Remove pfile database information ERROR!\n");
87  return (-1);
88  }
89  PQclear(result);
90 
91  /* delete upload info */
92  memset(SQL,'\0',MAXSQL);
93  snprintf(SQL,MAXSQL,"DELETE FROM upload WHERE upload_pk = %ld;", upload_pk);
94  result = PQexec(pgConn, SQL);
95  if (fo_checkPQcommand(pgConn, result, SQL, __FILE__ ,__LINE__))
96  {
97  printf("Remove pfile database information ERROR!\n");
98  return (-1);
99  }
100  PQclear(result);
101 
102  /* delete pfile info */
103  memset(SQL,'\0',MAXSQL);
104  snprintf(SQL,MAXSQL,"DELETE FROM pfile WHERE pfile_pk = %ld;", pfile_pk);
105  result = PQexec(pgConn, SQL);
106  if (fo_checkPQcommand(pgConn, result, SQL, __FILE__ ,__LINE__))
107  {
108  printf("Remove pfile database information ERROR!\n");
109  return (-1);
110  }
111  PQclear(result);
112 
113 
114  memset(SQL,'\0',MAXSQL);
115  snprintf(SQL,MAXSQL,"COMMIT;");
116  result = PQexec(pgConn, SQL);
117  if (fo_checkPQcommand(pgConn, result, SQL, __FILE__ ,__LINE__))
118  {
119  printf("Perpare pfile information ERROR!\n");
120  return (-1);
121  }
122  PQclear(result);
123 
124  if (pgConn) PQfinish(pgConn);
125  return 0;
126 }
127 
135 {
136  ContainerInfo *CI = NULL;
137  struct stat Stat = {0};
138  ParentInfo PI = {0, 1287725739, 1287725739, 0, 0};
139  ContainerInfo CITest = {"../testdata/test_1.orig.tar.gz", "./test-result/",
140  "test_1.orig.tar.gz", "test_1.orig.tar.gz.dir", 1, 1, 0, 0, Stat, PI, 0, 0, 0, 0, 0, 0};
141  CI = &CITest;
142  char *Fuid = "383A1791BA72A77F80698A90F22C1B7B04C59BEF.720B5CECCC4700FC90D628FCB45490E3.1aa248f65785e15aa9da4fa3701741d85653584544ab4003ef45e232a761a2f1.1312";
143  int result = DBInsertPfile(CI, Fuid);
144  CU_ASSERT_EQUAL(result, 1);
145 }
146 
154 {
155  ContainerInfo *CI = NULL;
156  struct stat Stat = {0};
157  ParentInfo PI = {0, 1287725739, 1287725739, 0, 0};
158  ContainerInfo CITest = {"../testdata/test_1.orig.tar.gz", "./test-result/",
159  "test_1.orig.tar.gz", "test_1.orig.tar.gz.dir", 1, 1, 0, 0, Stat, PI, 0, 0, 0, 0, 0, 0};
160  CI = &CITest;
161  int result = DBInsertUploadTree(CI, 1);
162  CU_ASSERT_EQUAL(result, 0);
163 }
164 
165 /* ************************************************************************** */
166 /* **** cunit test cases **************************************************** */
167 /* ************************************************************************** */
168 
169 CU_TestInfo DBInsertPfile_testcases[] =
170 {
171  {"DBInsertPfile:", testDBInsertPfile},
172  CU_TEST_INFO_NULL
173 };
175 {
176  {"DBInsertUploadTree:", testDBInsertUploadTree},
177  CU_TEST_INFO_NULL
178 };
179 
char SQL[256]
SQL query to execute.
Definition: adj2nest.c:78
PGconn * pgConn
Database connection.
Definition: adj2nest.c:86
char * Upload_Pk
Upload pk in DB.
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
const char * upload_pk
Definition: sqlstatements.h:82
Structure for storing information about a particular file.
Definition: ununpack.h:116
int DBInsertClean()
clean the database
Definition: test_DBInsert.c:68
CU_TestInfo DBInsertUploadTree_testcases[]
DB insertion test cases (uploadtree)
char * DBConfFile
DB conf file location.
Definition: testRun.c:21
CU_TestInfo DBInsertPfile_testcases[]
DB insertion test cases (pfile)
int DBInsertInit()
initialize
Definition: test_DBInsert.c:21
void testDBInsertUploadTree()
test DBInsertUploadTree function
void testDBInsertPfile()
test DBInsertPfile function
int DBInsertPfile(ContainerInfo *CI, char *Fuid)
Insert a Pfile record. Sets the pfile_pk in CI.
Definition: utils.c:1114
int DBInsertUploadTree(ContainerInfo *CI, int Mask)
Insert an UploadTree record.
Definition: utils.c:1286