19 #include <CUnit/CUnit.h> 28 static PGresult *result = NULL;
30 static long pfile_pk = -1;
44 LOG_FATAL(
"Unable to connect to database");
47 MagicCookie = magic_open(MAGIC_PRESERVE_ATIME|MAGIC_MIME);
48 memset(
SQL,
'\0',MAXCMD);
49 snprintf(
SQL,MAXCMD,
"BEGIN;");
53 printf(
"Perpare pfile information ERROR!\n");
58 memset(
SQL,
'\0',MAXCMD);
59 snprintf(
SQL,MAXCMD,
"INSERT INTO pfile (pfile_sha1,pfile_md5,pfile_size) VALUES ('%.40s','%.32s','%s');",
60 "F1D2319DF20ABC4CEB02CA5A3C2021BD87B26810",
"87972FC55E2CDD2609ED85051BE50BAF",
"722");
64 printf(
"Perpare pfile information ERROR!\n");
70 memset(
SQL,
'\0',MAXCMD);
71 snprintf(
SQL,MAXCMD,
"SELECT pfile_pk FROM pfile WHERE pfile_sha1 = '%.40s' AND pfile_md5 = '%.32s' AND pfile_size = '%s';",
72 "F1D2319DF20ABC4CEB02CA5A3C2021BD87B26810",
"87972FC55E2CDD2609ED85051BE50BAF",
"722");
76 printf(
"Get pfile information ERROR!\n");
79 pfile_pk = atoi(PQgetvalue(result, 0, 0));
83 memset(
SQL,
'\0',MAXCMD);
84 snprintf(
SQL,MAXCMD,
"INSERT INTO upload (upload_filename,upload_mode,upload_ts,pfile_fk) VALUES ('mimetype',40,now(),'%ld');", pfile_pk);
88 printf(
"Perpare pfile information ERROR!\n");
94 memset(
SQL,
'\0',MAXCMD);
95 snprintf(
SQL,MAXCMD,
"SELECT upload_pk FROM upload WHERE pfile_fk = '%ld';",
100 printf(
"Get pfile information ERROR!\n");
103 upload_pk = atoi(PQgetvalue(result, 0, 0));
107 memset(
SQL,
'\0', MAXCMD);
108 snprintf(
SQL,MAXCMD,
"INSERT INTO uploadtree (upload_fk,pfile_fk,lft,rgt,ufile_name) VALUES (%ld,%ld,1,48,'mimetype');",
upload_pk, pfile_pk);
117 memset(
SQL,
'\0',MAXCMD);
118 snprintf(
SQL,MAXCMD,
"COMMIT;");
122 printf(
"Perpare pfile information ERROR!\n");
126 MagicCookie = magic_open(MAGIC_PRESERVE_ATIME|MAGIC_MIME);
128 memset(
SQL,
'\0', MAXCMD);
129 snprintf(
SQL, MAXCMD,
"DELETE FROM mimetype;");
139 FMimetype = fopen(
"/etc/mime.types",
"rb");
142 LOG_WARNING(
"Unable to open /etc/mime.types\n");
152 memset(
SQL,
'\0',MAXCMD);
153 snprintf(
SQL,MAXCMD,
"BEGIN;");
157 printf(
"Remove pfile database information ERROR!\n");
163 memset(
SQL,
'\0',MAXCMD);
164 snprintf(
SQL,MAXCMD,
"DELETE FROM uploadtree WHERE upload_fk = %ld;",
upload_pk);
168 printf(
"Remove pfile database information ERROR!\n");
174 memset(
SQL,
'\0',MAXCMD);
175 snprintf(
SQL,MAXCMD,
"DELETE FROM upload WHERE upload_pk = %ld;",
upload_pk);
179 printf(
"Remove pfile database information ERROR!\n");
185 memset(
SQL,
'\0',MAXCMD);
186 snprintf(
SQL,MAXCMD,
"DELETE FROM pfile WHERE pfile_pk = %ld;", pfile_pk);
190 printf(
"Remove pfile database information ERROR!\n");
196 memset(
SQL,
'\0',MAXCMD);
197 snprintf(
SQL,MAXCMD,
"COMMIT;");
201 printf(
"Perpare pfile information ERROR!\n");
207 memset(
SQL,
'\0', MAXCMD);
208 snprintf(
SQL, MAXCMD,
"DELETE FROM mimetype where mimetype_name = 'application/octet-stream';");
233 char SQL[MAXCMD] = {0};
234 PGresult *result = NULL;
235 char file_path[MAXCMD] =
"../../agent/mimetype";
236 char mimetype_name[] =
"application/octet-stream";
237 int pfile_mimetypefk = 0;
238 int mimetype_id = -1;
242 memset(SQL,
'\0',
sizeof(SQL));
243 snprintf(SQL,
sizeof(SQL)-1,
"SELECT mimetype_pk FROM mimetype WHERE mimetype_name= '%s';", mimetype_name);
244 result = PQexec(
pgConn, SQL);
250 mimetype_id = atoi(PQgetvalue(result, 0, 0));
254 memset(SQL,
'\0',
sizeof(SQL));
255 snprintf(SQL,
sizeof(SQL)-1,
"SELECT pfile_mimetypefk FROM pfile WHERE pfile_pk= %ld;", pfile_pk);
256 result = PQexec(
pgConn, SQL);
262 pfile_mimetypefk = atoi(PQgetvalue(result, 0, 0));
265 CU_ASSERT_EQUAL(pfile_mimetypefk, mimetype_id);
267 memset(SQL,
'\0', MAXCMD);
268 snprintf(SQL, MAXCMD,
"DELETE FROM mimetype where mimetype_name = 'text/x-csrc';");
269 result = PQexec(
pgConn, SQL);
int fo_checkPQresult(PGconn *pgConn, PGresult *result, char *sql, char *FileID, int LineNumb)
Check the result status of a postgres SELECT.
char * DBConfFile
DB conf file location.
int DBCheckMimeClean()
Clean the env.
PGconn * pgConn
Database connection.
void testDBCheckMime()
For function DBCheckMime()
int DBCheckMimeInit()
Initialize DB.
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...
void DBCheckMime(char *Filename)
Given a file, check if it has a mime type in the DB.
char SQL[256]
SQL query to execute.
CU_TestInfo testcases_DBCheckMime[]
testcases for function DBCheckMime
FILE * FMimetype
for /etc/mime.types
magic_t MagicCookie
for Magic
PGconn * fo_dbconnect(char *DBConfFile, char **ErrorBuf)
Connect to a database. The default is Db.conf.