1*33f37583SAndroid Build Coastguard Worker /*
2*33f37583SAndroid Build Coastguard Worker * Copyright (C) 2018 The Android Open Source Project
3*33f37583SAndroid Build Coastguard Worker *
4*33f37583SAndroid Build Coastguard Worker * Licensed under the Apache License, Version 2.0 (the "License");
5*33f37583SAndroid Build Coastguard Worker * you may not use this file except in compliance with the License.
6*33f37583SAndroid Build Coastguard Worker * You may obtain a copy of the License at
7*33f37583SAndroid Build Coastguard Worker *
8*33f37583SAndroid Build Coastguard Worker * http://www.apache.org/licenses/LICENSE-2.0
9*33f37583SAndroid Build Coastguard Worker *
10*33f37583SAndroid Build Coastguard Worker * Unless required by applicable law or agreed to in writing, software
11*33f37583SAndroid Build Coastguard Worker * distributed under the License is distributed on an "AS IS" BASIS,
12*33f37583SAndroid Build Coastguard Worker * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13*33f37583SAndroid Build Coastguard Worker * See the License for the specific language governing permissions and
14*33f37583SAndroid Build Coastguard Worker * limitations under the License.
15*33f37583SAndroid Build Coastguard Worker */
16*33f37583SAndroid Build Coastguard Worker
17*33f37583SAndroid Build Coastguard Worker #include <algorithm>
18*33f37583SAndroid Build Coastguard Worker
19*33f37583SAndroid Build Coastguard Worker #include <android-base/file.h>
20*33f37583SAndroid Build Coastguard Worker #include <android-base/logging.h>
21*33f37583SAndroid Build Coastguard Worker #include <gtest/gtest.h>
22*33f37583SAndroid Build Coastguard Worker
23*33f37583SAndroid Build Coastguard Worker #include "apex_manifest.h"
24*33f37583SAndroid Build Coastguard Worker
25*33f37583SAndroid Build Coastguard Worker using ::apex::proto::ApexManifest;
26*33f37583SAndroid Build Coastguard Worker
27*33f37583SAndroid Build Coastguard Worker namespace android {
28*33f37583SAndroid Build Coastguard Worker namespace apex {
29*33f37583SAndroid Build Coastguard Worker
30*33f37583SAndroid Build Coastguard Worker namespace {
31*33f37583SAndroid Build Coastguard Worker
ToString(const ApexManifest & manifest)32*33f37583SAndroid Build Coastguard Worker std::string ToString(const ApexManifest& manifest) {
33*33f37583SAndroid Build Coastguard Worker std::string out;
34*33f37583SAndroid Build Coastguard Worker manifest.SerializeToString(&out);
35*33f37583SAndroid Build Coastguard Worker return out;
36*33f37583SAndroid Build Coastguard Worker }
37*33f37583SAndroid Build Coastguard Worker
38*33f37583SAndroid Build Coastguard Worker } // namespace
39*33f37583SAndroid Build Coastguard Worker
TEST(ApexManifestTest,SimpleTest)40*33f37583SAndroid Build Coastguard Worker TEST(ApexManifestTest, SimpleTest) {
41*33f37583SAndroid Build Coastguard Worker ApexManifest manifest;
42*33f37583SAndroid Build Coastguard Worker manifest.set_name("com.android.example.apex");
43*33f37583SAndroid Build Coastguard Worker manifest.set_version(1);
44*33f37583SAndroid Build Coastguard Worker auto apex_manifest = ParseManifest(ToString(manifest));
45*33f37583SAndroid Build Coastguard Worker ASSERT_RESULT_OK(apex_manifest);
46*33f37583SAndroid Build Coastguard Worker EXPECT_EQ("com.android.example.apex", std::string(apex_manifest->name()));
47*33f37583SAndroid Build Coastguard Worker EXPECT_EQ(1u, apex_manifest->version());
48*33f37583SAndroid Build Coastguard Worker EXPECT_FALSE(apex_manifest->nocode());
49*33f37583SAndroid Build Coastguard Worker }
50*33f37583SAndroid Build Coastguard Worker
TEST(ApexManifestTest,NameMissing)51*33f37583SAndroid Build Coastguard Worker TEST(ApexManifestTest, NameMissing) {
52*33f37583SAndroid Build Coastguard Worker ApexManifest manifest;
53*33f37583SAndroid Build Coastguard Worker manifest.set_version(1);
54*33f37583SAndroid Build Coastguard Worker auto apex_manifest = ParseManifest(ToString(manifest));
55*33f37583SAndroid Build Coastguard Worker ASSERT_FALSE(apex_manifest.ok());
56*33f37583SAndroid Build Coastguard Worker EXPECT_EQ(apex_manifest.error().message(),
57*33f37583SAndroid Build Coastguard Worker std::string("Missing required field \"name\" from APEX manifest."))
58*33f37583SAndroid Build Coastguard Worker << apex_manifest.error();
59*33f37583SAndroid Build Coastguard Worker }
60*33f37583SAndroid Build Coastguard Worker
TEST(ApexManifestTest,VersionMissing)61*33f37583SAndroid Build Coastguard Worker TEST(ApexManifestTest, VersionMissing) {
62*33f37583SAndroid Build Coastguard Worker ApexManifest manifest;
63*33f37583SAndroid Build Coastguard Worker manifest.set_name("com.android.example.apex");
64*33f37583SAndroid Build Coastguard Worker auto apex_manifest = ParseManifest(ToString(manifest));
65*33f37583SAndroid Build Coastguard Worker ASSERT_FALSE(apex_manifest.ok());
66*33f37583SAndroid Build Coastguard Worker EXPECT_EQ(
67*33f37583SAndroid Build Coastguard Worker apex_manifest.error().message(),
68*33f37583SAndroid Build Coastguard Worker std::string("Missing required field \"version\" from APEX manifest."))
69*33f37583SAndroid Build Coastguard Worker << apex_manifest.error();
70*33f37583SAndroid Build Coastguard Worker }
71*33f37583SAndroid Build Coastguard Worker
TEST(ApexManifestTest,NoPreInstallHook)72*33f37583SAndroid Build Coastguard Worker TEST(ApexManifestTest, NoPreInstallHook) {
73*33f37583SAndroid Build Coastguard Worker ApexManifest manifest;
74*33f37583SAndroid Build Coastguard Worker manifest.set_name("com.android.example.apex");
75*33f37583SAndroid Build Coastguard Worker manifest.set_version(1);
76*33f37583SAndroid Build Coastguard Worker auto apex_manifest = ParseManifest(ToString(manifest));
77*33f37583SAndroid Build Coastguard Worker ASSERT_RESULT_OK(apex_manifest);
78*33f37583SAndroid Build Coastguard Worker EXPECT_EQ("", std::string(apex_manifest->preinstallhook()));
79*33f37583SAndroid Build Coastguard Worker }
80*33f37583SAndroid Build Coastguard Worker
TEST(ApexManifestTest,PreInstallHook)81*33f37583SAndroid Build Coastguard Worker TEST(ApexManifestTest, PreInstallHook) {
82*33f37583SAndroid Build Coastguard Worker ApexManifest manifest;
83*33f37583SAndroid Build Coastguard Worker manifest.set_name("com.android.example.apex");
84*33f37583SAndroid Build Coastguard Worker manifest.set_version(1);
85*33f37583SAndroid Build Coastguard Worker manifest.set_preinstallhook("bin/preInstallHook");
86*33f37583SAndroid Build Coastguard Worker auto apex_manifest = ParseManifest(ToString(manifest));
87*33f37583SAndroid Build Coastguard Worker ASSERT_RESULT_OK(apex_manifest);
88*33f37583SAndroid Build Coastguard Worker EXPECT_EQ("bin/preInstallHook", std::string(apex_manifest->preinstallhook()));
89*33f37583SAndroid Build Coastguard Worker }
90*33f37583SAndroid Build Coastguard Worker
TEST(ApexManifestTest,NoPostInstallHook)91*33f37583SAndroid Build Coastguard Worker TEST(ApexManifestTest, NoPostInstallHook) {
92*33f37583SAndroid Build Coastguard Worker ApexManifest manifest;
93*33f37583SAndroid Build Coastguard Worker manifest.set_name("com.android.example.apex");
94*33f37583SAndroid Build Coastguard Worker manifest.set_version(1);
95*33f37583SAndroid Build Coastguard Worker auto apex_manifest = ParseManifest(ToString(manifest));
96*33f37583SAndroid Build Coastguard Worker ASSERT_RESULT_OK(apex_manifest);
97*33f37583SAndroid Build Coastguard Worker EXPECT_EQ("", std::string(apex_manifest->postinstallhook()));
98*33f37583SAndroid Build Coastguard Worker }
99*33f37583SAndroid Build Coastguard Worker
TEST(ApexManifestTest,PostInstallHook)100*33f37583SAndroid Build Coastguard Worker TEST(ApexManifestTest, PostInstallHook) {
101*33f37583SAndroid Build Coastguard Worker ApexManifest manifest;
102*33f37583SAndroid Build Coastguard Worker manifest.set_name("com.android.example.apex");
103*33f37583SAndroid Build Coastguard Worker manifest.set_version(1);
104*33f37583SAndroid Build Coastguard Worker manifest.set_postinstallhook("bin/postInstallHook");
105*33f37583SAndroid Build Coastguard Worker auto apex_manifest = ParseManifest(ToString(manifest));
106*33f37583SAndroid Build Coastguard Worker ASSERT_RESULT_OK(apex_manifest);
107*33f37583SAndroid Build Coastguard Worker EXPECT_EQ("bin/postInstallHook",
108*33f37583SAndroid Build Coastguard Worker std::string(apex_manifest->postinstallhook()));
109*33f37583SAndroid Build Coastguard Worker }
110*33f37583SAndroid Build Coastguard Worker
TEST(ApexManifestTest,UnparsableManifest)111*33f37583SAndroid Build Coastguard Worker TEST(ApexManifestTest, UnparsableManifest) {
112*33f37583SAndroid Build Coastguard Worker auto apex_manifest = ParseManifest("This is an invalid pony");
113*33f37583SAndroid Build Coastguard Worker ASSERT_FALSE(apex_manifest.ok());
114*33f37583SAndroid Build Coastguard Worker EXPECT_EQ(apex_manifest.error().message(),
115*33f37583SAndroid Build Coastguard Worker std::string("Can't parse APEX manifest."))
116*33f37583SAndroid Build Coastguard Worker << apex_manifest.error();
117*33f37583SAndroid Build Coastguard Worker }
118*33f37583SAndroid Build Coastguard Worker
TEST(ApexManifestTest,NoCode)119*33f37583SAndroid Build Coastguard Worker TEST(ApexManifestTest, NoCode) {
120*33f37583SAndroid Build Coastguard Worker ApexManifest manifest;
121*33f37583SAndroid Build Coastguard Worker manifest.set_name("com.android.example.apex");
122*33f37583SAndroid Build Coastguard Worker manifest.set_version(1);
123*33f37583SAndroid Build Coastguard Worker manifest.set_nocode(true);
124*33f37583SAndroid Build Coastguard Worker auto apex_manifest = ParseManifest(ToString(manifest));
125*33f37583SAndroid Build Coastguard Worker ASSERT_RESULT_OK(apex_manifest);
126*33f37583SAndroid Build Coastguard Worker EXPECT_TRUE(apex_manifest->nocode());
127*33f37583SAndroid Build Coastguard Worker }
128*33f37583SAndroid Build Coastguard Worker
129*33f37583SAndroid Build Coastguard Worker } // namespace apex
130*33f37583SAndroid Build Coastguard Worker } // namespace android
131