1 /*
2 * Copyright (C) 2019 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 <android/apex/ApexInfo.h>
18 #include <android/apex/ApexSessionInfo.h>
19 #include <binder/IServiceManager.h>
20 #include <gmock/gmock.h>
21 #include <gtest/gtest.h>
22
23 #include "session_state.pb.h"
24
25 using apex::proto::SessionState;
26
27 namespace android {
28 namespace apex {
29 namespace testing {
30
31 using ::testing::AllOf;
32 using ::testing::Eq;
33 using ::testing::ExplainMatchResult;
34 using ::testing::Field;
35
36 template <typename T>
IsOk(const android::base::Result<T> & result)37 inline ::testing::AssertionResult IsOk(const android::base::Result<T>& result) {
38 if (result.ok()) {
39 return ::testing::AssertionSuccess() << " is Ok";
40 } else {
41 return ::testing::AssertionFailure() << " failed with " << result.error();
42 }
43 }
44
IsOk(const android::binder::Status & status)45 inline ::testing::AssertionResult IsOk(const android::binder::Status& status) {
46 if (status.isOk()) {
47 return ::testing::AssertionSuccess() << " is Ok";
48 } else {
49 return ::testing::AssertionFailure()
50 << " failed with " << status.exceptionMessage().c_str();
51 }
52 }
53
54 MATCHER_P(SessionInfoEq, other, "") {
55 return ExplainMatchResult(
56 AllOf(
57 Field("sessionId", &ApexSessionInfo::sessionId, Eq(other.sessionId)),
58 Field("isUnknown", &ApexSessionInfo::isUnknown, Eq(other.isUnknown)),
59 Field("isVerified", &ApexSessionInfo::isVerified,
60 Eq(other.isVerified)),
61 Field("isStaged", &ApexSessionInfo::isStaged, Eq(other.isStaged)),
62 Field("isActivated", &ApexSessionInfo::isActivated,
63 Eq(other.isActivated)),
64 Field("isRevertInProgress", &ApexSessionInfo::isRevertInProgress,
65 Eq(other.isRevertInProgress)),
66 Field("isActivationFailed", &ApexSessionInfo::isActivationFailed,
67 Eq(other.isActivationFailed)),
68 Field("isSuccess", &ApexSessionInfo::isSuccess, Eq(other.isSuccess)),
69 Field("isReverted", &ApexSessionInfo::isReverted,
70 Eq(other.isReverted)),
71 Field("isRevertFailed", &ApexSessionInfo::isRevertFailed,
72 Eq(other.isRevertFailed))),
73 arg, result_listener);
74 }
75
76 MATCHER_P(ApexInfoEq, other, "") {
77 return ExplainMatchResult(
78 AllOf(Field("moduleName", &ApexInfo::moduleName, Eq(other.moduleName)),
79 Field("modulePath", &ApexInfo::modulePath, Eq(other.modulePath)),
80 Field("preinstalledModulePath", &ApexInfo::preinstalledModulePath,
81 Eq(other.preinstalledModulePath)),
82 Field("versionCode", &ApexInfo::versionCode, Eq(other.versionCode)),
83 Field("isFactory", &ApexInfo::isFactory, Eq(other.isFactory)),
84 Field("isActive", &ApexInfo::isActive, Eq(other.isActive))),
85 arg, result_listener);
86 }
87
CreateSessionInfo(int session_id)88 inline ApexSessionInfo CreateSessionInfo(int session_id) {
89 ApexSessionInfo info;
90 info.sessionId = session_id;
91 info.isUnknown = false;
92 info.isVerified = false;
93 info.isStaged = false;
94 info.isActivated = false;
95 info.isRevertInProgress = false;
96 info.isActivationFailed = false;
97 info.isSuccess = false;
98 info.isReverted = false;
99 info.isRevertFailed = false;
100 return info;
101 }
102
103 } // namespace testing
104
105 // Must be in apex::android namespace, otherwise gtest won't be able to find it.
PrintTo(const ApexSessionInfo & session,std::ostream * os)106 inline void PrintTo(const ApexSessionInfo& session, std::ostream* os) {
107 *os << "apex_session: {\n";
108 *os << " sessionId : " << session.sessionId << "\n";
109 *os << " isUnknown : " << session.isUnknown << "\n";
110 *os << " isVerified : " << session.isVerified << "\n";
111 *os << " isStaged : " << session.isStaged << "\n";
112 *os << " isActivated : " << session.isActivated << "\n";
113 *os << " isActivationFailed : " << session.isActivationFailed << "\n";
114 *os << " isSuccess : " << session.isSuccess << "\n";
115 *os << " isReverted : " << session.isReverted << "\n";
116 *os << " isRevertFailed : " << session.isRevertFailed << "\n";
117 *os << "}";
118 }
119
PrintTo(const ApexInfo & apex,std::ostream * os)120 inline void PrintTo(const ApexInfo& apex, std::ostream* os) {
121 *os << "apex_info: {\n";
122 *os << " moduleName : " << apex.moduleName << "\n";
123 *os << " modulePath : " << apex.modulePath << "\n";
124 *os << " preinstalledModulePath : " << apex.preinstalledModulePath << "\n";
125 *os << " versionCode : " << apex.versionCode << "\n";
126 *os << " isFactory : " << apex.isFactory << "\n";
127 *os << " isActive : " << apex.isActive << "\n";
128 *os << "}";
129 }
130
131 } // namespace apex
132 } // namespace android
133