Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(90)

Side by Side Diff: minidump/minidump_string_writer_test.cc

Issue 431363002: Introduce MinidumpStringWriter and its test (Closed) Base URL: https://chromium.googlesource.com/crashpad/crashpad@master
Patch Set: Rebase Created 6 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « minidump/minidump_string_writer.cc ('k') | minidump/minidump_writer_util.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(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_string_writer.h"
16
17 #include <dbghelp.h>
18
19 #include <string>
20
21 #include "base/basictypes.h"
22 #include "base/strings/string16.h"
23 #include "base/strings/stringprintf.h"
24 #include "gtest/gtest.h"
25 #include "util/file/string_file_writer.h"
26
27 namespace {
28
29 using namespace crashpad;
30 using namespace testing;
31
32 const MINIDUMP_STRING* MinidumpStringCast(const StringFileWriter& file_writer) {
33 return reinterpret_cast<const MINIDUMP_STRING*>(&file_writer.string()[0]);
34 }
35
36 TEST(MinidumpStringWriter, MinidumpUTF16StringWriter) {
37 StringFileWriter file_writer;
38
39 {
40 SCOPED_TRACE("unset");
41 file_writer.Reset();
42 crashpad::internal::MinidumpUTF16StringWriter string_writer;
43 EXPECT_TRUE(string_writer.WriteEverything(&file_writer));
44 ASSERT_EQ(6u, file_writer.string().size());
45 const MINIDUMP_STRING* minidump_string = MinidumpStringCast(file_writer);
46 EXPECT_EQ(0u, minidump_string->Length);
47 EXPECT_EQ(0, minidump_string->Buffer[0]);
48 }
49
50 const struct {
51 size_t input_length;
52 const char* input_string;
53 size_t output_length;
54 const char16 output_string[10];
55 } kTestData[] = {
56 {0, "", 0, {}},
57 {1, "a", 1, {'a'}},
58 {2, "\0b", 2, {0, 'b'}},
59 {3, "cde", 3, {'c', 'd', 'e'}},
60 {9, "Hi world!", 9, {'H', 'i', ' ', 'w', 'o', 'r', 'l', 'd', '!'}},
61 {7, "ret\nurn", 7, {'r', 'e', 't', '\n', 'u', 'r', 'n'}},
62 {2, "\303\251", 1, {0x00e9}}, // é
63
64 // oóöőo
65 {8, "o\303\263\303\266\305\221o", 5, {'o', 0x00f3, 0x00f6, 0x151, 'o'}},
66 {4, "\360\220\204\202", 2, {0xd800, 0xdd02}}, // 𐄂 (non-BMP)
67 };
68
69 for (size_t index = 0; index < arraysize(kTestData); ++index) {
70 SCOPED_TRACE(base::StringPrintf(
71 "index %zu, input %s", index, kTestData[index].input_string));
72
73 // Make sure that the expected output string with its NUL terminator fits in
74 // the space provided.
75 ASSERT_EQ(
76 0,
77 kTestData[index]
78 .output_string[arraysize(kTestData[index].output_string) - 1]);
79
80 file_writer.Reset();
81 crashpad::internal::MinidumpUTF16StringWriter string_writer;
82 string_writer.SetUTF8(std::string(kTestData[index].input_string,
83 kTestData[index].input_length));
84 EXPECT_TRUE(string_writer.WriteEverything(&file_writer));
85
86 const size_t expected_utf16_units_with_nul =
87 kTestData[index].output_length + 1;
88 const size_t expected_utf16_bytes =
89 expected_utf16_units_with_nul * sizeof(MINIDUMP_STRING::Buffer[0]);
90 ASSERT_EQ(sizeof(MINIDUMP_STRING) + expected_utf16_bytes,
91 file_writer.string().size());
92 const MINIDUMP_STRING* minidump_string = MinidumpStringCast(file_writer);
93 EXPECT_EQ(
94 kTestData[index].output_length * sizeof(minidump_string->Buffer[0]),
95 minidump_string->Length);
96 EXPECT_EQ(0,
97 base::c16memcmp(kTestData[index].output_string,
98 minidump_string->Buffer,
99 expected_utf16_units_with_nul));
100 }
101 }
102
103 TEST(MinidumpStringWriter, ConvertInvalidUTF8ToUTF16) {
104 StringFileWriter file_writer;
105
106 const char* kTestData[] = {
107 "\200", // continuation byte
108 "\300", // start byte followed by EOF
109 "\310\177", // start byte without continuation
110 "\340\200", // EOF in middle of 3-byte sequence
111 "\340\200\115", // invalid 3-byte sequence
112 "\303\0\251", // NUL in middle of valid sequence
113 };
114
115 for (size_t index = 0; index < arraysize(kTestData); ++index) {
116 SCOPED_TRACE(
117 base::StringPrintf("index %zu, input %s", index, kTestData[index]));
118 file_writer.Reset();
119 crashpad::internal::MinidumpUTF16StringWriter string_writer;
120 string_writer.SetUTF8(kTestData[index]);
121 EXPECT_TRUE(string_writer.WriteEverything(&file_writer));
122
123 // The requirements for conversion of invalid UTF-8 input are lax. Make sure
124 // that at least enough data was written for a string that has one unit and
125 // a NUL terminator, make sure that the length field matches the length of
126 // data written, make sure the data is NUL-terminated, and make sure that at
127 // least one U+FFFD replacement character was written.
128 ASSERT_GE(file_writer.string().size(),
129 sizeof(MINIDUMP_STRING) + 2 * sizeof(MINIDUMP_STRING::Buffer[0]));
130 const MINIDUMP_STRING* minidump_string = MinidumpStringCast(file_writer);
131 EXPECT_EQ(file_writer.string().size() - sizeof(MINIDUMP_STRING) -
132 sizeof(MINIDUMP_STRING::Buffer[0]),
133 minidump_string->Length);
134 size_t out_units =
135 minidump_string->Length / sizeof(minidump_string->Buffer[0]);
136 EXPECT_EQ(0, minidump_string->Buffer[out_units]);
137 EXPECT_NE(reinterpret_cast<char16*>(NULL),
138 base::c16memchr(minidump_string->Buffer, 0xfffd, out_units));
139 }
140 }
141
142 const MinidumpUTF8String* MinidumpUTF8StringCast(
143 const StringFileWriter& file_writer) {
144 return reinterpret_cast<const MinidumpUTF8String*>(&file_writer.string()[0]);
145 }
146
147 TEST(MinidumpStringWriter, MinidumpUTF8StringWriter) {
148 StringFileWriter file_writer;
149
150 {
151 SCOPED_TRACE("unset");
152 file_writer.Reset();
153 crashpad::internal::MinidumpUTF8StringWriter string_writer;
154 EXPECT_TRUE(string_writer.WriteEverything(&file_writer));
155 ASSERT_EQ(5u, file_writer.string().size());
156 const MinidumpUTF8String* minidump_string =
157 MinidumpUTF8StringCast(file_writer);
158 EXPECT_EQ(0u, minidump_string->Length);
159 EXPECT_EQ(0, minidump_string->Buffer[0]);
160 }
161
162 const struct {
163 size_t length;
164 const char* string;
165 } kTestData[] = {
166 {0, ""},
167 {1, "a"},
168 {2, "\0b"},
169 {3, "cde"},
170 {9, "Hi world!"},
171 {7, "ret\nurn"},
172 {2, "\303\251"}, // é
173
174 // oóöőo
175 {8, "o\303\263\303\266\305\221o"},
176 {4, "\360\220\204\202"}, // 𐄂 (non-BMP)
177 };
178
179 for (size_t index = 0; index < arraysize(kTestData); ++index) {
180 SCOPED_TRACE(base::StringPrintf(
181 "index %zu, input %s", index, kTestData[index].string));
182
183 file_writer.Reset();
184 crashpad::internal::MinidumpUTF8StringWriter string_writer;
185 string_writer.SetUTF8(
186 std::string(kTestData[index].string, kTestData[index].length));
187 EXPECT_TRUE(string_writer.WriteEverything(&file_writer));
188
189 const size_t expected_utf8_bytes_with_nul = kTestData[index].length + 1;
190 ASSERT_EQ(sizeof(MinidumpUTF8String) + expected_utf8_bytes_with_nul,
191 file_writer.string().size());
192 const MinidumpUTF8String* minidump_string =
193 MinidumpUTF8StringCast(file_writer);
194 EXPECT_EQ(kTestData[index].length, minidump_string->Length);
195 EXPECT_EQ(0,
196 memcmp(kTestData[index].string,
197 minidump_string->Buffer,
198 expected_utf8_bytes_with_nul));
199 }
200 }
201
202 } // namespace
OLDNEW
« no previous file with comments | « minidump/minidump_string_writer.cc ('k') | minidump/minidump_writer_util.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698