tfarina@chromium.org | ceddfeb | 2014-01-30 22:51:42 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2014 Google Inc. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license that can be |
| 5 | * found in the LICENSE file. |
| 6 | */ |
| 7 | |
Kevin Lubick | 8c73a59 | 2022-10-17 15:25:35 -0400 | [diff] [blame] | 8 | #include "include/core/SkString.h" |
Kevin Lubick | 46572b4 | 2023-01-18 13:11:06 -0500 | [diff] [blame] | 9 | #include "include/private/base/SkTemplates.h" |
Kevin Lubick | 19936eb | 2023-01-05 09:00:37 -0500 | [diff] [blame] | 10 | #include "include/private/base/SkTo.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 11 | #include "include/utils/SkBase64.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 12 | #include "tests/Test.h" |
tfarina@chromium.org | ceddfeb | 2014-01-30 22:51:42 +0000 | [diff] [blame] | 13 | |
Kevin Lubick | 8c73a59 | 2022-10-17 15:25:35 -0400 | [diff] [blame] | 14 | #include <cstring> |
| 15 | |
Herb Derby | f7e2ca2 | 2022-11-30 10:10:49 -0500 | [diff] [blame] | 16 | using namespace skia_private; |
| 17 | |
tfarina | 9ea53f9 | 2014-06-24 06:50:39 -0700 | [diff] [blame] | 18 | DEF_TEST(SkBase64, reporter) { |
tfarina@chromium.org | ceddfeb | 2014-01-30 22:51:42 +0000 | [diff] [blame] | 19 | char all[256]; |
Brian Osman | 50ea3c0 | 2019-02-04 10:01:53 -0500 | [diff] [blame] | 20 | for (int index = 0; index < 255; ++index) { |
tfarina@chromium.org | ceddfeb | 2014-01-30 22:51:42 +0000 | [diff] [blame] | 21 | all[index] = (signed char) (index + 1); |
| 22 | } |
Brian Osman | 50ea3c0 | 2019-02-04 10:01:53 -0500 | [diff] [blame] | 23 | all[255] = 0; |
tfarina@chromium.org | ceddfeb | 2014-01-30 22:51:42 +0000 | [diff] [blame] | 24 | |
robertphillips@google.com | 7017288 | 2014-02-01 15:45:23 +0000 | [diff] [blame] | 25 | for (int offset = 0; offset < 6; ++offset) { |
tfarina@chromium.org | ceddfeb | 2014-01-30 22:51:42 +0000 | [diff] [blame] | 26 | size_t length = 256 - offset; |
Ben Wagner | b06ebee | 2021-01-07 12:16:22 -0500 | [diff] [blame] | 27 | |
| 28 | // Encode |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 29 | size_t encodeLength = SkBase64::Encode(all + offset, length, nullptr); |
Herb Derby | f7e2ca2 | 2022-11-30 10:10:49 -0500 | [diff] [blame] | 30 | AutoTMalloc<char> src(encodeLength + 1); |
tfarina@chromium.org | ceddfeb | 2014-01-30 22:51:42 +0000 | [diff] [blame] | 31 | SkBase64::Encode(all + offset, length, src.get()); |
Ben Wagner | b06ebee | 2021-01-07 12:16:22 -0500 | [diff] [blame] | 32 | |
bsalomon | fbaace0 | 2014-12-12 16:41:46 -0800 | [diff] [blame] | 33 | src[SkToInt(encodeLength)] = '\0'; |
Ben Wagner | b06ebee | 2021-01-07 12:16:22 -0500 | [diff] [blame] | 34 | |
| 35 | // Decode |
| 36 | SkBase64::Error err; |
| 37 | |
| 38 | size_t decodeLength; |
| 39 | err = SkBase64::Decode(src.get(), encodeLength, nullptr, &decodeLength); |
| 40 | if (err != SkBase64::kNoError) { |
| 41 | REPORT_FAILURE(reporter, "err", SkString("SkBase64::Decode failed!")); |
| 42 | continue; |
| 43 | } |
| 44 | REPORTER_ASSERT(reporter, decodeLength == length); |
| 45 | |
Herb Derby | f7e2ca2 | 2022-11-30 10:10:49 -0500 | [diff] [blame] | 46 | AutoTMalloc<char> dst(decodeLength); |
Ben Wagner | b06ebee | 2021-01-07 12:16:22 -0500 | [diff] [blame] | 47 | err = SkBase64::Decode(src.get(), encodeLength, dst, &decodeLength); |
| 48 | if (err != SkBase64::kNoError) { |
| 49 | REPORT_FAILURE(reporter, "err", SkString("SkBase64::Decode failed!")); |
| 50 | continue; |
| 51 | } |
| 52 | REPORTER_ASSERT(reporter, decodeLength == length); |
| 53 | |
| 54 | REPORTER_ASSERT(reporter, (strcmp((const char*) (all + offset), dst.get()) == 0)); |
tfarina@chromium.org | ceddfeb | 2014-01-30 22:51:42 +0000 | [diff] [blame] | 55 | } |
| 56 | } |