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