1ddf861d1STrond Norbye/* -*- Mode: C; tab-width: 4; c-basic-offset: 4; indent-tabs-mode: nil -*- */
2ddf861d1STrond Norbye#include "config.h"
3ddf861d1STrond Norbye#include <cJSON.h>
4ddf861d1STrond Norbye#include <errno.h>
5ddf861d1STrond Norbye#include <string.h>
63a98d254STrond Norbye#include <stdio.h>
7ddf861d1STrond Norbye#include <sys/stat.h>
8ddf861d1STrond Norbye
9ddf861d1STrond Norbye#include "daemon/config_util.h"
10ddf861d1STrond Norbye
11ddf861d1STrond Norbye#ifdef WIN32
12ddf861d1STrond Norbyestatic void setenv(const char *key, const char *value, int overwrite)
13ddf861d1STrond Norbye{
14f3bdecf3STrond Norbye    char obj[1024];
15f3bdecf3STrond Norbye    sprintf(obj, "%s=%s", key, value);
16f3bdecf3STrond Norbye    _putenv(_strdup(obj));
17ddf861d1STrond Norbye}
18ddf861d1STrond Norbye
19ddf861d1STrond Norbyestatic void unsetenv(const char *key)
20ddf861d1STrond Norbye{
21f3bdecf3STrond Norbye    char obj[1024];
22f3bdecf3STrond Norbye    sprintf(obj, "%s=", key);
23f3bdecf3STrond Norbye    _putenv(_strdup(obj));
24ddf861d1STrond Norbye}
25ddf861d1STrond Norbye#endif
26ddf861d1STrond Norbye
27ddf861d1STrond Norbyestatic void einval(void)
28ddf861d1STrond Norbye{
29ddf861d1STrond Norbye   char buff[1];
30ddf861d1STrond Norbye   char *fnm = buff;
31ddf861d1STrond Norbye   cJSON *json;
32464d6f0bSTrond Norbye   cb_assert(config_load_file(NULL, NULL) == CONFIG_INVALID_ARGUMENTS);
33464d6f0bSTrond Norbye   cb_assert(config_load_file(fnm, NULL) == CONFIG_INVALID_ARGUMENTS);
34464d6f0bSTrond Norbye   cb_assert(config_load_file(NULL, &json) == CONFIG_INVALID_ARGUMENTS);
35ddf861d1STrond Norbye}
36ddf861d1STrond Norbye
37ddf861d1STrond Norbyestatic void no_such_file(void)
38ddf861d1STrond Norbye{
39ddf861d1STrond Norbye   cJSON *ptr;
40ddf861d1STrond Norbye   config_error_t err = config_load_file("/it/would/suck/if/this/file/exists",
41ddf861d1STrond Norbye                                         &ptr);
42464d6f0bSTrond Norbye   cb_assert(err == CONFIG_NO_SUCH_FILE);
43ddf861d1STrond Norbye}
44ddf861d1STrond Norbye
45ddf861d1STrond Norbyestatic void open_failed(void)
46ddf861d1STrond Norbye{
47ddf861d1STrond Norbye#ifndef WIN32
48ddf861d1STrond Norbye   /* @todo figure out how do chmod a file on windows ;) */
49ddf861d1STrond Norbye   cJSON *ptr;
50ddf861d1STrond Norbye   config_error_t err;
51ddf861d1STrond Norbye   FILE *fp = fopen("config_test", "w");
52ddf861d1STrond Norbye
53ddf861d1STrond Norbye   if (fp == NULL) {
54ddf861d1STrond Norbye      fprintf(stderr, "Failed to create file \"config_test\": %s\n",
55ddf861d1STrond Norbye              strerror(errno));
56ddf861d1STrond Norbye      exit(EXIT_FAILURE);
57ddf861d1STrond Norbye   }
58ddf861d1STrond Norbye
59ddf861d1STrond Norbye   fclose(fp);
60ddf861d1STrond Norbye   chmod("config_test", S_IWUSR);
61ddf861d1STrond Norbye
62ddf861d1STrond Norbye   err = config_load_file("config_test", &ptr);
63ddf861d1STrond Norbye   remove("config_test");
64464d6f0bSTrond Norbye   cb_assert(err == CONFIG_OPEN_FAILED);
65ddf861d1STrond Norbye#endif
66ddf861d1STrond Norbye}
67ddf861d1STrond Norbye
68ddf861d1STrond Norbyestatic void malloc_failed(void)
69ddf861d1STrond Norbye{
70ddf861d1STrond Norbye   cJSON *ptr;
71ddf861d1STrond Norbye   config_error_t err;
72ddf861d1STrond Norbye   FILE *fp = fopen("config_test", "w");
73ddf861d1STrond Norbye
74ddf861d1STrond Norbye   if (fp == NULL) {
75ddf861d1STrond Norbye      fprintf(stderr, "Failed to create file \"config_test\": %s\n",
76ddf861d1STrond Norbye              strerror(errno));
77ddf861d1STrond Norbye      exit(EXIT_FAILURE);
78ddf861d1STrond Norbye   }
79ddf861d1STrond Norbye
80ddf861d1STrond Norbye   fclose(fp);
81ddf861d1STrond Norbye
82ddf861d1STrond Norbye   setenv("CONFIG_TEST_MOCK_MALLOC_FAILURE", "on", 1);
83ddf861d1STrond Norbye   err = config_load_file("config_test", &ptr);
84ddf861d1STrond Norbye   unsetenv("CONFIG_TEST_MOCK_MALLOC_FAILURE");
85ddf861d1STrond Norbye
86ddf861d1STrond Norbye   remove("config_test");
87ddf861d1STrond Norbye
88464d6f0bSTrond Norbye   cb_assert(err == CONFIG_MALLOC_FAILED);
89ddf861d1STrond Norbye}
90ddf861d1STrond Norbye
91ddf861d1STrond Norbyestatic void io_error(void)
92ddf861d1STrond Norbye{
93ddf861d1STrond Norbye   cJSON *ptr;
94ddf861d1STrond Norbye   config_error_t err;
95ddf861d1STrond Norbye   FILE *fp = fopen("config_test", "w");
96ddf861d1STrond Norbye
97ddf861d1STrond Norbye   if (fp == NULL) {
98ddf861d1STrond Norbye      fprintf(stderr, "Failed to create file \"config_test\": %s\n",
99ddf861d1STrond Norbye              strerror(errno));
100ddf861d1STrond Norbye      exit(EXIT_FAILURE);
101ddf861d1STrond Norbye   }
102ddf861d1STrond Norbye
103ddf861d1STrond Norbye   fclose(fp);
104ddf861d1STrond Norbye
105ddf861d1STrond Norbye   setenv("CONFIG_TEST_MOCK_SPOOL_FAILURE", "on", 1);
106ddf861d1STrond Norbye   err = config_load_file("config_test", &ptr);
107ddf861d1STrond Norbye   unsetenv("CONFIG_TEST_MOCK_SPOOL_FAILURE");
108ddf861d1STrond Norbye
109ddf861d1STrond Norbye   remove("config_test");
110ddf861d1STrond Norbye
111464d6f0bSTrond Norbye   cb_assert(err == CONFIG_IO_ERROR);
112ddf861d1STrond Norbye}
113ddf861d1STrond Norbye
114ddf861d1STrond Norbyestatic void parse_error(void)
115ddf861d1STrond Norbye{
116ddf861d1STrond Norbye   cJSON *ptr;
117ddf861d1STrond Norbye   config_error_t err;
118ddf861d1STrond Norbye   FILE *fp = fopen("config_test", "w");
119ddf861d1STrond Norbye
120ddf861d1STrond Norbye   if (fp == NULL) {
121ddf861d1STrond Norbye      fprintf(stderr, "Failed to create file \"config_test\": %s\n",
122ddf861d1STrond Norbye              strerror(errno));
123ddf861d1STrond Norbye      exit(EXIT_FAILURE);
124ddf861d1STrond Norbye   }
125ddf861d1STrond Norbye
126ddf861d1STrond Norbye   fprintf(fp, "{ foo : bar }");
127ddf861d1STrond Norbye   fclose(fp);
128ddf861d1STrond Norbye
129ddf861d1STrond Norbye   err = config_load_file("config_test", &ptr);
130ddf861d1STrond Norbye   remove("config_test");
131ddf861d1STrond Norbye
132464d6f0bSTrond Norbye   cb_assert(err == CONFIG_PARSE_ERROR);
133ddf861d1STrond Norbye}
134ddf861d1STrond Norbye
135ddf861d1STrond Norbyestatic void parse_success(void)
136ddf861d1STrond Norbye{
137ddf861d1STrond Norbye   cJSON *ptr = NULL;
138ddf861d1STrond Norbye   config_error_t err;
139ddf861d1STrond Norbye   FILE *fp = fopen("config_test", "w");
140ddf861d1STrond Norbye
141ddf861d1STrond Norbye   if (fp == NULL) {
142ddf861d1STrond Norbye      fprintf(stderr, "Failed to create file \"config_test\": %s\n",
143ddf861d1STrond Norbye              strerror(errno));
144ddf861d1STrond Norbye      exit(EXIT_FAILURE);
145ddf861d1STrond Norbye   }
146ddf861d1STrond Norbye
147ddf861d1STrond Norbye   fprintf(fp, "{ \"string\" : \"stringval\", \"number\" : 123 }");
148ddf861d1STrond Norbye   fclose(fp);
149ddf861d1STrond Norbye
150ddf861d1STrond Norbye   err = config_load_file("config_test", &ptr);
151ddf861d1STrond Norbye   remove("config_test");
152464d6f0bSTrond Norbye   cb_assert(ptr != NULL);
153ddf861d1STrond Norbye   cJSON_Delete(ptr);
154464d6f0bSTrond Norbye   cb_assert(err == CONFIG_SUCCESS);
155ddf861d1STrond Norbye}
156ddf861d1STrond Norbye
157ddf861d1STrond Norbyestatic void test_config_strerror(void)
158ddf861d1STrond Norbye{
159ddf861d1STrond Norbye    int ii;
160ddf861d1STrond Norbye    errno = ENOENT;
161ddf861d1STrond Norbye    for (ii = 0; ii < 1000; ++ii) {
162ddf861d1STrond Norbye        char *msg = config_strerror("foo", (config_error_t)ii);
163464d6f0bSTrond Norbye        cb_assert(msg != NULL);
164ddf861d1STrond Norbye        free(msg);
165ddf861d1STrond Norbye    }
166ddf861d1STrond Norbye}
167ddf861d1STrond Norbye
168ddf861d1STrond Norbyeint main(void)
169ddf861d1STrond Norbye{
170ddf861d1STrond Norbye   einval();
171ddf861d1STrond Norbye   no_such_file();
172ddf861d1STrond Norbye   open_failed();
173ddf861d1STrond Norbye   malloc_failed();
174ddf861d1STrond Norbye   io_error();
175ddf861d1STrond Norbye   parse_error();
176ddf861d1STrond Norbye   parse_success();
177ddf861d1STrond Norbye
178ddf861d1STrond Norbye   test_config_strerror();
179ddf861d1STrond Norbye
180ddf861d1STrond Norbye   return 0;
181ddf861d1STrond Norbye}
182