| OLD | NEW |
| 1 // Copyright 2014 The Crashpad Authors. All rights reserved. | 1 // Copyright 2014 The Crashpad Authors. All rights reserved. |
| 2 // | 2 // |
| 3 // Licensed under the Apache License, Version 2.0 (the "License"); | 3 // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 // you may not use this file except in compliance with 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 | 5 // You may obtain a copy of the License at |
| 6 // | 6 // |
| 7 // http://www.apache.org/licenses/LICENSE-2.0 | 7 // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 // | 8 // |
| 9 // Unless required by applicable law or agreed to in writing, software | 9 // Unless required by applicable law or agreed to in writing, software |
| 10 // distributed under the License is distributed on an "AS IS" BASIS, | 10 // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 // See the License for the specific language governing permissions and | 12 // See the License for the specific language governing permissions and |
| 13 // limitations under the License. | 13 // limitations under the License. |
| 14 | 14 |
| 15 #include "minidump/minidump_simple_string_dictionary_writer.h" | 15 #include "minidump/minidump_simple_string_dictionary_writer.h" |
| 16 | 16 |
| 17 #include <map> |
| 17 #include <string> | 18 #include <string> |
| 18 | 19 |
| 19 #include "gtest/gtest.h" | 20 #include "gtest/gtest.h" |
| 20 #include "minidump/minidump_extensions.h" | 21 #include "minidump/minidump_extensions.h" |
| 21 #include "minidump/test/minidump_string_writer_test_util.h" | 22 #include "minidump/test/minidump_string_writer_test_util.h" |
| 22 #include "minidump/test/minidump_writable_test_util.h" | 23 #include "minidump/test/minidump_writable_test_util.h" |
| 23 #include "util/file/string_file_writer.h" | 24 #include "util/file/string_file_writer.h" |
| 24 | 25 |
| 25 namespace crashpad { | 26 namespace crashpad { |
| 26 namespace test { | 27 namespace test { |
| 27 namespace { | 28 namespace { |
| 28 | 29 |
| 29 const MinidumpSimpleStringDictionary* MinidumpSimpleStringDictionaryAtStart( | 30 const MinidumpSimpleStringDictionary* MinidumpSimpleStringDictionaryAtStart( |
| 30 const std::string& file_contents, | 31 const std::string& file_contents, |
| 31 size_t count) { | 32 size_t count) { |
| 32 MINIDUMP_LOCATION_DESCRIPTOR location_descriptor; | 33 MINIDUMP_LOCATION_DESCRIPTOR location_descriptor; |
| 33 location_descriptor.DataSize = | 34 location_descriptor.DataSize = |
| 34 sizeof(MinidumpSimpleStringDictionary) + | 35 sizeof(MinidumpSimpleStringDictionary) + |
| 35 count * sizeof(MinidumpSimpleStringDictionaryEntry); | 36 count * sizeof(MinidumpSimpleStringDictionaryEntry); |
| 36 location_descriptor.Rva = 0; | 37 location_descriptor.Rva = 0; |
| 37 return MinidumpWritableAtLocationDescriptor<MinidumpSimpleStringDictionary>( | 38 return MinidumpWritableAtLocationDescriptor<MinidumpSimpleStringDictionary>( |
| 38 file_contents, location_descriptor); | 39 file_contents, location_descriptor); |
| 39 } | 40 } |
| 40 | 41 |
| 41 TEST(MinidumpSimpleStringDictionaryWriter, EmptySimpleStringDictionary) { | 42 TEST(MinidumpSimpleStringDictionaryWriter, EmptySimpleStringDictionary) { |
| 42 StringFileWriter file_writer; | 43 StringFileWriter file_writer; |
| 43 | 44 |
| 44 MinidumpSimpleStringDictionaryWriter dictionary_writer; | 45 MinidumpSimpleStringDictionaryWriter dictionary_writer; |
| 45 | 46 |
| 47 EXPECT_FALSE(dictionary_writer.IsUseful()); |
| 48 |
| 46 EXPECT_TRUE(dictionary_writer.WriteEverything(&file_writer)); | 49 EXPECT_TRUE(dictionary_writer.WriteEverything(&file_writer)); |
| 47 ASSERT_EQ(sizeof(MinidumpSimpleStringDictionary), | 50 ASSERT_EQ(sizeof(MinidumpSimpleStringDictionary), |
| 48 file_writer.string().size()); | 51 file_writer.string().size()); |
| 49 | 52 |
| 50 const MinidumpSimpleStringDictionary* dictionary = | 53 const MinidumpSimpleStringDictionary* dictionary = |
| 51 MinidumpSimpleStringDictionaryAtStart(file_writer.string(), 0); | 54 MinidumpSimpleStringDictionaryAtStart(file_writer.string(), 0); |
| 52 ASSERT_TRUE(dictionary); | 55 ASSERT_TRUE(dictionary); |
| 53 EXPECT_EQ(0u, dictionary->count); | 56 EXPECT_EQ(0u, dictionary->count); |
| 54 } | 57 } |
| 55 | 58 |
| 56 TEST(MinidumpSimpleStringDictionaryWriter, EmptyKeyValue) { | 59 TEST(MinidumpSimpleStringDictionaryWriter, EmptyKeyValue) { |
| 57 StringFileWriter file_writer; | 60 StringFileWriter file_writer; |
| 58 | 61 |
| 59 MinidumpSimpleStringDictionaryWriter dictionary_writer; | 62 MinidumpSimpleStringDictionaryWriter dictionary_writer; |
| 60 auto entry_writer = | 63 auto entry_writer = |
| 61 make_scoped_ptr(new MinidumpSimpleStringDictionaryEntryWriter()); | 64 make_scoped_ptr(new MinidumpSimpleStringDictionaryEntryWriter()); |
| 62 dictionary_writer.AddEntry(entry_writer.Pass()); | 65 dictionary_writer.AddEntry(entry_writer.Pass()); |
| 63 | 66 |
| 67 EXPECT_TRUE(dictionary_writer.IsUseful()); |
| 68 |
| 64 EXPECT_TRUE(dictionary_writer.WriteEverything(&file_writer)); | 69 EXPECT_TRUE(dictionary_writer.WriteEverything(&file_writer)); |
| 65 ASSERT_EQ(sizeof(MinidumpSimpleStringDictionary) + | 70 ASSERT_EQ(sizeof(MinidumpSimpleStringDictionary) + |
| 66 sizeof(MinidumpSimpleStringDictionaryEntry) + | 71 sizeof(MinidumpSimpleStringDictionaryEntry) + |
| 67 2 * sizeof(MinidumpUTF8String) + 1 + 3 + 1, // 3 for padding | 72 2 * sizeof(MinidumpUTF8String) + 1 + 3 + 1, // 3 for padding |
| 68 file_writer.string().size()); | 73 file_writer.string().size()); |
| 69 | 74 |
| 70 const MinidumpSimpleStringDictionary* dictionary = | 75 const MinidumpSimpleStringDictionary* dictionary = |
| 71 MinidumpSimpleStringDictionaryAtStart(file_writer.string(), 1); | 76 MinidumpSimpleStringDictionaryAtStart(file_writer.string(), 1); |
| 72 ASSERT_TRUE(dictionary); | 77 ASSERT_TRUE(dictionary); |
| 73 EXPECT_EQ(1u, dictionary->count); | 78 EXPECT_EQ(1u, dictionary->count); |
| (...skipping 12 matching lines...) Expand all Loading... |
| 86 | 91 |
| 87 char kKey[] = "key"; | 92 char kKey[] = "key"; |
| 88 char kValue[] = "value"; | 93 char kValue[] = "value"; |
| 89 | 94 |
| 90 MinidumpSimpleStringDictionaryWriter dictionary_writer; | 95 MinidumpSimpleStringDictionaryWriter dictionary_writer; |
| 91 auto entry_writer = | 96 auto entry_writer = |
| 92 make_scoped_ptr(new MinidumpSimpleStringDictionaryEntryWriter()); | 97 make_scoped_ptr(new MinidumpSimpleStringDictionaryEntryWriter()); |
| 93 entry_writer->SetKeyValue(kKey, kValue); | 98 entry_writer->SetKeyValue(kKey, kValue); |
| 94 dictionary_writer.AddEntry(entry_writer.Pass()); | 99 dictionary_writer.AddEntry(entry_writer.Pass()); |
| 95 | 100 |
| 101 EXPECT_TRUE(dictionary_writer.IsUseful()); |
| 102 |
| 96 EXPECT_TRUE(dictionary_writer.WriteEverything(&file_writer)); | 103 EXPECT_TRUE(dictionary_writer.WriteEverything(&file_writer)); |
| 97 ASSERT_EQ(sizeof(MinidumpSimpleStringDictionary) + | 104 ASSERT_EQ(sizeof(MinidumpSimpleStringDictionary) + |
| 98 sizeof(MinidumpSimpleStringDictionaryEntry) + | 105 sizeof(MinidumpSimpleStringDictionaryEntry) + |
| 99 2 * sizeof(MinidumpUTF8String) + sizeof(kKey) + sizeof(kValue), | 106 2 * sizeof(MinidumpUTF8String) + sizeof(kKey) + sizeof(kValue), |
| 100 file_writer.string().size()); | 107 file_writer.string().size()); |
| 101 | 108 |
| 102 const MinidumpSimpleStringDictionary* dictionary = | 109 const MinidumpSimpleStringDictionary* dictionary = |
| 103 MinidumpSimpleStringDictionaryAtStart(file_writer.string(), 1); | 110 MinidumpSimpleStringDictionaryAtStart(file_writer.string(), 1); |
| 104 ASSERT_TRUE(dictionary); | 111 ASSERT_TRUE(dictionary); |
| 105 EXPECT_EQ(1u, dictionary->count); | 112 EXPECT_EQ(1u, dictionary->count); |
| (...skipping 24 matching lines...) Expand all Loading... |
| 130 dictionary_writer.AddEntry(entry_writer_0.Pass()); | 137 dictionary_writer.AddEntry(entry_writer_0.Pass()); |
| 131 auto entry_writer_1 = | 138 auto entry_writer_1 = |
| 132 make_scoped_ptr(new MinidumpSimpleStringDictionaryEntryWriter()); | 139 make_scoped_ptr(new MinidumpSimpleStringDictionaryEntryWriter()); |
| 133 entry_writer_1->SetKeyValue(kKey1, kValue1); | 140 entry_writer_1->SetKeyValue(kKey1, kValue1); |
| 134 dictionary_writer.AddEntry(entry_writer_1.Pass()); | 141 dictionary_writer.AddEntry(entry_writer_1.Pass()); |
| 135 auto entry_writer_2 = | 142 auto entry_writer_2 = |
| 136 make_scoped_ptr(new MinidumpSimpleStringDictionaryEntryWriter()); | 143 make_scoped_ptr(new MinidumpSimpleStringDictionaryEntryWriter()); |
| 137 entry_writer_2->SetKeyValue(kKey2, kValue2); | 144 entry_writer_2->SetKeyValue(kKey2, kValue2); |
| 138 dictionary_writer.AddEntry(entry_writer_2.Pass()); | 145 dictionary_writer.AddEntry(entry_writer_2.Pass()); |
| 139 | 146 |
| 147 EXPECT_TRUE(dictionary_writer.IsUseful()); |
| 148 |
| 140 EXPECT_TRUE(dictionary_writer.WriteEverything(&file_writer)); | 149 EXPECT_TRUE(dictionary_writer.WriteEverything(&file_writer)); |
| 141 ASSERT_EQ(sizeof(MinidumpSimpleStringDictionary) + | 150 ASSERT_EQ(sizeof(MinidumpSimpleStringDictionary) + |
| 142 3 * sizeof(MinidumpSimpleStringDictionaryEntry) + | 151 3 * sizeof(MinidumpSimpleStringDictionaryEntry) + |
| 143 6 * sizeof(MinidumpUTF8String) + sizeof(kKey2) + | 152 6 * sizeof(MinidumpUTF8String) + sizeof(kKey2) + |
| 144 sizeof(kValue2) + 3 + sizeof(kKey0) + 1 + sizeof(kValue0) + 1 + | 153 sizeof(kValue2) + 3 + sizeof(kKey0) + 1 + sizeof(kValue0) + 1 + |
| 145 sizeof(kKey1) + 3 + sizeof(kValue1), | 154 sizeof(kKey1) + 3 + sizeof(kValue1), |
| 146 file_writer.string().size()); | 155 file_writer.string().size()); |
| 147 | 156 |
| 148 const MinidumpSimpleStringDictionary* dictionary = | 157 const MinidumpSimpleStringDictionary* dictionary = |
| 149 MinidumpSimpleStringDictionaryAtStart(file_writer.string(), 3); | 158 MinidumpSimpleStringDictionaryAtStart(file_writer.string(), 3); |
| (...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 192 MinidumpSimpleStringDictionaryWriter dictionary_writer; | 201 MinidumpSimpleStringDictionaryWriter dictionary_writer; |
| 193 auto entry_writer_0 = | 202 auto entry_writer_0 = |
| 194 make_scoped_ptr(new MinidumpSimpleStringDictionaryEntryWriter()); | 203 make_scoped_ptr(new MinidumpSimpleStringDictionaryEntryWriter()); |
| 195 entry_writer_0->SetKeyValue(kKey, kValue0); | 204 entry_writer_0->SetKeyValue(kKey, kValue0); |
| 196 dictionary_writer.AddEntry(entry_writer_0.Pass()); | 205 dictionary_writer.AddEntry(entry_writer_0.Pass()); |
| 197 auto entry_writer_1 = | 206 auto entry_writer_1 = |
| 198 make_scoped_ptr(new MinidumpSimpleStringDictionaryEntryWriter()); | 207 make_scoped_ptr(new MinidumpSimpleStringDictionaryEntryWriter()); |
| 199 entry_writer_1->SetKeyValue(kKey, kValue1); | 208 entry_writer_1->SetKeyValue(kKey, kValue1); |
| 200 dictionary_writer.AddEntry(entry_writer_1.Pass()); | 209 dictionary_writer.AddEntry(entry_writer_1.Pass()); |
| 201 | 210 |
| 211 EXPECT_TRUE(dictionary_writer.IsUseful()); |
| 212 |
| 202 EXPECT_TRUE(dictionary_writer.WriteEverything(&file_writer)); | 213 EXPECT_TRUE(dictionary_writer.WriteEverything(&file_writer)); |
| 203 ASSERT_EQ(sizeof(MinidumpSimpleStringDictionary) + | 214 ASSERT_EQ(sizeof(MinidumpSimpleStringDictionary) + |
| 204 sizeof(MinidumpSimpleStringDictionaryEntry) + | 215 sizeof(MinidumpSimpleStringDictionaryEntry) + |
| 205 2 * sizeof(MinidumpUTF8String) + sizeof(kKey) + sizeof(kValue1), | 216 2 * sizeof(MinidumpUTF8String) + sizeof(kKey) + sizeof(kValue1), |
| 206 file_writer.string().size()); | 217 file_writer.string().size()); |
| 207 | 218 |
| 208 const MinidumpSimpleStringDictionary* dictionary = | 219 const MinidumpSimpleStringDictionary* dictionary = |
| 209 MinidumpSimpleStringDictionaryAtStart(file_writer.string(), 1); | 220 MinidumpSimpleStringDictionaryAtStart(file_writer.string(), 1); |
| 210 ASSERT_TRUE(dictionary); | 221 ASSERT_TRUE(dictionary); |
| 211 EXPECT_EQ(1u, dictionary->count); | 222 EXPECT_EQ(1u, dictionary->count); |
| 212 EXPECT_EQ(12u, dictionary->entries[0].key); | 223 EXPECT_EQ(12u, dictionary->entries[0].key); |
| 213 EXPECT_EQ(20u, dictionary->entries[0].value); | 224 EXPECT_EQ(20u, dictionary->entries[0].value); |
| 214 EXPECT_EQ(kKey, | 225 EXPECT_EQ(kKey, |
| 215 MinidumpUTF8StringAtRVAAsString(file_writer.string(), | 226 MinidumpUTF8StringAtRVAAsString(file_writer.string(), |
| 216 dictionary->entries[0].key)); | 227 dictionary->entries[0].key)); |
| 217 EXPECT_EQ(kValue1, | 228 EXPECT_EQ(kValue1, |
| 218 MinidumpUTF8StringAtRVAAsString(file_writer.string(), | 229 MinidumpUTF8StringAtRVAAsString(file_writer.string(), |
| 219 dictionary->entries[0].value)); | 230 dictionary->entries[0].value)); |
| 220 } | 231 } |
| 221 | 232 |
| 233 TEST(MinidumpSimpleStringDictionaryWriter, InitializeFromMap) { |
| 234 char kKey0[] = "Dictionaries"; |
| 235 char kValue0[] = "USEFUL*"; |
| 236 char kKey1[] = "#1 Key!"; |
| 237 char kValue1[] = ""; |
| 238 char kKey2[] = "key two"; |
| 239 char kValue2[] = "value two"; |
| 240 |
| 241 std::map<std::string, std::string> map; |
| 242 map[kKey0] = kValue0; |
| 243 map[kKey1] = kValue1; |
| 244 map[kKey2] = kValue2; |
| 245 |
| 246 MinidumpSimpleStringDictionaryWriter dictionary_writer; |
| 247 dictionary_writer.InitializeFromMap(map); |
| 248 |
| 249 EXPECT_TRUE(dictionary_writer.IsUseful()); |
| 250 |
| 251 StringFileWriter file_writer; |
| 252 ASSERT_TRUE(dictionary_writer.WriteEverything(&file_writer)); |
| 253 |
| 254 const MinidumpSimpleStringDictionary* dictionary = |
| 255 MinidumpSimpleStringDictionaryAtStart(file_writer.string(), map.size()); |
| 256 ASSERT_TRUE(dictionary); |
| 257 ASSERT_EQ(3u, dictionary->count); |
| 258 |
| 259 // The entries don’t appear in the order they were added. The current |
| 260 // implementation uses a std::map and sorts keys, so the entires appear in |
| 261 // alphabetical order. However, this is an implementation detail, and it’s OK |
| 262 // if the writer stops sorting in this order. Testing for a specific order is |
| 263 // just the easiest way to write this test while the writer will output things |
| 264 // in a known order. |
| 265 EXPECT_EQ(kKey1, |
| 266 MinidumpUTF8StringAtRVAAsString(file_writer.string(), |
| 267 dictionary->entries[0].key)); |
| 268 EXPECT_EQ(kValue1, |
| 269 MinidumpUTF8StringAtRVAAsString(file_writer.string(), |
| 270 dictionary->entries[0].value)); |
| 271 EXPECT_EQ(kKey0, |
| 272 MinidumpUTF8StringAtRVAAsString(file_writer.string(), |
| 273 dictionary->entries[1].key)); |
| 274 EXPECT_EQ(kValue0, |
| 275 MinidumpUTF8StringAtRVAAsString(file_writer.string(), |
| 276 dictionary->entries[1].value)); |
| 277 EXPECT_EQ(kKey2, |
| 278 MinidumpUTF8StringAtRVAAsString(file_writer.string(), |
| 279 dictionary->entries[2].key)); |
| 280 EXPECT_EQ(kValue2, |
| 281 MinidumpUTF8StringAtRVAAsString(file_writer.string(), |
| 282 dictionary->entries[2].value)); |
| 283 } |
| 284 |
| 222 } // namespace | 285 } // namespace |
| 223 } // namespace test | 286 } // namespace test |
| 224 } // namespace crashpad | 287 } // namespace crashpad |
| OLD | NEW |