FOSSology  4.4.0
Open Source License Compliance by Open Source Software
testDBFindMime.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 char *DBConfFile;
18 
23 {
24  char *ErrorBuf;
25 
26  pgConn = fo_dbconnect(DBConfFile, &ErrorBuf);
27  if (!pgConn)
28  {
29  LOG_FATAL("Unable to connect to database");
30  exit(-1);
31  }
32  MagicCookie = magic_open(MAGIC_PRESERVE_ATIME|MAGIC_MIME);
33  DBMime = NULL;
34 
35  return 0;
36 }
41 {
42  if (pgConn) PQfinish(pgConn);
43  DBMime = NULL;
44  return 0;
45 }
46 
47 /* test functions */
48 
59 {
60  char SQL[MAXCMD] = {0};
61  PGresult *result = NULL;
62  char mimetype_name[] = "application/octet-stream";
63  /* delete the record mimetype_name is application/octet-stream in mimetype */
64  memset(SQL, '\0', MAXCMD);
65  snprintf(SQL, MAXCMD, "DELETE FROM mimetype where mimetype_name = '%s';", mimetype_name);
66  result = PQexec(pgConn, SQL);
67  if (fo_checkPQcommand(pgConn, result, SQL, __FILE__, __LINE__))
68  {
69  PQfinish(pgConn);
70  exit(-1);
71  }
72  PQclear(result);
73  /* insert one record */
74  memset(SQL, '\0', MAXCMD);
75  snprintf(SQL, MAXCMD, "INSERT INTO mimetype (mimetype_name) VALUES ('%s');", mimetype_name);
76  result = PQexec(pgConn, SQL);
77  if (fo_checkPQcommand(pgConn, result, SQL, __FILE__, __LINE__))
78  {
79  PQfinish(pgConn);
80  exit(-1);
81  }
82  PQclear(result);
83  /* exectue the tested function */
84  /* 1. the Mimetype is already in table mimetype */
85  int ret = DBFindMime(mimetype_name);
86  /* select the record mimetype_name is application/octet-stream */
87  memset(SQL, '\0', MAXCMD);
88  snprintf(SQL, MAXCMD, "SELECT mimetype_name from mimetype where mimetype_name = ('%s');", mimetype_name);
89  result = PQexec(pgConn, SQL);
90  if (fo_checkPQresult(pgConn, result, SQL, __FILE__, __LINE__))
91  {
92  PQfinish(pgConn);
93  exit(-1);
94  }
95  int mimetype_id = atoi(PQgetvalue(result, 0, 0));
96  PQclear(result);
97 
98  CU_ASSERT_NOT_EQUAL(ret, mimetype_id);
99 
100  /* delete the record mimetype_name is application/octet-stream in mimetype */
101  memset(SQL, '\0', MAXCMD);
102  snprintf(SQL, MAXCMD, "DELETE FROM mimetype where mimetype_name = '%s';", mimetype_name);
103  result = PQexec(pgConn, SQL);
104  if (fo_checkPQcommand(pgConn, result, SQL, __FILE__, __LINE__))
105  {
106  PQfinish(pgConn);
107  exit(-1);
108  }
109  PQclear(result);
110 
111  DBMime = NULL;
112  /* 2. the Mimetype is not in table mimetype */
113  /* select the record mimetype_name is application/octet-stream */
114  ret = DBFindMime(mimetype_name);
115  memset(SQL, '\0', MAXCMD);
116  snprintf(SQL, MAXCMD, "SELECT mimetype_name from mimetype where mimetype_name = ('%s');", mimetype_name);
117  result = PQexec(pgConn, SQL);
118  if (fo_checkPQresult(pgConn, result, SQL, __FILE__, __LINE__))
119  {
120  PQfinish(pgConn);
121  exit(-1);
122  }
123  mimetype_id = 0;
124  mimetype_id = atoi(PQgetvalue(result, 0, 0));
125  PQclear(result);
126 
127  CU_ASSERT_NOT_EQUAL(ret, mimetype_id);
128  /* delete the record mimetype_name is application/octet-stream in mimetype */
129  memset(SQL, '\0', MAXCMD);
130  snprintf(SQL, MAXCMD, "DELETE FROM mimetype where mimetype_name = '%s';", mimetype_name);
131  result = PQexec(pgConn, SQL);
132  if (fo_checkPQcommand(pgConn, result, SQL, __FILE__, __LINE__))
133  {
134  PQfinish(pgConn);
135  exit(-1);
136  }
137  PQclear(result);
138 }
139 
143 CU_TestInfo testcases_DBFindMime[] =
144 {
145 #if 0
146 #endif
147 {"DBFindMime:ExistAndNot", testDBFindMime},
148  CU_TEST_INFO_NULL
149 };
150 
char SQL[256]
SQL query to execute.
Definition: adj2nest.c:78
PGconn * pgConn
Database connection.
Definition: adj2nest.c:86
int DBFindMime(char *Mimetype)
Find a mime type in the DBMime table.
Definition: finder.c:81
magic_t MagicCookie
for Magic
Definition: finder.c:23
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
CU_TestInfo testcases_DBFindMime[]
testcases for function DBFindMime
int DBFindMimeInit()
initialize
int DBFindMimeClean()
clean the env
void testDBFindMime()
for function DBFindMime()
char * DBConfFile
DB conf file location.
Definition: testRun.c:21