File: chunked_iostream_test.cc

package info (click to toggle)
libdap 3.18.2-2%2Bdeb9u1
  • links: PTS
  • area: main
  • in suites: stretch
  • size: 18,808 kB
  • sloc: sh: 54,436; cpp: 46,087; ansic: 16,793; xml: 2,730; yacc: 2,452; exp: 1,544; makefile: 1,001; lex: 305; perl: 52; fortran: 8
file content (564 lines) | stat: -rw-r--r-- 17,683 bytes parent folder | download
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
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564

// -*- mode: c++; c-basic-offset:4 -*-

// This file is part of libdap, A C++ implementation of the OPeNDAP Data
// Access Protocol.

// Copyright (c) 2013 OPeNDAP, Inc.
// Author: James Gallagher <jgallagher@opendap.org>
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
//
// You can contact OPeNDAP, Inc. at PO Box 112, Saunderstown, RI. 02874-0112.

#include <cppunit/TextTestRunner.h>
#include <cppunit/extensions/TestFactoryRegistry.h>
#include <cppunit/extensions/HelperMacros.h>

#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>

#include <iostream>
#include <fstream>
#include <string>

#include "GetOpt.h"

#include "chunked_ostream.h"
#include "chunked_istream.h"

#include "InternalErr.h"
#include "test_config.h"
#include "debug.h"

static bool debug = false;

const string path = (string)TEST_SRC_DIR + "/chunked-io";

#undef DBG
#define DBG(x) do { if (debug) (x); } while(false);

using namespace std;
using namespace CppUnit;
using namespace libdap;

/**
 * The intent is to test writing to and reading from a chunked iostream,
 * using various combinations of chunk/buffer sizes and character red/write
 * sizes. There are three write functions and three read functions and
 * all combinations are tested.
 */
class chunked_iostream_test: public TestFixture {
private:
	// This should be big enough to do meaningful timing tests
	string big_file, big_file_2, big_file_3;
	// This should be smaller than a single buffer
	string small_file;
	// A modest sized text file - makes looking at the results easier
	string text_file;
public:
    chunked_iostream_test()
    {
    }
    ~chunked_iostream_test()
    {
    }

    void setUp()
    {
    	big_file = path + "/test_big_binary_file.bin";
    	big_file_2 = path + "/test_big_binary_file_2.bin";
    	big_file_3 = path + "/test_big_binary_file_3.bin"; // not used yet

    	small_file = path + "/test_small_text_file.txt";
    	text_file = path + "/test_text_file.txt";
    }

    void tearDown()
    {
    }

    void
    single_char_write(const string &file, int buf_size)
    {
    	fstream infile(file.c_str(), ios::in|ios::binary);
    	DBG(cerr << "infile: " << file << endl);
    	if (!infile.good())
    		CPPUNIT_FAIL("File not open or eof");

    	string out = file + ".chunked";
    	fstream outfile(out.c_str(), ios::out|ios::binary);

    	chunked_ostream chunked_outfile(outfile, buf_size);

    	char c;
    	infile.read(&c, 1);
    	int num = infile.gcount();
    	while (num > 0 && !infile.eof()) {
    		chunked_outfile.write(&c, num);
    		infile.read(&c, 1);
    		num = infile.gcount();
    	}

    	if (num > 0 && !infile.bad()) {
    		chunked_outfile.write(&c, num);
    	}

    	chunked_outfile.flush();
    }

    void
    write_128char_data(const string &file, int buf_size)
    {
    	fstream infile(file.c_str(), ios::in|ios::binary);
    	if (!infile.good())
    		CPPUNIT_FAIL("File not open or eof");

    	string out = file + ".chunked";
    	fstream outfile(out.c_str(), ios::out|ios::binary);

    	chunked_ostream chunked_outfile(outfile, buf_size);

    	char str[128];
    	infile.read(str, 128);
    	int num = infile.gcount();
    	while (num > 0 && !infile.eof()) {
    		chunked_outfile.write(str, num);
    		infile.read(str, 128);
    		num = infile.gcount();
    	}

    	if (num > 0 && !infile.bad()) {
    		chunked_outfile.write(str, num);
    	}

        chunked_outfile.flush();
    }

    // This will not work with the small text file. This code assume that
    // the file to be written has at least 24 bytes for the first chunk,
    // which is deliberately sent using flush before the buffer is full and
    // then has at least 48 more bytes (but ideally 49, because this code
    // tries to send an End chunk with one or more bytes as opposed to
    // sending the last data chunk with fewer than buf_size and then sending a
    // zero length END chunk).
    void
    write_24char_data_with_error_option(const string &file, int buf_size, bool error = false)
    {
    	fstream infile(file.c_str(), ios::in|ios::binary);
    	if (!infile.good())
    		CPPUNIT_FAIL("File not open or eof");

    	string out = file + ".chunked";
    	fstream outfile(out.c_str(), ios::out|ios::binary);

    	chunked_ostream chunked_outfile(outfile, buf_size);

    	try {
    		char str[24];
    		infile.read(str, 24);
    		int num = infile.gcount();
    		if (num > 0 && !infile.eof()) {
    			chunked_outfile.write(str, num);
    			chunked_outfile.flush();
    		}

    		infile.read(str, 24);
    		num = infile.gcount();
    		if (num > 0 && !infile.eof()) chunked_outfile.write(str, num);

    		// Send an error chunk; the 24 bytes read here are lost...
    		if (error)
    			throw Error("Testing error transmission");

    		infile.read(str, 24);
    		num = infile.gcount();
    		while (num == 24 && !infile.eof()) {
    			chunked_outfile.write(str, num);
    			infile.read(str, 24);
    			num = infile.gcount();
    		}

    		if (num > 0 && !infile.bad()) {
    			chunked_outfile.write(str, num);
    		}

    		// flush() calls sync() which forces a DATA chunk to be sent, regardless of
    		// the amount of data in the buffer. When the stream is destroyed, end_chunk()
    		// is sent with the remain chars, so removing flush() here ensures that we test
    		// a non-empty END chunk.
            // chunked_outfile.flush();
    	}
    	catch (Error &e) {
    		chunked_outfile.write_err_chunk(e.get_error_message());
    	}
    }

    void
    single_char_read(const string &file, int buf_size)
    {
    	string in = file + ".chunked";
    	fstream infile(in.c_str(), ios::in|ios::binary);
    	if (!infile.good())
    		CPPUNIT_FAIL("File not open or eof");
#if BYTE_ORDER_PREFIX
    	chunked_istream chunked_infile(infile, buf_size, 0x00);
#else
    	chunked_istream chunked_infile(infile, buf_size);
#endif
    	string out = file + ".plain";
    	fstream outfile(out.c_str(), ios::out|ios::binary);

    	char c;
    	int count = 1;
    	chunked_infile.read(&c, 1);
    	int num = chunked_infile.gcount();
    	DBG(cerr << "num: " << num << ", " << count++ << endl);
    	while (num > 0 && !chunked_infile.eof()) {
    		outfile.write(&c, num);
    		chunked_infile.read(&c, 1);
    		num = chunked_infile.gcount();
    		DBG(cerr << "num: " << num << ", " <<  count++ << ", eof: " << chunked_infile.eof() << endl);
    	}

    	DBG(cerr << "eof is :" << chunked_infile.eof() << ", num: " << num << endl);

    	if (num > 0 && !chunked_infile.bad())
    	    outfile.write(&c, num);

    	outfile.flush();
    }

    void
    read_128char_data(const string &file, int buf_size)
    {
    	string in = file + ".chunked";
    	fstream infile(in.c_str(), ios::in|ios::binary);
    	if (!infile.good())
    		cerr << "File not open or eof" << endl;
    	chunked_istream chunked_infile(infile, buf_size);

    	string out = file + ".plain";
    	fstream outfile(out.c_str(), ios::out|ios::binary);

    	char str[128];
    	int count = 1;
    	chunked_infile.read(str, 128);
    	int num = chunked_infile.gcount();
    	DBG(cerr << "num: " << num << ", " << count++ << endl);
    	while (num > 0 && !chunked_infile.eof()) {
    		outfile.write(str, num);
    		chunked_infile.read(str, 128);
    		num = chunked_infile.gcount();
    		DBG(cerr << "num: " << num << ", " <<  count++ << ", eof: " << chunked_infile.eof() << endl);
    	}

    	if (num > 0 && !chunked_infile.bad()) {
    		outfile.write(str, num);
    	}

    	outfile.flush();
    }

    void
    read_24char_data_with_error_option(const string &file, int buf_size)
    {
    	string in = file + ".chunked";
    	fstream infile(in.c_str(), ios::in|ios::binary);
    	if (!infile.good())
    		cerr << "File not open or eof" << endl;
    	chunked_istream chunked_infile(infile, buf_size);

    	string out = file + ".plain";
    	fstream outfile(out.c_str(), ios::out|ios::binary);

    	try {
#if 0
    		chunked_infile.read(str, 24);
    		int num = chunked_infile.gcount();
    		if (num > 0 && !chunked_infile.eof()) {
    			outfile.write(str, num);
    			outfile.flush();
    		}
#endif
    		char str[24];
    		chunked_infile.read(str, 24);
    		int num = chunked_infile.gcount();
    		while (num > 0 && !chunked_infile.eof()) {
    			outfile.write(str, num);
    			chunked_infile.read(str, 24);
    			num = chunked_infile.gcount();
    		}

    		// The chunked_istream uses a chunked_inbuf and that signals error
    		// using EOF. The error message is stored in the buffer and can be
    		// detected and accessed using the error() error_message() methods
    		// that both the buffer and istream classes have.
    		if (chunked_infile.error())
    			throw Error("Found an error in the stream");

    		if (num > 0 && !chunked_infile.bad()) {
    			outfile.write(str, num);
    		}

    		outfile.flush();
    	}
    	catch (Error &e) {
    		DBG(cerr << "Error chunk found: " << e.get_error_message() << endl);
    		throw;
    	}
    }

    // these are the tests
    void test_write_1_read_1_small_file() {
    	single_char_write(small_file, 32);
    	single_char_read(small_file, 32);
    	string cmp = "cmp " + small_file + " " + small_file + ".plain";
    	CPPUNIT_ASSERT(system(cmp.c_str()) == 0);
    }

    void test_write_1_read_1_text_file() {
    	single_char_write(text_file, 32);
    	single_char_read(text_file, 32);
    	string cmp = "cmp " + text_file + " " + text_file + ".plain";
    	CPPUNIT_ASSERT(system(cmp.c_str()) == 0);
    }

    void test_write_1_read_1_big_file() {
        single_char_write(big_file, 28);
        single_char_read(big_file, 28);
        string cmp = "cmp " + big_file + " " + big_file + ".plain";
        CPPUNIT_ASSERT(system(cmp.c_str()) == 0);
    }
    void test_write_1_read_1_big_file_2() {
        single_char_write(big_file_2, 28);
        single_char_read(big_file_2, 28);
        string cmp = "cmp " + big_file_2 + " " + big_file_2 + ".plain";
        CPPUNIT_ASSERT(system(cmp.c_str()) == 0);
    }

    // these are the tests
    void test_write_1_read_128_small_file() {
        single_char_write(small_file, 32);
        read_128char_data(small_file, 32);
        string cmp = "cmp " + small_file + " " + small_file + ".plain";
        CPPUNIT_ASSERT(system(cmp.c_str()) == 0);
    }

    void test_write_1_read_128_text_file() {
        single_char_write(text_file, 32);
        read_128char_data(text_file, 32);
        string cmp = "cmp " + text_file + " " + text_file + ".plain";
        CPPUNIT_ASSERT(system(cmp.c_str()) == 0);
    }

    void test_write_1_read_128_big_file() {
        single_char_write(big_file, 28);
        read_128char_data(big_file, 28);
        string cmp = "cmp " + big_file + " " + big_file + ".plain";
        CPPUNIT_ASSERT(system(cmp.c_str()) == 0);
    }

    void test_write_1_read_128_big_file_2() {
        single_char_write(big_file_2, 28);
        read_128char_data(big_file_2, 28);
        string cmp = "cmp " + big_file_2 + " " + big_file_2 + ".plain";
        CPPUNIT_ASSERT(system(cmp.c_str()) == 0);
    }

    // 24 char write units

    void test_write_24_read_1_text_file() {
    	write_24char_data_with_error_option(text_file, 32);
    	single_char_read(text_file, 32);
        string cmp = "cmp " + text_file + " " + text_file + ".plain";
        CPPUNIT_ASSERT(system(cmp.c_str()) == 0);
    }

    void test_write_24_read_1_big_file() {
    	write_24char_data_with_error_option(big_file, 1024);
    	DBG(cerr << "Wrote the file" << endl);

    	single_char_read(big_file, 1024);
        string cmp = "cmp " + big_file + " " + big_file + ".plain";
        CPPUNIT_ASSERT(system(cmp.c_str()) == 0);
    }

    void test_write_24_read_1_big_file_2() {
    	write_24char_data_with_error_option(big_file_2, 1024);
    	DBG(cerr << "Wrote the file" << endl);

    	single_char_read(big_file_2, 1024);
        string cmp = "cmp " + big_file_2 + " " + big_file_2 + ".plain";
        CPPUNIT_ASSERT(system(cmp.c_str()) == 0);
    }

    void test_write_24_read_128_text_file() {
    	write_24char_data_with_error_option(text_file, 32);
        read_128char_data(text_file, 32);
        string cmp = "cmp " + text_file + " " + text_file + ".plain";
        CPPUNIT_ASSERT(system(cmp.c_str()) == 0);
    }

    void test_write_24_read_128_big_file() {
    	write_24char_data_with_error_option(big_file, 28);
    	DBG(cerr << "Wrote the file" << endl);

        read_128char_data(big_file, 28);
        string cmp = "cmp " + big_file + " " + big_file + ".plain";
        CPPUNIT_ASSERT(system(cmp.c_str()) == 0);
    }

    void test_write_24_read_128_big_file_2() {
    	write_24char_data_with_error_option(big_file_2, 28);
        read_128char_data(big_file_2, 28);
        string cmp = "cmp " + big_file_2 + " " + big_file_2 + ".plain";
        CPPUNIT_ASSERT(system(cmp.c_str()) == 0);
    }

    // 128 char writes
    void test_write_128_read_1_text_file() {
    	write_128char_data(text_file, 32);
    	single_char_read(text_file, 32);
        string cmp = "cmp " + text_file + " " + text_file + ".plain";
        CPPUNIT_ASSERT(system(cmp.c_str()) == 0);
    }

    void test_write_128_read_1_big_file() {
    	write_128char_data(big_file, 32);
    	DBG(cerr << "Wrote the file" << endl);

    	single_char_read(big_file, 32);
        string cmp = "cmp " + big_file + " " + big_file + ".plain";
        CPPUNIT_ASSERT(system(cmp.c_str()) == 0);
    }

    void test_write_128_read_1_big_file_2() {
    	write_128char_data(big_file_2, 32);
    	DBG(cerr << "Wrote the file" << endl);

    	single_char_read(big_file_2, 1024);
        string cmp = "cmp " + big_file_2 + " " + big_file_2 + ".plain";
        CPPUNIT_ASSERT(system(cmp.c_str()) == 0);
    }

    void test_write_128_read_128_text_file() {
    	write_128char_data(text_file, 32);
        read_128char_data(text_file, 32);
        string cmp = "cmp " + text_file + " " + text_file + ".plain";
        CPPUNIT_ASSERT(system(cmp.c_str()) == 0);
    }

    void test_write_128_read_128_big_file() {
    	write_128char_data(big_file, 28);
    	DBG(cerr << "Wrote the file" << endl);

        read_128char_data(big_file, 28);
        string cmp = "cmp " + big_file + " " + big_file + ".plain";
        CPPUNIT_ASSERT(system(cmp.c_str()) == 0);
    }

    void test_write_128_read_128_big_file_2() {
    	write_128char_data(big_file_2, 28);
        read_128char_data(big_file_2, 28);
        string cmp = "cmp " + big_file_2 + " " + big_file_2 + ".plain";
        CPPUNIT_ASSERT(system(cmp.c_str()) == 0);
    }

    // Send an error

    void test_write_24_read_24_big_file_2_error() {
		write_24char_data_with_error_option(big_file_2, 2048, true /*error*/);
		try {
			read_24char_data_with_error_option(big_file_2, 2048);
			CPPUNIT_FAIL("Should have caught an error message");
		}
		catch (Error &e) {
			CPPUNIT_ASSERT(!e.get_error_message().empty());
		}
	}

    CPPUNIT_TEST_SUITE(chunked_iostream_test);

    CPPUNIT_TEST(test_write_1_read_1_small_file);
    CPPUNIT_TEST(test_write_1_read_1_text_file);
    CPPUNIT_TEST(test_write_1_read_1_big_file);
    CPPUNIT_TEST(test_write_1_read_1_big_file_2);

    CPPUNIT_TEST(test_write_1_read_128_small_file);
    CPPUNIT_TEST(test_write_1_read_128_text_file);
    CPPUNIT_TEST(test_write_1_read_128_big_file);
    CPPUNIT_TEST(test_write_1_read_128_big_file_2);

    CPPUNIT_TEST(test_write_24_read_1_text_file);
    CPPUNIT_TEST(test_write_24_read_1_big_file);
    CPPUNIT_TEST(test_write_24_read_1_big_file_2);

    CPPUNIT_TEST(test_write_24_read_128_text_file);
    CPPUNIT_TEST(test_write_24_read_128_big_file);
    CPPUNIT_TEST(test_write_24_read_128_big_file_2);

    CPPUNIT_TEST(test_write_128_read_1_text_file);
    CPPUNIT_TEST(test_write_128_read_1_big_file);
    CPPUNIT_TEST(test_write_128_read_1_big_file_2);

    CPPUNIT_TEST(test_write_128_read_128_text_file);
    CPPUNIT_TEST(test_write_128_read_128_big_file);
    CPPUNIT_TEST(test_write_128_read_128_big_file_2);

    CPPUNIT_TEST(test_write_24_read_24_big_file_2_error);

    CPPUNIT_TEST_SUITE_END();
};

CPPUNIT_TEST_SUITE_REGISTRATION(chunked_iostream_test);

int
main(int argc, char *argv[])
{
    GetOpt getopt(argc, argv, "d");
    int option_char;

    while ((option_char = getopt()) != -1)
        switch (option_char) {
            case 'd':
                debug = 1;  // debug is a static global
                break;
            default:
                break;
        }

    CppUnit::TextTestRunner runner;
    runner.addTest(CppUnit::TestFactoryRegistry::getRegistry().makeTest());

    bool wasSuccessful = true;
    string test = "";
    int i = getopt.optind;
    if (i == argc) {
        // run them all
        wasSuccessful = runner.run("");
    }
    else {
        while (i < argc) {
            test = string("chunked_iostream_test::") + argv[i++];
            if (debug)
                cerr << "Running " << test << endl;
            wasSuccessful = wasSuccessful && runner.run(test);
        }
    }

    return wasSuccessful ? 0 : 1;
}