xref: /aosp_15_r20/cts/tests/tests/binder_ndk/libbinder_ndk_test/test_status.cpp (revision b7c941bb3fa97aba169d73cee0bed2de8ac964bf)
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 #define LOG_TAG "Cts-NdkBinderTest"
17 
18 #include "utilities.h"
19 
20 #include <android/binder_auto_utils.h>
21 #include <android/binder_status.h>
22 #include <gtest/gtest.h>
23 
24 #include <set>
25 
26 // clang-format off
27 const static std::set<binder_status_t> kErrorStatuses = {
28   STATUS_UNKNOWN_ERROR,
29   STATUS_NO_MEMORY,
30   STATUS_INVALID_OPERATION,
31   STATUS_BAD_VALUE,
32   STATUS_BAD_TYPE,
33   STATUS_NAME_NOT_FOUND,
34   STATUS_PERMISSION_DENIED,
35   STATUS_NO_INIT,
36   STATUS_ALREADY_EXISTS,
37   STATUS_DEAD_OBJECT,
38   STATUS_FAILED_TRANSACTION,
39   STATUS_BAD_INDEX,
40   STATUS_NOT_ENOUGH_DATA,
41   STATUS_WOULD_BLOCK,
42   STATUS_TIMED_OUT,
43   STATUS_UNKNOWN_TRANSACTION,
44   STATUS_FDS_NOT_ALLOWED,
45   STATUS_UNEXPECTED_NULL
46 };
47 // Not in the API or above list
48 const static std::set<binder_status_t> kUnknownStatuses = { -77, 1, 404, EX_TRANSACTION_FAILED };
49 
50 const static std::set<binder_exception_t> kErrorExceptions = {
51   EX_SECURITY,
52   EX_BAD_PARCELABLE,
53   EX_ILLEGAL_ARGUMENT,
54   EX_NULL_POINTER,
55   EX_ILLEGAL_STATE,
56   EX_NETWORK_MAIN_THREAD,
57   EX_UNSUPPORTED_OPERATION,
58   EX_SERVICE_SPECIFIC,
59   EX_PARCELABLE,
60   EX_TRANSACTION_FAILED
61 };
62 // Not in the API or above list
63 const static std::set<binder_exception_t> kUnknownExceptions = { -77, 1, 404, STATUS_UNKNOWN_ERROR };
64 // clang-format on
65 
66 // Checks the various attributes expected for an okay status.
checkIsOkay(const AStatus * status)67 static void checkIsOkay(const AStatus* status) {
68   EXPECT_TRUE(AStatus_isOk(status));
69   EXPECT_EQ(std::string(), AStatus_getMessage(status));
70   EXPECT_EQ(EX_NONE, AStatus_getExceptionCode(status));
71   EXPECT_EQ(0, AStatus_getServiceSpecificError(status));
72   EXPECT_EQ(STATUS_OK, AStatus_getStatus(status));
73 }
checkIsErrorException(const AStatus * status,binder_exception_t exception,const std::string & message)74 static void checkIsErrorException(const AStatus* status,
75                                   binder_exception_t exception,
76                                   const std::string& message) {
77   EXPECT_FALSE(AStatus_isOk(status));
78   EXPECT_EQ(message, AStatus_getMessage(status));
79   EXPECT_EQ(exception, AStatus_getExceptionCode(status));
80   // not a service-specific error, so other errorcodes return the default
81   EXPECT_EQ(0, AStatus_getServiceSpecificError(status));
82   EXPECT_EQ(exception == EX_TRANSACTION_FAILED ? STATUS_FAILED_TRANSACTION
83                                                : STATUS_OK,
84             AStatus_getStatus(status));
85 }
checkIsServiceSpecific(const AStatus * status,int32_t error,const std::string & message)86 static void checkIsServiceSpecific(const AStatus* status, int32_t error,
87                                    const std::string& message) {
88   EXPECT_FALSE(AStatus_isOk(status));
89   EXPECT_EQ(message, AStatus_getMessage(status));
90   EXPECT_EQ(EX_SERVICE_SPECIFIC, AStatus_getExceptionCode(status));
91   EXPECT_EQ(error, AStatus_getServiceSpecificError(status));
92   // not a service-specific error, so other errorcodes return the default
93   EXPECT_EQ(STATUS_OK, AStatus_getStatus(status));
94 }
checkIsErrorStatus(const AStatus * status,binder_status_t statusT)95 static void checkIsErrorStatus(const AStatus* status, binder_status_t statusT) {
96   EXPECT_FALSE(AStatus_isOk(status));
97   EXPECT_EQ(std::string(), AStatus_getMessage(status));
98   EXPECT_EQ(EX_TRANSACTION_FAILED, AStatus_getExceptionCode(status));
99   EXPECT_EQ(statusT, AStatus_getStatus(status));
100   // not a service-specific error, so other errorcodes return the default
101   EXPECT_EQ(0, AStatus_getServiceSpecificError(status));
102 }
103 
TEST(NdkBinderTest_AStatus,OkIsOk)104 TEST(NdkBinderTest_AStatus, OkIsOk) {
105   AStatus* status = AStatus_newOk();
106   checkIsOkay(status);
107   AStatus_delete(status);
108 }
109 
TEST(NdkBinderTest_AStatus,NoExceptionIsOkay)110 TEST(NdkBinderTest_AStatus, NoExceptionIsOkay) {
111   AStatus* status = AStatus_fromExceptionCode(EX_NONE);
112   checkIsOkay(status);
113   AStatus_delete(status);
114 }
115 
TEST(NdkBinderTest_AStatus,StatusOkIsOkay)116 TEST(NdkBinderTest_AStatus, StatusOkIsOkay) {
117   AStatus* status = AStatus_fromStatus(STATUS_OK);
118   checkIsOkay(status);
119   AStatus_delete(status);
120 }
121 
TEST(NdkBinderTest_AStatus,ExceptionIsNotOkay)122 TEST(NdkBinderTest_AStatus, ExceptionIsNotOkay) {
123   for (binder_exception_t exception : kErrorExceptions) {
124     AStatus* status = AStatus_fromExceptionCode(exception);
125     checkIsErrorException(status, exception, "" /*message*/);
126     AStatus_delete(status);
127   }
128 }
129 
TEST(NdkBinderTest_AStatus,ExceptionWithMessageIsNotOkay)130 TEST(NdkBinderTest_AStatus, ExceptionWithMessageIsNotOkay) {
131   const std::string kMessage = "Something arbitrary.";
132   for (binder_exception_t exception : kErrorExceptions) {
133     AStatus* status =
134         AStatus_fromExceptionCodeWithMessage(exception, kMessage.c_str());
135     checkIsErrorException(status, exception, kMessage);
136     AStatus_delete(status);
137   }
138 }
139 
TEST(NdkBinderTest_AStatus,ServiceSpecificIsNotOkay)140 TEST(NdkBinderTest_AStatus, ServiceSpecificIsNotOkay) {
141   for (int32_t error : {-404, -1, 0, 1, 23, 918}) {
142     AStatus* status = AStatus_fromServiceSpecificError(error);
143     checkIsServiceSpecific(status, error, "" /*message*/);
144     AStatus_delete(status);
145   }
146 }
147 
TEST(NdkBinderTest_AStatus,ServiceSpecificWithMessageIsNotOkay)148 TEST(NdkBinderTest_AStatus, ServiceSpecificWithMessageIsNotOkay) {
149   const std::string kMessage = "Something also arbitrary.";
150   for (int32_t error : {-404, -1, 0, 1, 23, 918}) {
151     AStatus* status =
152         AStatus_fromServiceSpecificErrorWithMessage(error, kMessage.c_str());
153     checkIsServiceSpecific(status, error, kMessage);
154     AStatus_delete(status);
155   }
156 }
157 
TEST(NdkBinderTest_AStatus,StatusIsNotOkay)158 TEST(NdkBinderTest_AStatus, StatusIsNotOkay) {
159   for (binder_status_t statusT : kErrorStatuses) {
160     AStatus* status = AStatus_fromStatus(statusT);
161     checkIsErrorStatus(status, statusT);
162     AStatus_delete(status);
163   }
164 }
165 
TEST(NdkBinderTest_AStatus,ExceptionsPruned)166 TEST(NdkBinderTest_AStatus, ExceptionsPruned) {
167   for (binder_exception_t exception : kUnknownExceptions) {
168     EXPECT_EQ(kErrorExceptions.find(exception), kErrorExceptions.end())
169         << exception;
170 
171     AStatus* status = AStatus_fromExceptionCode(exception);
172     checkIsErrorException(status, EX_TRANSACTION_FAILED, "" /*message*/);
173     AStatus_delete(status);
174   }
175 }
176 
TEST(NdkBinderTest_AStatus,ExceptionsPrunedWithMessage)177 TEST(NdkBinderTest_AStatus, ExceptionsPrunedWithMessage) {
178   const std::string kMessage = "Something else arbitrary.";
179   for (binder_exception_t exception : kUnknownExceptions) {
180     EXPECT_EQ(kErrorExceptions.find(exception), kErrorExceptions.end())
181         << exception;
182 
183     AStatus* status =
184         AStatus_fromExceptionCodeWithMessage(exception, kMessage.c_str());
185     checkIsErrorException(status, EX_TRANSACTION_FAILED, kMessage);
186     AStatus_delete(status);
187   }
188 }
189 
TEST(NdkBinderTest_AStatus,StatusesPruned)190 TEST(NdkBinderTest_AStatus, StatusesPruned) {
191   for (binder_status_t statusT : kUnknownStatuses) {
192     EXPECT_EQ(kErrorStatuses.find(statusT), kErrorStatuses.end()) << statusT;
193 
194     AStatus* status = AStatus_fromStatus(statusT);
195     checkIsErrorStatus(status, STATUS_UNKNOWN_ERROR);
196     AStatus_delete(status);
197   }
198 }
199 
TEST(NdkBinderTest_AStatus,StatusDescription)200 TEST(NdkBinderTest_AStatus, StatusDescription) {
201   using ndk::ScopedAStatus;
202 
203   EXPECT_TRUE(
204       ContainsSubstring(ScopedAStatus::fromExceptionCode(EX_TRANSACTION_FAILED).getDescription(),
205                         "TRANSACTION_FAILED"));
206   EXPECT_TRUE(ContainsSubstring(
207       ScopedAStatus::fromExceptionCodeWithMessage(EX_TRANSACTION_FAILED, "asdf").getDescription(),
208       "asdf"));
209   EXPECT_TRUE(
210       ContainsSubstring(ScopedAStatus::fromServiceSpecificError(42).getDescription(), "42"));
211   EXPECT_TRUE(ContainsSubstring(
212       ScopedAStatus::fromServiceSpecificErrorWithMessage(42, "asdf").getDescription(), "asdf"));
213   EXPECT_TRUE(
214       ContainsSubstring(ScopedAStatus::fromStatus(STATUS_BAD_TYPE).getDescription(), "BAD_TYPE"));
215 }
216