1 /*
2 * Copyright (C) 2024 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 "apex_blocklist.h"
18
19 #include <android-base/file.h>
20 #include <android-base/logging.h>
21 #include <google/protobuf/util/json_util.h>
22 #include <gtest/gtest.h>
23
24 #include <algorithm>
25
26 using ::apex::proto::ApexBlocklist;
27
28 namespace android::apex {
29
30 namespace {
31
ToString(const ApexBlocklist & blocklist)32 std::string ToString(const ApexBlocklist& blocklist) {
33 std::string out;
34 google::protobuf::util::MessageToJsonString(blocklist, &out);
35 return out;
36 }
37
38 } // namespace
39
TEST(ApexBlocklistTest,SimpleValid)40 TEST(ApexBlocklistTest, SimpleValid) {
41 ApexBlocklist blocklist;
42 ApexBlocklist::ApexItem* item = blocklist.add_blocked_apex();
43 item->set_name("com.android.example.apex");
44 item->set_version(1);
45 auto apex_blocklist = ParseBlocklist(ToString(blocklist));
46 ASSERT_RESULT_OK(apex_blocklist);
47 EXPECT_EQ(1, apex_blocklist->blocked_apex().size());
48 EXPECT_EQ("com.android.example.apex", apex_blocklist->blocked_apex(0).name());
49 EXPECT_EQ(1, apex_blocklist->blocked_apex(0).version());
50 }
51
TEST(ApexBlocklistTest,NameMissing)52 TEST(ApexBlocklistTest, NameMissing) {
53 ApexBlocklist blocklist;
54 ApexBlocklist::ApexItem* item = blocklist.add_blocked_apex();
55 item->set_version(1);
56 auto apex_blocklist = ParseBlocklist(ToString(blocklist));
57 ASSERT_FALSE(apex_blocklist.ok());
58 EXPECT_EQ(apex_blocklist.error().message(),
59 std::string("Missing required field \"name\" from APEX blocklist."))
60 << apex_blocklist.error();
61 }
62
TEST(ApexBlocklistTest,VersionMissing)63 TEST(ApexBlocklistTest, VersionMissing) {
64 ApexBlocklist blocklist;
65 ApexBlocklist::ApexItem* item = blocklist.add_blocked_apex();
66 item->set_name("com.android.example.apex");
67 auto apex_blocklist = ParseBlocklist(ToString(blocklist));
68 ASSERT_FALSE(apex_blocklist.ok());
69 EXPECT_EQ(
70 apex_blocklist.error().message(),
71 std::string(
72 "Missing positive value for field \"version\" from APEX blocklist."))
73 << apex_blocklist.error();
74 }
75
76 } // namespace android::apex
77