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 const std::string test_str = "foobar"; | |
16 | |
17 const std::string expected[] = {"", "MY", "MZXQ", "MZXW6", | |
18 "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; | |
23 Base32Encode(test_str.substr(0, i), Base32EncodePolicy::OMIT_PADDING, | |
sdefresne
2016/06/01 14:25:38
nit: Can you instead do the following to avoid all
Rob Percival
2016/06/01 14:37:57
Done. With it being test code, I had just opted fo
| |
24 &output); | |
25 EXPECT_EQ(expected[i], output); | |
26 } | |
27 } | |
28 | |
29 TEST(Base32Test, EncodesRfcTestVectorsCorrectlyWithPadding) { | |
30 // Tests from http://tools.ietf.org/html/rfc4648#section-10. | |
31 const std::string test_str = "foobar"; | |
32 | |
33 const std::string expected[] = { | |
34 "", "MY======", "MZXQ====", "MZXW6===", | |
35 "MZXW6YQ=", "MZXW6YTB", "MZXW6YTBOI======"}; | |
36 | |
37 // Run the tests, with one more letter in the input every pass. | |
38 for (size_t i = 0; i < arraysize(expected); ++i) { | |
39 std::string output; | |
40 Base32Encode(test_str.substr(0, i), Base32EncodePolicy::INCLUDE_PADDING, | |
sdefresne
2016/06/01 14:25:38
ditto
Rob Percival
2016/06/01 14:37:57
Done.
| |
41 &output); | |
42 EXPECT_EQ(expected[i], output); | |
43 } | |
44 } | |
45 | |
46 TEST(Base32Test, EncodesSha256HashCorrectly) { | |
47 // Useful to test with longer input than the RFC test vectors, and encoding | |
48 // SHA-256 hashes is one of the use cases for this component. | |
49 const std::string hash = | |
50 "\x1f\x25\xe1\xca\xba\x4f\xf9\xb8\x27\x24\x83\x0f\xca\x60\xe4\xc2\xbe\xa8" | |
51 "\xc3\xa9\x44\x1c\x27\xb0\xb4\x3e\x6a\x96\x94\xc7\xb8\x04"; | |
52 std::string output; | |
53 Base32Encode(hash, Base32EncodePolicy::OMIT_PADDING, &output); | |
54 EXPECT_EQ("D4S6DSV2J743QJZEQMH4UYHEYK7KRQ5JIQOCPMFUHZVJNFGHXACA", output); | |
55 } | |
56 | |
57 TEST(Base32Test, EncodesEmptyStringAsEmptyString) { | |
sdefresne
2016/06/01 14:25:38
Both EncodesRfcTestVectorsCorrectlyWithoutPadding
Rob Percival
2016/06/01 14:37:57
Done.
| |
58 std::string output = "foo"; | |
59 Base32Encode("", Base32EncodePolicy::INCLUDE_PADDING, &output); | |
60 EXPECT_EQ("", output); | |
61 } | |
62 | |
63 } // namespace | |
64 } // namespace base32 | |
OLD | NEW |