1 // 2 // Copyright (C) 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 #include <stddef.h> 18 #include <stdint.h> 19 20 #include <gtest/gtest.h> 21 #include <kver/kernel_release.h> 22 #include <kver/kmi_version.h> 23 #include <kver/utils.h> 24 25 using std::string_literals::operator""s; 26 27 namespace android::kver { 28 29 void CheckValidKmiVersion(const std::string& s) { 30 auto kmi_version = KmiVersion::Parse(s); 31 ASSERT_TRUE(kmi_version.has_value()); 32 ASSERT_EQ(s, kmi_version->string()); 33 } 34 35 TEST(KmiVersion, Valid) { 36 CheckValidKmiVersion("5.4-android12-0"); 37 CheckValidKmiVersion("0.0-android0-0"); 38 CheckValidKmiVersion("999.999-android999-999"); 39 CheckValidKmiVersion( 40 "18446744073709551615.18446744073709551615-android18446744073709551615-18446744073709551615"); 41 } 42 43 TEST(KmiVersion, Invalid) { 44 EXPECT_FALSE(KmiVersion::Parse("5.4.42-android12-0").has_value()); 45 46 EXPECT_FALSE(KmiVersion::Parse("4-android12-0").has_value()); 47 EXPECT_FALSE(KmiVersion::Parse("5.4-androd12-0").has_value()); 48 EXPECT_FALSE(KmiVersion::Parse("5.4-android12").has_value()); 49 EXPECT_FALSE(KmiVersion::Parse("5.4-android12-0\n").has_value()); 50 } 51 52 TEST(KmiVersion, Parse) { 53 auto res = KmiVersion::Parse("5.4-android12-1"); 54 ASSERT_TRUE(res.has_value()); 55 EXPECT_EQ(5, res->version()); 56 EXPECT_EQ(4, res->patch_level()); 57 EXPECT_EQ(12, res->android_release()); 58 EXPECT_EQ(1, res->generation()); 59 } 60 61 TEST(KmiVersion, ParseWithZero) { 62 // Explicit operator""s to ensure \0 character is part of the string. 63 auto res = KmiVersion::Parse("5.4-android12-1\0-something"s); 64 ASSERT_FALSE(res.has_value()); 65 } 66 67 void CheckValidKernelRelease(const std::string& s) { 68 auto kernel_release = KernelRelease::Parse(s); 69 ASSERT_TRUE(kernel_release.has_value()); 70 ASSERT_EQ(s, kernel_release->string()); 71 } 72 73 TEST(KernelRelease, Valid) { 74 CheckValidKernelRelease("5.4.42-android12-0"); 75 CheckValidKernelRelease("0.0.0-android0-0"); 76 CheckValidKernelRelease("999.999.999-android999-999"); 77 CheckValidKernelRelease( 78 "18446744073709551615.18446744073709551615.18446744073709551615-android18446744073709551615-" 79 "18446744073709551615"); 80 } 81 82 TEST(KernelRelease, Invalid) { 83 EXPECT_FALSE(KernelRelease::Parse("5.4-android12-0").has_value()); 84 85 EXPECT_FALSE(KernelRelease::Parse("4.42-android12-0").has_value()); 86 EXPECT_FALSE(KernelRelease::Parse("5.4.42-androd12-0").has_value()); 87 EXPECT_FALSE(KernelRelease::Parse("5.4.42-android12").has_value()); 88 EXPECT_FALSE(KernelRelease::Parse("5.4.42-android12-0\n").has_value()); 89 } 90 91 TEST(KernelRelease, Parse) { 92 auto res = KernelRelease::Parse("5.4.42-android12-1"); 93 ASSERT_TRUE(res.has_value()); 94 EXPECT_EQ(5, res->version()); 95 EXPECT_EQ(4, res->patch_level()); 96 EXPECT_EQ(42, res->sub_level()); 97 EXPECT_EQ(12, res->android_release()); 98 EXPECT_EQ(1, res->generation()); 99 100 EXPECT_EQ("5.4-android12-1", res->kmi_version().string()); 101 } 102 103 TEST(KernelRelease, ParseWithZero) { 104 // Explicit operator""s to ensure \0 character is part of the string. 105 auto res = KernelRelease::Parse("5.4.42-android12-1\0-something"s, false); 106 ASSERT_FALSE(res.has_value()); 107 } 108 109 TEST(KernelRelease, ParseWithSuffixDisallowed) { 110 auto res = KernelRelease::Parse("5.4.42-android12-1-something", false); 111 ASSERT_FALSE(res.has_value()); 112 } 113 114 TEST(KernelRelease, ParseWithSuffixAllowed) { 115 auto res = KernelRelease::Parse("5.4.42-android12-1-something", true); 116 ASSERT_TRUE(res.has_value()); 117 EXPECT_EQ(5, res->version()); 118 EXPECT_EQ(4, res->patch_level()); 119 EXPECT_EQ(42, res->sub_level()); 120 EXPECT_EQ(12, res->android_release()); 121 EXPECT_EQ(1, res->generation()); 122 } 123 124 bool IsKernelUpdateValid(const std::string& old_release, const std::string& new_release) { 125 return IsKernelUpdateValid(KernelRelease::Parse(old_release), KernelRelease::Parse(new_release)); 126 } 127 128 TEST(KernelRelease, IsUpdateValid) { 129 EXPECT_TRUE(IsKernelUpdateValid("5.4.42-not-gki", "")) << "Legacy update should pass"; 130 EXPECT_FALSE(IsKernelUpdateValid("5.4.42-android12-0", "")) 131 << "From GKI to non-GKI is a downgrade"; 132 133 EXPECT_TRUE(IsKernelUpdateValid("5.4.42-not-gki", "5.4.42-android12-0")) 134 << "Non-GKI to GKI should pass"; 135 EXPECT_TRUE(IsKernelUpdateValid("5.4.42-not-gki", "5.4.42-android12-0")) 136 << "Non-GKI to GKI should pass"; 137 138 EXPECT_TRUE(IsKernelUpdateValid("5.4.42-android12-0", "5.4.42-android12-0")) 139 << "Update to self should be fine"; 140 141 EXPECT_FALSE(IsKernelUpdateValid("5.4.42-android12-1", "5.4.42-android12-0")) 142 << "Should detect downgrade of KMI version"; 143 EXPECT_FALSE(IsKernelUpdateValid("5.4.42-android13-0", "5.4.42-android12-0")) 144 << "Should detect downgrade of KMI version"; 145 EXPECT_FALSE(IsKernelUpdateValid("5.10.42-android12-0", "5.4.42-android12-0")) 146 << "Should detect downgrade of KMI version"; 147 EXPECT_FALSE(IsKernelUpdateValid("6.1.42-android12-0", "5.4.42-android12-0")) 148 << "Should detect downgrade of KMI version"; 149 150 EXPECT_FALSE(IsKernelUpdateValid("5.4.42-android13-0", "5.10.42-android12-0")) 151 << "Should detect downgrade of Android release"; 152 153 EXPECT_FALSE(IsKernelUpdateValid("5.4.43-android12-0", "5.4.42-android12-0")) 154 << "Should detect downgrade of w.x.y"; 155 EXPECT_FALSE(IsKernelUpdateValid("5.10.5-android12-0", "5.4.42-android12-0")) 156 << "Should detect downgrade of w.x.y"; 157 EXPECT_FALSE(IsKernelUpdateValid("6.1.10-android12-0", "5.4.42-android12-0")) 158 << "Should detect downgrade of w.x.y"; 159 160 EXPECT_TRUE(IsKernelUpdateValid("5.4.42-android12-0", "5.4.42-android12-1")) 161 << "Update to newer KMI is fine"; 162 EXPECT_TRUE(IsKernelUpdateValid("5.4.42-android12-0", "5.4.42-android13-0")) 163 << "Update to newer KMI is fine"; 164 EXPECT_TRUE(IsKernelUpdateValid("5.4.42-android12-0", "5.10.42-android12-0")) 165 << "Update to newer KMI is fine"; 166 EXPECT_TRUE(IsKernelUpdateValid("5.4.42-android12-0", "6.1.42-android12-0")) 167 << "Update to newer KMI is fine"; 168 169 EXPECT_TRUE(IsKernelUpdateValid("5.4.42-android12-0", "5.4.42-android12-1")) 170 << "Update to newer KMI is fine"; 171 EXPECT_TRUE(IsKernelUpdateValid("5.4.42-android12-1", "5.4.42-android13-0")) 172 << "Update to newer KMI is fine"; 173 174 EXPECT_TRUE(IsKernelUpdateValid("5.4.42-android12-0", "5.4.43-android12-0")) 175 << "Update to newer w.x.y is fine"; 176 EXPECT_TRUE(IsKernelUpdateValid("5.4.42-android12-0", "5.10.5-android12-0")) 177 << "Update to newer w.x.y is fine"; 178 EXPECT_TRUE(IsKernelUpdateValid("5.4.42-android12-0", "6.1.5-android12-0")) 179 << "Update to newer w.x.y is fine"; 180 } 181 182 } // namespace android::kver 183