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, UNKNOWN_ERROR, NO_MEMORY, INVALID_OPERATION, BAD_VALUE, BAD_TYPE, NAME_NOT_FOUND,
42           PERMISSION_DENIED, NO_INIT, ALREADY_EXISTS, DEAD_OBJECT, FAILED_TRANSACTION, BAD_INDEX,
43           NOT_ENOUGH_DATA, WOULD_BLOCK, TIMED_OUT, UNKNOWN_TRANSACTION, FDS_NOT_ALLOWED}) {
44         ASSERT_EQ(status, statusTFromBinderStatus(binderStatusFromStatusT(status)));
45     }
46 }
47 
48 // Binder exceptions show as an error (not fixed at this time); these come fromExceptionCode().
TEST(audio_aidl_status_tests,binderStatusExceptions)49 TEST(audio_aidl_status_tests, binderStatusExceptions) {
50     for (int exceptionCode : {
51                  // Status::EX_NONE,
52                  Status::EX_SECURITY, Status::EX_BAD_PARCELABLE, Status::EX_ILLEGAL_ARGUMENT,
53                  Status::EX_NULL_POINTER, Status::EX_ILLEGAL_STATE, Status::EX_NETWORK_MAIN_THREAD,
54                  Status::EX_UNSUPPORTED_OPERATION,
55                  // Status::EX_SERVICE_SPECIFIC, -- tested fromServiceSpecificError()
56                  Status::EX_PARCELABLE,
57                  // This is special and Java specific; see Parcel.java.
58                  Status::EX_HAS_REPLY_HEADER,
59                  // This is special, and indicates to C++ binder proxies that the
60                  // transaction has failed at a low level.
61                  // Status::EX_TRANSACTION_FAILED, -- tested fromStatusT().
62          }) {
63         ASSERT_NE(OK, statusTFromBinderStatus(Status::fromExceptionCode(exceptionCode)));
64     }
65 }
66 
67 // Binder transaction errors show exactly in status_t; these come fromStatusT().
TEST(audio_aidl_status_tests,binderStatusTransactionError)68 TEST(audio_aidl_status_tests, binderStatusTransactionError) {
69     for (status_t status :
70          {OK,  // Note: fromStatusT does check if this is 0, so this is no error.
71           UNKNOWN_ERROR, NO_MEMORY, INVALID_OPERATION, BAD_VALUE, BAD_TYPE, NAME_NOT_FOUND,
72           PERMISSION_DENIED, NO_INIT, ALREADY_EXISTS, DEAD_OBJECT, FAILED_TRANSACTION, BAD_INDEX,
73           NOT_ENOUGH_DATA, WOULD_BLOCK, TIMED_OUT, UNKNOWN_TRANSACTION, FDS_NOT_ALLOWED}) {
74         ASSERT_EQ(status, statusTFromBinderStatus(Status::fromStatusT(status)));
75     }
76 }
77 
78 // Binder service specific errors show in status_t; these come fromServiceSpecificError().
TEST(audio_aidl_status_tests,binderStatusServiceSpecificError)79 TEST(audio_aidl_status_tests, binderStatusServiceSpecificError) {
80     // fromServiceSpecificError() still stores exception code if status is 0.
81     for (status_t status = -1; status > STATUS_T_SMALL_VALUE_LIMIT; --status) {
82         ASSERT_EQ(status, statusTFromBinderStatus(Status::fromServiceSpecificError(status)));
83     }
84 }
85 
86 // Binder status with message.
TEST(audio_aidl_status_tests,binderStatusMessage)87 TEST(audio_aidl_status_tests, binderStatusMessage) {
88     const String8 message("abcd");
89     for (status_t status = -1; status > STATUS_T_SMALL_VALUE_LIMIT; --status) {
90         const Status binderStatus = binderStatusFromStatusT(status, message.c_str());
91         ASSERT_EQ(status, statusTFromBinderStatus(binderStatus));
92         ASSERT_EQ(message, binderStatus.exceptionMessage());
93     }
94 }
95