zone_table_segment_mapped_unittest.cc 17.5 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
// Copyright (C) 2013  Internet Systems Consortium, Inc. ("ISC")
//
// Permission to use, copy, modify, and/or distribute this software for any
// purpose with or without fee is hereby granted, provided that the above
// copyright notice and this permission notice appear in all copies.
//
// THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
// REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
// AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
// INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
// LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
// OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
// PERFORMANCE OF THIS SOFTWARE.

#include <datasrc/memory/zone_writer.h>
#include <datasrc/memory/zone_table_segment_mapped.h>
17
18
#include <util/random/random_number_generator.h>
#include <util/unittests/check_valgrind.h>
19
20

#include <gtest/gtest.h>
21
#include <boost/format.hpp>
22
23
24
#include <boost/scoped_ptr.hpp>
#include <boost/interprocess/file_mapping.hpp>

25
26
#include <cerrno>

27
28
#include <sys/stat.h>

29
30
31
32
using namespace isc::dns;
using namespace isc::datasrc::memory;
using namespace isc::data;
using namespace isc::util;
33
using namespace isc::util::random;
34
35
36
37
38
using namespace std;
using boost::scoped_ptr;

namespace {

39
const char* const mapped_file  = TEST_DATA_BUILDDIR "/test.mapped";
40
const char* const mapped_file2 = TEST_DATA_BUILDDIR "/test2.mapped";
41
42
43
44

class ZoneTableSegmentMappedTest : public ::testing::Test {
protected:
    ZoneTableSegmentMappedTest() :
45
46
        ztable_segment_(
            ZoneTableSegment::create(RRClass::IN(), "mapped")),
47
        config_params_(
48
            Element::fromJSON(
Mukund Sivaraman's avatar
Mukund Sivaraman committed
49
50
51
52
                "{\"mapped-file\": \"" + std::string(mapped_file) + "\"}")),
        config_params2_(
            Element::fromJSON(
                "{\"mapped-file\": \"" + std::string(mapped_file2) + "\"}"))
53
54
    {
        EXPECT_NE(static_cast<void*>(NULL), ztable_segment_);
55
56
57
58
        // Verify that a ZoneTableSegmentMapped is created.
        ZoneTableSegmentMapped* mapped_segment =
            dynamic_cast<ZoneTableSegmentMapped*>(ztable_segment_);
        EXPECT_NE(static_cast<void*>(NULL), mapped_segment);
59
60

        createTestData();
61
    }
62
63
64

    ~ZoneTableSegmentMappedTest() {
        ZoneTableSegment::destroy(ztable_segment_);
65
        boost::interprocess::file_mapping::remove(mapped_file);
Mukund Sivaraman's avatar
Mukund Sivaraman committed
66
        boost::interprocess::file_mapping::remove(mapped_file2);
67
68
    }

69
70
71
72
73
74
75
76
77
78
79
    typedef std::pair<std::string, int> TestDataElement;

    void createTestData() {
        UniformRandomIntegerGenerator gen(0, INT_MAX);
        for (int i = 0; i < 256; ++i) {
            const string name(boost::str(boost::format("name%d") % i));
            const int value = gen();
            test_data_.push_back(TestDataElement(name, value));
        }
    }

Mukund Sivaraman's avatar
Mukund Sivaraman committed
80
    void setupMappedFiles();
81
82
    void addData(MemorySegment& segment);
    bool verifyData(const MemorySegment& segment);
Mukund Sivaraman's avatar
Mukund Sivaraman committed
83

84
    ZoneTableSegment* ztable_segment_;
85
    const ConstElementPtr config_params_;
Mukund Sivaraman's avatar
Mukund Sivaraman committed
86
    const ConstElementPtr config_params2_;
87
    std::vector<TestDataElement> test_data_;
88
89
};

90
91
92
93
94
95
96
97
98
99
100
bool
fileExists(const char* path) {
    struct stat sb;
    const int status = stat(path, &sb);
    if (status != 0) {
        EXPECT_EQ(ENOENT, errno);
        return (false);
    }
    return (true);
}

Mukund Sivaraman's avatar
Mukund Sivaraman committed
101
102
103
104
105
106
107
108
109
110
111
112
void
deleteChecksum(MemorySegment& segment) {
    segment.clearNamedAddress("zone_table_checksum");
}

void
corruptChecksum(MemorySegment& segment) {
    const MemorySegment::NamedAddressResult result =
        segment.getNamedAddress("zone_table_checksum");
    ASSERT_TRUE(result.first);

    size_t checksum = *static_cast<size_t*>(result.second);
113
    ++checksum;
Mukund Sivaraman's avatar
Mukund Sivaraman committed
114
115
116
117
118
119
120
121
    *static_cast<size_t*>(result.second) = checksum;
}

void
deleteHeader(MemorySegment& segment) {
    segment.clearNamedAddress("zone_table_header");
}

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
void
ZoneTableSegmentMappedTest::addData(MemorySegment& segment) {
    // For purposes of this test, we assume that the following
    // allocations do not resize the mapped segment. For this, we have
    // to keep the size of test data reasonably small in
    // createTestData().

    // One by one, add all the elements in test_data_.
    for (int i = 0; i < test_data_.size(); ++i) {
        void* ptr = segment.allocate(sizeof(int));
        ASSERT_TRUE(ptr);
        *static_cast<int*>(ptr) = test_data_[i].second;
        const bool grew = segment.setNamedAddress(test_data_[i].first.c_str(),
                                                  ptr);
        ASSERT_FALSE(grew);
    }
}

bool
ZoneTableSegmentMappedTest::verifyData(const MemorySegment& segment) {
    // One by one, verify all the elements in test_data_ exist and have
    // the expected values.
    for (int i = 0; i < test_data_.size(); ++i) {
        const MemorySegment::NamedAddressResult result =
            segment.getNamedAddress(test_data_[i].first.c_str());
        if (!result.first) {
            return (false);
        }
        if (*static_cast<int*>(result.second) != test_data_[i].second) {
            return (false);
        }
    }

    return (true);
}

Mukund Sivaraman's avatar
Mukund Sivaraman committed
158
159
160
void
ZoneTableSegmentMappedTest::setupMappedFiles() {
    ztable_segment_->reset(ZoneTableSegment::CREATE, config_params_);
161
    addData(ztable_segment_->getMemorySegment());
Mukund Sivaraman's avatar
Mukund Sivaraman committed
162
163
164
    EXPECT_TRUE(verifyData(ztable_segment_->getMemorySegment()));

    ztable_segment_->reset(ZoneTableSegment::CREATE, config_params2_);
165
    addData(ztable_segment_->getMemorySegment());
Mukund Sivaraman's avatar
Mukund Sivaraman committed
166
167
168
169
170
    EXPECT_TRUE(verifyData(ztable_segment_->getMemorySegment()));

    // Now, clear the segment, closing the underlying mapped file.
    ztable_segment_->clear();
}
171

172
173
TEST_F(ZoneTableSegmentMappedTest, getHeaderUninitialized) {
    // This should throw as we haven't called reset() yet.
174
    EXPECT_THROW(ztable_segment_->getHeader(), isc::InvalidOperation);
175
176
177
178
}

TEST_F(ZoneTableSegmentMappedTest, getMemorySegmentUninitialized) {
    // This should throw as we haven't called reset() yet.
179
    EXPECT_THROW(ztable_segment_->getMemorySegment(), isc::InvalidOperation);
180
181
182
}

TEST_F(ZoneTableSegmentMappedTest, isWritableUninitialized) {
183
184
185
    // isWritable() must return false by default, when the segment has
    // not been reset() yet.
    EXPECT_FALSE(ztable_segment_->isWritable());
186
187
188
}

TEST_F(ZoneTableSegmentMappedTest, resetBadConfig) {
189
190
191
192
    // Open a mapped file in create mode.
    ztable_segment_->reset(ZoneTableSegment::CREATE, config_params_);

    // Populate it with some data.
193
    addData(ztable_segment_->getMemorySegment());
194
195
196
197
198
199
    EXPECT_TRUE(verifyData(ztable_segment_->getMemorySegment()));

    // All the following resets() with invalid configuration must
    // provide a strong exception guarantee that the segment is still
    // usable as before.

200
201
202
203
204
205
    // NULL is passed in config params
    EXPECT_THROW({
        ztable_segment_->reset(ZoneTableSegment::CREATE,
                               ConstElementPtr());
    }, isc::InvalidParameter);

206
207
    EXPECT_TRUE(verifyData(ztable_segment_->getMemorySegment()));

208
209
    // Not a map
    EXPECT_THROW({
210
        ztable_segment_->reset(ZoneTableSegment::CREATE,
211
212
213
                               Element::fromJSON("42"));
    }, isc::InvalidParameter);

214
215
    EXPECT_TRUE(verifyData(ztable_segment_->getMemorySegment()));

216
217
    // Empty map
    EXPECT_THROW({
218
        ztable_segment_->reset(ZoneTableSegment::CREATE,
219
220
221
                               Element::fromJSON("{}"));
    }, isc::InvalidParameter);

222
223
    EXPECT_TRUE(verifyData(ztable_segment_->getMemorySegment()));

224
225
    // No "mapped-file" key
    EXPECT_THROW({
226
        ztable_segment_->reset(ZoneTableSegment::CREATE,
227
228
229
                               Element::fromJSON("{\"foo\": \"bar\"}"));
    }, isc::InvalidParameter);

230
231
    EXPECT_TRUE(verifyData(ztable_segment_->getMemorySegment()));

232
233
    // Value of "mapped-file" key is not a string
    EXPECT_THROW({
234
        ztable_segment_->reset(ZoneTableSegment::CREATE,
235
236
237
                               Element::fromJSON("{\"mapped-file\": 42}"));
    }, isc::InvalidParameter);

238
    EXPECT_TRUE(verifyData(ztable_segment_->getMemorySegment()));
239
240
241
242
243
244
}

TEST_F(ZoneTableSegmentMappedTest, reset) {
    // By default, the mapped file doesn't exist, so we cannot open it
    // in READ_ONLY mode (which does not create the file).
    EXPECT_THROW({
245
        ztable_segment_->reset(ZoneTableSegment::READ_ONLY, config_params_);
246
247
248
    }, MemorySegmentOpenError);

    // The following should still throw, unaffected by the failed open.
249
250
    EXPECT_THROW(ztable_segment_->getHeader(), isc::InvalidOperation);
    EXPECT_THROW(ztable_segment_->getMemorySegment(), isc::InvalidOperation);
251
252
253
254

    // isWritable() must still return false, because the segment has not
    // been successfully reset() yet.
    EXPECT_FALSE(ztable_segment_->isWritable());
255
256
257

    // READ_WRITE mode must create the mapped file if it doesn't exist
    // (and must not result in an exception).
258
    ztable_segment_->reset(ZoneTableSegment::READ_WRITE, config_params_);
259
260
261
262
263
264
265
266
    EXPECT_TRUE(ztable_segment_->isWritable());

    // The following method calls should no longer throw:
    EXPECT_NO_THROW(ztable_segment_->getHeader());
    EXPECT_NO_THROW(ztable_segment_->getMemorySegment());

    // Let's try to re-open the mapped file in READ_ONLY mode. It should
    // not fail now.
267
    ztable_segment_->reset(ZoneTableSegment::READ_ONLY, config_params_);
268
269
270
271
272
    EXPECT_FALSE(ztable_segment_->isWritable());

    // Re-creating the mapped file should erase old data and should not
    // trigger any exceptions inside reset() due to old data (such as
    // named addresses).
273
    ztable_segment_->reset(ZoneTableSegment::CREATE, config_params_);
274
275
    EXPECT_TRUE(ztable_segment_->isWritable());

276
277
    // When we reset() with an invalid paramter and it fails, then the
    // segment should still be usable.
278
    EXPECT_THROW({
279
        ztable_segment_->reset(ZoneTableSegment::CREATE,
280
281
                               Element::fromJSON("{}"));
    }, isc::InvalidParameter);
282
283
284
285
    EXPECT_TRUE(ztable_segment_->isWritable());
    // The following should not throw.
    EXPECT_NO_THROW(ztable_segment_->getHeader());
    EXPECT_NO_THROW(ztable_segment_->getMemorySegment());
286
287

    // READ_WRITE with an existing map file ought to work too. This
288
289
    // would use existing named addresses. This actually re-opens the
    // currently open map.
290
    ztable_segment_->reset(ZoneTableSegment::READ_WRITE, config_params_);
291
    EXPECT_TRUE(ztable_segment_->isWritable());
292
293
}

294
295
296
297
298
299
300
301
302
TEST_F(ZoneTableSegmentMappedTest, resetCreate) {
    // At this point, the underlying file must not exist.
    ASSERT_FALSE(fileExists(mapped_file));

    // Open the underlying mapped file in create mode.
    ztable_segment_->reset(ZoneTableSegment::CREATE, config_params_);

    ASSERT_TRUE(ztable_segment_->isWritable());

303
304
    // Add the data.
    addData(ztable_segment_->getMemorySegment());
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
    EXPECT_TRUE(verifyData(ztable_segment_->getMemorySegment()));

    // Close the segment.
    ztable_segment_->clear();

    // At this point, the underlying file must still exist.
    ASSERT_TRUE(fileExists(mapped_file));

    // Open the underlying mapped file in create mode again.
    ztable_segment_->reset(ZoneTableSegment::CREATE, config_params_);

    // The old data should be gone.
    EXPECT_FALSE(verifyData(ztable_segment_->getMemorySegment()));
}

TEST_F(ZoneTableSegmentMappedTest, resetReadWrite) {
    // At this point, the underlying file must not exist.
    ASSERT_FALSE(fileExists(mapped_file));

    // Open the underlying mapped file in read+write mode.
    ztable_segment_->reset(ZoneTableSegment::READ_WRITE, config_params_);

    ASSERT_TRUE(ztable_segment_->isWritable());

329
330
    // Add the data.
    addData(ztable_segment_->getMemorySegment());
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
    EXPECT_TRUE(verifyData(ztable_segment_->getMemorySegment()));

    // Close the segment.
    ztable_segment_->clear();

    // At this point, the underlying file must still exist.
    ASSERT_TRUE(fileExists(mapped_file));

    // Open the underlying mapped file in read+write mode again.
    ztable_segment_->reset(ZoneTableSegment::READ_WRITE, config_params_);

    // The old data should still be available.
    EXPECT_TRUE(verifyData(ztable_segment_->getMemorySegment()));
}

TEST_F(ZoneTableSegmentMappedTest, resetReadOnly) {
    // At this point, the underlying file must not exist.
    ASSERT_FALSE(fileExists(mapped_file));

    // Open the underlying mapped file in read+write mode.
    ztable_segment_->reset(ZoneTableSegment::READ_WRITE, config_params_);

    ASSERT_TRUE(ztable_segment_->isWritable());

355
356
    // Add the data.
    addData(ztable_segment_->getMemorySegment());
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
    EXPECT_TRUE(verifyData(ztable_segment_->getMemorySegment()));

    // Close the segment.
    ztable_segment_->clear();

    // At this point, the underlying file must still exist.
    ASSERT_TRUE(fileExists(mapped_file));

    // Open the underlying mapped file in read-only mode again.
    ztable_segment_->reset(ZoneTableSegment::READ_ONLY, config_params_);

    // The old data should still be available.
    EXPECT_TRUE(verifyData(ztable_segment_->getMemorySegment()));

    // But trying to allocate new data should result in an exception as
    // the segment is read-only!
373
    EXPECT_THROW(addData(ztable_segment_->getMemorySegment()),
374
375
376
                 MemorySegmentError);
}

377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
TEST_F(ZoneTableSegmentMappedTest, clearUninitialized) {
    // Clearing a segment that has not been reset() is a nop, as clear()
    // returns it to a fresh uninitialized state anyway.
    EXPECT_NO_THROW(ztable_segment_->clear());

    // The following should still throw, because the segment has not
    // been successfully reset() yet.
    EXPECT_THROW(ztable_segment_->getHeader(), isc::InvalidOperation);
    EXPECT_THROW(ztable_segment_->getMemorySegment(), isc::InvalidOperation);

    // isWritable() must still return false, because the segment has not
    // been successfully reset() yet.
    EXPECT_FALSE(ztable_segment_->isWritable());
}

392
TEST_F(ZoneTableSegmentMappedTest, clear) {
393
394
    // First, open an underlying mapped file in read+write mode (doesn't
    // exist yet)
395
    ztable_segment_->reset(ZoneTableSegment::READ_WRITE, config_params_);
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410

    EXPECT_TRUE(ztable_segment_->isWritable());
    // The following method calls should no longer throw:
    EXPECT_NO_THROW(ztable_segment_->getHeader());
    EXPECT_NO_THROW(ztable_segment_->getMemorySegment());

    // Now, clear the segment.
    ztable_segment_->clear();

    EXPECT_FALSE(ztable_segment_->isWritable());
    // The following method calls should now throw.
    EXPECT_THROW(ztable_segment_->getHeader(), isc::InvalidOperation);
    EXPECT_THROW(ztable_segment_->getMemorySegment(), isc::InvalidOperation);
}

Mukund Sivaraman's avatar
Mukund Sivaraman committed
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
TEST_F(ZoneTableSegmentMappedTest, resetFailedCorruptedChecksum) {
    setupMappedFiles();

    // Open mapped file 1 in read-write mode
    ztable_segment_->reset(ZoneTableSegment::READ_WRITE, config_params_);

    // Corrupt mapped file 2.
    scoped_ptr<MemorySegmentMapped> segment
        (new MemorySegmentMapped(mapped_file2,
                                 MemorySegmentMapped::OPEN_OR_CREATE));
    EXPECT_TRUE(verifyData(*segment));
    corruptChecksum(*segment);
    segment.reset();

    // Opening mapped file 2 in read-write mode should fail
    EXPECT_THROW({
427
        ztable_segment_->reset(ZoneTableSegment::READ_WRITE, config_params2_);
Mukund Sivaraman's avatar
Mukund Sivaraman committed
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
    }, ResetFailed);
}

TEST_F(ZoneTableSegmentMappedTest, resetFailedMissingChecksum) {
    setupMappedFiles();

    // Open mapped file 1 in read-write mode
    ztable_segment_->reset(ZoneTableSegment::READ_WRITE, config_params_);

    // Corrupt mapped file 2.
    scoped_ptr<MemorySegmentMapped> segment
        (new MemorySegmentMapped(mapped_file2,
                                 MemorySegmentMapped::OPEN_OR_CREATE));
    EXPECT_TRUE(verifyData(*segment));
    deleteChecksum(*segment);
    segment.reset();

    // Opening mapped file 2 in read-only mode should fail
    EXPECT_THROW({
447
        ztable_segment_->reset(ZoneTableSegment::READ_ONLY, config_params2_);
Mukund Sivaraman's avatar
Mukund Sivaraman committed
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
    }, ResetFailed);
}

TEST_F(ZoneTableSegmentMappedTest, resetFailedMissingHeader) {
    setupMappedFiles();

    // Open mapped file 1 in read-write mode
    ztable_segment_->reset(ZoneTableSegment::READ_WRITE, config_params_);

    // Corrupt mapped file 2.
    scoped_ptr<MemorySegmentMapped> segment
        (new MemorySegmentMapped(mapped_file2,
                                 MemorySegmentMapped::OPEN_OR_CREATE));
    EXPECT_TRUE(verifyData(*segment));
    deleteHeader(*segment);
    segment.reset();

    // Opening mapped file 2 in read-only mode should fail
    EXPECT_THROW({
467
        ztable_segment_->reset(ZoneTableSegment::READ_ONLY, config_params2_);
Mukund Sivaraman's avatar
Mukund Sivaraman committed
468
469
470
    }, ResetFailed);
}

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
TEST_F(ZoneTableSegmentMappedTest, resetHeaderUninitialized) {
    // This should throw as we haven't called reset() yet.
    EXPECT_THROW(ztable_segment_->resetHeader(), isc::InvalidOperation);
}

TEST_F(ZoneTableSegmentMappedTest, resetHeader) {
    // First, open an underlying mapped file in read+write mode (doesn't
    // exist yet)
    ztable_segment_->reset(ZoneTableSegment::READ_WRITE, config_params_);

    // Check if a valid ZoneTable is found.
    {
        const ZoneTableHeader& header = ztable_segment_->getHeader();
        const ZoneTable* table = header.getTable();
        EXPECT_EQ(0, table->getZoneCount());
    }

    // Grow the segment by allocating something large.
    EXPECT_THROW(ztable_segment_->getMemorySegment().allocate(1<<16),
                 MemorySegmentGrown);

    // Reset the header address. This should not throw now.
    EXPECT_NO_THROW(ztable_segment_->resetHeader());

    // Check if a valid ZoneTable is found.
    {
        const ZoneTableHeader& header = ztable_segment_->getHeader();
        const ZoneTable* table = header.getTable();
        EXPECT_EQ(0, table->getZoneCount());
    }
}

503
} // anonymous namespace