1 /*
2  * Copyright 2020 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #define LOG_TAG "AdbCAListTest"
18 
19 #include <gtest/gtest.h>
20 
21 #include <adb/tls/adb_ca_list.h>
22 #include <android-base/logging.h>
23 #include <android-base/strings.h>
24 #include <openssl/ssl.h>
25 
26 namespace adb {
27 namespace tls {
28 
29 class AdbCAListTest : public testing::Test {
30   protected:
SetUp()31     virtual void SetUp() override {}
32 
TearDown()33     virtual void TearDown() override {}
34 };
35 
TEST_F(AdbCAListTest,SHA256BitsToHexString_BadParam)36 TEST_F(AdbCAListTest, SHA256BitsToHexString_BadParam) {
37     // Should crash if not exactly SHA256_DIGEST_LENGTH size
38     ASSERT_DEATH(
39             {
40                 // empty
41                 std::string sha;
42                 SHA256BitsToHexString(sha);
43             },
44             "");
45     ASSERT_DEATH(
46             {
47                 std::string sha(1, 0x80);
48                 SHA256BitsToHexString(sha);
49             },
50             "");
51     ASSERT_DEATH(
52             {
53                 std::string sha(SHA256_DIGEST_LENGTH - 1, 0x80);
54                 SHA256BitsToHexString(sha);
55             },
56             "");
57     ASSERT_DEATH(
58             {
59                 std::string sha(SHA256_DIGEST_LENGTH + 1, 0x80);
60                 SHA256BitsToHexString(sha);
61             },
62             "");
63 }
64 
TEST_F(AdbCAListTest,SHA256HexStringToBits_BadParam)65 TEST_F(AdbCAListTest, SHA256HexStringToBits_BadParam) {
66     {
67         // empty
68         std::string sha_str;
69         auto res = SHA256HexStringToBits(sha_str);
70         EXPECT_FALSE(res.has_value());
71     }
72     {
73         std::string sha_str(1, 'a');
74         auto res = SHA256HexStringToBits(sha_str);
75         EXPECT_FALSE(res.has_value());
76     }
77     {
78         std::string sha_str(SHA256_DIGEST_LENGTH * 2 - 1, 'a');
79         auto res = SHA256HexStringToBits(sha_str);
80         EXPECT_FALSE(res.has_value());
81     }
82     {
83         std::string sha_str(SHA256_DIGEST_LENGTH * 2 + 1, 'a');
84         auto res = SHA256HexStringToBits(sha_str);
85         EXPECT_FALSE(res.has_value());
86     }
87     {
88         // Non-hex chars
89         std::string sha_str(SHA256_DIGEST_LENGTH * 2, 'a');
90         sha_str[32] = 'x';
91         auto res = SHA256HexStringToBits(sha_str);
92         EXPECT_FALSE(res.has_value());
93     }
94 }
95 
TEST_F(AdbCAListTest,SHA256BitsToHexString_ValidParam)96 TEST_F(AdbCAListTest, SHA256BitsToHexString_ValidParam) {
97     uint8_t ct = 0;
98     // Test every possible byte
99     std::vector<std::string> expectedStr = {
100             "000102030405060708090A0B0C0D0E0F"
101             "101112131415161718191A1B1C1D1E1F",
102 
103             "202122232425262728292A2B2C2D2E2F"
104             "303132333435363738393A3B3C3D3E3F",
105 
106             "404142434445464748494A4B4C4D4E4F"
107             "505152535455565758595A5B5C5D5E5F",
108 
109             "606162636465666768696A6B6C6D6E6F"
110             "707172737475767778797A7B7C7D7E7F",
111 
112             "808182838485868788898A8B8C8D8E8F"
113             "909192939495969798999A9B9C9D9E9F",
114 
115             "A0A1A2A3A4A5A6A7A8A9AAABACADAEAF"
116             "B0B1B2B3B4B5B6B7B8B9BABBBCBDBEBF",
117 
118             "C0C1C2C3C4C5C6C7C8C9CACBCCCDCECF"
119             "D0D1D2D3D4D5D6D7D8D9DADBDCDDDEDF",
120 
121             "E0E1E2E3E4E5E6E7E8E9EAEBECEDEEEF"
122             "F0F1F2F3F4F5F6F7F8F9FAFBFCFDFEFF",
123     };
124 
125     for (auto& expected : expectedStr) {
126         std::string sha;
127         while (sha.size() < SHA256_DIGEST_LENGTH) {
128             sha += ct++;
129         }
130 
131         auto sha_str = SHA256BitsToHexString(sha);
132         EXPECT_EQ(expected, sha_str);
133 
134         // try to convert back to bits
135         auto out_sha = SHA256HexStringToBits(sha_str);
136         ASSERT_TRUE(out_sha.has_value());
137         EXPECT_EQ(*out_sha, sha);
138     }
139 }
140 
TEST_F(AdbCAListTest,CreateCAIssuerFromEncodedKey_EmptyKey)141 TEST_F(AdbCAListTest, CreateCAIssuerFromEncodedKey_EmptyKey) {
142     ASSERT_DEATH({ auto issuer = CreateCAIssuerFromEncodedKey(""); }, "");
143 }
144 
TEST_F(AdbCAListTest,Smoke)145 TEST_F(AdbCAListTest, Smoke) {
146     {
147         std::string key =
148                 "A45BC1FF6C89BF0E"
149                 "65F9BA153FBC9876"
150                 "4969B4113F1CF878"
151                 "EEF9BF1C3F9C9227";
152         auto issuer = CreateCAIssuerFromEncodedKey(key);
153         ASSERT_NE(issuer, nullptr);
154 
155         // Try to parse the encoded key out of the X509_NAME
156         auto out_key = ParseEncodedKeyFromCAIssuer(issuer.get());
157         ASSERT_TRUE(out_key.has_value());
158         EXPECT_EQ(key, *out_key);
159     }
160 }
161 
162 }  // namespace tls
163 }  // namespace adb
164