]> pilppa.org Git - libplp.git/blob - src_test/test_config1.c
api fixes and cleanups
[libplp.git] / src_test / test_config1.c
1 /*
2  * test_w1.cc
3  *
4  *  Created on: Oct 20, 2010
5  *      Author: lamikr
6  */
7 #include <unistd.h>
8 #include <stdio.h>
9 #include <stdlib.h>
10 #include <string.h>
11 #include "../src/config.h"
12
13 #define CONF_FILENAME   "dev_cfg_txt"
14 #define SECTION_TYPE    "mysection_type"
15 #define SECTION_TYPE2   "mysection_type2"
16 #define SECTION_NAME    "mysection_name"
17 #define SECTION_NAME2   "mysection_name2"
18
19 #define KEY_NAME        "key_name"
20 #define KEY_NAME2       "key_name2"
21 #define KEY_NAME3       "key_name3"
22 #define KEY_NAME_INVALID_READ   "key_name_invalid"
23
24 #define VALUE1          "val1"
25 #define VALUE2          "val2"
26 #define VALUE3          "val3"
27 #define VALUE4          "val4"
28
29 int test_config() {
30         char work_dir[FILENAME_MAX];
31         char *val;
32
33         getcwd(work_dir, sizeof(work_dir));
34         printf("working directory: %s\n", work_dir);
35
36         set_config_value_and_save(work_dir,
37                         CONF_FILENAME,
38                         SECTION_TYPE,
39                         SECTION_NAME,
40                         KEY_NAME,
41                         VALUE1);
42         val     = get_config_value_and_close(work_dir,
43                                         CONF_FILENAME,
44                                         SECTION_NAME,
45                                         KEY_NAME);
46         if ((val != NULL) &&
47             (strcmp(val, VALUE1) == 0)) {
48                 printf("value 1 read ok: %s\n", val);
49                 free(val);
50         }
51         else {
52                 printf("failed to read value\n");
53                 return 1;
54         }
55
56         val     = get_config_value_and_close(work_dir,
57                                                 CONF_FILENAME,
58                                                 SECTION_NAME,
59                                                 KEY_NAME_INVALID_READ);
60         if ((val != NULL) &&
61             (strcmp(val, VALUE1) == 0)) {
62                 printf("pl, should not be possible to read invalid key value.\n");
63                 return 0;
64         }
65         else {
66                 printf("error, value was supposed to be NULL, but is: %s\n", val);
67         }
68         set_config_value_and_save(work_dir,
69                         CONF_FILENAME,
70                         SECTION_TYPE,
71                         SECTION_NAME,
72                         KEY_NAME,
73                         VALUE2);
74         set_config_value_and_save(work_dir,
75                         CONF_FILENAME,
76                         SECTION_TYPE,
77                         SECTION_NAME,
78                         KEY_NAME2,
79                         VALUE3);
80         set_config_value_and_save(work_dir,
81                         CONF_FILENAME,
82                         SECTION_TYPE2,
83                         SECTION_NAME2,
84                         KEY_NAME3,
85                         VALUE4);
86         return 0;
87 }
88
89 int main(int argc, char** argv) {
90         test_config();
91         return 0;
92 }