OLD | NEW |
| (Empty) |
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 | |
7 #include "components/base32/base32.h" | |
8 #include "testing/gtest/include/gtest/gtest.h" | |
9 | |
10 namespace base32 { | |
11 namespace { | |
12 | |
13 TEST(Base32Test, EncodesRfcTestVectorsCorrectlyWithoutPadding) { | |
14 // Tests from http://tools.ietf.org/html/rfc4648#section-10. | |
15 constexpr char test_str[] = "foobar"; | |
16 | |
17 constexpr const char* expected[] = { | |
18 "", "MY", "MZXQ", "MZXW6", "MZXW6YQ", "MZXW6YTB", "MZXW6YTBOI"}; | |
19 | |
20 // Run the tests, with one more letter in the input every pass. | |
21 for (size_t i = 0; i < arraysize(expected); ++i) { | |
22 std::string output = Base32Encode(base::StringPiece(test_str, i), | |
23 Base32EncodePolicy::OMIT_PADDING); | |
24 EXPECT_EQ(expected[i], output); | |
25 } | |
26 } | |
27 | |
28 TEST(Base32Test, EncodesRfcTestVectorsCorrectlyWithPadding) { | |
29 // Tests from http://tools.ietf.org/html/rfc4648#section-10. | |
30 constexpr char test_str[] = "foobar"; | |
31 | |
32 constexpr const char* expected[] = { | |
33 "", "MY======", "MZXQ====", "MZXW6===", | |
34 "MZXW6YQ=", "MZXW6YTB", "MZXW6YTBOI======"}; | |
35 | |
36 // Run the tests, with one more letter in the input every pass. | |
37 for (size_t i = 0; i < arraysize(expected); ++i) { | |
38 std::string output = Base32Encode(base::StringPiece(test_str, i)); | |
39 EXPECT_EQ(expected[i], output); | |
40 } | |
41 } | |
42 | |
43 TEST(Base32Test, EncodesSha256HashCorrectly) { | |
44 // 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 constexpr char hash[] = | |
47 "\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 std::string output = Base32Encode(base::StringPiece(hash, 32), | |
50 Base32EncodePolicy::OMIT_PADDING); | |
51 EXPECT_EQ("D4S6DSV2J743QJZEQMH4UYHEYK7KRQ5JIQOCPMFUHZVJNFGHXACA", output); | |
52 } | |
53 | |
54 } // namespace | |
55 } // namespace base32 | |
OLD | NEW |