OLD | NEW |
(Empty) | |
| 1 // Copyright 2014 The Crashpad Authors. All rights reserved. |
| 2 // |
| 3 // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 // you may not use this file except in compliance with the License. |
| 5 // You may obtain a copy of the License at |
| 6 // |
| 7 // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 // |
| 9 // Unless required by applicable law or agreed to in writing, software |
| 10 // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 // See the License for the specific language governing permissions and |
| 13 // limitations under the License. |
| 14 |
| 15 #include "minidump/minidump_simple_string_dictionary_writer.h" |
| 16 |
| 17 #include <string> |
| 18 |
| 19 #include "gtest/gtest.h" |
| 20 #include "minidump/minidump_extensions.h" |
| 21 #include "util/file/string_file_writer.h" |
| 22 |
| 23 namespace crashpad { |
| 24 namespace test { |
| 25 namespace { |
| 26 |
| 27 const MinidumpSimpleStringDictionary* MinidumpSimpleStringDictionaryCast( |
| 28 const StringFileWriter& file_writer) { |
| 29 return reinterpret_cast<const MinidumpSimpleStringDictionary*>( |
| 30 &file_writer.string()[0]); |
| 31 } |
| 32 |
| 33 TEST(MinidumpSimpleStringDictionaryWriter, EmptySimpleStringDictionary) { |
| 34 StringFileWriter file_writer; |
| 35 |
| 36 MinidumpSimpleStringDictionaryWriter dictionary_writer; |
| 37 |
| 38 EXPECT_TRUE(dictionary_writer.WriteEverything(&file_writer)); |
| 39 ASSERT_EQ(sizeof(MinidumpSimpleStringDictionary), |
| 40 file_writer.string().size()); |
| 41 |
| 42 const MinidumpSimpleStringDictionary* dictionary = |
| 43 MinidumpSimpleStringDictionaryCast(file_writer); |
| 44 EXPECT_EQ(0u, dictionary->count); |
| 45 } |
| 46 |
| 47 std::string MinidumpUTF8StringAtRVA(const StringFileWriter& file_writer, |
| 48 RVA rva) { |
| 49 const std::string& contents = file_writer.string(); |
| 50 if (rva == 0) { |
| 51 return std::string(); |
| 52 } |
| 53 |
| 54 if (rva + sizeof(MinidumpUTF8String) > contents.size()) { |
| 55 ADD_FAILURE() |
| 56 << "rva " << rva << " too large for contents " << contents.size(); |
| 57 return std::string(); |
| 58 } |
| 59 |
| 60 const MinidumpUTF8String* minidump_string = |
| 61 reinterpret_cast<const MinidumpUTF8String*>(&contents[rva]); |
| 62 |
| 63 // Verify that the file has enough data for the string’s stated length plus |
| 64 // its required NUL terminator. |
| 65 if (rva + sizeof(MinidumpUTF8String) + minidump_string->Length + 1 > |
| 66 contents.size()) { |
| 67 ADD_FAILURE() |
| 68 << "rva " << rva << ", length " << minidump_string->Length |
| 69 << " too large for contents " << contents.size(); |
| 70 return std::string(); |
| 71 } |
| 72 |
| 73 std::string minidump_string_data( |
| 74 reinterpret_cast<const char*>(&minidump_string->Buffer[0]), |
| 75 minidump_string->Length); |
| 76 return minidump_string_data; |
| 77 } |
| 78 |
| 79 TEST(MinidumpSimpleStringDictionaryWriter, EmptyKeyValue) { |
| 80 StringFileWriter file_writer; |
| 81 |
| 82 MinidumpSimpleStringDictionaryWriter dictionary_writer; |
| 83 MinidumpSimpleStringDictionaryEntryWriter entry_writer; |
| 84 dictionary_writer.AddEntry(&entry_writer); |
| 85 |
| 86 EXPECT_TRUE(dictionary_writer.WriteEverything(&file_writer)); |
| 87 ASSERT_EQ(sizeof(MinidumpSimpleStringDictionary) + |
| 88 sizeof(MinidumpSimpleStringDictionaryEntry) + |
| 89 2 * sizeof(MinidumpUTF8String) + 1 + 3 + 1, // 3 for padding |
| 90 file_writer.string().size()); |
| 91 |
| 92 const MinidumpSimpleStringDictionary* dictionary = |
| 93 MinidumpSimpleStringDictionaryCast(file_writer); |
| 94 EXPECT_EQ(1u, dictionary->count); |
| 95 EXPECT_EQ(12u, dictionary->entries[0].key); |
| 96 EXPECT_EQ(20u, dictionary->entries[0].value); |
| 97 EXPECT_EQ("", |
| 98 MinidumpUTF8StringAtRVA(file_writer, dictionary->entries[0].key)); |
| 99 EXPECT_EQ("", |
| 100 MinidumpUTF8StringAtRVA(file_writer, dictionary->entries[0].value)); |
| 101 } |
| 102 |
| 103 TEST(MinidumpSimpleStringDictionaryWriter, OneKeyValue) { |
| 104 StringFileWriter file_writer; |
| 105 |
| 106 char kKey[] = "key"; |
| 107 char kValue[] = "value"; |
| 108 |
| 109 MinidumpSimpleStringDictionaryWriter dictionary_writer; |
| 110 MinidumpSimpleStringDictionaryEntryWriter entry_writer; |
| 111 entry_writer.SetKeyValue(kKey, kValue); |
| 112 dictionary_writer.AddEntry(&entry_writer); |
| 113 |
| 114 EXPECT_TRUE(dictionary_writer.WriteEverything(&file_writer)); |
| 115 ASSERT_EQ(sizeof(MinidumpSimpleStringDictionary) + |
| 116 sizeof(MinidumpSimpleStringDictionaryEntry) + |
| 117 2 * sizeof(MinidumpUTF8String) + sizeof(kKey) + sizeof(kValue), |
| 118 file_writer.string().size()); |
| 119 |
| 120 const MinidumpSimpleStringDictionary* dictionary = |
| 121 MinidumpSimpleStringDictionaryCast(file_writer); |
| 122 EXPECT_EQ(1u, dictionary->count); |
| 123 EXPECT_EQ(12u, dictionary->entries[0].key); |
| 124 EXPECT_EQ(20u, dictionary->entries[0].value); |
| 125 EXPECT_EQ(kKey, |
| 126 MinidumpUTF8StringAtRVA(file_writer, dictionary->entries[0].key)); |
| 127 EXPECT_EQ(kValue, |
| 128 MinidumpUTF8StringAtRVA(file_writer, dictionary->entries[0].value)); |
| 129 } |
| 130 |
| 131 TEST(MinidumpSimpleStringDictionaryWriter, ThreeKeysValues) { |
| 132 StringFileWriter file_writer; |
| 133 |
| 134 char kKey0[] = "m0"; |
| 135 char kValue0[] = "value0"; |
| 136 char kKey1[] = "zzz1"; |
| 137 char kValue1[] = "v1"; |
| 138 char kKey2[] = "aa2"; |
| 139 char kValue2[] = "val2"; |
| 140 |
| 141 MinidumpSimpleStringDictionaryWriter dictionary_writer; |
| 142 MinidumpSimpleStringDictionaryEntryWriter entry_writer_0; |
| 143 entry_writer_0.SetKeyValue(kKey0, kValue0); |
| 144 dictionary_writer.AddEntry(&entry_writer_0); |
| 145 MinidumpSimpleStringDictionaryEntryWriter entry_writer_1; |
| 146 entry_writer_1.SetKeyValue(kKey1, kValue1); |
| 147 dictionary_writer.AddEntry(&entry_writer_1); |
| 148 MinidumpSimpleStringDictionaryEntryWriter entry_writer_2; |
| 149 entry_writer_2.SetKeyValue(kKey2, kValue2); |
| 150 dictionary_writer.AddEntry(&entry_writer_2); |
| 151 |
| 152 EXPECT_TRUE(dictionary_writer.WriteEverything(&file_writer)); |
| 153 ASSERT_EQ(sizeof(MinidumpSimpleStringDictionary) + |
| 154 3 * sizeof(MinidumpSimpleStringDictionaryEntry) + |
| 155 6 * sizeof(MinidumpUTF8String) + sizeof(kKey2) + |
| 156 sizeof(kValue2) + 3 + sizeof(kKey0) + 1 + sizeof(kValue0) + 1 + |
| 157 sizeof(kKey1) + 3 + sizeof(kValue1), |
| 158 file_writer.string().size()); |
| 159 |
| 160 const MinidumpSimpleStringDictionary* dictionary = |
| 161 MinidumpSimpleStringDictionaryCast(file_writer); |
| 162 EXPECT_EQ(3u, dictionary->count); |
| 163 EXPECT_EQ(28u, dictionary->entries[0].key); |
| 164 EXPECT_EQ(36u, dictionary->entries[0].value); |
| 165 EXPECT_EQ(48u, dictionary->entries[1].key); |
| 166 EXPECT_EQ(56u, dictionary->entries[1].value); |
| 167 EXPECT_EQ(68u, dictionary->entries[2].key); |
| 168 EXPECT_EQ(80u, dictionary->entries[2].value); |
| 169 |
| 170 // The entries don’t appear in the order they were added. The current |
| 171 // implementation uses a std::map and sorts keys, so the entires appear in |
| 172 // alphabetical order. However, this is an implementation detail, and it’s OK |
| 173 // if the writer stops sorting in this order. Testing for a specific order is |
| 174 // just the easiest way to write this test while the writer will output things |
| 175 // in a known order. |
| 176 EXPECT_EQ(kKey2, |
| 177 MinidumpUTF8StringAtRVA(file_writer, dictionary->entries[0].key)); |
| 178 EXPECT_EQ(kValue2, |
| 179 MinidumpUTF8StringAtRVA(file_writer, dictionary->entries[0].value)); |
| 180 EXPECT_EQ(kKey0, |
| 181 MinidumpUTF8StringAtRVA(file_writer, dictionary->entries[1].key)); |
| 182 EXPECT_EQ(kValue0, |
| 183 MinidumpUTF8StringAtRVA(file_writer, dictionary->entries[1].value)); |
| 184 EXPECT_EQ(kKey1, |
| 185 MinidumpUTF8StringAtRVA(file_writer, dictionary->entries[2].key)); |
| 186 EXPECT_EQ(kValue1, |
| 187 MinidumpUTF8StringAtRVA(file_writer, dictionary->entries[2].value)); |
| 188 } |
| 189 |
| 190 TEST(MinidumpSimpleStringDictionaryWriter, DuplicateKeyValue) { |
| 191 StringFileWriter file_writer; |
| 192 |
| 193 char kKey[] = "key"; |
| 194 char kValue0[] = "fake_value"; |
| 195 char kValue1[] = "value"; |
| 196 |
| 197 MinidumpSimpleStringDictionaryWriter dictionary_writer; |
| 198 MinidumpSimpleStringDictionaryEntryWriter entry_writer_0; |
| 199 entry_writer_0.SetKeyValue(kKey, kValue0); |
| 200 dictionary_writer.AddEntry(&entry_writer_0); |
| 201 MinidumpSimpleStringDictionaryEntryWriter entry_writer_1; |
| 202 entry_writer_1.SetKeyValue(kKey, kValue1); |
| 203 dictionary_writer.AddEntry(&entry_writer_1); |
| 204 |
| 205 EXPECT_TRUE(dictionary_writer.WriteEverything(&file_writer)); |
| 206 ASSERT_EQ(sizeof(MinidumpSimpleStringDictionary) + |
| 207 sizeof(MinidumpSimpleStringDictionaryEntry) + |
| 208 2 * sizeof(MinidumpUTF8String) + sizeof(kKey) + sizeof(kValue1), |
| 209 file_writer.string().size()); |
| 210 |
| 211 const MinidumpSimpleStringDictionary* dictionary = |
| 212 MinidumpSimpleStringDictionaryCast(file_writer); |
| 213 EXPECT_EQ(1u, dictionary->count); |
| 214 EXPECT_EQ(12u, dictionary->entries[0].key); |
| 215 EXPECT_EQ(20u, dictionary->entries[0].value); |
| 216 EXPECT_EQ(kKey, |
| 217 MinidumpUTF8StringAtRVA(file_writer, dictionary->entries[0].key)); |
| 218 EXPECT_EQ(kValue1, |
| 219 MinidumpUTF8StringAtRVA(file_writer, dictionary->entries[0].value)); |
| 220 } |
| 221 |
| 222 } // namespace |
| 223 } // namespace test |
| 224 } // namespace crashpad |
OLD | NEW |