FOSSology  4.4.0
Open Source License Compliance by Open Source Software
testUtilities.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 
17 /* test functions */
18 
27 {
28  int pid = system("echo 'hello world' > ./test.file");
29  if (WIFEXITED(pid)) pid = WEXITSTATUS(pid);
30  else pid = -1;
31  char Fname[] = "./test.file";
32  int isFile = IsFile(Fname, 1);
33  CU_ASSERT_EQUAL(isFile, 1);
34  RemoveDir(Fname);
35 }
36 
46 {
47  int pid = system("echo 'hello world' > ./test.file");
48  if (WIFEXITED(pid)) pid = WEXITSTATUS(pid);
49  else pid = -1;
50  char Fname[] = "./test.file";
51  int isFile = IsFile(Fname, 0);
52  CU_ASSERT_EQUAL(isFile, 1);
53  char NewFname[] = "./link.file";
54  pid = symlink(Fname, NewFname);
55  isFile = IsFile(NewFname, 1);
56  CU_ASSERT_EQUAL(isFile, 1);
57 #if 0
58 #endif
59  RemoveDir(Fname);
60  RemoveDir(NewFname);
61 }
62 
71 {
72  char URL[MAX_LENGTH];
73  strcpy(URL, "http://fossology.org");
74  int pos = GetPosition(URL);
75  CU_ASSERT_EQUAL(pos, 7);
76  memset(URL, 0, MAX_LENGTH);
77  strcpy(URL, "https://encrypted.google.com/");
78  pos = GetPosition(URL);
79  CU_ASSERT_EQUAL(pos, 8);
80  memset(URL, 0, MAX_LENGTH);
81  strcpy(URL, "ftp://osms.chn.hp.com/pub/fossology/");
82  pos = GetPosition(URL);
83  CU_ASSERT_EQUAL(pos, 6);
84 }
85 
94 {
95  char Sin[MAX_LENGTH];
96  char Sout[MAX_LENGTH];
97  int SoutSize = MAX_LENGTH;
98  /* the URL is failed to taint*/
99  strcpy(Sin, "http://fossology.org #");
100  int result = TaintURL(Sin, Sout, SoutSize);
101  CU_ASSERT_EQUAL(result, 0); /* failed to taint */
102  /* the URL is tainted */
103  strcpy(Sin, "http://fossology.org/`debian/ 1.0.0/");
104  result = TaintURL(Sin, Sout, SoutSize);
105  CU_ASSERT_EQUAL(result, 1); /* tainted */
106 #if 0
107 #endif
108 }
109 
119 {
120  char source_path[] = "/srv/fossology/testDbRepo12704556/%H/wget";
121  char des_path[1024] = {0};
122  char HostName[1024] = {0};
123  char *taint_path = PathCheck(source_path);
124  gethostname(HostName, sizeof(HostName));
125  snprintf(des_path, sizeof(des_path), "/srv/fossology/testDbRepo12704556/%s/wget", HostName);
126  CU_ASSERT_STRING_EQUAL(des_path, taint_path); /* tainted */
127  free(taint_path);
128 }
129 
138 {
139  char file_path[] = "./";
140  char tar_file[] = "/tmp/Suckupfs.tar.dir/test.tar";
141  char des_dir[] = "/tmp/Suckupfs.tar.dir/";
142  int tar_status = -1;
143  char commands[1024] = "";
144  struct stat Status;
145  if (stat(file_path, &Status) != 0) return; // file_path is not exist or can not access
146 
147  int res = Archivefs(file_path, tar_file, des_dir, Status);
148  CU_ASSERT_EQUAL(1, res);
149  tar_status = stat(file_path, &Status);
150  CU_ASSERT_EQUAL(0, tar_status);
151  snprintf(commands, sizeof(commands), "file %s |grep 'tar archive' >/dev/null 2>&1", tar_file);
152  int rc = system(commands);
153  CU_ASSERT_EQUAL(1, rc != -1 && (WEXITSTATUS(rc) == 0));
154  rmdir(des_dir);
155 }
156 
165 {
166  char file_path[] = "./Makefile";
167  char tar_file[] = "/tmp/Suckupfs.tar.dir/testfile";
168  char des_dir[] = "/tmp/Suckupfs.tar.dir/";
169  int tar_status = -1;
170  char commands[1024] = "";
171  struct stat Status;
172  if (stat(file_path, &Status) != 0) return; // file_path is not exist or can not access
173 
174  int res = Archivefs(file_path, tar_file, des_dir, Status);
175  CU_ASSERT_EQUAL(1, res);
176  tar_status = stat(file_path, &Status);
177  CU_ASSERT_EQUAL(0, tar_status);
178  snprintf(commands, sizeof(commands), "file %s |grep ASCII >/dev/null 2>&1", tar_file);
179  int rc = system(commands);
180  CU_ASSERT_EQUAL(1, rc != -1 && (WEXITSTATUS(rc) == 0));
181  rmdir(des_dir);
182 }
183 
187 CU_TestInfo testcases_Utiliies[] =
188 {
189 #if 0
190 #endif
191 {"Utiliies:IsFile_file", testIsFileNormal_RegulerFile},
192 {"Utiliies:IsFile_link", testIsFileNormal_SymLink},
193 {"Utiliies:GetPosition_normal", testGetPositionNormal},
194 {"Utiliies:TaintURL_normal", testTaintURL},
195 {"Utiliies:PathCheck", test_PathCheck},
196 {"Utiliies:Archivefs_dir", test_Archivefs_dir},
197 {"Utiliies:Archivefs_file", test_Archivefs_file},
198  CU_TEST_INFO_NULL
199 };
200 
void test_Archivefs_dir()
Test for function Archivefs(), dir.
void test_Archivefs_file()
Test for function Archivefs(), reguler file.
void testIsFileNormal_SymLink()
Test for function IsFile() a symlink.
Definition: testUtilities.c:45
void testIsFileNormal_RegulerFile()
Test for function IsFile()
Definition: testUtilities.c:26
CU_TestInfo testcases_Utiliies[]
testcases for function SetEnv
void testGetPositionNormal()
Test for function GetPosition()
Definition: testUtilities.c:70
void test_PathCheck()
Test for function PathCheck()
void testTaintURL()
Test for function TaintURL()
Definition: testUtilities.c:93
int RemoveDir(char *dirpath)
Remove all files under dirpath (rm -rf)
Definition: utils.c:1641
char * PathCheck(char *DirPath)
Check if path contains a "%U" or "%H". If so, substitute a unique ID for U.
Definition: utils.c:1662
int IsFile(long mode)
Check if the pfile_id is a file.
Definition: wc_agent.c:55
int GetPosition(char *URL)
Get the position (ending + 1) of http|https|ftp:// of one url.
Definition: wget_agent.c:66
int TaintURL(char *Sin, char *Sout, int SoutSize)
Given a URL string, taint-protect it.
Definition: wget_agent.c:269
int Archivefs(char *Path, char *TempFile, char *TempFileDir, struct stat Status)
Copy downloaded files to temporary directory.
Definition: wget_agent.c:819