OLD | NEW |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
| 5 #include "base/base32.h" |
| 6 |
5 #include <string> | 7 #include <string> |
6 | 8 |
7 #include "components/base32/base32.h" | |
8 #include "testing/gtest/include/gtest/gtest.h" | 9 #include "testing/gtest/include/gtest/gtest.h" |
9 | 10 |
10 namespace base32 { | 11 namespace base { |
11 namespace { | 12 namespace { |
12 | 13 |
13 TEST(Base32Test, EncodesRfcTestVectorsCorrectlyWithoutPadding) { | 14 TEST(Base32Test, EncodesRfcTestVectorsCorrectlyWithoutPadding) { |
14 // Tests from http://tools.ietf.org/html/rfc4648#section-10. | 15 // Tests from http://tools.ietf.org/html/rfc4648#section-10. |
15 constexpr char test_str[] = "foobar"; | 16 constexpr char test_str[] = "foobar"; |
16 | 17 |
17 constexpr const char* expected[] = { | 18 constexpr const char* expected[] = { |
18 "", "MY", "MZXQ", "MZXW6", "MZXW6YQ", "MZXW6YTB", "MZXW6YTBOI"}; | 19 "", "MY", "MZXQ", "MZXW6", "MZXW6YQ", "MZXW6YTB", "MZXW6YTBOI"}; |
19 | 20 |
20 // Run the tests, with one more letter in the input every pass. | 21 // Run the tests, with one more letter in the input every pass. |
21 for (size_t i = 0; i < arraysize(expected); ++i) { | 22 for (size_t i = 0; i < arraysize(expected); ++i) { |
22 std::string output = Base32Encode(base::StringPiece(test_str, i), | 23 std::string output = Base32Encode(StringPiece(test_str, i), |
23 Base32EncodePolicy::OMIT_PADDING); | 24 Base32EncodePolicy::OMIT_PADDING); |
24 EXPECT_EQ(expected[i], output); | 25 EXPECT_EQ(expected[i], output); |
25 } | 26 } |
26 } | 27 } |
27 | 28 |
28 TEST(Base32Test, EncodesRfcTestVectorsCorrectlyWithPadding) { | 29 TEST(Base32Test, EncodesRfcTestVectorsCorrectlyWithPadding) { |
29 // Tests from http://tools.ietf.org/html/rfc4648#section-10. | 30 // Tests from http://tools.ietf.org/html/rfc4648#section-10. |
30 constexpr char test_str[] = "foobar"; | 31 constexpr char test_str[] = "foobar"; |
31 | 32 |
32 constexpr const char* expected[] = { | 33 constexpr const char* expected[] = { |
33 "", "MY======", "MZXQ====", "MZXW6===", | 34 "", "MY======", "MZXQ====", "MZXW6===", |
34 "MZXW6YQ=", "MZXW6YTB", "MZXW6YTBOI======"}; | 35 "MZXW6YQ=", "MZXW6YTB", "MZXW6YTBOI======"}; |
35 | 36 |
36 // Run the tests, with one more letter in the input every pass. | 37 // Run the tests, with one more letter in the input every pass. |
37 for (size_t i = 0; i < arraysize(expected); ++i) { | 38 for (size_t i = 0; i < arraysize(expected); ++i) { |
38 std::string output = Base32Encode(base::StringPiece(test_str, i)); | 39 std::string output = Base32Encode(StringPiece(test_str, i)); |
39 EXPECT_EQ(expected[i], output); | 40 EXPECT_EQ(expected[i], output); |
40 } | 41 } |
41 } | 42 } |
42 | 43 |
43 TEST(Base32Test, EncodesSha256HashCorrectly) { | 44 TEST(Base32Test, EncodesSha256HashCorrectly) { |
44 // Useful to test with longer input than the RFC test vectors, and encoding | 45 // Useful to test with longer input than the RFC test vectors, and encoding |
45 // SHA-256 hashes is one of the use cases for this component. | 46 // SHA-256 hashes is one of the use cases for this component. |
46 constexpr char hash[] = | 47 constexpr char hash[] = |
47 "\x1f\x25\xe1\xca\xba\x4f\xf9\xb8\x27\x24\x83\x0f\xca\x60\xe4\xc2\xbe\xa8" | 48 "\x1f\x25\xe1\xca\xba\x4f\xf9\xb8\x27\x24\x83\x0f\xca\x60\xe4\xc2\xbe\xa8" |
48 "\xc3\xa9\x44\x1c\x27\xb0\xb4\x3e\x6a\x96\x94\xc7\xb8\x04"; | 49 "\xc3\xa9\x44\x1c\x27\xb0\xb4\x3e\x6a\x96\x94\xc7\xb8\x04"; |
49 std::string output = Base32Encode(base::StringPiece(hash, 32), | 50 std::string output = |
50 Base32EncodePolicy::OMIT_PADDING); | 51 Base32Encode(StringPiece(hash, 32), Base32EncodePolicy::OMIT_PADDING); |
51 EXPECT_EQ("D4S6DSV2J743QJZEQMH4UYHEYK7KRQ5JIQOCPMFUHZVJNFGHXACA", output); | 52 EXPECT_EQ("D4S6DSV2J743QJZEQMH4UYHEYK7KRQ5JIQOCPMFUHZVJNFGHXACA", output); |
52 } | 53 } |
53 | 54 |
54 } // namespace | 55 } // namespace |
55 } // namespace base32 | 56 } // namespace base |
OLD | NEW |