Debian Codegraph @

ini/ini_save_ut.c

raw symbols SCIP
1 /*
2 INI LIBRARY
3
4 Unit test for the configuration object modification API.
5
6 Copyright (C) Dmitri Pal <[email protected]> 2014
7
8 INI Library is free software: you can redistribute it and/or modify
9 it under the terms of the GNU Lesser General Public License as published by
10 the Free Software Foundation, either version 3 of the License, or
11 (at your option) any later version.
12
13 INI Library is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU Lesser General Public License for more details.
17
18 You should have received a copy of the GNU Lesser General Public License
19 along with INI Library. If not, see <http://www.gnu.org/licenses/>.
20 */
21
22 #include "config.h"
23 #include <errno.h>
24 #include <stdio.h>
25 #include <string.h>
26 #include <stdlib.h>
27 #include <sys/wait.h>
28 /* #define TRACE_LEVEL 7 */
29 #define TRACE_HOME
30 #include "trace.h"
31 #include "ini_configobj.h"
32 #include "ini_config_priv.h"
33 #include "collection_tools.h"
34 #include "path_utils.h"
35
36 int verbose = 0;
37
38 #define WRAP_SIZE 80
39
40 #define INIOUT(foo) \
41 do { \
42 if (verbose) { printf("%30s(%4d): ",__FUNCTION__,__LINE__); foo; } \
43 } while(0)
44
45 typedef int (*test_fn)(void);
46
47 /* Basic test */
48 static int basic_test(void)
49 {
50 int error = EOK;
51 char srcname[PATH_MAX];
52 char resname[PATH_MAX];
53 char cmpname[PATH_MAX];
54 char command[PATH_MAX * 3];
55 struct ini_cfgfile *file_ctx = NULL;
56 char baktpl[] = "test_real_%d.conf.save";
57 char *builddir = NULL;
58 char *srcdir = NULL;
59 struct ini_cfgobj *ini_config = NULL;
60 char **error_list = NULL;
61 int i;
62 struct access_check acc = { INI_ACCESS_CHECK_MODE, /* Use only mode */
63 0, /* Ignore uid */
64 0, /* Ignore gid */
65 0770,
66 0 }; /* Mask is ignored */
67 struct access_check new_access =
68 { INI_ACCESS_CHECK_MODE, /* Use only mode */
69 0, /* Ignore uid */
70 0, /* Ignore gid */
71 0660,
72 0 }; /* Mask is ignored */
73
74
75 const char *cmp_files[] = { "real16be.conf",
76 "real16le.conf",
77 "real32le.conf",
78 "real32be.conf" };
79 enum index_utf_t bom;
80 enum index_utf_t bom_ar[] = { INDEX_UTF16BE,
81 INDEX_UTF16LE,
82 INDEX_UTF32LE,
83 INDEX_UTF32BE };
84
85 INIOUT(printf("<==== Start of basic test ====>\n"));
86
87 srcdir = getenv("srcdir");
88 builddir = getenv("builddir");
89
90 snprintf(srcname, PATH_MAX, "%s/ini/ini2.d/real8.conf",
91 (srcdir == NULL) ? "." : srcdir);
92
93 /* Create config collection */
94 error = ini_config_create(&ini_config);
95 if (error) {
96 printf("Failed to create configuration. Error %d.\n", error);
97 return error;
98 }
99
100 error = ini_config_file_open(srcname,
101 INI_META_STATS,
102 &file_ctx);
103 if (error) {
104 printf("Failed to open file %s for reading. Error %d.\n",
105 srcname, error);
106 ini_config_destroy(ini_config);
107 return error;
108 }
109
110 error = ini_config_parse(file_ctx,
111 INI_STOP_ON_NONE,
112 0,
113 0,
114 ini_config);
115 if (error) {
116 INIOUT(printf("Failed to parse configuration. Error %d.\n", error));
117
118 if (ini_config_error_count(ini_config)) {
119 INIOUT(printf("Errors detected while parsing: %s\n",
120 ini_config_get_filename(file_ctx)));
121 ini_config_get_errors(ini_config, &error_list);
122 INIOUT(ini_config_print_errors(stdout, error_list));
123 ini_config_free_errors(error_list);
124 ini_config_file_destroy(file_ctx);
125 ini_config_destroy(ini_config);
126 return error;
127 }
128 }
129
130 bom = ini_config_get_bom(file_ctx);
131 INIOUT(printf("BOM %d\n", bom));
132
133 for (i = 0; i < 4; i++) {
134
135 INIOUT(printf("Processing file %s\n", cmp_files[i]));
136
137 /* Create backup */
138 error = ini_config_file_backup(file_ctx,
139 (builddir == NULL) ? "." : builddir,
140 baktpl,
141 &acc,
142 1000);
143 if (error) {
144 printf("Failed to create backup file. Error %d.\n", error);
145 ini_config_file_destroy(file_ctx);
146 ini_config_destroy(ini_config);
147 return error;
148 }
149
150 /* Set a new bom */
151 error = ini_config_set_bom(file_ctx, bom_ar[i]);
152 if (error) {
153 printf("Failed to set bom. Error %d.\n", error);
154 ini_config_file_destroy(file_ctx);
155 ini_config_destroy(ini_config);
156 return error;
157 }
158
159 /* Save as a different file */
160 INIOUT(printf("Saving as file %s\n", cmp_files[i]));
161
162 snprintf(resname, PATH_MAX, "%s/test_%s",
163 (builddir == NULL) ? "." : builddir,
164 cmp_files[i]);
165
166 error = ini_config_save_as(file_ctx,
167 resname,
168 &acc,
169 ini_config);
170 if (error) {
171 printf("Failed to save file as %s. Error %d.\n", resname, error);
172 ini_config_file_destroy(file_ctx);
173 ini_config_destroy(ini_config);
174 return error;
175 }
176
177 /* Do comparison of the original file with the created one */
178 INIOUT(printf("Comparing file %s\n", cmp_files[i]));
179
180 snprintf(cmpname, PATH_MAX, "%s/ini/ini2.d/%s",
181 (srcdir == NULL) ? "." : srcdir,
182 cmp_files[i]);
183
184 snprintf(command, PATH_MAX * 3, "cmp -l -b %s %s", resname, cmpname);
185 error = system(command);
186 if ((error) || (WEXITSTATUS(error))) {
187 printf("Failed to compare files %d %d.\n", error,
188 WEXITSTATUS(error));
189 ini_config_file_destroy(file_ctx);
190 ini_config_destroy(ini_config);
191 return -1;
192 }
193
194 /* Change access to the saved file */
195
196 INIOUT(printf("Changing access to file %s\n", cmp_files[i]));
197
198 error = ini_config_change_access(file_ctx,
199 &new_access);
200 if (error) {
201 printf("Failed to change access for file %s. Error %d.\n",
202 resname, error);
203 ini_config_file_destroy(file_ctx);
204 ini_config_destroy(ini_config);
205 return error;
206 }
207
208 /* Check that access is as expected */
209 INIOUT(printf("Check access to the file %s\n", cmp_files[i]));
210
211 error = ini_config_access_check(file_ctx,
212 INI_ACCESS_CHECK_MODE,
213 0,
214 0,
215 0660,
216 0);
217 if (error) {
218 printf("Failed to check access %s. Error %d.\n", resname, error);
219 ini_config_file_destroy(file_ctx);
220 ini_config_destroy(ini_config);
221 return error;
222 }
223 }
224
225 ini_config_file_destroy(file_ctx);
226 ini_config_destroy(ini_config);
227
228 INIOUT(printf("<==== END ====>\n"));
229 return 0;
230 }
231
232
233 int main(int argc, char *argv[])
234 {
235 int error = EOK;
236 test_fn tests[] = { basic_test,
237 NULL };
238 test_fn t;
239 int i = 0;
240 char *var;
241
242 if ((argc > 1) && (strcmp(argv[1], "-v") == 0)) verbose = 1;
243 else {
244 var = getenv("COMMON_TEST_VERBOSE");
245 if (var) verbose = 1;
246 }
247
248 INIOUT(printf("Start\n"));
249
250 while ((t = tests[i++])) {
251 error = t();
252 if (error) {
253 printf("Failed with error %d!\n", error);
254 return error;
255 }
256 }
257
258 INIOUT(printf("Success!\n"));
259 return 0;
260 }