Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / components / proximity_auth / cryptauth / base64url_unittest.cc
blob487ea4449ab9c7d97363b1d0630e4c701d53c6a7
1 // Copyright 2014 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.
5 #include "components/proximity_auth/cryptauth/base64url.h"
7 #include "base/base64.h"
8 #include "testing/gtest/include/gtest/gtest.h"
10 namespace proximity_auth {
12 TEST(ProximityAuthBase64UrlTest, EncodeRegularString) {
13 const std::string input = "Hello world!";
14 const std::string expected_output = "SGVsbG8gd29ybGQh";
16 std::string web_safe_output;
17 Base64UrlEncode(input, &web_safe_output);
18 EXPECT_EQ(expected_output, web_safe_output);
20 // For good measure, make sure that the encoding matches the //base
21 // implemenation as well.
22 std::string non_web_safe_output;
23 base::Base64Encode(input, &non_web_safe_output);
24 EXPECT_EQ(web_safe_output, non_web_safe_output);
27 TEST(ProximityAuthBase64UrlTest, DecodeRegularString) {
28 const std::string input = "SGVsbG8gd29ybGQh";
29 const std::string expected_output = "Hello world!";
31 std::string web_safe_output;
32 EXPECT_TRUE(Base64UrlDecode(input, &web_safe_output));
33 EXPECT_EQ(expected_output, web_safe_output);
35 // For good measure, make sure that the encoding matches the //base
36 // implemenation as well.
37 std::string non_web_safe_output;
38 EXPECT_TRUE(base::Base64Decode(input, &non_web_safe_output));
39 EXPECT_EQ(web_safe_output, non_web_safe_output);
42 TEST(ProximityAuthBase64UrlTest, EncodeSpecialCharacters) {
43 // This happens to be a stable encoding, i.e. encode(decode(s)) gives back s.
44 const std::string encoded = "/+Y=";
45 std::string decoded;
46 ASSERT_TRUE(base::Base64Decode(encoded, &decoded));
48 // Decoded strings that encode to special characters are non-printable, so,
49 // for ease of testing, just compare the web-safe and non-web-safe encodings.
50 std::string web_safe_encoded;
51 Base64UrlEncode(decoded, &web_safe_encoded);
52 EXPECT_EQ("_-Y=", web_safe_encoded);
55 TEST(ProximityAuthBase64UrlTest, DecodeSpecialCharacters) {
56 const std::string encoded = "_-Y=";
57 std::string decoded;
58 ASSERT_TRUE(Base64UrlDecode(encoded, &decoded));
60 // Decoded strings that encode to special characters are non-printable, so,
61 // for ease of testing, just compare the web-safe and non-web-safe encodings.
62 std::string non_web_safe_encoded;
63 base::Base64Encode(decoded, &non_web_safe_encoded);
64 EXPECT_EQ("/+Y=", non_web_safe_encoded);
67 TEST(ProximityAuthBase64UrlTest, DecodeBailsOnPlus) {
68 // Note that "_-Y=" is a valid encoded string, as tested above. This test
69 // simply verifies that an otherwise correctly encoded string cannot use '+'
70 // in place of '-', since the proximity auth code expects websafe encodings.
71 const std::string encoded = "_+Y=";
72 std::string decoded;
73 EXPECT_FALSE(Base64UrlDecode(encoded, &decoded));
76 TEST(ProximityAuthBase64UrlTest, DecodeBailsOnSlash) {
77 // Note that "_-Y=" is a valid encoded string, as tested above. This test
78 // simply verifies that an otherwise correctly encoded string cannot use '/'
79 // in place of '_', since the proximity auth code expects websafe encodings.
80 const std::string encoded = "/-Y=";
81 std::string decoded;
82 EXPECT_FALSE(Base64UrlDecode(encoded, &decoded));
85 } // namespace proximity_auth