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