File: inifile_test.cpp

package info (click to toggle)
libassa 3.5.1-8.1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 3,424 kB
  • sloc: cpp: 15,703; sh: 12,083; makefile: 379; perl: 51
file content (466 lines) | stat: -rw-r--r-- 12,356 bytes parent folder | download | duplicates (9)
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
// -*- c++ -*-
// Generated by assa-genesis
//------------------------------------------------------------------------------
// $Id: inifile_test.cpp,v 1.11 2006/07/20 02:30:56 vlg Exp $
//------------------------------------------------------------------------------
//                            IniFile_Test.cpp
//------------------------------------------------------------------------------
//  Copyright (c) 2003,2005 by Vladislav Grinchenko 
//
//  This program is free software; you can redistribute it and/or 
//  modify it under the terms of the GNU General Public License   
//  as published by the Free Software Foundation; either version  
//  2 of the License, or (at your option) any later version.      
//------------------------------------------------------------------------------
//
// Date   : Sat Sep  6 22:10:05 2003
//
//------------------------------------------------------------------------------

static const char help_msg[]=
"                                                                            \n"
" NAME:                                                                      \n"
"                                                                            \n"
"   inifile_test                                                             \n"
"                                                                            \n"
" DESCRIPTION:                                                               \n"
"                                                                            \n"
"   Driver for IniFile class. Tests IniFile class interface.                 \n"
"                                                                            \n"
" USAGE:                                                                     \n"
"                                                                            \n"
"   shell>  inifile_test [OPTIONS]                                           \n"
"                                                                            \n"
" OPTIONS:                                                                   \n"
"                                                                            \n"
" -D, --log-file NAME     - Write debug to NAME file                         \n"
" -d, --log-stdout        - Write debug to standard output                   \n"
" -z, --log-size NUM      - Maximum size debug file can reach (dfl: is 10Mb) \n"
"                                                                            \n"
" -c, --log-level NUM     - Log verbosity                                    \n"
" -s, --with-log-server   - Redirect log messages to the log server          \n"
" -S, --log-server NAME   - Define assa-logd server address                  \n"
" -m, --mask MASK         - Mask (default: ALL = 0x7fffffff)                 \n"
"                                                                            \n"
" -h, --help              - Print this messag                                \n"
" -v, --version           - Print version number                            \n";
//------------------------------------------------------------------------------

#include <fstream>
#include <iostream>

#ifdef HAVE_CONFIG_H
#    include "config.h"
#endif
#include <string>
using std::string;

#include <assa/Assure.h>
#include <assa/GenServer.h>
#include <assa/Singleton.h>
#include <assa/IniFile.h>
using ASSA::IniFile;

enum { TEST = ASSA::USR1 };

/* Perform double-logging: to the file and STDOUT
 */
void log_status (const std::string& m_)
{
    std::cout << m_ << std::flush;
    DL((TEST," %s", m_.c_str ()));
}

//------------------------------------------------------------------------------
class IniFile_Test :
    public ASSA::GenServer,
    public ASSA::Singleton<IniFile_Test>
{
public:
    IniFile_Test ();
    ~IniFile_Test ();

    virtual void init_service ();
    virtual void process_events ();

private:
	int run_test_one   ();
	int run_test_two   ();
	int run_test_three ();
	int run_test_four  ();
	int run_test_five  ();

private:
	string m_test_fname;
	IniFile m_ini_file;
};
//------------------------------------------------------------------------------
/* Useful definitions */

#define INIFILE_TEST  IniFile_Test::get_instance()
#define REACTOR INIFILE_TEST->get_reactor()

// Static declarations mandated by Singleton class
ASSA_DECL_SINGLETON(IniFile_Test);

//------------------------------------------------------------------------------
IniFile_Test::
IniFile_Test () : 
	m_test_fname ("test-inifile.conf"),
	m_ini_file (m_test_fname)
{
    // ---Configuration---
    rm_opt ('f', "config-file"  );
    rm_opt ('n', "instance"     );
    rm_opt ('p', "port"         );

    // ---Process bookkeeping---
    rm_opt ('b', "daemon"       );
    rm_opt ('l', "pidfile"      );
    rm_opt ('L', "ommit-pidfile");

    /*---
     * By defauil disable all debugging
     *---*/
    // m_debug_mask = ASSA::APP | ASSA::ASSAERR;
    m_log_file = "IniFile_Test.log";

	// Enable if you want to see internal messages
    m_mask = TEST | ASSA::INIFILE | ASSA::ASSAERR;
}

IniFile_Test::
~IniFile_Test () 
{
	if (get_exit_value () == 0) {
		::unlink (m_test_fname.c_str ());
	}
}

void
IniFile_Test::
init_service ()
{
    trace("IniFile_Test::init_service");

	std::ofstream test_file;

	test_file.open (m_test_fname.c_str (), std::ios::out);

	if (!test_file) {
		set_exit_value (1);
		stop_service ();
		return;
	}

	test_file << "# This is the input test file for \n"
			  << "# inifile_test driver\n"
			  << "#\n"
			  << "\n"
			  << "[options]  \n"
			  << "gtk_options = --g-fatal-warnings --disable-crash-dialog  \n"
			  << "wavjoin_options=\n"
			  << "log_file=gwm.log\n"
			  << "  log_size  = 0\n"
			  << "\tmask =  \t 0x0\n"
			  << "log_stdout=false\n"
			  << "\n"
			  << "[History] \n"
			  << "0=/Segment25/Segment25.gwm\n"
			  << "1=/Segment26/Segment26.gwm\n"
			  << "2=/Segment27/Segment27.gwm\n"
			  << "\n"
			  << "[Default]\n"
			  << "jump=true\n"
			  << "\n";

	test_file.close ();
}

void
IniFile_Test::
process_events ()
{
	if (!service_is_active ()) {
		goto failed;
    }

	log_status ("= Running inifile_test Test =\n");

	if (run_test_one () < 0) {
		goto failed;
	}

	if (run_test_two () < 0) {
		goto failed;
	}

	if (run_test_three () < 0) {
		goto failed;
	}

	if (run_test_four () < 0) {
		goto failed;
	}

	if (run_test_five () < 0) {
		goto failed;
	}

	log_status ("inifile_test Passed\n");
	m_reactor.stopReactor ();
	return;

 failed:
	log_status ("inifile_test Failed!\n");
	set_exit_value (1);
    m_reactor.stopReactor ();
}

/*
  Load configuration file that was generated by init_service().
  Save configuration in cache to inifile01.conf.
  Reload it into another IniFile object, test01, and compare.
*/
int 
IniFile_Test::
run_test_one ()
{
	std::string alt_fname = "inifile01.conf";
	std::string err;

	log_status ("Testing load() ... ");

	if (m_ini_file.load () < 0) {
		err = " failed to load(\"" + m_test_fname + "\" file ...!\n";
		log_status (err);
		return -1;

	}
	m_ini_file.dump ();
	log_status ("ok\n");

	log_status ("Testing sync(file) ... ");
	if (m_ini_file.sync (alt_fname) < 0) {
		err = " failed to sync(\"" + alt_fname + "\") file ...!\n";
		log_status (err);
		return -1;
	}

	IniFile test01 (alt_fname);
	if (test01.load () < 0) {
		err = " failed to re-load(\"" + alt_fname + "\") !\n";
		log_status (err);
		return -1;
	}

	if (test01 != m_ini_file) {
		err = " failed to sync(\"" + alt_fname + "\") !\n";
		log_status (err);
		return -1;
	}

	log_status ("ok\n");
	return 0;
}

/*
  Check for expected values.
  Check for non-existing value.
*/
int 
IniFile_Test::
run_test_two ()
{
	log_status ("Testing get_value() ... ");

	if (m_ini_file.get_value ("options", "log_file") != "gwm.log") {
		log_status (" failed to get_value(\"options\", \"log_file\")\n");
		return -1;
	}

	if (m_ini_file.get_value ("History", "2") != "/Segment27/Segment27.gwm") {
		log_status (" failed to get_value(\"options\", \"log_file\")\n");
		return -1;
	}

	if (m_ini_file.get_value ("Bogus", "Not an option") == "failure") {
		log_status (" get_value(\"Bogus\", \"Not an option\") succeeded\n");
		return -1;
	}

	log_status ("ok\n");
	return 0;
}

/**
   Add new section and test to see if it was added.
   Fill up newly created section with values and test
   to see if they were added.
*/

int 
IniFile_Test::
run_test_three ()
{
	log_status ("Testing add_section() / set_pair() ... ");

	m_ini_file.add_section ("Interface");

	if (m_ini_file.find_section ("Interface") == m_ini_file.sect_end ()) {
		log_status (" failed to find_section (\"Interface\") ...!\n");
		return -1;

	}

	if (m_ini_file.set_pair ("Interface", 
							 IniFile::tuple_type ("zoom", "true")) < 0) 
	{
		log_status (" failed to set_pair(\"Interface\","
					"(\"zoom\",\"true\"))!\n");
		return -1;
	}
		
	if (m_ini_file.set_pair ("Interface", 
							 IniFile::tuple_type ("profile", "default")) < 0) 
	{
		log_status (" failed to set_pair(\"Interface\", "
					"(\"profile\",\"default\"))!\n");
		return -1;
	}
	
	if (m_ini_file.get_value ("Interface", "zoom") != "true") {
		log_status (" failed to get_value (\"Interface\", \"zoom\") ...!\n");
		return -1;
	}
	
	if (m_ini_file.get_value ("Interface", "profile") != "default") {
		log_status (" failed to get_value (\"Interface\", \"profile\")!\n");
		return -1;
	}
	
	log_status ("ok\n");
	return 0;
}

/**
   Traverse through the list of sections and print out section names.
   Traverse through a section and print out its name/value pairs.
*/
int 
IniFile_Test::
run_test_four ()
{
	log_status ("Testing iteration through list of sections  ...\n");

	int count = 0;
	std::ostringstream msg;

	IniFile::const_config_iterator i = m_ini_file.sect_begin ();

	while (i != m_ini_file.sect_end ()) {
		msg << "    " << (*i).first.c_str () << "\n";
		log_status (msg.str ().c_str ());
		i++, count++;
		msg.str (string ()), msg.clear ();
	}
	msg.str (string ()), msg.clear ();

	if (count != m_ini_file.size ()) {
		msg << " failed! # of sections " << count << " != " 
			<< m_ini_file.size () << "\n" << std::ends;
		log_status (msg.str ().c_str ());
		return -1;
	}
	msg.str (string ()), msg.clear ();

	log_status ("ok\n");

	log_status ("Testing iteration through a section  ...\n");
	count = 0;
	IniFile::const_config_iterator j = m_ini_file.find_section ("History");

	if (j != m_ini_file.sect_end ()) {
		IniFile::const_tuple_iterator k = (*j).second.begin ();
		while (k != (*j).second.end ()) {
			msg << "    " << (*k).first.c_str () << "=" << (*k).second.c_str ()
				<< "\n";
			log_status (msg.str ());
			k++, count++;
			msg.str (string ()), msg.clear ();
		}
	}
	msg.str (string ()), msg.clear ();

	if (count != 3) {
		msg << " failed! # of name/value pairs " << count << " != 3\n";
		log_status (msg.str ());
		return -1;
	}
	log_status ("ok\n");

	return 0;
}

/** 
	Remove section and test that it has been deleted.
	Clean up the entire configuration cache.
*/
int 
IniFile_Test::
run_test_five ()
{

	log_status ("Testing drop_pair() ... ");

	if (m_ini_file.drop_pair ("Interface", "profile") < 0) {
		log_status (" failed to drop_pair (\"Interface\", \"profile\")!\n");
		return -1;
	}
	if (m_ini_file.get_value ("Interface", "profile") != "") {
		log_status (" failed to get_value (\"Interface\",\"profile\")!\n");
		return -1;
	}
	log_status ("ok\n");

	log_status ("Testing drop_section() ... ");
	if (m_ini_file.drop_section ("Interface") < 0) {
		log_status (" failed to drop_section (\"Interface\")!\n");
		return -1;
	}
	if (m_ini_file.find_section ("Interface") != m_ini_file.sect_end ()) {
		log_status (" failed to remove \"Interface\" section!\n");
		return -1;
	}
	log_status ("ok\n");

	log_status ("Testing drop_all() ... ");
	m_ini_file.drop_all ();
	if (m_ini_file.size () != 0) {
		log_status (" failed to drop_all() !\n");
		return -1;
	}
	log_status ("ok\n");

	return 0;
}

int
main (int argc, char* argv[])
{
    static const char release[] = "1.0";
    int patch_level = 0;

    INIFILE_TEST->set_version (release, patch_level);
    INIFILE_TEST->set_author  ("Vladislav Grinchenko");
    INIFILE_TEST->set_flags   (ASSA::GenServer::RMLOG);

    INIFILE_TEST->init (&argc, argv, help_msg);
 
    INIFILE_TEST->init_service ();
    INIFILE_TEST->process_events ();

    return INIFILE_TEST->get_exit_value ();
}