Flutter Engine
base32_unittest.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/fml/base32.h"
6 
7 #include <iostream>
8 
9 #include "gtest/gtest.h"
10 
11 TEST(Base32Test, CanEncode) {
12  {
13  auto result = fml::Base32Encode("hello");
14  ASSERT_TRUE(result.first);
15  ASSERT_EQ(result.second, "NBSWY3DP");
16  }
17 
18  {
19  auto result = fml::Base32Encode("helLo");
20  ASSERT_TRUE(result.first);
21  ASSERT_EQ(result.second, "NBSWYTDP");
22  }
23 
24  {
25  auto result = fml::Base32Encode("");
26  ASSERT_TRUE(result.first);
27  ASSERT_EQ(result.second, "");
28  }
29 
30  {
31  auto result = fml::Base32Encode("1");
32  ASSERT_TRUE(result.first);
33  ASSERT_EQ(result.second, "GE");
34  }
35 
36  {
37  auto result = fml::Base32Encode("helLo");
38  ASSERT_TRUE(result.first);
39  ASSERT_EQ(result.second, "NBSWYTDP");
40  }
41 }
42 
43 TEST(Base32Test, CanEncodeDecodeStrings) {
44  std::vector<std::string> strings = {"hello", "helLo", "", "1", "\0"};
45  for (size_t i = 0; i < strings.size(); i += 1) {
46  auto encode_result = fml::Base32Encode(strings[i]);
47  ASSERT_TRUE(encode_result.first);
48  auto decode_result = fml::Base32Decode(encode_result.second);
49  ASSERT_TRUE(decode_result.first);
50  const std::string& decoded = decode_result.second;
51  std::string decoded_string(decoded.data(), decoded.size());
52  ASSERT_EQ(strings[i], decoded_string);
53  }
54 }
55 
56 TEST(Base32Test, DecodeReturnsFalseForInvalideInput) {
57  // "B" is invalid because it has a non-zero padding.
58  std::vector<std::string> invalid_inputs = {"a", "1", "9", "B"};
59  for (const std::string& input : invalid_inputs) {
60  auto decode_result = fml::Base32Decode(input);
61  if (decode_result.first) {
62  std::cout << "Base32Decode should return false on " << input << std::endl;
63  }
64  ASSERT_FALSE(decode_result.first);
65  }
66 }
67 
68 TEST(Base32Test, CanDecodeSkSLKeys) {
69  std::vector<std::string> inputs = {
70  "CAZAAAACAAAAADQAAAABKAAAAAJQAAIA7777777777776EYAAEAP777777777777AAAAAABA"
71  "ABTAAAAAAAAAAAAAAABAAAAAGQAGGAA",
72  "CAZAAAICAAAAAAAAAAADOAAAAAJQAAIA777777Y4AAKAAEYAAEAP777777777777EAAGMAAA"
73  "AAAAAAAAAAACQACNAAAAAAAAAAAAAAACAAAAAPAAMMAA",
74  "CAZACAACAAAABAYACAAAAAAAAAJQAAIADQABIAH777777777777RQAAOAAAAAAAAAAAAAABE"
75  "AANQAAAAAAAAAAAAAAYAAJYAAAAAAAANAAAQAAAAAAAEAAAHAAAAAAAAAAAAAAANAAAQAAAA"
76  "AAAFIADKAAAAAAAAAAAAAAACAAAAAZAAMMAA"};
77  for (const std::string& input : inputs) {
78  auto decode_result = fml::Base32Decode(input);
79  if (!decode_result.first) {
80  std::cout << "Base32Decode should return true on " << input << std::endl;
81  }
82  ASSERT_TRUE(decode_result.first);
83  auto encode_result = fml::Base32Encode(decode_result.second);
84  ASSERT_TRUE(encode_result.first);
85  ASSERT_EQ(encode_result.second, input);
86  }
87 }
std::pair< bool, std::string > Base32Encode(std::string_view input)
Definition: base32.cc:14
std::pair< bool, std::string > Base32Decode(const std::string &input)
Definition: base32.cc:54
TEST(Base32Test, CanEncode)