FOSSology  3.2.0rc1
Open Source License Compliance by Open Source Software
testDBLoadGold.c
Go to the documentation of this file.
1 /*********************************************************************
2 Copyright (C) 2011-2012 Hewlett-Packard Development Company, L.P.
3 
4 This program is free software; you can redistribute it and/or
5 modify it under the terms of the GNU General Public License
6 version 2 as published by the Free Software Foundation.
7 
8 This program is distributed in the hope that it will be useful,
9 but WITHOUT ANY WARRANTY; without even the implied warranty of
10 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 GNU General Public License for more details.
12 
13 You should have received a copy of the GNU General Public License along
14 with this program; if not, write to the Free Software Foundation, Inc.,
15 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16 *********************************************************************/
17 
18 /* cunit includes */
19 #include <CUnit/CUnit.h>
20 #include "wget_agent.h"
21 #include "../utility.h"
22 #include <string.h>
23 #include <ctype.h>
24 #include "libfodbreposysconf.h"
25 
26 #define AGENT_DIR "../../"
27 
34 static PGresult *result = NULL;
35 extern fo_conf* sysconfig;
36 
37 static fo_dbManager* dbManager;
44 {
45  char URL[URLMAX];
46  char TempFileDir[STRMAX];
47  char TempFile[STRMAX];
48 
50  dbManager = createTestEnvironment(AGENT_DIR, "wget_agent", 1);
51  if (!dbManager) {
52  LOG_FATAL("Unable to connect to database");
53  return 1;
54  }
55 
56  pgConn = fo_dbManager_getWrappedConnection(dbManager);
57 
59  strcpy(GlobalParam, "-l 1 -A *.list -R *.deb");
60  strcpy(URL, "https://mirrors.kernel.org/fossology/releases/3.0.0/ubuntu/14.04/");
61  strcpy(TempFileDir, "./test_result/");
62  strcpy(TempFile, "./test_result/fossology.sources.list");
63  GetURL(TempFile, URL, TempFileDir);
64  strcpy(GlobalTempFile,"./test_result/fossology.sources.list");
65  strcpy(GlobalURL, "https://mirrors.kernel.org/fossology/releases/3.0.0/ubuntu/14.04/");
66 
68  memset(SQL,'\0',STRMAX);
69  snprintf(SQL,STRMAX, "DELETE FROM upload where upload_filename = 'fossology.sources.list';");
70  result = PQexec(pgConn, SQL);
71  if (fo_checkPQcommand(pgConn, result, SQL, __FILE__ ,__LINE__))
72  {
73  printf("Perpare delete information ERROR!\n");
74  return 1;
75  }
76  PQclear(result);
77 
79  memset(SQL,'\0',STRMAX);
80  snprintf(SQL,STRMAX,"INSERT INTO upload (upload_filename,upload_mode,upload_ts) VALUES ('fossology.sources.list',40,now());");
81  result = PQexec(pgConn, SQL);
82  if (fo_checkPQcommand(pgConn, result, SQL, __FILE__ ,__LINE__))
83  {
84  printf("Perpare upload information ERROR!\n");
85  return 1;
86  }
87  PQclear(result);
89  memset(SQL,'\0',STRMAX);
90  snprintf(SQL,STRMAX,"SELECT upload_pk from upload where upload_filename = 'fossology.sources.list';");
91  result = PQexec(pgConn, SQL);
92  if (fo_checkPQresult(pgConn, result, SQL, __FILE__ ,__LINE__))
93  {
94  printf("Perpare upload information ERROR!\n");
95  return 1;
96  }
97  GlobalUploadKey = atoi(PQgetvalue(result,0,0));
98  PQclear(result);
99 
100  GError* error = NULL;
101  char* foConf = get_confFile();
102 
103  char cmd[STRMAX+1];
104  snprintf(cmd, STRMAX, "sed -i 's|depth.*|depth=3|' %s", foConf);
105  if (system(cmd) != 0) {
106  printf("cannot reset depth to 3 with %s\n", cmd);
107  return 1;
108  }
109 
110  sysconfig = fo_config_load(foConf, &error);
111 
112  if (error) {
113  printf("cannot load config from '%s' error: %s\n", foConf, error->message);
114  return 1;
115  }
116 
117  return 0;
118 }
123 {
124  memset(GlobalTempFile, 0, STRMAX);
125  memset(GlobalURL, 0, URLMAX);
126  memset(GlobalParam, 0, STRMAX);
127  char TempFileDir[STRMAX];
128 
129  strcpy(TempFileDir, "./test_result");
130  if (file_dir_existed(TempFileDir))
131  {
132  RemoveDir(TempFileDir);
133  }
134 
135  if (sysconfig) {
136  fo_config_free(sysconfig);
137  }
138 
139  char repoDir[STRMAX+1];
140  if (snprintf(repoDir, STRMAX, "%s/repo", get_sysconfdir())>0) {
141  RemoveDir(repoDir);
142  }
143 
144  dropTestEnvironment(dbManager, AGENT_DIR, "wget_agent");
145  GlobalUploadKey = -1;
146 
147  return 0;
148 }
149 
154 void string_tolower(char *string)
155 {
156  int length = strlen(string);
157  int i = 0;
158  for (i = 0; i < length; i++)
159  {
160  string[i] = tolower(string[i]);
161  }
162 }
163 
164 /* test functions */
165 
174 {
175  //printf("db start\n");
176  DBLoadGold();
177  //printf("db end\n");
178  char SQL[STRMAX];
179  char *pfile_sha1;
180  char *pfile_md5;
181  memset(SQL, 0, STRMAX);
182  PGresult *result;
183  snprintf(SQL, STRMAX-1, "select pfile_sha1, pfile_md5 from pfile where pfile_pk in (select pfile_fk from "
184  "upload where upload_pk = %ld);", GlobalUploadKey);
185  result = PQexec(pgConn, SQL); /* SELECT */
186  if (fo_checkPQresult(pgConn, result, SQL, __FILE__, __LINE__))
187  {
188  if (pgConn) PQfinish(pgConn);
189  SafeExit(-1);
190  }
191  pfile_sha1 = PQgetvalue(result,0,0);
192  pfile_md5 = PQgetvalue(result,0,1);
193  //printf("pfile_sha1, pfile_md5 are:%s, %s\n", pfile_sha1, pfile_md5 );
194  string_tolower(pfile_sha1);
195  string_tolower(pfile_md5);
196  //printf("pfile_sha1, pfile_md5 are:%s, %s\n", pfile_sha1, pfile_md5 );
197  char file_name_file[STRMAX] = {0};
198  char file_name_gold[STRMAX] = {0};
199  char string0[3] = {0};
200  char string1[3] = {0};
201  char string2[3] = {0};
202  char *string4 = get_sysconfdir();
203  strncpy(string0, pfile_sha1, 2);
204  strncpy(string1, pfile_sha1 + 2, 2);
205  strncpy(string2, pfile_sha1 + 4, 2);
206  //printf("string0, string1, string2 are:%s, %s, %s\n", string0, string1, string2);
207  sprintf(file_name_file, "%s/repo/files/%s/%s/%s/%s.%s.10240", string4, string0, string1, string2, pfile_sha1, pfile_md5);
208  sprintf(file_name_gold, "%s/repo/gold/%s/%s/%s/%s.%s.10240", string4, string0, string1, string2, pfile_sha1, pfile_md5);
209  int existed = file_dir_existed(file_name_file);
210  CU_ASSERT_EQUAL(existed, 1); /* the file into repo? */
211  if (existed)
212  {
213  RemoveDir(file_name_file);
214  }
215  existed = 0;
216  existed = file_dir_existed(file_name_gold);
217  CU_ASSERT_EQUAL(existed, 1); /* the file into repo? */
218  //printf("file_name_file, file_name_gold are:%s,%s\n", file_name_file, file_name_gold);
219  if (existed)
220  {
221  RemoveDir(file_name_gold);
222  }
223  PQclear(result);
224  //printf("testDBLoadGold end\n");
225 }
226 
230 CU_TestInfo testcases_DBLoadGold[] =
231 {
232 #if 0
233 #endif
234 {"DBLoadGold:Insert", testDBLoadGold},
235  CU_TEST_INFO_NULL
236 };
237 
int fo_checkPQresult(PGconn *pgConn, PGresult *result, char *sql, char *FileID, int LineNumb)
Check the result status of a postgres SELECT.
Definition: libfossdb.c:181
fo_conf * sysconfig
PGconn * pgConn
Database connection.
Definition: adj2nest.c:98
int RemoveDir(char *dirpath)
Remove all files under dirpath (rm -rf)
Definition: utils.c:1651
long GlobalUploadKey
Input for this system.
Definition: wget_agent.c:35
char GlobalParam[STRMAX]
Additional parameters.
Definition: wget_agent.c:39
int GetURL(char *TempFile, char *URL, char *TempFileDir)
Do the wget.
Definition: wget_agent.c:340
CU_TestInfo testcases_DBLoadGold[]
testcases for function DBLoadGold
char GlobalURL[URLMAX]
URL to download.
Definition: wget_agent.c:37
void testDBLoadGold()
Function to test DBLoadGold.
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:215
int DBLoadGoldClean()
Clean the env.
void DBLoadGold()
Insert a file into the database and repository.
Definition: wget_agent.c:93
char SQL[256]
SQL query to execute.
Definition: adj2nest.c:90
char GlobalTempFile[STRMAX]
Temp file to be used.
Definition: wget_agent.c:36
int DBLoadGoldInit()
initialize
fo_dbManager * dbManager
fo_dbManager object
Definition: process.c:28
void fo_config_free(fo_conf *conf)
Frees the memory associated with the internal configuration data structures.
Definition: fossconfig.c:511
char * get_sysconfdir()
get sysconfig dir path just created by create_db_repo_sysconf()
fo_conf * fo_config_load(char *rawname, GError **error)
Load the configuration information from the provided file.
Definition: fossconfig.c:280
void string_tolower(char *string)
Convert a string to lower case.
void SafeExit(int rc)
Close scheduler and database connections, then exit.
Definition: utils.c:88