FOSSology  4.4.0
Open Source License Compliance by Open Source Software
testDBLoadGold.c
Go to the documentation of this file.
1 /*
2  SPDX-FileCopyrightText: © 2011-2012 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 "wget_agent.h"
10 #include "../utility.h"
11 #include <string.h>
12 #include <ctype.h>
13 #include "libfodbreposysconf.h"
14 
15 // #define AGENT_DIR "../"
23 static PGresult *result = NULL;
24 extern fo_conf* sysconfig;
25 
26 static fo_dbManager* dbManager;
33 {
34  char URL[URLMAX];
35  char TempFileDir[STRMAX];
36  char TempFile[STRMAX];
37 
39  dbManager = createTestEnvironment(AGENT_DIR, "wget_agent", 1);
40  if (!dbManager) {
41  LOG_FATAL("Unable to connect to database");
42  return 1;
43  }
44 
45  pgConn = fo_dbManager_getWrappedConnection(dbManager);
46 
48  strcpy(GlobalParam, "-l 1 -A *.list -R *.deb");
49  strcpy(URL, "https://mirrors.kernel.org/fossology/releases/3.0.0/ubuntu/14.04/");
50  strcpy(TempFileDir, "./test_result/");
51  strcpy(TempFile, "./test_result/fossology.sources.list");
52  GetURL(TempFile, URL, TempFileDir);
53  strcpy(GlobalTempFile,"./test_result/fossology.sources.list");
54  strcpy(GlobalURL, "https://mirrors.kernel.org/fossology/releases/3.0.0/ubuntu/14.04/");
55 
57  memset(SQL,'\0',STRMAX);
58  snprintf(SQL,STRMAX, "DELETE FROM upload where upload_filename = 'fossology.sources.list';");
59  result = PQexec(pgConn, SQL);
60  if (fo_checkPQcommand(pgConn, result, SQL, __FILE__ ,__LINE__))
61  {
62  printf("Perpare delete information ERROR!\n");
63  return 1;
64  }
65  PQclear(result);
66 
68  memset(SQL,'\0',STRMAX);
69  snprintf(SQL,STRMAX,"INSERT INTO upload (upload_filename,upload_mode,upload_ts) VALUES ('fossology.sources.list',40,now());");
70  result = PQexec(pgConn, SQL);
71  if (fo_checkPQcommand(pgConn, result, SQL, __FILE__ ,__LINE__))
72  {
73  printf("Perpare upload information ERROR!\n");
74  return 1;
75  }
76  PQclear(result);
78  memset(SQL,'\0',STRMAX);
79  snprintf(SQL,STRMAX,"SELECT upload_pk from upload where upload_filename = 'fossology.sources.list';");
80  result = PQexec(pgConn, SQL);
81  if (fo_checkPQresult(pgConn, result, SQL, __FILE__ ,__LINE__))
82  {
83  printf("Perpare upload information ERROR!\n");
84  return 1;
85  }
86  GlobalUploadKey = atoi(PQgetvalue(result,0,0));
87  PQclear(result);
88 
89  GError* error = NULL;
90  char* foConf = get_confFile();
91 
92  char cmd[STRMAX+1];
93  snprintf(cmd, STRMAX, "sed -i 's|depth.*|depth=3|' %s", foConf);
94  if (system(cmd) != 0) {
95  printf("cannot reset depth to 3 with %s\n", cmd);
96  return 1;
97  }
98 
99  sysconfig = fo_config_load(foConf, &error);
100 
101  if (error) {
102  printf("cannot load config from '%s' error: %s\n", foConf, error->message);
103  return 1;
104  }
105 
106  return 0;
107 }
112 {
113  memset(GlobalTempFile, 0, STRMAX);
114  memset(GlobalURL, 0, URLMAX);
115  memset(GlobalParam, 0, STRMAX);
116  char TempFileDir[STRMAX];
117 
118  strcpy(TempFileDir, "./test_result");
119  if (file_dir_existed(TempFileDir))
120  {
121  RemoveDir(TempFileDir);
122  }
123 
124  char repoDir[STRMAX+1];
125  if (snprintf(repoDir, STRMAX, "%s/repo", get_sysconfdir())>0) {
126  RemoveDir(repoDir);
127  }
128 
129  dropTestEnvironment(dbManager, AGENT_DIR, "wget_agent");
130  GlobalUploadKey = -1;
131 
132  return 0;
133 }
134 
139 void string_tolower(char *string)
140 {
141  int length = strlen(string);
142  int i = 0;
143  for (i = 0; i < length; i++)
144  {
145  string[i] = tolower(string[i]);
146  }
147 }
148 
149 /* test functions */
150 
159 {
160  //printf("db start\n");
161  DBLoadGold();
162  //printf("db end\n");
163  char SQL[STRMAX];
164  char *pfile_sha1;
165  char *pfile_md5;
166  memset(SQL, 0, STRMAX);
167  PGresult *result;
168  snprintf(SQL, STRMAX-1, "select pfile_sha1, pfile_md5 from pfile where pfile_pk in (select pfile_fk from "
169  "upload where upload_pk = %ld);", GlobalUploadKey);
170  result = PQexec(pgConn, SQL); /* SELECT */
171  if (fo_checkPQresult(pgConn, result, SQL, __FILE__, __LINE__))
172  {
173  if (pgConn) PQfinish(pgConn);
174  SafeExit(-1);
175  }
176  pfile_sha1 = PQgetvalue(result,0,0);
177  pfile_md5 = PQgetvalue(result,0,1);
178  //printf("pfile_sha1, pfile_md5 are:%s, %s\n", pfile_sha1, pfile_md5 );
179  string_tolower(pfile_sha1);
180  string_tolower(pfile_md5);
181  //printf("pfile_sha1, pfile_md5 are:%s, %s\n", pfile_sha1, pfile_md5 );
182  char file_name_file[STRMAX] = {0};
183  char file_name_gold[STRMAX] = {0};
184  char string0[3] = {0};
185  char string1[3] = {0};
186  char string2[3] = {0};
187  char *string4 = get_sysconfdir();
188  strncpy(string0, pfile_sha1, 2);
189  strncpy(string1, pfile_sha1 + 2, 2);
190  strncpy(string2, pfile_sha1 + 4, 2);
191  //printf("string0, string1, string2 are:%s, %s, %s\n", string0, string1, string2);
192  sprintf(file_name_file, "%s/repo/files/%s/%s/%s/%s.%s.10240", string4, string0, string1, string2, pfile_sha1, pfile_md5);
193  sprintf(file_name_gold, "%s/repo/gold/%s/%s/%s/%s.%s.10240", string4, string0, string1, string2, pfile_sha1, pfile_md5);
194  int existed = file_dir_existed(file_name_file);
195  CU_ASSERT_EQUAL(existed, 1); /* the file into repo? */
196  if (existed)
197  {
198  RemoveDir(file_name_file);
199  }
200  existed = 0;
201  existed = file_dir_existed(file_name_gold);
202  CU_ASSERT_EQUAL(existed, 1); /* the file into repo? */
203  //printf("file_name_file, file_name_gold are:%s,%s\n", file_name_file, file_name_gold);
204  if (existed)
205  {
206  RemoveDir(file_name_gold);
207  }
208  PQclear(result);
209  //printf("testDBLoadGold end\n");
210 }
211 
215 CU_TestInfo testcases_DBLoadGold[] =
216 {
217 #if 0
218 #endif
219 {"DBLoadGold:Insert", testDBLoadGold},
220  CU_TEST_INFO_NULL
221 };
222 
char SQL[256]
SQL query to execute.
Definition: adj2nest.c:78
PGconn * pgConn
Database connection.
Definition: adj2nest.c:86
fo_conf * fo_config_load(char *rawname, GError **error)
Load the configuration information from the provided file.
Definition: fossconfig.c:275
char * get_sysconfdir()
get sysconfig dir path just created by create_db_repo_sysconf()
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 testDBLoadGold()
Function to test DBLoadGold.
CU_TestInfo testcases_DBLoadGold[]
testcases for function DBLoadGold
int DBLoadGoldInit()
initialize
fo_conf * sysconfig
void string_tolower(char *string)
Convert a string to lower case.
int DBLoadGoldClean()
Clean the env.
int RemoveDir(char *dirpath)
Remove all files under dirpath (rm -rf)
Definition: utils.c:1641
void SafeExit(int rc)
Close scheduler and database connections, then exit.
Definition: utils.c:77
char GlobalParam[STRMAX]
Additional parameters.
Definition: wget_agent.c:27
int GetURL(char *TempFile, char *URL, char *TempFileDir)
Do the wget.
Definition: wget_agent.c:328
char GlobalURL[URLMAX]
URL to download.
Definition: wget_agent.c:25
long GlobalUploadKey
Input for this system.
Definition: wget_agent.c:23
char GlobalTempFile[STRMAX]
Temp file to be used.
Definition: wget_agent.c:24
void DBLoadGold()
Insert a file into the database and repository.
Definition: wget_agent.c:81