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 #ifndef _GTS_NANOAPPS_GENERAL_TEST_TEST_H_
18 #define _GTS_NANOAPPS_GENERAL_TEST_TEST_H_
19 
20 #include <shared/send_message.h>
21 
22 #include <chre.h>
23 
24 namespace general_test {
25 
26 /**
27  * Abstract base for all test cases.
28  */
29 class Test {
30  public:
31   Test(uint32_t minSupportedVersion);
~Test()32   virtual ~Test() {}
33 
34   void testHandleEvent(uint32_t senderInstanceId, uint16_t eventType,
35                        const void *eventData);
36 
37   void testSetUp(uint32_t messageSize, const void *message);
38 
39  protected:
40   /**
41    * Report a test-ending error due to an unexpectedEvent.
42    * This method halts execution on an unexpected event,
43    * and never returns.
44    *
45    * @param eventType  The event type
46    */
47   static void unexpectedEvent(uint16_t eventType);
48 
49   /**
50    * Wrapper structure to store the current async request.
51    */
52   struct chreAsyncRequest {
53     //! An opaque value that will be included in the chreAsyncResult
54     //! sent in relation to a chre async request.
55     const void *cookie;
56 
57     //! A type of request. Same field as that in {@link #chreAsyncResult}
58     uint8_t requestType;
59 
60     //! Timestamp when mading the request.
61     uint64_t requestTimeNs;
62 
63     //! Timeout to receive the chre async result.
64     uint64_t timeoutNs;
65   };
66 
67   /**
68    * Reports a test-ending error due to failure in chreAsyncResult.
69    *
70    * 1. chre async result is not success.
71    * 2. chre async result success, but errorCode is not CHRE_ERROR_NONE.
72    * 3. request cookie mismatch.
73    * 4. requestType mismatch.
74    * 5. result timeout.
75    *
76    * @param result chreAsyncResult of an async request.
77    * @param request lastest chre async request.
78    */
79   static void validateChreAsyncResult(const chreAsyncResult *result,
80                                       const chreAsyncRequest &request);
81 
82   /**
83    * Get the message data sent from the host, after performing consistency
84    * checks.
85    *
86    * The method centralizes a number of common consistency checks that tests
87    * will perform in taking the given CHRE event data and extracting out
88    * the raw data payload sent by the host.  This method is still useful
89    * when no message data is expected from the host, as we'll still
90    * perform the checks.
91    *
92    * This method will end the test in failure if any of the following happen:
93    * o 'senderInstanceId' != CHRE_INSTANCE_ID
94    * o 'eventType' != CHRE_EVENT_MESSAGE_FROM_HOST
95    * o 'eventData'->reservedMessageType != expectedMessageType
96    * o 'eventData'->messageSize != expectedMessageSize
97    *
98    * On passing all consistency checks, the message data can be expected
99    * to be in 'eventData->message'
100    *
101    * @param senderInstanceId  From handleEvent()
102    * @param eventType  From handleEvent()
103    * @param eventData  From handleEvent()
104    * @param expectedMessageType  The expected 'reservedMessageType' field
105    *     when 'eventData' is seen as a chreMessageFromHostData.
106    * @param expectedMessageSize  The expected 'messageSize' field
107    *     when 'eventData' is seen as a chreMessageFromHostData.
108    */
109   static const void *getMessageDataFromHostEvent(
110       uint32_t senderInstanceId, uint16_t eventType, const void *eventData,
111       nanoapp_testing::MessageType expectedMessageType,
112       uint32_t expectedMessageSize);
113 
114   virtual void handleEvent(uint32_t senderInstanceId, uint16_t eventType,
115                            const void *eventData) = 0;
116   virtual void setUp(uint32_t messageSize, const void *message) = 0;
117 
118   /**
119    * The platform reported CHRE API version.
120    *
121    * Nanoapps may use this to determine what version they are running
122    * on and perform any version specific behaviours.
123    */
124   const uint32_t mApiVersion;
125 
126  private:
127   /**
128    * Is the nanoapp supported by the platform reported CHRE API version.
129    *
130    * Nanoapps specify the minimum CHRE API version required during
131    * construction. If it is at least the version that is being reported
132    * by the platform then mIsSupported will be true. Else, the nanoapp
133    * will skip the test.
134    */
135   const bool mIsSupported;
136 };
137 
138 }  // namespace general_test
139 
140 #endif  // _GTS_NANOAPPS_GENERAL_TEST_TEST_H_
141