1 /*
2 * Copyright (C) 2016 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 <general_test/test.h>
18
19 #include <shared/abort.h>
20 #include <shared/send_message.h>
21 #include <shared/time_util.h>
22
23 #include <chre.h>
24
25 using nanoapp_testing::sendFatalFailureToHost;
26 using nanoapp_testing::sendFatalFailureToHostUint8;
27
28 namespace general_test {
29
Test(uint32_t minSupportedVersion)30 Test::Test(uint32_t minSupportedVersion)
31 : mApiVersion(chreGetApiVersion()),
32 mIsSupported(mApiVersion >= minSupportedVersion) {}
33
testSetUp(uint32_t messageSize,const void * message)34 void Test::testSetUp(uint32_t messageSize, const void *message) {
35 if (mIsSupported) {
36 setUp(messageSize, message);
37 } else {
38 sendMessageToHost(nanoapp_testing::MessageType::kSkipped);
39 }
40 }
41
testHandleEvent(uint32_t senderInstanceId,uint16_t eventType,const void * eventData)42 void Test::testHandleEvent(uint32_t senderInstanceId, uint16_t eventType,
43 const void *eventData) {
44 if (mIsSupported) {
45 handleEvent(senderInstanceId, eventType, eventData);
46 }
47 }
48
unexpectedEvent(uint16_t eventType)49 void Test::unexpectedEvent(uint16_t eventType) {
50 uint32_t localEvent = eventType;
51 sendFatalFailureToHost("Test received unexpected event:", &localEvent);
52 }
53
validateChreAsyncResult(const chreAsyncResult * result,const chreAsyncRequest & request)54 void Test::validateChreAsyncResult(const chreAsyncResult *result,
55 const chreAsyncRequest &request) {
56 if (!result->success) {
57 sendFatalFailureToHostUint8("chre async result error: %d",
58 result->errorCode);
59 }
60 if (result->success && result->errorCode != CHRE_ERROR_NONE) {
61 sendFatalFailureToHostUint8(
62 "Request was successfully processed, but got errorCode: %d",
63 result->errorCode);
64 }
65 if (result->reserved != 0) {
66 sendFatalFailureToHostUint8("reserved should be 0, got: %d",
67 result->reserved);
68 }
69 if (result->cookie != request.cookie) {
70 chreLog(CHRE_LOG_ERROR, "Request cookie is %p, got %p", request.cookie,
71 result->cookie);
72 sendFatalFailureToHost("Request cookie mismatch");
73 }
74 if (result->requestType != request.requestType) {
75 chreLog(CHRE_LOG_ERROR, "Request requestType is %d, got %d",
76 request.requestType, result->requestType);
77 sendFatalFailureToHost("Request requestType mismatch");
78 }
79 if (chreGetTime() - request.requestTimeNs > request.timeoutNs) {
80 nanoapp_testing::sendFatalFailureToHostUint8(
81 "Did not receive chreWifiAsyncEvent within %d seconds.",
82 request.timeoutNs / nanoapp_testing::kOneSecondInNanoseconds);
83 }
84 }
85
getMessageDataFromHostEvent(uint32_t senderInstanceId,uint16_t eventType,const void * eventData,nanoapp_testing::MessageType expectedMessageType,uint32_t expectedMessageSize)86 const void *Test::getMessageDataFromHostEvent(
87 uint32_t senderInstanceId, uint16_t eventType, const void *eventData,
88 nanoapp_testing::MessageType expectedMessageType,
89 uint32_t expectedMessageSize) {
90 if (senderInstanceId != CHRE_INSTANCE_ID) {
91 sendFatalFailureToHost("Unexpected sender ID:", &senderInstanceId);
92 }
93 if (eventType != CHRE_EVENT_MESSAGE_FROM_HOST) {
94 unexpectedEvent(eventType);
95 }
96 if (eventData == nullptr) {
97 sendFatalFailureToHost("NULL eventData given");
98 }
99 auto data = static_cast<const chreMessageFromHostData *>(eventData);
100 if (data->reservedMessageType != uint32_t(expectedMessageType)) {
101 sendFatalFailureToHost("Unexpected reservedMessageType:",
102 &(data->reservedMessageType));
103 }
104 if (data->messageSize != expectedMessageSize) {
105 sendFatalFailureToHost("Unexpected messageSize:", &(data->messageSize));
106 }
107 return data->message;
108 }
109
110 } // namespace general_test
111