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 "status.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
IsOk(const Status & status)36 inline ::testing::AssertionResult IsOk(const Status& status) {
37 if (status.Ok()) {
38 return ::testing::AssertionSuccess() << " is Ok";
39 } else {
40 return ::testing::AssertionFailure()
41 << " failed with " << status.ErrorMessage();
42 }
43 }
44
45 template <typename T>
IsOk(const StatusOr<T> & status_or)46 inline ::testing::AssertionResult IsOk(const StatusOr<T>& status_or) {
47 if (status_or.Ok()) {
48 return ::testing::AssertionSuccess() << " is Ok";
49 } else {
50 return ::testing::AssertionFailure()
51 << " failed with " << status_or.ErrorMessage();
52 }
53 }
54
IsOk(const android::binder::Status & status)55 inline ::testing::AssertionResult IsOk(const android::binder::Status& status) {
56 if (status.isOk()) {
57 return ::testing::AssertionSuccess() << " is Ok";
58 } else {
59 return ::testing::AssertionFailure()
60 << " failed with " << status.exceptionMessage().c_str();
61 }
62 }
63
64 MATCHER_P(SessionInfoEq, other, "") {
65 return ExplainMatchResult(
66 AllOf(
67 Field("sessionId", &ApexSessionInfo::sessionId, Eq(other.sessionId)),
68 Field("isUnknown", &ApexSessionInfo::isUnknown, Eq(other.isUnknown)),
69 Field("isVerified", &ApexSessionInfo::isVerified,
70 Eq(other.isVerified)),
71 Field("isStaged", &ApexSessionInfo::isStaged, Eq(other.isStaged)),
72 Field("isActivated", &ApexSessionInfo::isActivated,
73 Eq(other.isActivated)),
74 Field("isRollbackInProgress", &ApexSessionInfo::isRollbackInProgress,
75 Eq(other.isRollbackInProgress)),
76 Field("isActivationFailed", &ApexSessionInfo::isActivationFailed,
77 Eq(other.isActivationFailed)),
78 Field("isSuccess", &ApexSessionInfo::isSuccess, Eq(other.isSuccess)),
79 Field("isRolledBack", &ApexSessionInfo::isRolledBack,
80 Eq(other.isRolledBack)),
81 Field("isRollbackFailed", &ApexSessionInfo::isRollbackFailed,
82 Eq(other.isRollbackFailed))),
83 arg, result_listener);
84 }
85
86 MATCHER_P(ApexInfoEq, other, "") {
87 return ExplainMatchResult(
88 AllOf(Field("packageName", &ApexInfo::packageName, Eq(other.packageName)),
89 Field("packagePath", &ApexInfo::packagePath, Eq(other.packagePath)),
90 Field("versioncode", &ApexInfo::versionCode, Eq(other.versionCode)),
91 Field("isFactory", &ApexInfo::isFactory, Eq(other.isFactory)),
92 Field("isActive", &ApexInfo::isActive, Eq(other.isActive))),
93 arg, result_listener);
94 }
95
CreateSessionInfo(int session_id)96 inline ApexSessionInfo CreateSessionInfo(int session_id) {
97 ApexSessionInfo info;
98 info.sessionId = session_id;
99 info.isUnknown = false;
100 info.isVerified = false;
101 info.isStaged = false;
102 info.isActivated = false;
103 info.isRollbackInProgress = false;
104 info.isActivationFailed = false;
105 info.isSuccess = false;
106 info.isRolledBack = false;
107 info.isRollbackFailed = false;
108 return info;
109 }
110
111 } // namespace testing
112
113 // Must be in apex::android namespace, otherwise gtest won't be able to find it.
PrintTo(const ApexSessionInfo & session,std::ostream * os)114 inline void PrintTo(const ApexSessionInfo& session, std::ostream* os) {
115 *os << "apex_session: {\n";
116 *os << " sessionId : " << session.sessionId << "\n";
117 *os << " isUnknown : " << session.isUnknown << "\n";
118 *os << " isVerified : " << session.isVerified << "\n";
119 *os << " isStaged : " << session.isStaged << "\n";
120 *os << " isActivated : " << session.isActivated << "\n";
121 *os << " isActivationFailed : " << session.isActivationFailed << "\n";
122 *os << " isSuccess : " << session.isSuccess << "\n";
123 *os << " isRolledBack : " << session.isRolledBack << "\n";
124 *os << " isRollbackFailed : " << session.isRollbackFailed << "\n";
125 *os << "}";
126 }
127
128 } // namespace apex
129 } // namespace android
130