FOSSology  3.2.0rc1
Open Source License Compliance by Open Source Software
testUtilities.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 
28 /* test functions */
29 
38 {
39  int pid = system("echo 'hello world' > ./test.file");
40  if (WIFEXITED(pid)) pid = WEXITSTATUS(pid);
41  else pid = -1;
42  char Fname[] = "./test.file";
43  int isFile = IsFile(Fname, 1);
44  CU_ASSERT_EQUAL(isFile, 1);
45  RemoveDir(Fname);
46 }
47 
57 {
58  int pid = system("echo 'hello world' > ./test.file");
59  if (WIFEXITED(pid)) pid = WEXITSTATUS(pid);
60  else pid = -1;
61  char Fname[] = "./test.file";
62  int isFile = IsFile(Fname, 0);
63  CU_ASSERT_EQUAL(isFile, 1);
64  char NewFname[] = "./link.file";
65  pid = symlink(Fname, NewFname);
66  isFile = IsFile(NewFname, 1);
67  CU_ASSERT_EQUAL(isFile, 1);
68 #if 0
69 #endif
70  RemoveDir(Fname);
71  RemoveDir(NewFname);
72 }
73 
82 {
83  char URL[MAX_LENGTH];
84  strcpy(URL, "http://fossology.org");
85  int pos = GetPosition(URL);
86  CU_ASSERT_EQUAL(pos, 7);
87  memset(URL, 0, MAX_LENGTH);
88  strcpy(URL, "https://encrypted.google.com/");
89  pos = GetPosition(URL);
90  CU_ASSERT_EQUAL(pos, 8);
91  memset(URL, 0, MAX_LENGTH);
92  strcpy(URL, "ftp://osms.chn.hp.com/pub/fossology/");
93  pos = GetPosition(URL);
94  CU_ASSERT_EQUAL(pos, 6);
95 }
96 
105 {
106  char Sin[MAX_LENGTH];
107  char Sout[MAX_LENGTH];
108  int SoutSize = MAX_LENGTH;
109  /* the URL is failed to taint*/
110  strcpy(Sin, "http://fossology.org #");
111  int result = TaintURL(Sin, Sout, SoutSize);
112  CU_ASSERT_EQUAL(result, 0); /* failed to taint */
113  /* the URL is tainted */
114  strcpy(Sin, "http://fossology.org/`debian/ 1.0.0/");
115  result = TaintURL(Sin, Sout, SoutSize);
116  CU_ASSERT_EQUAL(result, 1); /* tainted */
117 #if 0
118 #endif
119 }
120 
130 {
131  char source_path[] = "/srv/fossology/testDbRepo12704556/%H/wget";
132  char des_path[1024] = {0};
133  char HostName[1024] = {0};
134  char *taint_path = PathCheck(source_path);
135  gethostname(HostName, sizeof(HostName));
136  snprintf(des_path, sizeof(des_path), "/srv/fossology/testDbRepo12704556/%s/wget", HostName);
137  CU_ASSERT_STRING_EQUAL(des_path, taint_path); /* tainted */
138  free(taint_path);
139 }
140 
149 {
150  char file_path[] = "./";
151  char tar_file[] = "/tmp/Suckupfs.tar.dir/test.tar";
152  char des_dir[] = "/tmp/Suckupfs.tar.dir/";
153  int tar_status = -1;
154  char commands[1024] = "";
155  struct stat Status;
156  if (stat(file_path, &Status) != 0) return; // file_path is not exist or can not access
157 
158  int res = Archivefs(file_path, tar_file, des_dir, Status);
159  CU_ASSERT_EQUAL(1, res);
160  tar_status = stat(file_path, &Status);
161  CU_ASSERT_EQUAL(0, tar_status);
162  snprintf(commands, sizeof(commands), "file %s |grep 'tar archive' >/dev/null 2>&1", tar_file);
163  int rc = system(commands);
164  CU_ASSERT_EQUAL(1, rc != -1 && (WEXITSTATUS(rc) == 0));
165  rmdir(des_dir);
166 }
167 
176 {
177  char file_path[] = "./Makefile";
178  char tar_file[] = "/tmp/Suckupfs.tar.dir/testfile";
179  char des_dir[] = "/tmp/Suckupfs.tar.dir/";
180  int tar_status = -1;
181  char commands[1024] = "";
182  struct stat Status;
183  if (stat(file_path, &Status) != 0) return; // file_path is not exist or can not access
184 
185  int res = Archivefs(file_path, tar_file, des_dir, Status);
186  CU_ASSERT_EQUAL(1, res);
187  tar_status = stat(file_path, &Status);
188  CU_ASSERT_EQUAL(0, tar_status);
189  snprintf(commands, sizeof(commands), "file %s |grep ASCII >/dev/null 2>&1", tar_file);
190  int rc = system(commands);
191  CU_ASSERT_EQUAL(1, rc != -1 && (WEXITSTATUS(rc) == 0));
192  rmdir(des_dir);
193 }
194 
198 CU_TestInfo testcases_Utiliies[] =
199 {
200 #if 0
201 #endif
202 {"Utiliies:IsFile_file", testIsFileNormal_RegulerFile},
203 {"Utiliies:IsFile_link", testIsFileNormal_SymLink},
204 {"Utiliies:GetPosition_normal", testGetPositionNormal},
205 {"Utiliies:TaintURL_normal", testTaintURL},
206 {"Utiliies:PathCheck", test_PathCheck},
207 {"Utiliies:Archivefs_dir", test_Archivefs_dir},
208 {"Utiliies:Archivefs_file", test_Archivefs_file},
209  CU_TEST_INFO_NULL
210 };
211 
char * PathCheck(char *DirPath)
Check if path contains a "%U" or "%H". If so, substitute a unique ID for U.
Definition: utils.c:1672
int Archivefs(char *Path, char *TempFile, char *TempFileDir, struct stat Status)
Copy downloaded files to temporary directory.
Definition: wget_agent.c:831
int GetPosition(char *URL)
Get the position (ending + 1) of http|https|ftp:// of one url.
Definition: wget_agent.c:78
void testIsFileNormal_RegulerFile()
Test for function IsFile()
Definition: testUtilities.c:37
int RemoveDir(char *dirpath)
Remove all files under dirpath (rm -rf)
Definition: utils.c:1651
int IsFile(long mode)
Check if the pfile_id is a file.
Definition: wc_agent.c:67
void testTaintURL()
Test for function TaintURL()
void testIsFileNormal_SymLink()
Test for function IsFile() a symlink.
Definition: testUtilities.c:56
void test_PathCheck()
Test for function PathCheck()
CU_TestInfo testcases_Utiliies[]
testcases for function SetEnv
void test_Archivefs_file()
Test for function Archivefs(), reguler file.
void testGetPositionNormal()
Test for function GetPosition()
Definition: testUtilities.c:81
int TaintURL(char *Sin, char *Sout, int SoutSize)
Given a URL string, taint-protect it.
Definition: wget_agent.c:281
void test_Archivefs_dir()
Test for function Archivefs(), dir.