9 #include <CUnit/CUnit.h>
18 static PGresult *result = NULL;
20 static long pfile_pk = -1;
34 LOG_FATAL(
"Unable to connect to database");
37 MagicCookie = magic_open(MAGIC_PRESERVE_ATIME|MAGIC_MIME);
38 memset(
SQL,
'\0',MAXCMD);
39 snprintf(
SQL,MAXCMD,
"BEGIN;");
43 printf(
"Perpare pfile information ERROR!\n");
48 memset(
SQL,
'\0',MAXCMD);
49 snprintf(
SQL,MAXCMD,
"INSERT INTO pfile (pfile_sha1,pfile_md5,pfile_size) VALUES ('%.40s','%.32s','%s');",
50 "F1D2319DF20ABC4CEB02CA5A3C2021BD87B26810",
"87972FC55E2CDD2609ED85051BE50BAF",
"722");
54 printf(
"Perpare pfile information ERROR!\n");
60 memset(
SQL,
'\0',MAXCMD);
61 snprintf(
SQL,MAXCMD,
"SELECT pfile_pk FROM pfile WHERE pfile_sha1 = '%.40s' AND pfile_md5 = '%.32s' AND pfile_size = '%s';",
62 "F1D2319DF20ABC4CEB02CA5A3C2021BD87B26810",
"87972FC55E2CDD2609ED85051BE50BAF",
"722");
66 printf(
"Get pfile information ERROR!\n");
69 pfile_pk = atoi(PQgetvalue(result, 0, 0));
73 memset(
SQL,
'\0',MAXCMD);
74 snprintf(
SQL,MAXCMD,
"INSERT INTO upload (upload_filename,upload_mode,upload_ts,pfile_fk) VALUES ('mimetype',40,now(),'%ld');", pfile_pk);
78 printf(
"Perpare pfile information ERROR!\n");
84 memset(
SQL,
'\0',MAXCMD);
85 snprintf(
SQL,MAXCMD,
"SELECT upload_pk FROM upload WHERE pfile_fk = '%ld';",
90 printf(
"Get pfile information ERROR!\n");
93 upload_pk = atoi(PQgetvalue(result, 0, 0));
97 memset(
SQL,
'\0', MAXCMD);
98 snprintf(
SQL,MAXCMD,
"INSERT INTO uploadtree (upload_fk,pfile_fk,lft,rgt,ufile_name) VALUES (%ld,%ld,1,48,'mimetype');",
upload_pk, pfile_pk);
107 memset(
SQL,
'\0',MAXCMD);
108 snprintf(
SQL,MAXCMD,
"COMMIT;");
112 printf(
"Perpare pfile information ERROR!\n");
116 MagicCookie = magic_open(MAGIC_PRESERVE_ATIME|MAGIC_MIME);
118 memset(
SQL,
'\0', MAXCMD);
119 snprintf(
SQL, MAXCMD,
"DELETE FROM mimetype;");
129 FMimetype = fopen(
"/etc/mime.types",
"rb");
132 LOG_WARNING(
"Unable to open /etc/mime.types\n");
142 memset(
SQL,
'\0',MAXCMD);
143 snprintf(
SQL,MAXCMD,
"BEGIN;");
147 printf(
"Remove pfile database information ERROR!\n");
153 memset(
SQL,
'\0',MAXCMD);
154 snprintf(
SQL,MAXCMD,
"DELETE FROM uploadtree WHERE upload_fk = %ld;",
upload_pk);
158 printf(
"Remove pfile database information ERROR!\n");
164 memset(
SQL,
'\0',MAXCMD);
165 snprintf(
SQL,MAXCMD,
"DELETE FROM upload WHERE upload_pk = %ld;",
upload_pk);
169 printf(
"Remove pfile database information ERROR!\n");
175 memset(
SQL,
'\0',MAXCMD);
176 snprintf(
SQL,MAXCMD,
"DELETE FROM pfile WHERE pfile_pk = %ld;", pfile_pk);
180 printf(
"Remove pfile database information ERROR!\n");
186 memset(
SQL,
'\0',MAXCMD);
187 snprintf(
SQL,MAXCMD,
"COMMIT;");
191 printf(
"Perpare pfile information ERROR!\n");
197 memset(
SQL,
'\0', MAXCMD);
198 snprintf(
SQL, MAXCMD,
"DELETE FROM mimetype where mimetype_name = 'application/octet-stream';");
223 char SQL[MAXCMD] = {0};
224 PGresult *result = NULL;
225 char file_path[MAXCMD] =
"../../agent/mimetype";
226 char mimetype_name[] =
"application/octet-stream";
227 int pfile_mimetypefk = 0;
228 int mimetype_id = -1;
232 memset(
SQL,
'\0',
sizeof(
SQL));
233 snprintf(
SQL,
sizeof(
SQL)-1,
"SELECT mimetype_pk FROM mimetype WHERE mimetype_name= '%s';", mimetype_name);
240 mimetype_id = atoi(PQgetvalue(result, 0, 0));
244 memset(
SQL,
'\0',
sizeof(
SQL));
245 snprintf(
SQL,
sizeof(
SQL)-1,
"SELECT pfile_mimetypefk FROM pfile WHERE pfile_pk= %ld;", pfile_pk);
252 pfile_mimetypefk = atoi(PQgetvalue(result, 0, 0));
255 CU_ASSERT_EQUAL(pfile_mimetypefk, mimetype_id);
257 memset(
SQL,
'\0', MAXCMD);
258 snprintf(
SQL, MAXCMD,
"DELETE FROM mimetype where mimetype_name = 'text/x-csrc';");
char SQL[256]
SQL query to execute.
PGconn * pgConn
Database connection.
FILE * FMimetype
for /etc/mime.types
magic_t MagicCookie
for Magic
void DBCheckMime(char *Filename)
Given a file, check if it has a mime type in the DB.
PGconn * fo_dbconnect(char *DBConfFile, char **ErrorBuf)
Connect to a database. The default is Db.conf.
int fo_checkPQresult(PGconn *pgConn, PGresult *result, char *sql, char *FileID, int LineNumb)
Check the result status of a postgres SELECT.
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...
int DBCheckMimeClean()
Clean the env.
char * DBConfFile
DB conf file location.
int DBCheckMimeInit()
Initialize DB.
CU_TestInfo testcases_DBCheckMime[]
testcases for function DBCheckMime
void testDBCheckMime()
For function DBCheckMime()