• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * conf-unittest.c - config parser unit tests
3  * Copyright (c) 2012 The Chromium OS Authors. All rights reserved.
4  * Use of this source code is governed by a BSD-style license that can be
5  * found in the LICENSE file.
6  */
7 
8 #include "config.h"
9 
10 #include <errno.h>
11 #include <stdio.h>
12 #include <stdlib.h>
13 #include <string.h>
14 
15 #include "src/conf.h"
16 #include "src/test_harness.h"
17 
18 #ifndef HAVE_FMEMOPEN
19 #include "src/common/fmemopen.h"
20 #endif
21 
fopenstr(const char * str)22 FILE *fopenstr (const char *str)
23 {
24   /* strlen(str) instead of strlen(str) + 1 because files shouldn't appear
25    * null-terminated. Cast away constness because we're in read mode, but the
26    * fmemopen prototype has no way to express that. */
27   return fmemopen ( (char *) str, strlen (str), "r");
28 }
29 
TEST(parse_empty)30 TEST (parse_empty)
31 {
32   /* can't do a truly empty file - fmemopen() combusts */
33   FILE *f = fopenstr ("\n");
34   ASSERT_NE (NULL, f);
35   struct conf_entry *e = conf_parse (f);
36   EXPECT_NULL (e);
37   conf_free (e);
38 }
39 
TEST(parse_basic)40 TEST (parse_basic)
41 {
42   FILE *f = fopenstr ("foo bar\nbaz quxx\n");
43   ASSERT_NE (NULL, f);
44   struct conf_entry *e = conf_parse (f);
45   ASSERT_NE (NULL, e);
46   EXPECT_STREQ (e->key, "foo");
47   EXPECT_STREQ (e->value, "bar");
48   ASSERT_NE (NULL, e->next);
49   EXPECT_STREQ (e->next->key, "baz");
50   EXPECT_STREQ (e->next->value, "quxx");
51   ASSERT_NULL (e->next->next);
52   conf_free (e);
53 }
54 
TEST(parse_novalue)55 TEST (parse_novalue)
56 {
57   FILE *f = fopenstr ("abcdef\n");
58   ASSERT_NE (NULL, f);
59   struct conf_entry *e = conf_parse (f);
60   ASSERT_NE (NULL, e);
61   EXPECT_STREQ (e->key, "abcdef");
62   EXPECT_NULL (e->value);
63   EXPECT_NULL (e->next);
64   conf_free (e);
65 }
66 
TEST(parse_whitespace)67 TEST (parse_whitespace)
68 {
69   FILE *f = fopenstr ("         fribble		  grotz  \n");
70   ASSERT_NE (NULL, f);
71   struct conf_entry *e = conf_parse (f);
72   ASSERT_NE (NULL, e);
73   EXPECT_STREQ (e->key, "fribble");
74   EXPECT_STREQ (e->value, "grotz  ");
75   EXPECT_NULL (e->next);
76   conf_free (e);
77 }
78 
TEST(parse_comment)79 TEST (parse_comment)
80 {
81   FILE *f = fopenstr ("#foo bar\nbaz quxx\n");
82   ASSERT_NE (NULL, f);
83   struct conf_entry *e = conf_parse (f);
84   ASSERT_NE (NULL, e);
85   EXPECT_STREQ (e->key, "baz");
86   EXPECT_STREQ (e->value, "quxx");
87   EXPECT_NULL (e->next);
88   conf_free (e);
89 }
90 
91 TEST_HARNESS_MAIN
92