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