1 // Copyright (C) 2012-2016 Internet Systems Consortium, Inc. ("ISC")
2 //
3 // This Source Code Form is subject to the terms of the Mozilla Public
4 // License, v. 2.0. If a copy of the MPL was not distributed with this
5 // file, You can obtain one at http://mozilla.org/MPL/2.0/.
6
7 #include <config.h>
8
9 #include <util/tests/memory_segment_common_unittest.h>
10
11 #include <util/memory_segment_local.h>
12 #include <exceptions/exceptions.h>
13 #include <gtest/gtest.h>
14 #include <boost/scoped_ptr.hpp>
15 #include <memory>
16 #include <limits.h>
17
18 using namespace std;
19 using namespace isc::util;
20
21 namespace {
22
TEST(MemorySegmentLocal,TestLocal)23 TEST(MemorySegmentLocal, TestLocal) {
24 boost::scoped_ptr<MemorySegment> segment(new MemorySegmentLocal());
25
26 // By default, nothing is allocated.
27 EXPECT_TRUE(segment->allMemoryDeallocated());
28
29 void* ptr = segment->allocate(1024);
30
31 // Now, we have an allocation:
32 EXPECT_FALSE(segment->allMemoryDeallocated());
33
34 void* ptr2 = segment->allocate(42);
35
36 // Still:
37 EXPECT_FALSE(segment->allMemoryDeallocated());
38
39 // These should not fail, because the buffers have been allocated.
40 EXPECT_NO_FATAL_FAILURE(memset(ptr, 0, 1024));
41 EXPECT_NO_FATAL_FAILURE(memset(ptr, 0, 42));
42
43 segment->deallocate(ptr, 1024);
44
45 // Still:
46 EXPECT_FALSE(segment->allMemoryDeallocated());
47
48 segment->deallocate(ptr2, 42);
49
50 // Now, we have an deallocated everything:
51 EXPECT_TRUE(segment->allMemoryDeallocated());
52 }
53
54 /// @todo: disabled, see ticket #3510
TEST(MemorySegmentLocal,DISABLED_TestTooMuchMemory)55 TEST(MemorySegmentLocal, DISABLED_TestTooMuchMemory) {
56 boost::scoped_ptr<MemorySegment> segment(new MemorySegmentLocal());
57
58 // Although it should be perfectly fine to use the ULONG_MAX
59 // instead of LONG_MAX as the size_t value should be unsigned,
60 // Valgrind appears to be using the signed value and hence the
61 // maximum positive value is LONG_MAX for Valgrind. But, this
62 // should be sufficient to test the "too much memory" conditions.
63 EXPECT_THROW(segment->allocate(LONG_MAX), bad_alloc);
64 }
65
TEST(MemorySegmentLocal,TestBadDeallocate)66 TEST(MemorySegmentLocal, TestBadDeallocate) {
67 boost::scoped_ptr<MemorySegment> segment(new MemorySegmentLocal());
68
69 // By default, nothing is allocated.
70 EXPECT_TRUE(segment->allMemoryDeallocated());
71
72 void* ptr = segment->allocate(1024);
73
74 // Now, we have an allocation:
75 EXPECT_FALSE(segment->allMemoryDeallocated());
76
77 // This should not throw
78 EXPECT_NO_THROW(segment->deallocate(ptr, 1024));
79
80 // Now, we have an deallocated everything:
81 EXPECT_TRUE(segment->allMemoryDeallocated());
82
83 ptr = segment->allocate(1024);
84
85 // Now, we have another allocation:
86 EXPECT_FALSE(segment->allMemoryDeallocated());
87
88 // This should throw as the size passed to deallocate() is larger
89 // than what was allocated.
90 EXPECT_THROW(segment->deallocate(ptr, 2048), isc::OutOfRange);
91
92 // This should not throw
93 EXPECT_NO_THROW(segment->deallocate(ptr, 1024));
94
95 // Now, we have an deallocated everything:
96 EXPECT_TRUE(segment->allMemoryDeallocated());
97 }
98
TEST(MemorySegmentLocal,TestNullDeallocate)99 TEST(MemorySegmentLocal, TestNullDeallocate) {
100 boost::scoped_ptr<MemorySegment> segment(new MemorySegmentLocal());
101
102 // By default, nothing is allocated.
103 EXPECT_TRUE(segment->allMemoryDeallocated());
104
105 // NULL deallocation is a no-op.
106 EXPECT_NO_THROW(segment->deallocate(NULL, 1024));
107
108 // This should still return true.
109 EXPECT_TRUE(segment->allMemoryDeallocated());
110 }
111
TEST(MemorySegmentLocal,namedAddress)112 TEST(MemorySegmentLocal, namedAddress) {
113 MemorySegmentLocal segment;
114 isc::util::test::checkSegmentNamedAddress(segment, true);
115 }
116
117 } // anonymous namespace
118