test_utils.c 10.5 KB
Newer Older
Sebastian Ramacher's avatar
Sebastian Ramacher committed
1 2
// See LICENSE file for license and copyright information

3 4
#include <check.h>

Sebastian Ramacher's avatar
Sebastian Ramacher committed
5
#include <glib.h>
Sebastian Ramacher's avatar
Sebastian Ramacher committed
6
#include <glib/gstdio.h>
Sebastian Ramacher's avatar
Sebastian Ramacher committed
7 8
#include <sys/types.h>
#include <pwd.h>
9
#include <errno.h>
Moritz Lipp's avatar
Moritz Lipp committed
10
#include <unistd.h>
Sebastian Ramacher's avatar
Sebastian Ramacher committed
11 12
#include <stdlib.h>
#include <stdio.h>
13

14 15
#include "utils.h"
#include "datastructures.h"
16
#include "tests.h"
17

18 19 20 21
static girara_list_t*
read_pwd_info(void)
{
  girara_list_t* list = girara_list_new();
22
  girara_list_set_free_function(list, g_free);
23 24 25 26

  struct passwd* pw;
  errno = 0;
  while ((pw = getpwent()) != NULL) {
27
    girara_list_append(list, g_strdup(pw->pw_name));
28
    errno = 0;
29
  }
30
  fail_unless(errno == 0, "Non-zero errno :%d", errno, NULL);
31 32 33 34
  endpwent();

  return list;
}
Sebastian Ramacher's avatar
Sebastian Ramacher committed
35

36
START_TEST(test_home_directory) {
Sebastian Ramacher's avatar
Sebastian Ramacher committed
37
  const gchar* user = g_get_home_dir();
38
  const gchar* home = g_getenv("HOME");
Sebastian Ramacher's avatar
Sebastian Ramacher committed
39

40 41
  girara_list_t* list = read_pwd_info();
  girara_list_iterator_t* iter = girara_list_iterator(list);
42
  fail_unless(iter != NULL, "Could not create iterator", NULL);
43
  while (girara_list_iterator_is_valid(iter))
44
  {
45 46
    const char* username = (const char*) girara_list_iterator_data(iter);
    gchar* result = girara_get_home_directory(username);
47 48 49
    if (!home || g_strcmp0(user, username) != 0) {
      fail_unless(result != NULL && strlen(result) != 0, "Home directory is empty", NULL);
    }
Moritz Lipp's avatar
Moritz Lipp committed
50
    g_free(result);
51 52
    girara_list_iterator_next(iter);
  }
53 54
  girara_list_iterator_free(iter);
  girara_list_free(list);
55
} END_TEST
Sebastian Ramacher's avatar
Sebastian Ramacher committed
56

57 58 59 60 61 62 63 64
START_TEST(test_home_directory_get_HOME) {
  const gchar* home = g_getenv("HOME");

  if (home) {
    gchar* result = girara_get_home_directory(NULL);
    fail_unless(g_strcmp0(result, home) == 0, "Home directory is not the same", NULL);
    g_free(result);
  }
65 66
} END_TEST

67
START_TEST(test_fix_path_basic) {
Sebastian Ramacher's avatar
Sebastian Ramacher committed
68 69
  gchar* result = girara_fix_path("/test");
  fail_unless(g_strcmp0(result, "/test") == 0,
70
      "Fix path result does not match (got: %s, expected: %s)", result, "test", NULL);
Moritz Lipp's avatar
Moritz Lipp committed
71
  g_free(result);
Sebastian Ramacher's avatar
Sebastian Ramacher committed
72

Sebastian Ramacher's avatar
Sebastian Ramacher committed
73 74
  result = girara_fix_path("/test/test");
  fail_unless(g_strcmp0(result, "/test/test") == 0,
75
      "Fix path result does not match (got: %s, expected: %s)", result, "test/test", NULL);
Moritz Lipp's avatar
Moritz Lipp committed
76
  g_free(result);
77 78 79

  result = girara_fix_path("test");
  fail_unless(g_str_has_suffix(result, "/test") == TRUE,
Sebastian Ramacher's avatar
Sebastian Ramacher committed
80
      "Fix path result does not match (got: %s, expected: %s)", result, ".../test", NULL);
81
  g_free(result);
82
} END_TEST
Sebastian Ramacher's avatar
Sebastian Ramacher committed
83

84
START_TEST(test_fix_path_extended) {
85 86
  const gchar* user = g_get_home_dir();
  const gchar* home = g_getenv("HOME");
87

88
  girara_list_t* list = read_pwd_info();
89 90
  GIRARA_LIST_FOREACH(list, const char*, iter, username)
    gchar* path = g_strdup_printf("~%s/test", username);
Moritz Lipp's avatar
Moritz Lipp committed
91
    gchar* result = girara_fix_path(path);
92 93 94 95
    if (!home || g_strcmp0(user, username) != 0) {
      fail_unless(result != NULL && strlen(result) != 0,
          "Fix path result is empty");
    }
Moritz Lipp's avatar
Moritz Lipp committed
96
    g_free(result);
97
    g_free(path);
98
  GIRARA_LIST_FOREACH_END(list, const char*, iter, pwdinfo);
99
  girara_list_free(list);
100
} END_TEST
Sebastian Ramacher's avatar
Sebastian Ramacher committed
101 102 103 104 105

static void
xdg_path_impl(girara_xdg_path_t path, const gchar* envvar,
    const gchar* expected)
{
106 107 108
  const char* xdg_test_helper_path = g_getenv("XDG_TEST_HELPER_PATH");
  fail_unless(xdg_test_helper_path != NULL, "XDG_TEST_HELPER_PATH is not set", NULL);

Sebastian Ramacher's avatar
Sebastian Ramacher committed
109
  gchar** envp = g_get_environ();
110

Sebastian Ramacher's avatar
Sebastian Ramacher committed
111
  envp = g_environ_setenv(envp, envvar, "", TRUE);
112
  gchar* argv[] = { g_build_filename(xdg_test_helper_path, "xdg_test_helper", NULL), g_strdup_printf("%d", path), NULL };
Sebastian Ramacher's avatar
Sebastian Ramacher committed
113 114

  gchar* output = NULL;
Moritz Lipp's avatar
Moritz Lipp committed
115 116
  bool result = g_spawn_sync(NULL, argv, envp, G_SPAWN_STDERR_TO_DEV_NULL, NULL, NULL, &output, NULL, NULL, NULL);
  g_assert(result);
Sebastian Ramacher's avatar
Sebastian Ramacher committed
117
  g_assert(output);
118
  fail_unless(g_strcmp0(output, expected) == 0, "Output is not the same (got: %s, expected: %s)",
119
      output, expected, NULL);
Sebastian Ramacher's avatar
Sebastian Ramacher committed
120 121
  g_free(output);

Sebastian Ramacher's avatar
Sebastian Ramacher committed
122
  envp = g_environ_setenv(envp, envvar, "~/xdg", TRUE);
Sebastian Ramacher's avatar
Sebastian Ramacher committed
123

Moritz Lipp's avatar
Moritz Lipp committed
124 125
  result = g_spawn_sync(NULL, argv, envp, G_SPAWN_STDERR_TO_DEV_NULL, NULL, NULL, &output, NULL, NULL, NULL);
  g_assert(result);
Sebastian Ramacher's avatar
Sebastian Ramacher committed
126
  g_assert(output);
127
  fail_unless(g_strcmp0(output, "~/xdg") == 0, "Output is not the same (got: %s, expected: %s)",
128
      output, "~/xdg", NULL);
Sebastian Ramacher's avatar
Sebastian Ramacher committed
129

Sebastian Ramacher's avatar
Sebastian Ramacher committed
130
  envp = g_environ_setenv(envp, envvar, "/home/test/xdg", TRUE);
Sebastian Ramacher's avatar
Sebastian Ramacher committed
131

Moritz Lipp's avatar
Moritz Lipp committed
132 133
  result= g_spawn_sync(NULL, argv, envp, G_SPAWN_STDERR_TO_DEV_NULL, NULL, NULL, &output, NULL, NULL, NULL);
  g_assert(result);
Sebastian Ramacher's avatar
Sebastian Ramacher committed
134
  g_assert(output);
135
  fail_unless(g_strcmp0(output, "/home/test/xdg") == 0, "Output is not the same (got: %s, expected: %s)",
136
      output, "/home/test/xdg", NULL);
Sebastian Ramacher's avatar
Sebastian Ramacher committed
137

138
  g_free(argv[0]);
Sebastian Ramacher's avatar
Sebastian Ramacher committed
139
  g_free(argv[1]);
Sebastian Ramacher's avatar
Sebastian Ramacher committed
140
  g_strfreev(envp);
Sebastian Ramacher's avatar
Sebastian Ramacher committed
141 142
}

143
START_TEST(test_xdg_path) {
Moritz Lipp's avatar
Moritz Lipp committed
144 145
  xdg_path_impl(XDG_CONFIG,      "XDG_CONFIG_HOME", g_get_user_config_dir());
  xdg_path_impl(XDG_DATA,        "XDG_DATA_HOME",   g_get_user_data_dir());
Sebastian Ramacher's avatar
Sebastian Ramacher committed
146
  xdg_path_impl(XDG_CONFIG_DIRS, "XDG_CONFIG_DIRS", "/etc/xdg");
Moritz Lipp's avatar
Moritz Lipp committed
147
  xdg_path_impl(XDG_DATA_DIRS,   "XDG_DATA_DIRS",   "/usr/local/share/:/usr/share");
Sebastian Ramacher's avatar
Sebastian Ramacher committed
148
  xdg_path_impl(XDG_CACHE,       "XDG_CACHE_HOME",  g_get_user_cache_dir());
149
} END_TEST
Sebastian Ramacher's avatar
Sebastian Ramacher committed
150

151
START_TEST(test_file_invariants) {
152 153 154
  fail_unless(girara_file_open(NULL, NULL) == NULL, NULL);
  fail_unless(girara_file_open("somefile", NULL) == NULL, NULL);
  fail_unless(girara_file_open(NULL, "r") == NULL, NULL);
Sebastian Ramacher's avatar
Sebastian Ramacher committed
155

156 157
  fail_unless(girara_file_read_line(NULL) == NULL, NULL);
  fail_unless(girara_file_read(NULL) == NULL, NULL);
158
} END_TEST
Sebastian Ramacher's avatar
Sebastian Ramacher committed
159

160
START_TEST(test_file_read) {
Sebastian Ramacher's avatar
Sebastian Ramacher committed
161 162 163 164 165 166
  static const char CONTENT[] = "test1\ntest2\ntest3";
  static const char* LINES[] = { "test1", "test2", "test3" };
  static size_t NUMLINES = 3;

  gchar* path = NULL;
  int fd = g_file_open_tmp("girara.test.XXXXXX", &path, NULL);
167 168
  fail_unless(fd != -1, "Failed to open temporary file.", NULL);
  fail_unless(g_strcmp0(path, "") != 0, "Failed to open temporary file.", NULL);
Sebastian Ramacher's avatar
Sebastian Ramacher committed
169

170 171
  GError* error = NULL;
  if (g_file_set_contents(path, CONTENT, -1, &error) == FALSE) {
172
    fail_unless(false, "Couldn't set content: %s", error->message, NULL);
173 174
    g_error_free(error);
  }
Sebastian Ramacher's avatar
Sebastian Ramacher committed
175 176

  char* content = girara_file_read(path);
177
  fail_unless(g_strcmp0(content, CONTENT) == 0, "Reading file failed", NULL);
Sebastian Ramacher's avatar
Sebastian Ramacher committed
178 179 180
  free(content);

  FILE* file = girara_file_open(path, "r");
181
  fail_unless(file != NULL, NULL);
Sebastian Ramacher's avatar
Sebastian Ramacher committed
182 183
  for (size_t i = 0; i != NUMLINES; ++i) {
    char* line = girara_file_read_line(file);
184
    fail_unless(g_strcmp0(line, LINES[i]) == 0, "Line doesn't match (got: %s, expected: %s)",
185
        line, LINES[i], NULL);
186
    g_free(line);
Sebastian Ramacher's avatar
Sebastian Ramacher committed
187 188 189
  }
  fclose(file);

190
  close(fd);
191
  fail_unless(g_remove(path) == 0, "Failed to remove temporary file.", NULL);
Sebastian Ramacher's avatar
Sebastian Ramacher committed
192
  g_free(path);
193
} END_TEST
194

195
START_TEST(test_safe_realloc) {
196
  fail_unless(girara_safe_realloc(NULL, 0u) == NULL, NULL);
197 198

  void* ptr = NULL;
199 200 201 202 203 204
  fail_unless(girara_safe_realloc(&ptr, sizeof(int)) != NULL, NULL);
  fail_unless(ptr != NULL, NULL);
  fail_unless(girara_safe_realloc(&ptr, 1024*sizeof(int)) != NULL, NULL);
  fail_unless(ptr != NULL, NULL);
  fail_unless(girara_safe_realloc(&ptr, 0u) == NULL, NULL);
  fail_unless(ptr == NULL, NULL);
205 206
} END_TEST

207
START_TEST(test_split_path) {
208 209
  fail_unless(girara_split_path_array(NULL) == NULL, NULL);
  fail_unless(girara_split_path_array("") == NULL, NULL);
210 211

  girara_list_t* res = girara_split_path_array("one/path");
212 213 214
  fail_unless(res != NULL, NULL);
  fail_unless(girara_list_size(res) == 1, NULL);
  fail_unless(g_strcmp0(girara_list_nth(res, 0), "one/path") == 0, NULL);
215 216 217
  girara_list_free(res);

  res = girara_split_path_array("first/path:second/path");
218 219 220 221
  fail_unless(res != NULL, NULL);
  fail_unless(girara_list_size(res) == 2, NULL);
  fail_unless(g_strcmp0(girara_list_nth(res, 0), "first/path") == 0, NULL);
  fail_unless(g_strcmp0(girara_list_nth(res, 1), "second/path") == 0, NULL);
222 223 224
  girara_list_free(res);
} END_TEST

225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258
START_TEST(test_strings_replace_substrings_invalid) {
  fail_unless(girara_replace_substring(NULL, NULL, NULL) == NULL);
  fail_unless(girara_replace_substring("", NULL, NULL) == NULL);
  fail_unless(girara_replace_substring("", "", NULL) == NULL);
} END_TEST

START_TEST(test_strings_replace_substrings_nothing_to_replace) {
  char* result = girara_replace_substring("test", "n", "y");
  fail_unless(result != NULL);
  fail_unless(strncmp(result, "test", 5) == 0);
  g_free(result);
} END_TEST

START_TEST(test_strings_replace_substrings_1) {
  char* result = girara_replace_substring("test", "e", "f");
  fail_unless(result != NULL);
  fail_unless(strncmp(result, "tfst", 5) == 0);
  g_free(result);
} END_TEST

START_TEST(test_strings_replace_substrings_2) {
  char* result = girara_replace_substring("test", "es", "f");
  fail_unless(result != NULL);
  fail_unless(strncmp(result, "tft", 4) == 0);
  g_free(result);
} END_TEST

START_TEST(test_strings_replace_substrings_3) {
  char* result = girara_replace_substring("test", "e", "fg");
  fail_unless(result != NULL);
  fail_unless(strncmp(result, "tfgst", 6) == 0);
  g_free(result);
} END_TEST

Sebastian Ramacher's avatar
Sebastian Ramacher committed
259
static Suite* suite_utils(void)
260 261 262 263 264 265 266
{
  TCase* tcase = NULL;
  Suite* suite = suite_create("Utils");

  /* home directory */
  tcase = tcase_create("home_directory");
  tcase_add_test(tcase, test_home_directory);
267
  tcase_add_test(tcase, test_home_directory_get_HOME);
268 269 270 271
  suite_add_tcase(suite, tcase);

  /* fix path */
  tcase = tcase_create("fix_path");
272 273
  tcase_add_test(tcase, test_fix_path_basic);
  tcase_add_test(tcase, test_fix_path_extended);
274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295
  suite_add_tcase(suite, tcase);

  /* xdg path */
  tcase = tcase_create("xdg_path");
  tcase_add_test(tcase, test_xdg_path);
  suite_add_tcase(suite, tcase);

  /* file invariants */
  tcase = tcase_create("file_invariants");
  tcase_add_test(tcase, test_file_invariants);
  suite_add_tcase(suite, tcase);

  /* read file */
  tcase = tcase_create("file_read");
  tcase_add_test(tcase, test_file_read);
  suite_add_tcase(suite, tcase);

  /* safe realloc */
  tcase = tcase_create("safe_realloc");
  tcase_add_test(tcase, test_safe_realloc);
  suite_add_tcase(suite, tcase);

296 297 298 299 300
  /* split path */
  tcase = tcase_create("split_path");
  tcase_add_test(tcase, test_split_path);
  suite_add_tcase(suite, tcase);

301 302 303 304 305 306 307 308 309
  /* strings */
  tcase = tcase_create("strings");
  tcase_add_test(tcase, test_strings_replace_substrings_invalid);
  tcase_add_test(tcase, test_strings_replace_substrings_nothing_to_replace);
  tcase_add_test(tcase, test_strings_replace_substrings_1);
  tcase_add_test(tcase, test_strings_replace_substrings_2);
  tcase_add_test(tcase, test_strings_replace_substrings_3);
  suite_add_tcase(suite, tcase);

310
  return suite;
311
}
Sebastian Ramacher's avatar
Sebastian Ramacher committed
312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327

int main()
{
  Suite* suite          = NULL;
  SRunner* suite_runner = NULL;
  int number_failed     = 0;

  /* test utils */
  suite        = suite_utils();
  suite_runner = srunner_create(suite);
  srunner_run_all(suite_runner, CK_NORMAL);
  number_failed += srunner_ntests_failed(suite_runner);
  srunner_free(suite_runner);

  return (number_failed == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
}