blob: b63a6301685a84e2ae780004586f0382f1480c25 [file] [log] [blame]
tfarina@chromium.orgceddfeb2014-01-30 22:51:42 +00001/*
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 Lubick8c73a592022-10-17 15:25:35 -04008#include "include/core/SkString.h"
Kevin Lubick46572b42023-01-18 13:11:06 -05009#include "include/private/base/SkTemplates.h"
Kevin Lubick19936eb2023-01-05 09:00:37 -050010#include "include/private/base/SkTo.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050011#include "include/utils/SkBase64.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050012#include "tests/Test.h"
tfarina@chromium.orgceddfeb2014-01-30 22:51:42 +000013
Kevin Lubick8c73a592022-10-17 15:25:35 -040014#include <cstring>
15
Herb Derbyf7e2ca22022-11-30 10:10:49 -050016using namespace skia_private;
17
tfarina9ea53f92014-06-24 06:50:39 -070018DEF_TEST(SkBase64, reporter) {
tfarina@chromium.orgceddfeb2014-01-30 22:51:42 +000019 char all[256];
Brian Osman50ea3c02019-02-04 10:01:53 -050020 for (int index = 0; index < 255; ++index) {
tfarina@chromium.orgceddfeb2014-01-30 22:51:42 +000021 all[index] = (signed char) (index + 1);
22 }
Brian Osman50ea3c02019-02-04 10:01:53 -050023 all[255] = 0;
tfarina@chromium.orgceddfeb2014-01-30 22:51:42 +000024
robertphillips@google.com70172882014-02-01 15:45:23 +000025 for (int offset = 0; offset < 6; ++offset) {
tfarina@chromium.orgceddfeb2014-01-30 22:51:42 +000026 size_t length = 256 - offset;
Ben Wagnerb06ebee2021-01-07 12:16:22 -050027
28 // Encode
halcanary96fcdcc2015-08-27 07:41:13 -070029 size_t encodeLength = SkBase64::Encode(all + offset, length, nullptr);
Herb Derbyf7e2ca22022-11-30 10:10:49 -050030 AutoTMalloc<char> src(encodeLength + 1);
tfarina@chromium.orgceddfeb2014-01-30 22:51:42 +000031 SkBase64::Encode(all + offset, length, src.get());
Ben Wagnerb06ebee2021-01-07 12:16:22 -050032
bsalomonfbaace02014-12-12 16:41:46 -080033 src[SkToInt(encodeLength)] = '\0';
Ben Wagnerb06ebee2021-01-07 12:16:22 -050034
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 Derbyf7e2ca22022-11-30 10:10:49 -050046 AutoTMalloc<char> dst(decodeLength);
Ben Wagnerb06ebee2021-01-07 12:16:22 -050047 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.orgceddfeb2014-01-30 22:51:42 +000055 }
56}