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