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 "util/stdlib/strlcpy.h" |
| 16 |
| 17 #include <string.h> |
| 18 |
| 19 #include <algorithm> |
| 20 |
| 21 #include "base/strings/string16.h" |
| 22 #include "base/strings/stringprintf.h" |
| 23 #include "gtest/gtest.h" |
| 24 |
| 25 namespace { |
| 26 |
| 27 using namespace crashpad; |
| 28 |
| 29 TEST(strlcpy, c16lcpy) { |
| 30 // Use a destination buffer that’s larger than the length passed to c16lcpy. |
| 31 // The unused portion is a guard area that must not be written to. |
| 32 struct TestBuffer { |
| 33 char16 lead_guard[64]; |
| 34 char16 data[128]; |
| 35 char16 trail_guard[64]; |
| 36 }; |
| 37 TestBuffer expected_untouched; |
| 38 memset(&expected_untouched, 0xa5, sizeof(expected_untouched)); |
| 39 |
| 40 // Test with M, é, Ā, ő, and Ḙ. This is a mix of characters that have zero and |
| 41 // nonzero low and high bytes. |
| 42 const char16 test_characters[] = {0x4d, 0xe9, 0x100, 0x151, 0x1e18}; |
| 43 |
| 44 for (size_t index = 0; index < arraysize(test_characters); ++index) { |
| 45 char16 test_character = test_characters[index]; |
| 46 SCOPED_TRACE(base::StringPrintf( |
| 47 "character index %zu, character 0x%x", index, test_character)); |
| 48 for (size_t length = 0; length < 256; ++length) { |
| 49 SCOPED_TRACE(base::StringPrintf("index %zu", length)); |
| 50 string16 test_string(length, test_character); |
| 51 |
| 52 TestBuffer destination; |
| 53 memset(&destination, 0xa5, sizeof(destination)); |
| 54 |
| 55 EXPECT_EQ(length, |
| 56 c16lcpy(destination.data, |
| 57 test_string.c_str(), |
| 58 arraysize(destination.data))); |
| 59 |
| 60 // Make sure that the destination buffer is NUL-terminated, and that as |
| 61 // much of the test string was copied as could fit. |
| 62 size_t expected_destination_length = |
| 63 std::min(length, arraysize(destination.data) - 1); |
| 64 |
| 65 EXPECT_EQ('\0', destination.data[expected_destination_length]); |
| 66 EXPECT_EQ(expected_destination_length, base::c16len(destination.data)); |
| 67 EXPECT_TRUE(base::c16memcmp(test_string.c_str(), |
| 68 destination.data, |
| 69 expected_destination_length) == 0); |
| 70 |
| 71 // Make sure that the portion of the destination buffer that was not used |
| 72 // was not touched. This includes the guard areas and the unused portion |
| 73 // of the buffer passed to c16lcpy. |
| 74 EXPECT_TRUE(base::c16memcmp(expected_untouched.lead_guard, |
| 75 destination.lead_guard, |
| 76 arraysize(destination.lead_guard)) == 0); |
| 77 size_t expected_untouched_length = |
| 78 arraysize(destination.data) - expected_destination_length - 1; |
| 79 EXPECT_TRUE( |
| 80 base::c16memcmp(expected_untouched.data, |
| 81 &destination.data[expected_destination_length + 1], |
| 82 expected_untouched_length) == 0); |
| 83 EXPECT_TRUE(base::c16memcmp(expected_untouched.trail_guard, |
| 84 destination.trail_guard, |
| 85 arraysize(destination.trail_guard)) == 0); |
| 86 } |
| 87 } |
| 88 } |
| 89 |
| 90 } // namespace |
OLD | NEW |