1 /*
2 * Copyright (C) 2016 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 #include <gtest/gtest.h>
18
19 #include <keymaster/keymaster_configuration.h>
20
21 #ifdef HOST_BUILD
22 extern "C" {
23 int __android_log_print(int prio, const char* tag, const char* fmt);
__android_log_print(int prio,const char * tag,const char * fmt)24 int __android_log_print(int prio, const char* tag, const char* fmt) {
25 (void)prio, (void)tag, (void)fmt;
26 std::cout << fmt << std::endl;
27 return 0;
28 }
29 } // extern "C"
30 #endif // HOST_BUILD
31
32 namespace keymaster {
33 namespace test {
34
TEST(VersionParsingTest,Full)35 TEST(VersionParsingTest, Full) {
36 EXPECT_EQ(612334U, GetOsVersion("61.23.34"));
37 EXPECT_EQ(680000U, GetOsVersion("681.23.24"));
38 EXPECT_EQ(682300U, GetOsVersion("68.231.24"));
39 EXPECT_EQ(682324U, GetOsVersion("68.23.241"));
40 EXPECT_EQ(60102U, GetOsVersion("6.1.2-extrajunk"));
41 EXPECT_EQ(0U, GetOsVersion("extra6.1.2"));
42 }
43
TEST(VersionParsingTest,FullWithExtraChars)44 TEST(VersionParsingTest, FullWithExtraChars) {}
45
TEST(VersionParsingTest,MajorOnly)46 TEST(VersionParsingTest, MajorOnly) {
47 EXPECT_EQ(60000U, GetOsVersion("6"));
48 EXPECT_EQ(680000U, GetOsVersion("68"));
49 EXPECT_EQ(680000U, GetOsVersion("681"));
50 EXPECT_EQ(60000U, GetOsVersion("6.junk"));
51 }
52
TEST(VersionParsingTest,MajorMinorOnly)53 TEST(VersionParsingTest, MajorMinorOnly) {
54 EXPECT_EQ(60100U, GetOsVersion("6.1"));
55 EXPECT_EQ(60100U, GetOsVersion("6.1junk"));
56 }
57
TEST(PatchLevelParsingTest,Full)58 TEST(PatchLevelParsingTest, Full) {
59 EXPECT_EQ(201603U, GetOsPatchlevel("2016-03-23"));
60 EXPECT_EQ(0U, GetOsPatchlevel("2016-13-23"));
61 EXPECT_EQ(0U, GetOsPatchlevel("2016-03"));
62 EXPECT_EQ(0U, GetOsPatchlevel("2016-3-23"));
63 EXPECT_EQ(0U, GetOsPatchlevel("2016-03-23r"));
64 EXPECT_EQ(0U, GetOsPatchlevel("r2016-03-23"));
65 }
66
TEST(VbmetaParsing,OddLengthInput)67 TEST(VbmetaParsing, OddLengthInput) {
68 EXPECT_EQ(std::nullopt, GetVbmetaDigest("1"));
69 EXPECT_EQ(std::nullopt, GetVbmetaDigest("333"));
70 EXPECT_EQ(std::nullopt, GetVbmetaDigest("7777777"));
71 }
72
TEST(VbmetaParsing,NonHexInput)73 TEST(VbmetaParsing, NonHexInput) {
74 EXPECT_EQ(std::nullopt, GetVbmetaDigest("1s"));
75 EXPECT_EQ(std::nullopt, GetVbmetaDigest("ag"));
76 EXPECT_EQ(std::nullopt, GetVbmetaDigest("0011z3"));
77 EXPECT_EQ(std::nullopt, GetVbmetaDigest(" 44"));
78 }
79
TEST(VbmetaParsing,BasicHexInput)80 TEST(VbmetaParsing, BasicHexInput) {
81 auto parsed = GetVbmetaDigest("0123456789abcdefABCDEF");
82 ASSERT_TRUE(parsed.has_value());
83 const std::vector<uint8_t> bytes = {0x01, 0x23, 0x45, 0x67, 0x89, 0xab,
84 0xcd, 0xef, 0xab, 0xcd, 0xef};
85 EXPECT_EQ(bytes, *parsed);
86 }
87
88 } // namespace test
89 } // namespace keymaster
90