1 /*
2 * This file is part of Licq, an instant messaging client for UNIX.
3 * Copyright (C) 2010-2013 Licq Developers <licq-dev@googlegroups.com>
4 *
5 * Licq is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * Licq is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with Licq; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18 */
19
20 #include <licq/inifile.h>
21
22 #include <gtest/gtest.h>
23 #include <string>
24
25 using Licq::IniFile;
26 using std::list;
27 using std::string;
28
29 namespace LicqTest {
30
TEST(IniFile,rawConfigAccess)31 TEST(IniFile, rawConfigAccess)
32 {
33 IniFile ini("/tmp/testini.conf");
34
35 // Verify the default config
36 EXPECT_EQ("", ini.getRawConfiguration());
37
38 // Verify the raw load and get functions
39 string miniConfig("[Section1]\nparam=value\n");
40 ini.loadRawConfiguration(miniConfig);
41 EXPECT_EQ(miniConfig, ini.getRawConfiguration());
42
43 // Verify that missing newline at end of config is added
44 ini.loadRawConfiguration("[Section1]\nparam=0");
45 EXPECT_EQ("[Section1]\nparam=0\n", ini.getRawConfiguration());
46
47 // Verify that config is migrated from old format
48 ini.loadRawConfiguration("[Section1]\nparam = 0\n[Section2]\ndata = value\n");
49 EXPECT_EQ("[Section1]\nparam=0\n[Section2]\ndata=value\n", ini.getRawConfiguration());
50 }
51
TEST(IniFile,setSection)52 TEST(IniFile, setSection)
53 {
54 IniFile ini("/tmp/testini.conf");
55
56 // Empty config, try to set non-existing section
57 EXPECT_FALSE(ini.setSection("Missing", false));
58
59 // Empty config, try to create non-existing section
60 EXPECT_TRUE(ini.setSection("NewSection", true));
61
62 // Find created section
63 EXPECT_TRUE(ini.setSection("NewSection", false));
64
65 // Verify that section was created
66 EXPECT_EQ("[NewSection]\n", ini.getRawConfiguration());
67
68
69 // Existing configuration, find sections
70 ini.loadRawConfiguration("[FirstSection]\nval=0\n[EmptySection]\n#Comment\n[LastSection]\n");
71 EXPECT_TRUE(ini.setSection("EmptySection", false));
72 EXPECT_TRUE(ini.setSection("FirstSection", false));
73 EXPECT_TRUE(ini.setSection("LastSection", false));
74 EXPECT_FALSE(ini.setSection("Missing", false));
75
76 // Create a section, find it and make sure last sections wasn't destroyed
77 EXPECT_TRUE(ini.setSection("NewSection", true));
78 EXPECT_TRUE(ini.setSection("NewSection", false));
79 EXPECT_TRUE(ini.setSection("LastSection", false));
80
81 // Verify that section was created
82 EXPECT_EQ("[FirstSection]\nval=0\n[EmptySection]\n#Comment\n[LastSection]\n\n[NewSection]\n", ini.getRawConfiguration());
83 }
84
TEST(IniFile,getDefault)85 TEST(IniFile, getDefault)
86 {
87 IniFile ini("/tmp/testini.conf");
88
89 // Test with empty config, no section set
90
91 string strRet("");
92 EXPECT_FALSE(ini.get("missing", strRet, "default"));
93 EXPECT_EQ("default", strRet);
94
95 int signedRet(0);
96 EXPECT_FALSE(ini.get("missing", signedRet, -10));
97 EXPECT_EQ(-10, signedRet);
98
99 unsigned unsignedRet(0);
100 EXPECT_FALSE(ini.get("missing", unsignedRet, 10));
101 EXPECT_EQ(10u, unsignedRet);
102
103 bool boolRet(false);
104 EXPECT_FALSE(ini.get("missing", boolRet, true));
105 EXPECT_EQ(true, boolRet);
106
107 // Test with section set
108 ini.loadRawConfiguration("[Section1]\nparam1=1\nparam2=2\n");
109 EXPECT_TRUE(ini.setSection("Section1", false));
110
111 string strRet2("");
112 EXPECT_FALSE(ini.get("missing", strRet2, "default"));
113 EXPECT_EQ("default", strRet2);
114
115 int signedRet2(0);
116 EXPECT_FALSE(ini.get("missing", signedRet2, -10));
117 EXPECT_EQ(-10, signedRet2);
118
119 unsigned unsignedRet2(0);
120 EXPECT_FALSE(ini.get("missing", unsignedRet2, 10));
121 EXPECT_EQ(10u, unsignedRet2);
122
123 bool boolRet2(false);
124 EXPECT_FALSE(ini.get("missing", boolRet2, true));
125 EXPECT_EQ(true, boolRet2);
126 }
127
TEST(IniFile,get)128 TEST(IniFile, get)
129 {
130 IniFile ini("/tmp/testini.conf");
131
132 ini.loadRawConfiguration("[Section1]\nparam1=0\nparam2=1\n;Comment\nparam3=-10\nparam4=test\n[Section2]\nparam1=1\nparam2=0\n");
133
134 string strRet("");
135 int signedRet(0);
136 unsigned unsignedRet(0);
137 bool boolRet(false);
138
139 // Verify parsing of the different data types
140 EXPECT_TRUE(ini.setSection("Section1"));
141 EXPECT_TRUE(ini.get("param4", strRet));
142 EXPECT_EQ("test", strRet);
143 EXPECT_TRUE(ini.get("param2", unsignedRet));
144 EXPECT_EQ(1u, unsignedRet);
145 EXPECT_TRUE(ini.get("param3", signedRet));
146 EXPECT_EQ(-10, signedRet);
147 EXPECT_TRUE(ini.get("param2", boolRet));
148 EXPECT_TRUE(boolRet);
149 EXPECT_TRUE(ini.get("param1", boolRet));
150 EXPECT_FALSE(boolRet);
151
152 // Verify that same parameter in different sections aren't mixed
153 EXPECT_TRUE(ini.setSection("Section2"));
154 EXPECT_TRUE(ini.get("param1", strRet));
155 EXPECT_EQ("1", strRet);
156 EXPECT_TRUE(ini.get("param2", strRet));
157 EXPECT_EQ("0", strRet);
158 }
159
TEST(IniFile,set)160 TEST(IniFile, set)
161 {
162 IniFile ini("/tmp/testini.conf");
163
164 // Can't set a value with no section set
165 EXPECT_FALSE(ini.set("NoSection", "0"));
166
167 // Add a new parameter at the end
168 EXPECT_TRUE(ini.setSection("Section", true));
169 EXPECT_TRUE(ini.set("param", "value"));
170 EXPECT_EQ("[Section]\nparam=value\n", ini.getRawConfiguration());
171
172 // Modify the last parameter
173 EXPECT_TRUE(ini.set("param", "newdata"));
174 EXPECT_EQ("[Section]\nparam=newdata\n", ini.getRawConfiguration());
175
176 // Add a new parameter in the middle
177 ini.loadRawConfiguration("[FirstSection]\nparam1=0\n[Section2]\nparam1=1\n");
178 EXPECT_TRUE(ini.setSection("FirstSection", false));
179 EXPECT_TRUE(ini.set("param2", 2));
180 EXPECT_EQ("[FirstSection]\nparam1=0\nparam2=2\n[Section2]\nparam1=1\n", ini.getRawConfiguration());
181
182 // Modify a parameter in the middle
183 EXPECT_TRUE(ini.set("param1", true));
184 EXPECT_EQ("[FirstSection]\nparam1=1\nparam2=2\n[Section2]\nparam1=1\n", ini.getRawConfiguration());
185 }
186
TEST(IniFile,unset)187 TEST(IniFile, unset)
188 {
189 IniFile ini("/tmp/testini.conf");
190
191 ini.loadRawConfiguration("[Section]\nparam1=1\nparam2=2\nparam3=3\n");
192 EXPECT_TRUE(ini.setSection("Section", true));
193
194 // Remove non-existing parameter
195 EXPECT_FALSE(ini.unset("param"));
196 EXPECT_EQ("[Section]\nparam1=1\nparam2=2\nparam3=3\n", ini.getRawConfiguration());
197
198 // Remove middle parameter
199 EXPECT_TRUE(ini.unset("param2"));
200 EXPECT_EQ("[Section]\nparam1=1\nparam3=3\n", ini.getRawConfiguration());
201
202 // Remove last parameter
203 EXPECT_TRUE(ini.unset("param3"));
204 EXPECT_EQ("[Section]\nparam1=1\n", ini.getRawConfiguration());
205
206 // Remove only parameter
207 EXPECT_TRUE(ini.unset("param1"));
208 EXPECT_EQ("[Section]\n", ini.getRawConfiguration());
209 }
210
TEST(IniFile,getSections)211 TEST(IniFile, getSections)
212 {
213 IniFile ini("/tmp/testini.conf");
214
215 ini.loadRawConfiguration("[Section1]\nparam1=1\n#[Section2]\n[section3]\n[Section4]\n[Section5\n]\n");
216
217 // Get all sections
218 list<string> ret;
219 ini.getSections(ret, "");
220 EXPECT_EQ(3u, ret.size());
221 ret.clear();
222
223 // Verify prefix parameter
224 ini.getSections(ret, "Section");
225 EXPECT_EQ(2u, ret.size());
226 ret.clear();
227 }
228
TEST(IniFile,removeSection)229 TEST(IniFile, removeSection)
230 {
231 IniFile ini("/tmp/testini.conf");
232
233 ini.loadRawConfiguration("[FirstSection]\nparam1=0\nparam2=1\n[EmptySection]\n[AnotherSection]\nparam3=4\n\n[LastSection]\n");
234
235 // Remove empty section
236 ini.removeSection("EmptySection");
237 EXPECT_EQ("[FirstSection]\nparam1=0\nparam2=1\n[AnotherSection]\nparam3=4\n\n[LastSection]\n", ini.getRawConfiguration());
238
239 // Remove section with empty line at end
240 ini.removeSection("AnotherSection");
241 EXPECT_EQ("[FirstSection]\nparam1=0\nparam2=1\n[LastSection]\n", ini.getRawConfiguration());
242
243 // Remove last section
244 ini.removeSection("LastSection");
245 EXPECT_EQ("[FirstSection]\nparam1=0\nparam2=1\n", ini.getRawConfiguration());
246
247 // Remove first (and only) section
248 ini.removeSection("FirstSection");
249 EXPECT_EQ("", ini.getRawConfiguration());
250 }
251
TEST(IniFile,getKeyList)252 TEST(IniFile, getKeyList)
253 {
254 IniFile ini("/tmp/testini.conf");
255
256 ini.loadRawConfiguration("[Section1]\nparam1=1\nData=2\n#param3=3\nparam4=4\n[Section2]\nparam1=1\nparam2=5\n");
257
258 // No section set should return nothing
259 list<string> ret;
260 ini.getKeyList(ret, "");
261 EXPECT_EQ(0u, ret.size());
262 ret.clear();
263
264 // Get all parameters for Section1
265 ini.setSection("Section1");
266 ini.getKeyList(ret, "");
267 EXPECT_EQ(3u, ret.size());
268 EXPECT_EQ("param1", *ret.begin());
269 ret.clear();
270
271 // Verify prefix parameter
272 ini.getKeyList(ret, "param");
273 EXPECT_EQ(2u, ret.size());
274 EXPECT_EQ("param1", *ret.begin());
275 ret.clear();
276 }
277
TEST(IniFile,escapedChars)278 TEST(IniFile, escapedChars)
279 {
280 IniFile ini("/tmp/testini.conf");
281
282 // Verify special characters in set
283 EXPECT_TRUE(ini.setSection("Section", true));
284 EXPECT_TRUE(ini.set("key", "line\\1\nline\\2\nline\\3"));
285 EXPECT_EQ("[Section]\nkey=line\\\\1\\nline\\\\2\\nline\\\\3\n", ini.getRawConfiguration());
286
287 // Verify special characters in get
288 string retStr("");
289 EXPECT_TRUE(ini.get("key", retStr));
290 EXPECT_EQ("line\\1\nline\\2\nline\\3", retStr);
291 }
292
TEST(IniFile,hexData)293 TEST(IniFile, hexData)
294 {
295 IniFile ini("/tmp/testini.conf");
296
297 ini.loadRawConfiguration("[Section]\nparam=000123456789ABCDEF00\n");
298 EXPECT_TRUE(ini.setSection("Section"));
299
300 // Verify getHex
301 string strRet("");
302 EXPECT_TRUE(ini.getHex("param", strRet));
303 EXPECT_EQ(string("\0\x01\x23\x45\x67\x89\xAB\xCD\xEF\0", 10), strRet);
304
305 // Verify setHex
306 EXPECT_TRUE(ini.setHex("param", string("\0\xFE\xDC\xBA\x98\x76\x54\x32\x10\0", 10)));
307 EXPECT_EQ("[Section]\nparam=00FEDCBA987654321000\n", ini.getRawConfiguration());
308 }
309
310 } // namespace LicqTest
311