robpercival | 633e8bc | 2016-06-08 13:45:11 | [diff] [blame] | 1 | // 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 Drissman | 650c3b0 | 2019-01-09 22:59:26 | [diff] [blame] | 7 | #include "base/stl_util.h" |
robpercival | 633e8bc | 2016-06-08 13:45:11 | [diff] [blame] | 8 | #include "components/base32/base32.h" |
Daniel Hosseinian | f4386ba | 2019-10-22 00:29:36 | [diff] [blame] | 9 | #include "components/base32/base32_test_util.h" |
robpercival | 633e8bc | 2016-06-08 13:45:11 | [diff] [blame] | 10 | #include "testing/gtest/include/gtest/gtest.h" |
| 11 | |
| 12 | namespace base32 { |
| 13 | namespace { |
| 14 | |
| 15 | TEST(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 Drissman | 650c3b0 | 2019-01-09 22:59:26 | [diff] [blame] | 23 | for (size_t i = 0; i < base::size(expected); ++i) { |
Daniel Hosseinian | f4386ba | 2019-10-22 00:29:36 | [diff] [blame] | 24 | 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); |
robpercival | 633e8bc | 2016-06-08 13:45:11 | [diff] [blame] | 30 | } |
| 31 | } |
| 32 | |
| 33 | TEST(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 Drissman | 650c3b0 | 2019-01-09 22:59:26 | [diff] [blame] | 42 | for (size_t i = 0; i < base::size(expected); ++i) { |
Daniel Hosseinian | f4386ba | 2019-10-22 00:29:36 | [diff] [blame] | 43 | 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); |
robpercival | 633e8bc | 2016-06-08 13:45:11 | [diff] [blame] | 48 | } |
| 49 | } |
| 50 | |
| 51 | TEST(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 Hosseinian | f4386ba | 2019-10-22 00:29:36 | [diff] [blame] | 57 | 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); |
robpercival | 633e8bc | 2016-06-08 13:45:11 | [diff] [blame] | 64 | } |
| 65 | |
| 66 | } // namespace |
| 67 | } // namespace base32 |