Denis Yaroshevskiy | 63dbcdf | 2018-03-28 02:44:10 | [diff] [blame] | 1 | // Copyright (c) 2018 The Chromium Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #include "base/logging.h" |
| 6 | #include "base/strings/old_utf_string_conversions.h" |
| 7 | #include "base/strings/utf_string_conversions.h" |
| 8 | |
| 9 | namespace { |
| 10 | |
| 11 | void UTF8ToCheck(const uint8_t* data, size_t size) { |
| 12 | const auto* src = reinterpret_cast<const char*>(data); |
| 13 | const size_t src_len = size; |
| 14 | |
| 15 | // UTF16 |
| 16 | { |
| 17 | base::string16 new_out; |
| 18 | bool new_res = base::UTF8ToUTF16(src, src_len, &new_out); |
| 19 | |
| 20 | base::string16 old_out; |
| 21 | bool old_res = base_old::UTF8ToUTF16(src, src_len, &old_out); |
| 22 | |
| 23 | CHECK(new_res == old_res); |
| 24 | CHECK(new_out == old_out); |
| 25 | } |
| 26 | |
| 27 | // Wide |
| 28 | { |
| 29 | std::wstring new_out; |
| 30 | bool new_res = base::UTF8ToWide(src, src_len, &new_out); |
| 31 | |
| 32 | std::wstring old_out; |
| 33 | bool old_res = base_old::UTF8ToWide(src, src_len, &old_out); |
| 34 | |
| 35 | CHECK(new_res == old_res); |
| 36 | CHECK(new_out == old_out); |
| 37 | } |
| 38 | } |
| 39 | |
| 40 | void UTF16ToCheck(const uint8_t* data, size_t size) { |
| 41 | const auto* src = reinterpret_cast<const base::char16*>(data); |
| 42 | const size_t src_len = size / 2; |
| 43 | |
| 44 | // UTF8 |
| 45 | { |
| 46 | std::string new_out; |
| 47 | bool new_res = base::UTF16ToUTF8(src, src_len, &new_out); |
| 48 | |
| 49 | std::string old_out; |
| 50 | bool old_res = base_old::UTF16ToUTF8(src, src_len, &old_out); |
| 51 | |
| 52 | CHECK(new_res == old_res); |
| 53 | CHECK(new_out == old_out); |
| 54 | } |
| 55 | |
| 56 | // Wide |
| 57 | { |
| 58 | std::wstring new_out; |
| 59 | bool new_res = base::UTF16ToWide(src, src_len, &new_out); |
| 60 | |
| 61 | std::wstring old_out; |
| 62 | bool old_res = base_old::UTF16ToWide(src, src_len, &old_out); |
| 63 | |
| 64 | CHECK(new_res == old_res); |
| 65 | CHECK(new_out == old_out); |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | void WideToCheck(const uint8_t* data, size_t size) { |
| 70 | const auto* src = reinterpret_cast<const wchar_t*>(data); |
| 71 | const size_t src_len = size / 4; // It's OK even if Wide is 16bit. |
| 72 | |
| 73 | // UTF8 |
| 74 | { |
| 75 | std::string new_out; |
| 76 | bool new_res = base::WideToUTF8(src, src_len, &new_out); |
| 77 | |
| 78 | std::string old_out; |
| 79 | bool old_res = base_old::WideToUTF8(src, src_len, &old_out); |
| 80 | |
| 81 | CHECK(new_res == old_res); |
| 82 | CHECK(new_out == old_out); |
| 83 | } |
| 84 | |
| 85 | // UTF16 |
| 86 | { |
| 87 | base::string16 new_out; |
| 88 | bool new_res = base::WideToUTF16(src, src_len, &new_out); |
| 89 | |
| 90 | base::string16 old_out; |
| 91 | bool old_res = base_old::WideToUTF16(src, src_len, &old_out); |
| 92 | |
| 93 | CHECK(new_res == old_res); |
| 94 | CHECK(new_out == old_out); |
| 95 | } |
| 96 | } |
| 97 | |
| 98 | } // namespace |
| 99 | |
| 100 | extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) { |
| 101 | UTF8ToCheck(data, size); |
| 102 | UTF16ToCheck(data, size); |
| 103 | WideToCheck(data, size); |
| 104 | return 0; |
| 105 | } |