1 /*
2  * Copyright (C) 2022 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 "odr_common.h"
18 
19 #include "gmock/gmock.h"
20 #include "gtest/gtest.h"
21 
22 namespace art {
23 namespace odrefresh {
24 
25 namespace {
26 
27 using ::android::base::Result;
28 
29 }
30 
TEST(OdrCommonTest,ParseSecurityPatchStr)31 TEST(OdrCommonTest, ParseSecurityPatchStr) {
32   Result<int> result = ParseSecurityPatchStr("2022-03-08");
33   EXPECT_TRUE(result.ok());
34   EXPECT_EQ(result.value(), 20220308);
35   EXPECT_FALSE(ParseSecurityPatchStr("").ok());
36   EXPECT_FALSE(ParseSecurityPatchStr("20-2203-08").ok());
37   EXPECT_FALSE(ParseSecurityPatchStr("20220308").ok());
38 }
39 
TEST(OdrCommonTest,ShouldDisablePartialCompilation)40 TEST(OdrCommonTest, ShouldDisablePartialCompilation) {
41   EXPECT_TRUE(ShouldDisablePartialCompilation("2021-03-05"));
42   EXPECT_TRUE(ShouldDisablePartialCompilation("2022-02-05"));
43   EXPECT_TRUE(ShouldDisablePartialCompilation("2022-03-04"));
44   EXPECT_FALSE(ShouldDisablePartialCompilation("2022-03-05"));
45   EXPECT_FALSE(ShouldDisablePartialCompilation("2022-03-06"));
46   EXPECT_FALSE(ShouldDisablePartialCompilation("2022-04-04"));
47   EXPECT_FALSE(ShouldDisablePartialCompilation("2023-03-04"));
48 }
49 
TEST(OdrCommonTest,ShouldDisableRefresh)50 TEST(OdrCommonTest, ShouldDisableRefresh) {
51   EXPECT_TRUE(ShouldDisableRefresh("32"));
52   EXPECT_TRUE(ShouldDisableRefresh("33"));
53   EXPECT_FALSE(ShouldDisableRefresh("31"));
54   EXPECT_FALSE(ShouldDisableRefresh(""));
55   EXPECT_FALSE(ShouldDisableRefresh("invalid"));
56 }
57 
TEST(OdrCommonTest,CheckBuildUserfaultFdGc)58 TEST(OdrCommonTest, CheckBuildUserfaultFdGc) {
59   EXPECT_TRUE(CheckBuildUserfaultFdGc(
60       /*build_enable_uffd_gc=*/false, /*is_at_most_u=*/false, /*kernel_supports_uffd=*/false));
61   EXPECT_FALSE(CheckBuildUserfaultFdGc(
62       /*build_enable_uffd_gc=*/true, /*is_at_most_u=*/false, /*kernel_supports_uffd=*/false));
63   EXPECT_TRUE(CheckBuildUserfaultFdGc(
64       /*build_enable_uffd_gc=*/false, /*is_at_most_u=*/true, /*kernel_supports_uffd=*/false));
65   EXPECT_FALSE(CheckBuildUserfaultFdGc(
66       /*build_enable_uffd_gc=*/true, /*is_at_most_u=*/true, /*kernel_supports_uffd=*/false));
67   EXPECT_TRUE(CheckBuildUserfaultFdGc(
68       /*build_enable_uffd_gc=*/false, /*is_at_most_u=*/false, /*kernel_supports_uffd=*/true));
69   EXPECT_TRUE(CheckBuildUserfaultFdGc(
70       /*build_enable_uffd_gc=*/true, /*is_at_most_u=*/false, /*kernel_supports_uffd=*/true));
71   EXPECT_FALSE(CheckBuildUserfaultFdGc(
72       /*build_enable_uffd_gc=*/false, /*is_at_most_u=*/true, /*kernel_supports_uffd=*/true));
73   EXPECT_TRUE(CheckBuildUserfaultFdGc(
74       /*build_enable_uffd_gc=*/true, /*is_at_most_u=*/true, /*kernel_supports_uffd=*/true));
75 }
76 
77 }  // namespace odrefresh
78 }  // namespace art
79