1 /*
2  * Copyright (C) 2021 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 "android-modules-utils/sdk_level.h"
18 #include "android-modules-utils/unbounded_sdk_level.h"
19 #include <android-base/properties.h>
20 #include <gtest/gtest.h>
21 
22 namespace android {
23 namespace modules {
24 namespace sdklevel {
25 
26 namespace nostl {
27 bool IsAtLeast(const char *);
28 bool IsAtMost(const char *);
29 } // namespace nostl
30 
31 namespace unbounded {
32 
33 class UnboundedSdkLevelTest : public ::testing::Test {
34 protected:
UnboundedSdkLevelTest()35   UnboundedSdkLevelTest() {
36     device_codename_ =
37         android::base::GetProperty("ro.build.version.codename", "");
38     device_api_level_ = android_get_device_api_level();
39   }
40 
41   std::string device_codename_;
42   int device_api_level_;
43 };
44 
45 using UnboundedSdkLevelDeathTest = UnboundedSdkLevelTest;
46 
TEST_F(UnboundedSdkLevelTest,IntegerVersionsTest)47 TEST_F(UnboundedSdkLevelTest, IntegerVersionsTest) {
48   EXPECT_TRUE(IsAtLeast("1"));
49   EXPECT_TRUE(IsAtLeast("31"));
50   EXPECT_TRUE(IsAtLeast(std::to_string(device_api_level_).c_str()));
51 
52   EXPECT_FALSE(IsAtLeast(std::to_string(device_api_level_ + 1).c_str()));
53   EXPECT_FALSE(IsAtLeast(std::to_string(__ANDROID_API_FUTURE__).c_str()));
54 
55   EXPECT_FALSE(IsAtMost("1"));
56   EXPECT_FALSE(IsAtMost("30"));
57   if ("REL" == device_codename_) {
58     EXPECT_TRUE(IsAtMost(std::to_string(device_api_level_).c_str()));
59   } else {
60     EXPECT_FALSE(IsAtMost(std::to_string(device_api_level_).c_str()));
61   }
62 
63   EXPECT_TRUE(IsAtMost(std::to_string(device_api_level_ + 1).c_str()));
64   EXPECT_TRUE(IsAtMost(std::to_string(__ANDROID_API_FUTURE__).c_str()));
65 }
66 
TEST_F(UnboundedSdkLevelTest,CodenameVersionsTest)67 TEST_F(UnboundedSdkLevelTest, CodenameVersionsTest) {
68   if ("REL" == device_codename_) {
69     EXPECT_FALSE(IsAtLeast("Aaa"));
70     EXPECT_TRUE(IsAtMost("Aaa"));
71   } else {
72     EXPECT_TRUE(IsAtLeast("R"));
73     EXPECT_TRUE(IsAtLeast("S"));
74     EXPECT_TRUE(IsAtLeast("Sv2"));
75     EXPECT_TRUE(IsAtLeast("Tiramisu"));
76 
77     EXPECT_FALSE(IsAtLeast("Aaa"));
78     EXPECT_FALSE(IsAtLeast("Zzz"));
79 
80     EXPECT_FALSE(IsAtMost("R"));
81     EXPECT_FALSE(IsAtMost("S"));
82     EXPECT_FALSE(IsAtMost("Sv2"));
83     EXPECT_TRUE(IsAtMost("Tiramisu"));
84 
85     EXPECT_TRUE(IsAtMost("Aaa"));
86     EXPECT_TRUE(IsAtMost("Zzz"));
87   }
88 }
89 
TEST_F(UnboundedSdkLevelTest,NoStlTest)90 TEST_F(UnboundedSdkLevelTest, NoStlTest) {
91   EXPECT_TRUE(android::modules::sdklevel::nostl::IsAtLeast(
92       std::to_string(device_api_level_).c_str()));
93   EXPECT_TRUE(android::modules::sdklevel::nostl::IsAtMost(
94       std::to_string(device_api_level_ + 1).c_str()));
95 }
96 
TEST_F(UnboundedSdkLevelDeathTest,IsAtLeast_EmptyVersionDeathTest)97 TEST_F(UnboundedSdkLevelDeathTest, IsAtLeast_EmptyVersionDeathTest) {
98   EXPECT_DEATH(IsAtLeast(""), "");
99 }
100 
TEST_F(UnboundedSdkLevelDeathTest,IsAtMost_EmptyVersionDeathTest)101 TEST_F(UnboundedSdkLevelDeathTest, IsAtMost_EmptyVersionDeathTest) {
102   EXPECT_DEATH(IsAtMost(""), "");
103 }
104 
TEST_F(UnboundedSdkLevelDeathTest,IsAtLeast_CurrentVersionDeathTest)105 TEST_F(UnboundedSdkLevelDeathTest, IsAtLeast_CurrentVersionDeathTest) {
106   EXPECT_DEATH(IsAtLeast("current"), "");
107 }
108 
TEST_F(UnboundedSdkLevelDeathTest,IsAtMost_CurrentVersionDeathTest)109 TEST_F(UnboundedSdkLevelDeathTest, IsAtMost_CurrentVersionDeathTest) {
110   EXPECT_DEATH(IsAtMost("current"), "");
111 }
112 
TEST_F(UnboundedSdkLevelDeathTest,IsAtLeast_ReleaseVersionDeathTest)113 TEST_F(UnboundedSdkLevelDeathTest, IsAtLeast_ReleaseVersionDeathTest) {
114   if ("REL" == device_codename_) {
115     EXPECT_DEATH(IsAtLeast("Q"), "");
116   }
117 }
118 
TEST_F(UnboundedSdkLevelDeathTest,IsAtMost_ReleaseVersionDeathTest)119 TEST_F(UnboundedSdkLevelDeathTest, IsAtMost_ReleaseVersionDeathTest) {
120   if ("REL" == device_codename_) {
121     EXPECT_DEATH(IsAtMost("Q"), "");
122   }
123 }
124 
125 } // namespace unbounded
126 } // namespace sdklevel
127 } // namespace modules
128 } // namespace android
129