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 "apexutil.h"
18 
19 #include <unistd.h>
20 
21 #include <string>
22 #include <utility>
23 #include <vector>
24 
25 #include <android-base/file.h>
26 #include <apex_manifest.pb.h>
27 #include <gmock/gmock.h>
28 #include <gtest/gtest.h>
29 
30 using namespace std::literals;
31 
32 using ::android::apex::GetActivePackages;
33 using ::android::base::WriteStringToFile;
34 using ::apex::proto::ApexManifest;
35 using ::testing::Contains;
36 using ::testing::Pair;
37 using ::testing::UnorderedElementsAre;
38 
39 namespace {
40 
CreateApexManifest(std::string apex_name,int version)41 ApexManifest CreateApexManifest(std::string apex_name, int version) {
42   ApexManifest manifest;
43   manifest.set_name(apex_name);
44   manifest.set_version(version);
45   return manifest;
46 }
47 
Mkdir(std::string dir_path)48 void Mkdir(std::string dir_path) {
49   if (access(dir_path.c_str(), F_OK) == 0)
50     return;
51   ASSERT_NE(-1, mkdir(dir_path.c_str(), 0755) == -1)
52       << "Failed to create a directory: " << dir_path;
53 }
54 
WriteFile(std::string file_path,std::string content)55 void WriteFile(std::string file_path, std::string content) {
56   ASSERT_TRUE(WriteStringToFile(content, file_path))
57       << "Failed to write a file: " << file_path;
58 }
59 
60 } // namespace
61 
62 namespace apex {
63 namespace proto {
64 
65 // simple implementation for testing purpose
operator ==(const ApexManifest & lhs,const ApexManifest & rhs)66 bool operator==(const ApexManifest &lhs, const ApexManifest &rhs) {
67   return lhs.SerializeAsString() == rhs.SerializeAsString();
68 }
69 
70 } // namespace proto
71 } // namespace apex
72 
TEST(ApexUtil,GetActivePackages)73 TEST(ApexUtil, GetActivePackages) {
74   TemporaryDir td;
75 
76   // com.android.foo: valid
77   auto foo_path = td.path + "/com.android.foo"s;
78   auto foo_manifest = CreateApexManifest("com.android.foo", 1);
79   Mkdir(foo_path);
80   WriteFile(foo_path + "/apex_manifest.pb", foo_manifest.SerializeAsString());
81 
82   // com.android.foo@1: ignore versioned
83   Mkdir(foo_path + "@1");
84   WriteFile(foo_path + "@1/apex_manifest.pb", foo_manifest.SerializeAsString());
85 
86   // com.android.baz: valid
87   auto bar_path = td.path + "/com.android.bar"s;
88   auto bar_manifest = CreateApexManifest("com.android.bar", 2);
89   Mkdir(bar_path);
90   WriteFile(bar_path + "/apex_manifest.pb", bar_manifest.SerializeAsString());
91 
92   // invalid: a directory without apex_manifest.pb
93   Mkdir(td.path + "/com.android.baz"s);
94   // invalid: a file
95   WriteFile(td.path + "/com.android.qux.apex"s, "");
96 
97   auto apexes = GetActivePackages(td.path);
98   ASSERT_EQ(2U, apexes.size());
99 
100   ASSERT_THAT(apexes, UnorderedElementsAre(Pair(foo_path, foo_manifest),
101                                            Pair(bar_path, bar_manifest)));
102 }