Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
base64_unittests.cc
Go to the documentation of this file.
1// Copyright 2013 The Flutter 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 "flutter/shell/common/base64.h"
6
7#include "fml/logging.h"
8#include "gtest/gtest.h"
9
10#include <string>
11
12namespace flutter {
13namespace testing {
14
15TEST(Base64, EncodeStrings) {
16 auto test = [](const std::string& input, const std::string& output) {
17 char buffer[256];
18 size_t len = Base64::Encode(input.c_str(), input.length(), &buffer);
19 FML_CHECK(len <= 256);
20 ASSERT_EQ(len, Base64::EncodedSize(input.length()));
21 std::string actual(buffer, len);
22 ASSERT_STREQ(actual.c_str(), output.c_str());
23 };
24 // Some arbitrary strings
25 test("apple", "YXBwbGU=");
26 test("BANANA", "QkFOQU5B");
27 test("Cherry Pie", "Q2hlcnJ5IFBpZQ==");
28 test("fLoCcInAuCiNiHiLiPiLiFiCaTiOn",
29 "ZkxvQ2NJbkF1Q2lOaUhpTGlQaUxpRmlDYVRpT24=");
30 test("", "");
31}
32
33TEST(Base64, EncodeBytes) {
34 auto test = [](const uint8_t input[], size_t num, const std::string& output) {
35 char buffer[512];
36 size_t len = Base64::Encode(input, num, &buffer);
37 FML_CHECK(len <= 512);
38 ASSERT_EQ(len, Base64::EncodedSize(num));
39 std::string actual(buffer, len);
40 ASSERT_STREQ(actual.c_str(), output.c_str());
41 };
42 // Some arbitrary raw bytes
43 uint8_t e[] = {0x02, 0x71, 0x82, 0x81, 0x82, 0x84, 0x59};
44 test(e, sizeof(e), "AnGCgYKEWQ==");
45
46 uint8_t pi[] = {0x03, 0x24, 0x3F, 0x6A, 0x88, 0x85};
47 test(pi, sizeof(pi), "AyQ/aoiF");
48
49 uint8_t bytes[256];
50 for (int i = 0; i < 256; i++) {
51 bytes[i] = i;
52 }
53 test(bytes, sizeof(bytes),
54 "AAECAwQFBgcICQoLDA0ODxAREhMUFRYXGBkaGxwdHh8gIS"
55 "IjJCUmJygpKissLS4vMDEyMzQ1Njc4OTo7PD0+P0BBQkNERUZHSElKS0xNTk9QUVJTVFV"
56 "WV1hZWltcXV5fYGFiY2RlZmdoaWprbG1ub3BxcnN0dXZ3eHl6e3x9fn+AgYKDhIWGh4iJ"
57 "iouMjY6PkJGSk5SVlpeYmZqbnJ2en6ChoqOkpaanqKmqq6ytrq+wsbKztLW2t7i5uru8v"
58 "b6/wMHCw8TFxsfIycrLzM3Oz9DR0tPU1dbX2Nna29zd3t/g4eLj5OXm5+jp6uvs7e7v8P"
59 "Hy8/T19vf4+fr7/P3+/w==");
60}
61
62TEST(Base64, DecodeStringsSuccess) {
63 auto test = [](const std::string& input, const std::string& output) {
64 char buffer[256];
65 size_t len = 0;
66 auto err = Base64::Decode(input.c_str(), input.length(), &buffer, &len);
67 ASSERT_EQ(err, Base64::Error::kNone);
68 FML_CHECK(len <= 256);
69 std::string actual(buffer, len);
70 ASSERT_STREQ(actual.c_str(), output.c_str());
71 };
72 // Some arbitrary strings
73 test("ZGF0ZQ==", "date");
74 test("RWdncGxhbnQ=", "Eggplant");
75 test("RmlzaCAmIENoaXBz", "Fish & Chips");
76 test("U3VQZVJjQWxJZlJhR2lMaVN0SWNFeFBpQWxJZE9jSW9Vcw==",
77 "SuPeRcAlIfRaGiLiStIcExPiAlIdOcIoUs");
78
79 // Spaces are ignored
80 test("Y X Bwb GU=", "apple");
81}
82
83TEST(Base64, DecodeStringsHasErrors) {
84 auto test = [](const std::string& input, Base64::Error expectedError) {
85 char buffer[256];
86 size_t len = 0;
87 auto err = Base64::Decode(input.c_str(), input.length(), &buffer, &len);
88 ASSERT_EQ(err, expectedError) << input;
89 };
90
91 test("Nuts&Bolts", Base64::Error::kBadChar);
94
95 test("RmlzaCAmIENoaXBz=", Base64::Error::kBadPadding);
96 // Some cases of bad padding may be ignored due to an internal optimization
97 // test("ZGF0ZQ=", Base64::Error::kBadPadding);
98 // test("RWdncGxhbnQ", Base64::Error::kBadPadding);
99}
100
101TEST(Base64, DecodeBytes) {
102 auto test = [](const std::string& input, const uint8_t output[], size_t num) {
103 char buffer[256];
104 size_t len = 0;
105 auto err = Base64::Decode(input.c_str(), input.length(), &buffer, &len);
106 ASSERT_EQ(err, Base64::Error::kNone);
107 FML_CHECK(len <= 256);
108 ASSERT_EQ(num, len) << input;
109 for (int i = 0; i < int(len); i++) {
110 ASSERT_EQ(uint8_t(buffer[i]), output[i]) << input << i;
111 }
112 };
113 // Some arbitrary raw bytes, same as the byte output above
114 uint8_t e[] = {0x02, 0x71, 0x82, 0x81, 0x82, 0x84, 0x59};
115 test("AnGCgYKEWQ==", e, sizeof(e));
116
117 uint8_t pi[] = {0x03, 0x24, 0x3F, 0x6A, 0x88, 0x85};
118 test("AyQ/aoiF", pi, sizeof(pi));
119}
120
121} // namespace testing
122} // namespace flutter
#define TEST(S, s, D, expected)
#define test(name)
Type::kYUV Type::kRGBA() int(0.7 *637)
#define FML_CHECK(condition)
Definition logging.h:85
DEF_SWITCHES_START aot vmservice shared library Name of the *so containing AOT compiled Dart assets for launching the service isolate vm snapshot The VM snapshot data that will be memory mapped as read only SnapshotAssetPath must be present isolate snapshot The isolate snapshot data that will be memory mapped as read only SnapshotAssetPath must be present cache dir Path to the cache directory This is different from the persistent_cache_path in embedder which is used for Skia shader cache icu native lib Path to the library file that exports the ICU data vm service The hostname IP address on which the Dart VM Service should be served If not defaults to or::depending on whether ipv6 is specified vm service A custom Dart VM Service port The default is to pick a randomly available open port disable vm Disable the Dart VM Service The Dart VM Service is never available in release mode disable vm service Disable mDNS Dart VM Service publication Bind to the IPv6 localhost address for the Dart VM Service Ignored if vm service host is set endless trace buffer
Definition switches.h:126
static size_t Encode(const void *src, size_t length, void *dst)
Definition base64.cc:118
static Error Decode(const void *src, size_t srcLength, void *dst, size_t *dstLength)
Definition base64.cc:28
static size_t EncodedSize(size_t srcDataLength)
Definition base64.h:33