blob: 49b8e64ed25d62127d966aac19c905d7f21a121c [file] [log] [blame]
robpercival633e8bc2016-06-08 13:45:111// Copyright 2016 The Chromium 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 <string>
6
Avi Drissman650c3b02019-01-09 22:59:267#include "base/stl_util.h"
robpercival633e8bc2016-06-08 13:45:118#include "components/base32/base32.h"
Daniel Hosseinianf4386ba2019-10-22 00:29:369#include "components/base32/base32_test_util.h"
robpercival633e8bc2016-06-08 13:45:1110#include "testing/gtest/include/gtest/gtest.h"
11
12namespace base32 {
13namespace {
14
15TEST(Base32Test, EncodesRfcTestVectorsCorrectlyWithoutPadding) {
16 // Tests from http://tools.ietf.org/html/rfc4648#section-10.
17 constexpr char test_str[] = "foobar";
18
19 constexpr const char* expected[] = {
20 "", "MY", "MZXQ", "MZXW6", "MZXW6YQ", "MZXW6YTB", "MZXW6YTBOI"};
21
22 // Run the tests, with one more letter in the input every pass.
Avi Drissman650c3b02019-01-09 22:59:2623 for (size_t i = 0; i < base::size(expected); ++i) {
Daniel Hosseinianf4386ba2019-10-22 00:29:3624 base::StringPiece test_substr(test_str, i);
25 std::string encoded_output =
26 Base32Encode(test_substr, Base32EncodePolicy::OMIT_PADDING);
27 EXPECT_EQ(expected[i], encoded_output);
28 std::string decoded_output = Base32Decode(encoded_output);
29 EXPECT_EQ(test_substr, decoded_output);
robpercival633e8bc2016-06-08 13:45:1130 }
31}
32
33TEST(Base32Test, EncodesRfcTestVectorsCorrectlyWithPadding) {
34 // Tests from http://tools.ietf.org/html/rfc4648#section-10.
35 constexpr char test_str[] = "foobar";
36
37 constexpr const char* expected[] = {
38 "", "MY======", "MZXQ====", "MZXW6===",
39 "MZXW6YQ=", "MZXW6YTB", "MZXW6YTBOI======"};
40
41 // Run the tests, with one more letter in the input every pass.
Avi Drissman650c3b02019-01-09 22:59:2642 for (size_t i = 0; i < base::size(expected); ++i) {
Daniel Hosseinianf4386ba2019-10-22 00:29:3643 base::StringPiece test_substr(test_str, i);
44 std::string encoded_output = Base32Encode(test_substr);
45 EXPECT_EQ(expected[i], encoded_output);
46 std::string decoded_output = Base32Decode(encoded_output);
47 EXPECT_EQ(test_substr, decoded_output);
robpercival633e8bc2016-06-08 13:45:1148 }
49}
50
51TEST(Base32Test, EncodesSha256HashCorrectly) {
52 // Useful to test with longer input than the RFC test vectors, and encoding
53 // SHA-256 hashes is one of the use cases for this component.
54 constexpr char hash[] =
55 "\x1f\x25\xe1\xca\xba\x4f\xf9\xb8\x27\x24\x83\x0f\xca\x60\xe4\xc2\xbe\xa8"
56 "\xc3\xa9\x44\x1c\x27\xb0\xb4\x3e\x6a\x96\x94\xc7\xb8\x04";
Daniel Hosseinianf4386ba2019-10-22 00:29:3657 base::StringPiece test_str(hash, 32);
58 std::string encoded_output =
59 Base32Encode(test_str, Base32EncodePolicy::OMIT_PADDING);
60 EXPECT_EQ("D4S6DSV2J743QJZEQMH4UYHEYK7KRQ5JIQOCPMFUHZVJNFGHXACA",
61 encoded_output);
62 std::string decoded_output = Base32Decode(encoded_output);
63 EXPECT_EQ(test_str, decoded_output);
robpercival633e8bc2016-06-08 13:45:1164}
65
66} // namespace
67} // namespace base32