summaryrefslogtreecommitdiffstats
path: root/winpr/libwinpr/utils/test/TestIni.c
blob: 2dd24f0e7bb589fb127661dc5c75de9b92e51bd6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
#include <winpr/crt.h>
#include <winpr/ini.h>

static const char TEST_INI_01[] = "; This is a sample .ini config file\n"
                                  "\n"
                                  "[first_section]\n"
                                  "one = 1\n"
                                  "five = 5\n"
                                  "animal = BIRD\n"
                                  "\n"
                                  "[second_section]\n"
                                  "path = \"/usr/local/bin\"\n"
                                  "URL = \"http://www.example.com/~username\"\n"
                                  "\n";

static const char TEST_INI_02[] = "[FreeRDS]\n"
                                  "prefix=\"/usr/local\"\n"
                                  "bindir=\"bin\"\n"
                                  "sbindir=\"sbin\"\n"
                                  "libdir=\"lib\"\n"
                                  "datarootdir=\"share\"\n"
                                  "localstatedir=\"var\"\n"
                                  "sysconfdir=\"etc\"\n"
                                  "\n";

static const char TEST_INI_03[] = "[FreeRDS]\n"
                                  "prefix=\"/usr/local\"\n"
                                  "bindir=\"bin\"\n"
                                  "# some illegal string\n"
                                  "sbindir=\"sbin\"\n"
                                  "libdir=\"lib\"\n"
                                  "invalid key-value pair\n"
                                  "datarootdir=\"share\"\n"
                                  "localstatedir=\"var\"\n"
                                  "sysconfdir=\"etc\"\n"
                                  "\n";

int TestIni(int argc, char* argv[])
{
	int rc = -1;
	size_t nKeys = 0;
	size_t nSections = 0;
	UINT32 iValue = 0;
	wIniFile* ini = NULL;
	const char* sValue = NULL;
	char** keyNames = NULL;
	char** sectionNames = NULL;

	WINPR_UNUSED(argc);
	WINPR_UNUSED(argv);

	/* First Sample */
	ini = IniFile_New();
	if (!ini)
		goto fail;

	if (IniFile_ReadBuffer(ini, TEST_INI_01) < 0)
		goto fail;

	free(sectionNames);
	sectionNames = IniFile_GetSectionNames(ini, &nSections);
	if (!sectionNames && (nSections > 0))
		goto fail;

	for (size_t i = 0; i < nSections; i++)
	{
		free(keyNames);
		keyNames = IniFile_GetSectionKeyNames(ini, sectionNames[i], &nKeys);
		printf("[%s]\n", sectionNames[i]);
		if (!keyNames && (nKeys > 0))
			goto fail;
		for (size_t j = 0; j < nKeys; j++)
		{
			sValue = IniFile_GetKeyValueString(ini, sectionNames[i], keyNames[j]);
			printf("%s = %s\n", keyNames[j], sValue);
		}
	}

	iValue = IniFile_GetKeyValueInt(ini, "first_section", "one");

	if (iValue != 1)
	{
		printf("IniFile_GetKeyValueInt failure\n");
		goto fail;
	}

	iValue = IniFile_GetKeyValueInt(ini, "first_section", "five");

	if (iValue != 5)
	{
		printf("IniFile_GetKeyValueInt failure\n");
		goto fail;
	}

	sValue = IniFile_GetKeyValueString(ini, "first_section", "animal");

	if (strcmp(sValue, "BIRD") != 0)
	{
		printf("IniFile_GetKeyValueString failure\n");
		goto fail;
	}

	sValue = IniFile_GetKeyValueString(ini, "second_section", "path");

	if (strcmp(sValue, "/usr/local/bin") != 0)
	{
		printf("IniFile_GetKeyValueString failure\n");
		goto fail;
	}

	sValue = IniFile_GetKeyValueString(ini, "second_section", "URL");

	if (strcmp(sValue, "http://www.example.com/~username") != 0)
	{
		printf("IniFile_GetKeyValueString failure\n");
		goto fail;
	}

	IniFile_Free(ini);
	/* Second Sample */
	ini = IniFile_New();
	if (!ini)
		goto fail;
	if (IniFile_ReadBuffer(ini, TEST_INI_02) < 0)
		goto fail;
	free(sectionNames);
	sectionNames = IniFile_GetSectionNames(ini, &nSections);
	if (!sectionNames && (nSections > 0))
		goto fail;

	for (size_t i = 0; i < nSections; i++)
	{
		free(keyNames);
		keyNames = IniFile_GetSectionKeyNames(ini, sectionNames[i], &nKeys);
		printf("[%s]\n", sectionNames[i]);

		if (!keyNames && (nKeys > 0))
			goto fail;
		for (size_t j = 0; j < nKeys; j++)
		{
			sValue = IniFile_GetKeyValueString(ini, sectionNames[i], keyNames[j]);
			printf("%s = %s\n", keyNames[j], sValue);
		}
	}

	IniFile_Free(ini);
	/* Third sample - invalid input */
	ini = IniFile_New();

	if (IniFile_ReadBuffer(ini, TEST_INI_03) != -1)
		goto fail;

	rc = 0;
fail:
	free(keyNames);
	free(sectionNames);
	IniFile_Free(ini);
	return rc;
}