1 /*
2 * Copyright (C) 2018 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 <algorithm>
18
19 #include <android-base/file.h>
20 #include <android-base/logging.h>
21 #include <gtest/gtest.h>
22
23 #include "apex_manifest.h"
24
25 using ::apex::proto::ApexManifest;
26
27 namespace android {
28 namespace apex {
29
30 namespace {
31
ToString(const ApexManifest & manifest)32 std::string ToString(const ApexManifest& manifest) {
33 std::string out;
34 manifest.SerializeToString(&out);
35 return out;
36 }
37
38 } // namespace
39
TEST(ApexManifestTest,SimpleTest)40 TEST(ApexManifestTest, SimpleTest) {
41 ApexManifest manifest;
42 manifest.set_name("com.android.example.apex");
43 manifest.set_version(1);
44 auto apex_manifest = ParseManifest(ToString(manifest));
45 ASSERT_RESULT_OK(apex_manifest);
46 EXPECT_EQ("com.android.example.apex", std::string(apex_manifest->name()));
47 EXPECT_EQ(1u, apex_manifest->version());
48 EXPECT_FALSE(apex_manifest->nocode());
49 }
50
TEST(ApexManifestTest,NameMissing)51 TEST(ApexManifestTest, NameMissing) {
52 ApexManifest manifest;
53 manifest.set_version(1);
54 auto apex_manifest = ParseManifest(ToString(manifest));
55 ASSERT_FALSE(apex_manifest.ok());
56 EXPECT_EQ(apex_manifest.error().message(),
57 std::string("Missing required field \"name\" from APEX manifest."))
58 << apex_manifest.error();
59 }
60
TEST(ApexManifestTest,VersionMissing)61 TEST(ApexManifestTest, VersionMissing) {
62 ApexManifest manifest;
63 manifest.set_name("com.android.example.apex");
64 auto apex_manifest = ParseManifest(ToString(manifest));
65 ASSERT_FALSE(apex_manifest.ok());
66 EXPECT_EQ(
67 apex_manifest.error().message(),
68 std::string("Missing required field \"version\" from APEX manifest."))
69 << apex_manifest.error();
70 }
71
TEST(ApexManifestTest,NoPreInstallHook)72 TEST(ApexManifestTest, NoPreInstallHook) {
73 ApexManifest manifest;
74 manifest.set_name("com.android.example.apex");
75 manifest.set_version(1);
76 auto apex_manifest = ParseManifest(ToString(manifest));
77 ASSERT_RESULT_OK(apex_manifest);
78 EXPECT_EQ("", std::string(apex_manifest->preinstallhook()));
79 }
80
TEST(ApexManifestTest,PreInstallHook)81 TEST(ApexManifestTest, PreInstallHook) {
82 ApexManifest manifest;
83 manifest.set_name("com.android.example.apex");
84 manifest.set_version(1);
85 manifest.set_preinstallhook("bin/preInstallHook");
86 auto apex_manifest = ParseManifest(ToString(manifest));
87 ASSERT_RESULT_OK(apex_manifest);
88 EXPECT_EQ("bin/preInstallHook", std::string(apex_manifest->preinstallhook()));
89 }
90
TEST(ApexManifestTest,NoPostInstallHook)91 TEST(ApexManifestTest, NoPostInstallHook) {
92 ApexManifest manifest;
93 manifest.set_name("com.android.example.apex");
94 manifest.set_version(1);
95 auto apex_manifest = ParseManifest(ToString(manifest));
96 ASSERT_RESULT_OK(apex_manifest);
97 EXPECT_EQ("", std::string(apex_manifest->postinstallhook()));
98 }
99
TEST(ApexManifestTest,PostInstallHook)100 TEST(ApexManifestTest, PostInstallHook) {
101 ApexManifest manifest;
102 manifest.set_name("com.android.example.apex");
103 manifest.set_version(1);
104 manifest.set_postinstallhook("bin/postInstallHook");
105 auto apex_manifest = ParseManifest(ToString(manifest));
106 ASSERT_RESULT_OK(apex_manifest);
107 EXPECT_EQ("bin/postInstallHook",
108 std::string(apex_manifest->postinstallhook()));
109 }
110
TEST(ApexManifestTest,UnparsableManifest)111 TEST(ApexManifestTest, UnparsableManifest) {
112 auto apex_manifest = ParseManifest("This is an invalid pony");
113 ASSERT_FALSE(apex_manifest.ok());
114 EXPECT_EQ(apex_manifest.error().message(),
115 std::string("Can't parse APEX manifest."))
116 << apex_manifest.error();
117 }
118
TEST(ApexManifestTest,NoCode)119 TEST(ApexManifestTest, NoCode) {
120 ApexManifest manifest;
121 manifest.set_name("com.android.example.apex");
122 manifest.set_version(1);
123 manifest.set_nocode(true);
124 auto apex_manifest = ParseManifest(ToString(manifest));
125 ASSERT_RESULT_OK(apex_manifest);
126 EXPECT_TRUE(apex_manifest->nocode());
127 }
128
129 } // namespace apex
130 } // namespace android
131