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