~ubuntu-branches/ubuntu/intrepid/upstart/intrepid

« back to all changes in this revision

Viewing changes to nih/tests/test_config.c

  • Committer: Bazaar Package Importer
  • Author(s): Scott James Remnant
  • Date: 2007-10-28 10:51:59 UTC
  • mfrom: (1.1.10 upstream) (16.1.1 experimental)
  • Revision ID: james.westby@ubuntu.com-20071028105159-x9pypymnb3kigxo7
Tags: 0.3.9-1
* New upstream release:
  - many bug fixes.

* Update reference to "edgy" in README.Debian to "hardy".  LP: #140037.

Show diffs side-by-side

added added

removed removed

Lines of Context:
102
102
test_token (void)
103
103
{
104
104
        char      buf[1024], dest[1024];
105
 
        size_t    pos, lineno;
106
 
        ssize_t   ret;
 
105
        size_t    pos, lineno, len;
 
106
        int       ret;
107
107
        NihError *err;
108
108
 
109
109
        TEST_FUNCTION ("nih_config_token");
117
117
        strcpy (buf, "this is a test");
118
118
        pos = 0;
119
119
 
 
120
        len = 0;
120
121
        ret = nih_config_token (buf, strlen (buf), &pos, NULL,
121
 
                                NULL, " ", FALSE);
 
122
                                NULL, " ", FALSE, &len);
122
123
 
123
 
        TEST_EQ (ret, 4);
 
124
        TEST_EQ (ret, 0);
 
125
        TEST_EQ (len, 4);
124
126
        TEST_EQ (pos, 4);
125
127
 
126
128
 
130
132
        TEST_FEATURE ("with token filling string");
131
133
        strcpy (buf, "wibble");
132
134
        pos = 0;
 
135
        len = 0;
133
136
        ret = nih_config_token (buf, strlen (buf), &pos, NULL,
134
 
                                NULL, " ", FALSE);
 
137
                                NULL, " ", FALSE, &len);
135
138
 
136
 
        TEST_EQ (ret, 6);
 
139
        TEST_EQ (ret, 0);
 
140
        TEST_EQ (len, 6);
137
141
        TEST_EQ (pos, 6);
138
142
 
139
143
 
143
147
        TEST_FEATURE ("with token to extract");
144
148
        strcpy (buf, "this is a test");
145
149
        ret = nih_config_token (buf, strlen (buf), NULL, NULL,
146
 
                                dest, " ", FALSE);
 
150
                                dest, " ", FALSE, NULL);
147
151
 
148
 
        TEST_EQ (ret, 4);
 
152
        TEST_EQ (ret, 0);
149
153
        TEST_EQ_STR (dest, "this");
150
154
 
151
155
 
154
158
         */
155
159
        TEST_FEATURE ("with token inside string");
156
160
        pos = 5;
 
161
        len = 0;
157
162
        ret = nih_config_token (buf, strlen (buf), &pos, NULL,
158
 
                                NULL, " ", FALSE);
 
163
                                NULL, " ", FALSE, &len);
159
164
 
160
 
        TEST_EQ (ret, 2);
 
165
        TEST_EQ (ret, 0);
 
166
        TEST_EQ (len, 2);
161
167
        TEST_EQ (pos, 7);
162
168
 
163
169
 
168
174
        TEST_FEATURE ("with double quotes inside token");
169
175
        strcpy (buf, "\"this is a\" test");
170
176
        pos = 0;
 
177
        len = 0;
171
178
        ret = nih_config_token (buf, strlen (buf), &pos, NULL,
172
 
                                NULL, " ", FALSE);
 
179
                                NULL, " ", FALSE, &len);
173
180
 
174
 
        TEST_EQ (ret, 11);
 
181
        TEST_EQ (ret, 0);
 
182
        TEST_EQ (len, 11);
175
183
        TEST_EQ (pos, 11);
176
184
 
177
185
 
179
187
         * quotes, we should still get those.
180
188
         */
181
189
        TEST_FEATURE ("with double quotes around token to extract");
 
190
        len = 0;
182
191
        ret = nih_config_token (buf, strlen (buf), NULL, NULL,
183
 
                                dest, " ", FALSE);
 
192
                                dest, " ", FALSE, &len);
184
193
 
185
 
        TEST_EQ (ret, 11);
 
194
        TEST_EQ (ret, 0);
 
195
        TEST_EQ (len, 11);
186
196
        TEST_EQ_STR (dest, "\"this is a\"");
187
197
 
188
198
 
191
201
         */
192
202
        TEST_FEATURE ("with double quotes and dequoting");
193
203
        pos = 0;
 
204
        len = 0;
194
205
        ret = nih_config_token (buf, strlen (buf), &pos, NULL,
195
 
                                NULL, " ", TRUE);
 
206
                                NULL, " ", TRUE, &len);
196
207
 
197
 
        TEST_EQ (ret, 9);
 
208
        TEST_EQ (ret, 0);
 
209
        TEST_EQ (len, 9);
198
210
        TEST_EQ (pos, 11);
199
211
 
200
212
 
203
215
         */
204
216
        TEST_FEATURE ("with double quotes and extract with dequoting");
205
217
        ret = nih_config_token (buf, strlen (buf), NULL, NULL,
206
 
                                dest, " ", TRUE);
 
218
                                dest, " ", TRUE, NULL);
207
219
 
208
 
        TEST_EQ (ret, 9);
 
220
        TEST_EQ (ret, 0);
209
221
        TEST_EQ_STR (dest, "this is a");
210
222
 
211
223
 
216
228
        TEST_FEATURE ("with single quotes inside token");
217
229
        strcpy (buf, "\'this is a\' test");
218
230
        pos = 0;
 
231
        len = 0;
219
232
        ret = nih_config_token (buf, strlen (buf), &pos, NULL,
220
 
                                NULL, " ", FALSE);
 
233
                                NULL, " ", FALSE, &len);
221
234
 
222
 
        TEST_EQ (ret, 11);
 
235
        TEST_EQ (ret, 0);
 
236
        TEST_EQ (len, 11);
223
237
        TEST_EQ (pos, 11);
224
238
 
225
239
 
230
244
        TEST_FEATURE ("with escaped spaces inside token");
231
245
        strcpy (buf, "this\\ is\\ a test");
232
246
        pos = 0;
 
247
        len = 0;
233
248
        ret = nih_config_token (buf, strlen (buf), &pos, NULL,
234
 
                                NULL, " ", FALSE);
 
249
                                NULL, " ", FALSE, &len);
235
250
 
236
 
        TEST_EQ (ret, 11);
 
251
        TEST_EQ (ret, 0);
 
252
        TEST_EQ (len, 11);
237
253
        TEST_EQ (pos, 11);
238
254
 
239
255
 
242
258
         */
243
259
        TEST_FEATURE ("with escaped spaces within extracted token");
244
260
        ret = nih_config_token (buf, strlen (buf), NULL, NULL,
245
 
                                dest, " ", FALSE);
 
261
                                dest, " ", FALSE, &len);
246
262
 
247
 
        TEST_EQ (ret, 11);
 
263
        TEST_EQ (ret, 0);
 
264
        TEST_EQ (len, 11);
248
265
        TEST_EQ_STR (dest, "this\\ is\\ a");
249
266
 
250
267
 
253
270
         */
254
271
        TEST_FEATURE ("with escaped spaces inside token and dequoting");
255
272
        pos = 0;
 
273
        len = 0;
256
274
        ret = nih_config_token (buf, strlen (buf), &pos, NULL,
257
 
                                NULL, " ", TRUE);
 
275
                                NULL, " ", TRUE, &len);
258
276
 
259
 
        TEST_EQ (ret, 9);
 
277
        TEST_EQ (ret, 0);
 
278
        TEST_EQ (len, 9);
260
279
        TEST_EQ (pos, 11);
261
280
 
262
281
 
264
283
         * around the delimiter, while removing them.
265
284
         */
266
285
        TEST_FEATURE ("with escaped spaces within extracted dequoted token");
 
286
        len = 0;
267
287
        ret = nih_config_token (buf, strlen (buf), NULL, NULL,
268
 
                                dest, " ", TRUE);
 
288
                                dest, " ", TRUE, &len);
269
289
 
270
 
        TEST_EQ (ret, 9);
 
290
        TEST_EQ (ret, 0);
 
291
        TEST_EQ (len, 9);
271
292
        TEST_EQ_STR (dest, "this is a");
272
293
 
273
294
 
278
299
        strcpy (buf, "\"this is \n a\" test");
279
300
        pos = 0;
280
301
        lineno = 1;
 
302
        len = 0;
281
303
        ret = nih_config_token (buf, strlen (buf), &pos, &lineno,
282
 
                                NULL, " ", FALSE);
 
304
                                NULL, " ", FALSE, &len);
283
305
 
284
 
        TEST_EQ (ret, 11);
 
306
        TEST_EQ (ret, 0);
 
307
        TEST_EQ (len, 11);
285
308
        TEST_EQ (pos, 13);
286
309
        TEST_EQ (lineno, 2);
287
310
 
290
313
         * string only returns a single space for the newline.
291
314
         */
292
315
        TEST_FEATURE ("with newline inside extracted quoted string");
 
316
        len = 0;
293
317
        ret = nih_config_token (buf, strlen (buf), NULL, NULL,
294
 
                                dest, " ", FALSE);
 
318
                                dest, " ", FALSE, &len);
295
319
 
296
 
        TEST_EQ (ret, 11);
 
320
        TEST_EQ (ret, 0);
 
321
        TEST_EQ (len, 11);
297
322
        TEST_EQ_STR (dest, "\"this is a\"");
298
323
 
299
324
 
303
328
        TEST_FEATURE ("with newline inside quoted string and lineno set");
304
329
        pos = 0;
305
330
        lineno = 1;
 
331
        len = 0;
306
332
        ret = nih_config_token (buf, strlen (buf), &pos, &lineno,
307
 
                                NULL, " ", FALSE);
 
333
                                NULL, " ", FALSE, &len);
308
334
 
309
 
        TEST_EQ (ret, 11);
 
335
        TEST_EQ (ret, 0);
 
336
        TEST_EQ (len, 11);
310
337
        TEST_EQ (pos, 13);
311
338
        TEST_EQ (lineno, 2);
312
339
 
318
345
        strcpy (buf, "this \\\n is a:test");
319
346
        pos = 0;
320
347
        lineno = 1;
 
348
        len = 0;
321
349
        ret = nih_config_token (buf, strlen (buf), &pos, &lineno,
322
 
                                NULL, ":", FALSE);
 
350
                                NULL, ":", FALSE, &len);
323
351
 
324
 
        TEST_EQ (ret, 9);
 
352
        TEST_EQ (ret, 0);
 
353
        TEST_EQ (len, 9);
325
354
        TEST_EQ (pos, 12);
326
355
        TEST_EQ (lineno, 2);
327
356
 
331
360
         */
332
361
        TEST_FEATURE ("with escaped newline inside extracted string");
333
362
        ret = nih_config_token (buf, strlen (buf), NULL, NULL,
334
 
                                dest, ":", FALSE);
 
363
                                dest, ":", FALSE, NULL);
335
364
 
336
 
        TEST_EQ (ret, 9);
 
365
        TEST_EQ (ret, 0);
337
366
        TEST_EQ_STR (dest, "this is a");
338
367
 
339
368
 
343
372
        TEST_FEATURE ("with escaped newline inside string and lineno set");
344
373
        pos = 0;
345
374
        lineno = 1;
 
375
        len = 0;
346
376
        ret = nih_config_token (buf, strlen (buf), &pos, &lineno,
347
 
                                NULL, ":", FALSE);
 
377
                                NULL, ":", FALSE, &len);
348
378
 
349
 
        TEST_EQ (ret, 9);
 
379
        TEST_EQ (ret, 0);
 
380
        TEST_EQ (len, 9);
350
381
        TEST_EQ (pos, 12);
351
382
        TEST_EQ (lineno, 2);
352
383
 
357
388
        TEST_FEATURE ("with slash at end of string");
358
389
        strcpy (buf, "wibble\\");
359
390
        pos = 0;
 
391
        len = 0;
360
392
        lineno = 1;
361
393
 
362
394
        ret = nih_config_token (buf, strlen (buf), &pos, &lineno,
363
 
                                NULL, " ", FALSE);
 
395
                                NULL, " ", FALSE, NULL);
364
396
 
365
397
        TEST_LT (ret, 0);
366
398
        TEST_EQ (pos, 7);
380
412
        lineno = 1;
381
413
 
382
414
        ret = nih_config_token (buf, strlen (buf), &pos, &lineno,
383
 
                                NULL, " ", FALSE);
 
415
                                NULL, " ", FALSE, NULL);
384
416
 
385
417
        TEST_LT (ret, 0);
386
418
        TEST_EQ (pos, 8);
397
429
        TEST_FEATURE ("with empty token");
398
430
        strcpy (buf, " wibble");
399
431
        pos = 0;
 
432
        len = 0;
400
433
        ret = nih_config_token (buf, strlen (buf), &pos, NULL,
401
 
                                NULL, " ", FALSE);
 
434
                                NULL, " ", FALSE, &len);
402
435
 
403
436
        TEST_EQ (ret, 0);
 
437
        TEST_EQ (len, 0);
404
438
        TEST_EQ (pos, 0);
405
439
}
406
440
 
2064
2098
        }
2065
2099
}
2066
2100
 
 
2101
void
 
2102
test_skip_block (void)
 
2103
{
 
2104
        char      buf[1024];
 
2105
        int       ret;
 
2106
        size_t    pos, lineno, endpos;
 
2107
        NihError *err;
 
2108
 
 
2109
        TEST_FUNCTION ("nih_config_skip_block");
 
2110
        program_name = "test";
 
2111
 
 
2112
 
 
2113
        /* Check that we can find the end of a simple block.  pos should be
 
2114
         * updated to point past the block, and the returned endpos should
 
2115
         * point at the end of the block itself.
 
2116
         */
 
2117
        TEST_FEATURE ("with simple block");
 
2118
        strcpy (buf, "this is\na test\nend foo\nblah\n");
 
2119
        pos = 0;
 
2120
 
 
2121
        ret = nih_config_skip_block (buf, strlen (buf), &pos, NULL,
 
2122
                                     "foo", &endpos);
 
2123
 
 
2124
        TEST_EQ (ret, 0);
 
2125
        TEST_EQ (pos, 23);
 
2126
        TEST_EQ (endpos, 15);
 
2127
 
 
2128
 
 
2129
        /* Check that the line number is incremented for each line that we
 
2130
         * discover in the block, including the terminating line.
 
2131
         */
 
2132
        TEST_FEATURE ("with line number set");
 
2133
        pos = 0;
 
2134
        lineno = 2;
 
2135
 
 
2136
        ret = nih_config_skip_block (buf, strlen (buf), &pos, &lineno,
 
2137
                                     "foo", &endpos);
 
2138
 
 
2139
        TEST_EQ (ret, 0);
 
2140
        TEST_EQ (pos, 23);
 
2141
        TEST_EQ (endpos, 15);
 
2142
        TEST_EQ (lineno, 5);
 
2143
 
 
2144
 
 
2145
        /* Check that we can find the end of a block that ends in a terminator
 
2146
         * with extraneous whitespace around the words.
 
2147
         */
 
2148
        TEST_FEATURE ("with whitespace in terminator");
 
2149
        strcpy (buf, "this is\na test\n  end \t foo  \nblah\n");
 
2150
        pos = 0;
 
2151
 
 
2152
        ret = nih_config_skip_block (buf, strlen (buf), &pos, NULL,
 
2153
                                     "foo", &endpos);
 
2154
 
 
2155
        TEST_EQ (ret, 0);
 
2156
        TEST_EQ (pos, 29);
 
2157
        TEST_EQ (endpos, 15);
 
2158
 
 
2159
 
 
2160
        /* Check that we can find the end of a block that ends in a
 
2161
         * terminator which is at the end of the file.
 
2162
         */
 
2163
        TEST_FEATURE ("with terminator at end of file");
 
2164
        strcpy (buf, "this is\na test\nend foo");
 
2165
        pos = 0;
 
2166
 
 
2167
        ret = nih_config_skip_block (buf, strlen (buf), &pos, NULL,
 
2168
                                     "foo", &endpos);
 
2169
 
 
2170
        TEST_EQ (ret, 0);
 
2171
        TEST_EQ (pos, 22);
 
2172
        TEST_EQ (endpos, 15);
 
2173
 
 
2174
 
 
2175
        /* Check that we can find the end of a block that ends in a
 
2176
         * terminator which has a comment following it.
 
2177
         */
 
2178
        TEST_FEATURE ("with terminator and comment");
 
2179
        strcpy (buf, "this is\na test\nend foo # comment\ntest\n");
 
2180
        pos = 0;
 
2181
 
 
2182
        ret = nih_config_skip_block (buf, strlen (buf), &pos, NULL,
 
2183
                                     "foo", &endpos);
 
2184
 
 
2185
        TEST_EQ (ret, 0);
 
2186
        TEST_EQ (pos, 33);
 
2187
        TEST_EQ (endpos, 15);
 
2188
 
 
2189
 
 
2190
        /* Check that we can find the end of a block that ends in a
 
2191
         * terminator which has a comment and then the end of file.
 
2192
         */
 
2193
        TEST_FEATURE ("with terminator and comment at end of file");
 
2194
        strcpy (buf, "this is\na test\nend foo # comment");
 
2195
        pos = 0;
 
2196
 
 
2197
        ret = nih_config_skip_block (buf, strlen (buf), &pos, NULL,
 
2198
                                     "foo", &endpos);
 
2199
 
 
2200
        TEST_EQ (ret, 0);
 
2201
        TEST_EQ (pos, 32);
 
2202
        TEST_EQ (endpos, 15);
 
2203
 
 
2204
 
 
2205
        /* Check that various bogus forms of terminator are ignored.
 
2206
         */
 
2207
        TEST_FEATURE ("with various things that aren't terminators");
 
2208
        strcpy (buf, "endfoo\nend a\nend fooish\nend foo\ntest\n");
 
2209
        pos = 0;
 
2210
 
 
2211
        ret = nih_config_skip_block (buf, strlen (buf), &pos, NULL,
 
2212
                                     "foo", &endpos);
 
2213
 
 
2214
        TEST_EQ (ret, 0);
 
2215
        TEST_EQ (pos, 32);
 
2216
        TEST_EQ (endpos, 24);
 
2217
 
 
2218
 
 
2219
        /* Check that reaching the end of the file without finding the block
 
2220
         * terminator causes an error to be raised and NULL to be returned.
 
2221
         */
 
2222
        TEST_FEATURE ("with no terminator before end of file");
 
2223
        strcpy (buf, "this is\na test\n");
 
2224
        pos = 0;
 
2225
        lineno = 2;
 
2226
 
 
2227
        ret = nih_config_skip_block (buf, strlen (buf), &pos, &lineno,
 
2228
                                     "foo", &endpos);
 
2229
 
 
2230
        TEST_LT (ret, 0);
 
2231
        TEST_EQ (pos, 15);
 
2232
        TEST_EQ (lineno, 4);
 
2233
 
 
2234
        err = nih_error_get ();
 
2235
        TEST_EQ (err->number, NIH_CONFIG_UNTERMINATED_BLOCK);
 
2236
        nih_free (err);
 
2237
}
 
2238
 
2067
2239
 
2068
2240
static int handler_called = 0;
2069
2241
static void *last_data = NULL;
2215
2387
 
2216
2388
 
2217
2389
        /* Check that finding an unknown stanza results in an error being
2218
 
         * raised at its first argument, and no handler called.
 
2390
         * raised, and no handler called.
2219
2391
         */
2220
2392
        TEST_FEATURE ("with unknown stanza");
2221
2393
        strcpy (buf, "wibble this is a test\nwibble\n");
2229
2401
 
2230
2402
        TEST_FALSE (handler_called);
2231
2403
        TEST_LT (ret, 0);
2232
 
        TEST_EQ (pos, 7);
 
2404
        TEST_EQ (pos, 0);
2233
2405
        TEST_EQ (lineno, 1);
2234
2406
 
2235
2407
        err = nih_error_get ();
2646
2818
        test_parse_args ();
2647
2819
        test_parse_command ();
2648
2820
        test_parse_block ();
 
2821
        test_skip_block ();
2649
2822
        test_parse_stanza ();
2650
2823
        test_parse_file ();
2651
2824
        test_parse ();