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 <gtest/gtest.h>
18 #include <media/AidlConversionUtil.h>
19 #include <utils/Errors.h>
20
21 using namespace android;
22 using namespace android::aidl_utils;
23 using android::binder::Status;
24
25 // Tests for statusTFromBinderStatus() and binderStatusFromStatusT().
26
27 // STATUS_T_SMALL_VALUE_LIMIT is an arbitrary limit where we exhaustively check status_t errors.
28 // It is known that this limit doesn't cover UNKNOWN_ERROR ~ INT32_MIN.
29 constexpr status_t STATUS_T_SMALL_VALUE_LIMIT = -1000;
30
31 // Small status values are preserved on round trip
TEST(audio_aidl_status_tests,statusRoundTripSmallValues)32 TEST(audio_aidl_status_tests, statusRoundTripSmallValues) {
33 for (status_t status = 0; status > STATUS_T_SMALL_VALUE_LIMIT; --status) {
34 ASSERT_EQ(status, statusTFromBinderStatus(binderStatusFromStatusT(status)));
35 }
36 }
37
38 // Special status values are preserved on round trip.
TEST(audio_aidl_status_tests,statusRoundTripSpecialValues)39 TEST(audio_aidl_status_tests, statusRoundTripSpecialValues) {
40 for (status_t status : {
41 OK,
42 UNKNOWN_ERROR,
43 NO_MEMORY,
44 INVALID_OPERATION,
45 BAD_VALUE,
46 BAD_TYPE,
47 NAME_NOT_FOUND,
48 PERMISSION_DENIED,
49 NO_INIT,
50 ALREADY_EXISTS,
51 DEAD_OBJECT,
52 FAILED_TRANSACTION,
53 BAD_INDEX,
54 NOT_ENOUGH_DATA,
55 WOULD_BLOCK,
56 TIMED_OUT,
57 UNKNOWN_TRANSACTION,
58 FDS_NOT_ALLOWED}) {
59 ASSERT_EQ(status, statusTFromBinderStatus(binderStatusFromStatusT(status)));
60 }
61 }
62
63 // Binder exceptions show as an error (not fixed at this time); these come fromExceptionCode().
TEST(audio_aidl_status_tests,binderStatusExceptions)64 TEST(audio_aidl_status_tests, binderStatusExceptions) {
65 for (int exceptionCode : {
66 //Status::EX_NONE,
67 Status::EX_SECURITY,
68 Status::EX_BAD_PARCELABLE,
69 Status::EX_ILLEGAL_ARGUMENT,
70 Status::EX_NULL_POINTER,
71 Status::EX_ILLEGAL_STATE,
72 Status::EX_NETWORK_MAIN_THREAD,
73 Status::EX_UNSUPPORTED_OPERATION,
74 //Status::EX_SERVICE_SPECIFIC, -- tested fromServiceSpecificError()
75 Status::EX_PARCELABLE,
76 // This is special and Java specific; see Parcel.java.
77 Status::EX_HAS_REPLY_HEADER,
78 // This is special, and indicates to C++ binder proxies that the
79 // transaction has failed at a low level.
80 //Status::EX_TRANSACTION_FAILED, -- tested fromStatusT().
81 }) {
82 ASSERT_NE(OK, statusTFromBinderStatus(Status::fromExceptionCode(exceptionCode)));
83 }
84 }
85
86 // Binder transaction errors show exactly in status_t; these come fromStatusT().
TEST(audio_aidl_status_tests,binderStatusTransactionError)87 TEST(audio_aidl_status_tests, binderStatusTransactionError) {
88 for (status_t status : {
89 OK, // Note: fromStatusT does check if this is 0, so this is no error.
90 UNKNOWN_ERROR,
91 NO_MEMORY,
92 INVALID_OPERATION,
93 BAD_VALUE,
94 BAD_TYPE,
95 NAME_NOT_FOUND,
96 PERMISSION_DENIED,
97 NO_INIT,
98 ALREADY_EXISTS,
99 DEAD_OBJECT,
100 FAILED_TRANSACTION,
101 BAD_INDEX,
102 NOT_ENOUGH_DATA,
103 WOULD_BLOCK,
104 TIMED_OUT,
105 UNKNOWN_TRANSACTION,
106 FDS_NOT_ALLOWED}) {
107 ASSERT_EQ(status, statusTFromBinderStatus(Status::fromStatusT(status)));
108 }
109 }
110
111 // Binder service specific errors show in status_t; these come fromServiceSpecificError().
TEST(audio_aidl_status_tests,binderStatusServiceSpecificError)112 TEST(audio_aidl_status_tests, binderStatusServiceSpecificError) {
113 // fromServiceSpecificError() still stores exception code if status is 0.
114 for (status_t status = -1; status > STATUS_T_SMALL_VALUE_LIMIT; --status) {
115 ASSERT_EQ(status, statusTFromBinderStatus(Status::fromServiceSpecificError(status)));
116 }
117 }
118
119 // Binder status with message.
TEST(audio_aidl_status_tests,binderStatusMessage)120 TEST(audio_aidl_status_tests, binderStatusMessage) {
121 const String8 message("abcd");
122 for (status_t status = -1; status > STATUS_T_SMALL_VALUE_LIMIT; --status) {
123 const Status binderStatus = binderStatusFromStatusT(status, message.c_str());
124 ASSERT_EQ(status, statusTFromBinderStatus(binderStatus));
125 ASSERT_EQ(message, binderStatus.exceptionMessage());
126 }
127 }
128