FOSSology  4.4.0
Open Source License Compliance by Open Source Software
testDBLoadMime.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 
7 /* cunit includes */
8 #include <CUnit/CUnit.h>
9 #include "finder.h"
10 #include <string.h>
11 
17 extern void DBLoadMime();
18 extern char *DBConfFile;
19 
24 {
25  char *ErrorBuf;
26 
27  pgConn = fo_dbconnect(DBConfFile, &ErrorBuf);
28  if (!pgConn)
29  {
30  LOG_FATAL("Unable to connect to database");
31  exit(-1);
32  }
33  MagicCookie = magic_open(MAGIC_PRESERVE_ATIME|MAGIC_MIME);
34  DBMime = NULL;
35 
36  return 0;
37 }
42 {
43  if (pgConn) PQfinish(pgConn);
44  DBMime = NULL;
45  return 0;
46 }
47 
48 /* test functions */
49 
57 {
58  char SQL[MAXCMD] = {0};
59  PGresult *result = NULL;
60  char mimetype_name[] = "application/octet-stream";
61  /* delete the record mimetype_name is application/octet-stream in mimetype */
62  memset(SQL, '\0', MAXCMD);
63  snprintf(SQL, MAXCMD, "DELETE FROM mimetype where mimetype_name = '%s';", mimetype_name);
64  result = PQexec(pgConn, SQL);
65  if (fo_checkPQcommand(pgConn, result, SQL, __FILE__, __LINE__))
66  {
67  PQfinish(pgConn);
68  exit(-1);
69  }
70  PQclear(result);
71  memset(SQL, '\0', MAXCMD);
72  snprintf(SQL, MAXCMD, "INSERT INTO mimetype (mimetype_name) VALUES ('%s');", mimetype_name);
73  result = PQexec(pgConn, SQL);
74  if (fo_checkPQcommand(pgConn, result, SQL, __FILE__, __LINE__))
75  {
76  PQfinish(pgConn);
77  exit(-1);
78  }
79  PQclear(result);
80  MaxDBMime = 0;
81  /* exectue the tested function */
82  DBLoadMime();
83  /* select the record mimetype_name is application/octet-stream */
84  memset(SQL, '\0', MAXCMD);
85  snprintf(SQL, MAXCMD, "SELECT mimetype_name from mimetype where mimetype_name = ('%s');", mimetype_name);
86  result = PQexec(pgConn, SQL);
87  if (fo_checkPQresult(pgConn, result, SQL, __FILE__, __LINE__))
88  {
89  PQfinish(pgConn);
90  exit(-1);
91  }
92  int count = PQntuples(result);
93  PQclear(result);
94 
95  CU_ASSERT_EQUAL(MaxDBMime, count);
96  /* delete the record mimetype_name is application/octet-stream in mimetype */
97  memset(SQL, '\0', MAXCMD);
98  snprintf(SQL, MAXCMD, "DELETE FROM mimetype where mimetype_name = '%s';", mimetype_name);
99  result = PQexec(pgConn, SQL);
100  /* reset the evn, that is clear all data in mimetype */
101  if (fo_checkPQcommand(pgConn, result, SQL, __FILE__, __LINE__))
102  {
103  PQfinish(pgConn);
104  exit(-1);
105  }
106  MaxDBMime = 0;
107  PQclear(result);
108 }
109 
113 CU_TestInfo testcases_DBLoadMime[] =
114 {
115 #if 0
116 #endif
117 {"DBLoadMime:InsertOctet", testDBLoadMime},
118  CU_TEST_INFO_NULL
119 };
120 
char SQL[256]
SQL query to execute.
Definition: adj2nest.c:78
PGconn * pgConn
Database connection.
Definition: adj2nest.c:86
magic_t MagicCookie
for Magic
Definition: finder.c:23
int MaxDBMime
how many rows in DBMime
Definition: finder.c:17
PGresult * DBMime
contents of mimetype table
Definition: finder.c:16
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
void DBLoadMime()
Populate the DBMime table.
Definition: finder.c:58
int DBLoadMimeClean()
clean the env
int DBLoadMimeInit()
initialize DB
char * DBConfFile
DB conf file location.
Definition: testRun.c:21
CU_TestInfo testcases_DBLoadMime[]
testcases for function DBLoadGold
void testDBLoadMime()
for function DBLoadMime()