]>
Commit | Line | Data |
---|---|---|
7c673cae FG |
1 | // -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*- |
2 | // vim: ts=8 sw=2 smarttab | |
3 | ||
4 | #include "journal/Entry.h" | |
5 | #include "gtest/gtest.h" | |
6 | ||
7 | class TestEntry : public ::testing::Test { | |
8 | }; | |
9 | ||
10 | TEST_F(TestEntry, DefaultConstructor) { | |
11 | journal::Entry entry; | |
12 | ASSERT_EQ(0U, entry.get_entry_tid()); | |
13 | ASSERT_EQ(0U, entry.get_tag_tid()); | |
14 | ||
15 | bufferlist data(entry.get_data()); | |
16 | bufferlist expected_data; | |
17 | ASSERT_TRUE(data.contents_equal(expected_data)); | |
18 | } | |
19 | ||
20 | TEST_F(TestEntry, Constructor) { | |
21 | bufferlist data; | |
22 | data.append("data"); | |
23 | journal::Entry entry(234, 123, data); | |
24 | ||
25 | data.clear(); | |
26 | data = entry.get_data(); | |
27 | ||
28 | bufferlist expected_data; | |
29 | expected_data.append("data"); | |
30 | ||
31 | ASSERT_EQ(123U, entry.get_entry_tid()); | |
32 | ASSERT_EQ(234U, entry.get_tag_tid()); | |
33 | ASSERT_TRUE(data.contents_equal(expected_data)); | |
34 | } | |
35 | ||
36 | TEST_F(TestEntry, IsReadable) { | |
37 | bufferlist data; | |
38 | data.append("data"); | |
39 | journal::Entry entry(234, 123, data); | |
40 | ||
41 | bufferlist full_bl; | |
11fdf7f2 | 42 | encode(entry, full_bl); |
7c673cae FG |
43 | |
44 | uint32_t bytes_needed; | |
45 | for (size_t i = 0; i < full_bl.length() - 1; ++i) { | |
46 | bufferlist partial_bl; | |
47 | if (i > 0) { | |
48 | partial_bl.substr_of(full_bl, 0, i); | |
49 | } | |
50 | ASSERT_FALSE(journal::Entry::is_readable(partial_bl.begin(), | |
51 | &bytes_needed)); | |
52 | ASSERT_GT(bytes_needed, 0U); | |
53 | } | |
54 | ASSERT_TRUE(journal::Entry::is_readable(full_bl.begin(), &bytes_needed)); | |
55 | ASSERT_EQ(0U, bytes_needed); | |
56 | } | |
57 | ||
58 | TEST_F(TestEntry, IsReadableBadPreamble) { | |
59 | bufferlist data; | |
60 | data.append("data"); | |
61 | journal::Entry entry(234, 123, data); | |
62 | ||
63 | uint64_t stray_bytes = 0x1122334455667788; | |
64 | bufferlist full_bl; | |
11fdf7f2 TL |
65 | encode(stray_bytes, full_bl); |
66 | encode(entry, full_bl); | |
7c673cae FG |
67 | |
68 | uint32_t bytes_needed; | |
69 | bufferlist::iterator it = full_bl.begin(); | |
70 | ASSERT_FALSE(journal::Entry::is_readable(it, &bytes_needed)); | |
71 | ASSERT_EQ(0U, bytes_needed); | |
72 | ||
73 | it.advance(sizeof(stray_bytes)); | |
74 | ASSERT_TRUE(journal::Entry::is_readable(it, &bytes_needed)); | |
75 | ASSERT_EQ(0U, bytes_needed); | |
76 | } | |
77 | ||
78 | TEST_F(TestEntry, IsReadableBadCRC) { | |
79 | bufferlist data; | |
80 | data.append("data"); | |
81 | journal::Entry entry(234, 123, data); | |
82 | ||
83 | bufferlist full_bl; | |
11fdf7f2 | 84 | encode(entry, full_bl); |
7c673cae FG |
85 | |
86 | bufferlist bad_bl; | |
87 | bad_bl.substr_of(full_bl, 0, full_bl.length() - 4); | |
11fdf7f2 | 88 | encode(full_bl.crc32c(1), bad_bl); |
7c673cae FG |
89 | |
90 | uint32_t bytes_needed; | |
91 | ASSERT_FALSE(journal::Entry::is_readable(bad_bl.begin(), &bytes_needed)); | |
92 | ASSERT_EQ(0U, bytes_needed); | |
93 | ||
94 | ||
95 | ||
96 | } |