xref: /aosp_15_r20/external/skia/tests/SkBase64Test.cpp (revision c8dee2aa9b3f27cf6c858bd81872bdeb2c07ed17)
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 
8 #include "include/core/SkString.h"
9 #include "include/private/base/SkTemplates.h"
10 #include "include/private/base/SkTo.h"
11 #include "src/base/SkBase64.h"
12 #include "tests/Test.h"
13 
14 #include <cstring>
15 
16 using namespace skia_private;
17 
DEF_TEST(SkBase64,reporter)18 DEF_TEST(SkBase64, reporter) {
19     char all[256];
20     for (int index = 0; index < 255; ++index) {
21         all[index] = (signed char) (index + 1);
22     }
23     all[255] = 0;
24 
25     for (int offset = 0; offset < 6; ++offset) {
26         size_t length = 256 - offset;
27 
28         // Encode
29         size_t predictedEncodeLength = SkBase64::EncodedSize(length);
30         size_t actualEncodeLength = SkBase64::Encode(all + offset, length, nullptr);
31 
32         REPORTER_ASSERT(reporter, actualEncodeLength == predictedEncodeLength,
33                         "input size %zu; output size %zu != %zu", length,
34                         actualEncodeLength, predictedEncodeLength);
35         AutoTMalloc<char> src(actualEncodeLength + 1);
36         size_t n = SkBase64::Encode(all + offset, length, src.get());
37         REPORTER_ASSERT(reporter, n == predictedEncodeLength);
38 
39         src[SkToInt(actualEncodeLength)] = '\0';
40 
41         // Decode
42         SkBase64::Error err;
43 
44         size_t decodeLength;
45         err = SkBase64::Decode(src.get(), actualEncodeLength, nullptr, &decodeLength);
46         if (err != SkBase64::kNoError) {
47             REPORT_FAILURE(reporter, "err", SkString("SkBase64::Decode failed!"));
48             continue;
49         }
50         REPORTER_ASSERT(reporter, decodeLength == length);
51 
52         AutoTMalloc<char> dst(decodeLength);
53         err = SkBase64::Decode(src.get(), actualEncodeLength, dst, &decodeLength);
54         if (err != SkBase64::kNoError) {
55             REPORT_FAILURE(reporter, "err", SkString("SkBase64::Decode failed!"));
56             continue;
57         }
58         REPORTER_ASSERT(reporter, decodeLength == length);
59 
60         REPORTER_ASSERT(reporter, (strcmp((const char*) (all + offset), dst.get()) == 0));
61     }
62 }
63