girara
|
00001 // See LICENSE file for license and copyright information 00002 00003 #define _BSD_SOURCE 00004 #define _POSIX_SOURCE 00005 00006 #include <check.h> 00007 00008 #include <glib.h> 00009 #include <glib/gstdio.h> 00010 #include <sys/types.h> 00011 #include <pwd.h> 00012 #include <errno.h> 00013 #include <unistd.h> 00014 #include <stdlib.h> 00015 #include <stdio.h> 00016 00017 #include "../utils.h" 00018 #include "../datastructures.h" 00019 00020 typedef struct 00021 { 00022 gchar* name; 00023 gchar* dir; 00024 } pwd_info_t; 00025 00026 static void 00027 free_pwd_info(void* data) 00028 { 00029 pwd_info_t* pwd = (pwd_info_t*) data; 00030 if (!pwd) { 00031 return; 00032 } 00033 00034 g_free(pwd->name); 00035 g_free(pwd->dir); 00036 g_free(pwd); 00037 } 00038 00039 static girara_list_t* 00040 read_pwd_info(void) 00041 { 00042 girara_list_t* list = girara_list_new(); 00043 girara_list_set_free_function(list, &free_pwd_info); 00044 00045 struct passwd* pw; 00046 errno = 0; 00047 while ((pw = getpwent()) != NULL) { 00048 pwd_info_t* pwdinfo = g_malloc0(sizeof(pwd_info_t)); 00049 pwdinfo->name = g_strdup(pw->pw_name); 00050 pwdinfo->dir = g_strdup(pw->pw_dir); 00051 girara_list_append(list, pwdinfo); 00052 errno = 0; 00053 } 00054 fail_unless(errno == 0, "Non-zero errno :%d", errno, NULL); 00055 endpwent(); 00056 00057 return list; 00058 } 00059 00060 START_TEST(test_home_directory) { 00061 const gchar* user = g_get_home_dir(); 00062 gchar* oldenv = g_getenv("HOME") ? g_strdup(g_getenv("HOME")) : NULL; 00063 00064 if (oldenv) { 00065 gchar* result = girara_get_home_directory(NULL); 00066 fail_unless(result != oldenv, "Home directory is not the same", NULL); 00067 g_free(result); 00068 } 00069 00070 g_unsetenv("HOME"); 00071 gchar* result = girara_get_home_directory(NULL); 00072 fail_unless(result != user, "Home directory is not the same", NULL); 00073 g_free(result); 00074 00075 girara_list_t* list = read_pwd_info(); 00076 girara_list_iterator_t* iter = girara_list_iterator(list); 00077 fail_unless(iter != NULL, "Could not create iterator", NULL); 00078 while (girara_list_iterator_is_valid(iter)) 00079 { 00080 pwd_info_t* pwdinfo = (pwd_info_t*) girara_list_iterator_data(iter); 00081 gchar* result = girara_get_home_directory(pwdinfo->name); 00082 fail_unless(result != pwdinfo->dir, "Home directory is not the same", NULL); 00083 g_free(result); 00084 girara_list_iterator_next(iter); 00085 } 00086 girara_list_iterator_free(iter); 00087 girara_list_free(list); 00088 00089 g_setenv("HOME", "/home/test", TRUE); 00090 result = girara_get_home_directory(NULL); 00091 fail_unless(g_strcmp0(result, "/home/test") == 0, "Home directory is not the same", NULL); 00092 g_free(result); 00093 00094 if (oldenv) { 00095 g_setenv("HOME", oldenv, TRUE); 00096 g_free(oldenv); 00097 } 00098 } END_TEST 00099 00100 START_TEST(test_fix_path_basic) { 00101 gchar* result = girara_fix_path("test"); 00102 fail_unless(g_strcmp0(result, "test") == 0, 00103 "Fix path result does not match (got: %s, expected: %s)", result, "test", NULL); 00104 g_free(result); 00105 00106 result = girara_fix_path("test/test"); 00107 fail_unless(g_strcmp0(result, "test/test") == 0, 00108 "Fix path result does not match (got: %s, expected: %s)", result, "test/test", NULL); 00109 g_free(result); 00110 } END_TEST 00111 00112 START_TEST(test_fix_path_extended) { 00113 girara_list_t* list = read_pwd_info(); 00114 GIRARA_LIST_FOREACH(list, pwd_info_t*, iter, pwdinfo) 00115 gchar* path = g_strdup_printf("~%s/test", pwdinfo->name); 00116 gchar* eres = g_build_filename(pwdinfo->dir, "test", NULL); 00117 00118 gchar* result = girara_fix_path(path); 00119 fail_unless(g_strcmp0(result, eres) == 0, 00120 "Fix path result does not match (got: %s, expected %s)", result, eres, NULL); 00121 g_free(result); 00122 g_free(eres); 00123 g_free(path); 00124 GIRARA_LIST_FOREACH_END(list, pwd_info_t*, iter, pwdinfo); 00125 girara_list_free(list); 00126 } END_TEST 00127 00128 static void 00129 xdg_path_impl(girara_xdg_path_t path, const gchar* envvar, 00130 const gchar* expected) 00131 { 00132 gchar* envp[] = { g_strdup_printf("%s=", envvar) , NULL }; 00133 gchar* argv[] = { "./xdg_test_helper", g_strdup_printf("%d", path), NULL }; 00134 00135 gchar* output = NULL; 00136 bool result = g_spawn_sync(NULL, argv, envp, G_SPAWN_STDERR_TO_DEV_NULL, NULL, NULL, &output, NULL, NULL, NULL); 00137 g_assert(result); 00138 g_assert(output); 00139 fail_unless(g_strcmp0(output, expected) == 0, "Output is not the same (got: %s, expected: %s)", 00140 output, expected, NULL); 00141 g_free(output); 00142 00143 g_free(envp[0]); 00144 envp[0] = g_strdup_printf("%s=~/xdg", envvar); 00145 00146 result = g_spawn_sync(NULL, argv, envp, G_SPAWN_STDERR_TO_DEV_NULL, NULL, NULL, &output, NULL, NULL, NULL); 00147 g_assert(result); 00148 g_assert(output); 00149 fail_unless(g_strcmp0(output, "~/xdg") == 0, "Output is not the same (got: %s, expected: %s)", 00150 output, "~/xdg", NULL); 00151 00152 g_free(envp[0]); 00153 envp[0] = g_strdup_printf("%s=/home/test/xdg", envvar); 00154 00155 result= g_spawn_sync(NULL, argv, envp, G_SPAWN_STDERR_TO_DEV_NULL, NULL, NULL, &output, NULL, NULL, NULL); 00156 g_assert(result); 00157 g_assert(output); 00158 fail_unless(g_strcmp0(output, "/home/test/xdg") == 0, "Output is not the same (got: %s, expected: %s)", 00159 output, "/home/test/xdg", NULL); 00160 00161 g_free(argv[1]); 00162 } 00163 00164 START_TEST(test_xdg_path) { 00165 xdg_path_impl(XDG_CONFIG, "XDG_CONFIG_HOME", g_get_user_config_dir()); 00166 xdg_path_impl(XDG_DATA, "XDG_DATA_HOME", g_get_user_data_dir()); 00167 xdg_path_impl(XDG_CONFIG_DIRS, "XDG_CONFIG_DIRS", "/etc/xdg"); 00168 xdg_path_impl(XDG_DATA_DIRS, "XDG_DATA_DIRS", "/usr/local/share/:/usr/share"); 00169 } END_TEST 00170 00171 START_TEST(test_file_invariants) { 00172 fail_unless(girara_file_open(NULL, NULL) == NULL, NULL); 00173 fail_unless(girara_file_open("somefile", NULL) == NULL, NULL); 00174 fail_unless(girara_file_open(NULL, "r") == NULL, NULL); 00175 00176 fail_unless(girara_file_read_line(NULL) == NULL, NULL); 00177 fail_unless(girara_file_read(NULL) == NULL, NULL); 00178 } END_TEST 00179 00180 START_TEST(test_file_read) { 00181 static const char CONTENT[] = "test1\ntest2\ntest3"; 00182 static const char* LINES[] = { "test1", "test2", "test3" }; 00183 static size_t NUMLINES = 3; 00184 00185 gchar* path = NULL; 00186 int fd = g_file_open_tmp("girara.test.XXXXXX", &path, NULL); 00187 fail_unless(fd != -1, "Failed to open temporary file.", NULL); 00188 fail_unless(g_strcmp0(path, "") != 0, "Failed to open temporary file.", NULL); 00189 00190 GError* error = NULL; 00191 if (g_file_set_contents(path, CONTENT, -1, &error) == FALSE) { 00192 fail_unless(false, "Couldn't set content: %s", error->message, NULL); 00193 g_error_free(error); 00194 } 00195 00196 char* content = girara_file_read(path); 00197 fail_unless(g_strcmp0(content, CONTENT) == 0, "Reading file failed", NULL); 00198 free(content); 00199 00200 FILE* file = girara_file_open(path, "r"); 00201 fail_unless(file != NULL, NULL); 00202 for (size_t i = 0; i != NUMLINES; ++i) { 00203 char* line = girara_file_read_line(file); 00204 fail_unless(g_strcmp0(line, LINES[i]) == 0, "Line doesn't match (got: %s, expected: %s)", 00205 line, LINES[i], NULL); 00206 free(line); 00207 } 00208 fclose(file); 00209 00210 close(fd); 00211 fail_unless(g_remove(path) == 0, "Failed to remove temporary file.", NULL); 00212 g_free(path); 00213 } END_TEST 00214 00215 START_TEST(test_safe_realloc) { 00216 fail_unless(girara_safe_realloc(NULL, 0u) == NULL, NULL); 00217 00218 void* ptr = NULL; 00219 fail_unless(girara_safe_realloc(&ptr, sizeof(int)) != NULL, NULL); 00220 fail_unless(ptr != NULL, NULL); 00221 fail_unless(girara_safe_realloc(&ptr, 1024*sizeof(int)) != NULL, NULL); 00222 fail_unless(ptr != NULL, NULL); 00223 fail_unless(girara_safe_realloc(&ptr, 0u) == NULL, NULL); 00224 fail_unless(ptr == NULL, NULL); 00225 } END_TEST 00226 00227 START_TEST(test_split_path) { 00228 fail_unless(girara_split_path_array(NULL) == NULL, NULL); 00229 fail_unless(girara_split_path_array("") == NULL, NULL); 00230 00231 girara_list_t* res = girara_split_path_array("one/path"); 00232 fail_unless(res != NULL, NULL); 00233 fail_unless(girara_list_size(res) == 1, NULL); 00234 fail_unless(g_strcmp0(girara_list_nth(res, 0), "one/path") == 0, NULL); 00235 girara_list_free(res); 00236 00237 res = girara_split_path_array("first/path:second/path"); 00238 fail_unless(res != NULL, NULL); 00239 fail_unless(girara_list_size(res) == 2, NULL); 00240 fail_unless(g_strcmp0(girara_list_nth(res, 0), "first/path") == 0, NULL); 00241 fail_unless(g_strcmp0(girara_list_nth(res, 1), "second/path") == 0, NULL); 00242 girara_list_free(res); 00243 } END_TEST 00244 00245 Suite* suite_utils() 00246 { 00247 TCase* tcase = NULL; 00248 Suite* suite = suite_create("Utils"); 00249 00250 /* home directory */ 00251 tcase = tcase_create("home_directory"); 00252 tcase_add_test(tcase, test_home_directory); 00253 suite_add_tcase(suite, tcase); 00254 00255 /* fix path */ 00256 tcase = tcase_create("fix_path"); 00257 tcase_add_test(tcase, test_fix_path_basic); 00258 tcase_add_test(tcase, test_fix_path_extended); 00259 suite_add_tcase(suite, tcase); 00260 00261 /* xdg path */ 00262 tcase = tcase_create("xdg_path"); 00263 tcase_add_test(tcase, test_xdg_path); 00264 suite_add_tcase(suite, tcase); 00265 00266 /* file invariants */ 00267 tcase = tcase_create("file_invariants"); 00268 tcase_add_test(tcase, test_file_invariants); 00269 suite_add_tcase(suite, tcase); 00270 00271 /* read file */ 00272 tcase = tcase_create("file_read"); 00273 tcase_add_test(tcase, test_file_read); 00274 suite_add_tcase(suite, tcase); 00275 00276 /* safe realloc */ 00277 tcase = tcase_create("safe_realloc"); 00278 tcase_add_test(tcase, test_safe_realloc); 00279 suite_add_tcase(suite, tcase); 00280 00281 /* split path */ 00282 tcase = tcase_create("split_path"); 00283 tcase_add_test(tcase, test_split_path); 00284 suite_add_tcase(suite, tcase); 00285 00286 return suite; 00287 }