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 <cstddef> // max_align_t
18 #include <cstdint>
19 #include <new> // placement new
20
21 #include <chre.h>
22
23 #include <general_test/basic_audio_test.h>
24 #include <general_test/basic_flush_async_test.h>
25 #include <general_test/basic_gnss_test.h>
26 #include <general_test/basic_sensor_tests.h>
27 #include <general_test/basic_wifi_test.h>
28 #include <general_test/estimated_host_time_test.h>
29 #include <general_test/event_between_apps_test.h>
30 #include <general_test/get_time_test.h>
31 #include <general_test/gnss_capabilities_test.h>
32 #include <general_test/heap_alloc_stress_test.h>
33 #include <general_test/heap_exhaustion_stability_test.h>
34 #include <general_test/hello_world_test.h>
35 #include <general_test/host_awake_suspend_test.h>
36 #include <general_test/logging_consistency_test.h>
37 #include <general_test/nanoapp_info_by_app_id_test.h>
38 #include <general_test/nanoapp_info_by_instance_id_test.h>
39 #include <general_test/nanoapp_info_events_test_observer.h>
40 #include <general_test/nanoapp_info_events_test_performer.h>
41 #include <general_test/send_event_stress_test.h>
42 #include <general_test/send_event_test.h>
43 #include <general_test/send_message_to_host_test.h>
44 #include <general_test/sensor_info_test.h>
45 #include <general_test/simple_heap_alloc_test.h>
46 #include <general_test/test.h>
47 #include <general_test/test_names.h>
48 #include <general_test/timer_cancel_test.h>
49 #include <general_test/timer_set_test.h>
50 #include <general_test/timer_stress_test.h>
51 #include <general_test/version_consistency_test.h>
52 #include <general_test/wifi_capabilities_test.h>
53 #include <general_test/wwan_capabilities_test.h>
54 #include <general_test/wwan_cell_info_test.h>
55 #include <shared/abort.h>
56 #include <shared/nano_endian.h>
57 #include <shared/nano_string.h>
58 #include <shared/send_message.h>
59
60 using nanoapp_testing::AbortBlame;
61 using nanoapp_testing::MessageType;
62 using nanoapp_testing::sendFatalFailureToHost;
63 using nanoapp_testing::sendInternalFailureToHost;
64
65 namespace general_test {
66
67 // The size of this array is checked at compile time by the static_assert
68 // in getNew().
69 alignas(alignof(max_align_t)) static uint8_t gGetNewBackingMemory[128];
70
71 template <typename N>
getNew()72 static N *getNew() {
73 // We intentionally avoid using chreHeapAlloc() to reduce dependencies
74 // for our tests, especially things like HelloWorld. This obviously
75 // cannot be called more than once, but our usage doesn't require it.
76 static_assert(sizeof(gGetNewBackingMemory) >= sizeof(N),
77 "getNew() backing memory is undersized");
78
79 return new (gGetNewBackingMemory) N();
80 }
81
82 // TODO(b/32114261): Remove this variable.
83 bool gUseNycMessageHack = true;
84
85 class App {
86 public:
App()87 App() : mConstructionCookie(kConstructed), mCurrentTest(nullptr) {}
88
~App()89 ~App() {
90 // Yes, it's very odd to actively set a value in our destructor.
91 // However, since we're making a static instance of this class,
92 // the space for this class will stick around (unlike heap memory
93 // which might get reused), so we can still use this to perform
94 // some testing.
95 mConstructionCookie = kDestructed;
96 }
97
wasConstructed() const98 bool wasConstructed() const {
99 return mConstructionCookie == kConstructed;
100 }
wasDestructed() const101 bool wasDestructed() const {
102 return mConstructionCookie == kDestructed;
103 }
104
105 void handleEvent(uint32_t senderInstanceId, uint16_t eventType,
106 const void *eventData);
107
108 void createTest(const void *eventData);
109 void freeTest();
110
111 private:
112 uint32_t mConstructionCookie;
113 Test *mCurrentTest;
114
115 static constexpr uint32_t kConstructed = UINT32_C(0x51501984);
116 static constexpr uint32_t kDestructed = UINT32_C(0x19845150);
117
118 // TODO(b/32114261): Remove this method.
119 chreMessageFromHostData adjustHostMessageForNYC(
120 const chreMessageFromHostData *data);
121 };
122
123 // In the NYC version of the CHRE, the "reservedMessageType" isn't
124 // assured to be sent correctly from the host. But we want our
125 // tests to be written using this field (it's cleaner). So in NYC
126 // the host prefixes this value in the first four bytes of 'message',
127 // and here we reconstruct the message to be correct.
128 // TODO(b/32114261): Remove this method.
adjustHostMessageForNYC(const chreMessageFromHostData * data)129 chreMessageFromHostData App::adjustHostMessageForNYC(
130 const chreMessageFromHostData *data) {
131 if (!gUseNycMessageHack) {
132 return *data;
133 }
134 chreMessageFromHostData ret;
135
136 if (data->messageSize < sizeof(uint32_t)) {
137 sendFatalFailureToHost("Undersized message in adjustHostMessageForNYC");
138 }
139 const uint8_t *messageBytes = static_cast<const uint8_t *>(data->message);
140 nanoapp_testing::memcpy(&(ret.reservedMessageType), messageBytes,
141 sizeof(ret.reservedMessageType));
142 ret.reservedMessageType =
143 nanoapp_testing::littleEndianToHost(ret.reservedMessageType);
144 ret.messageSize = data->messageSize - sizeof(ret.reservedMessageType);
145 ret.message = messageBytes + sizeof(ret.reservedMessageType);
146 return ret;
147 }
148
handleEvent(uint32_t senderInstanceId,uint16_t eventType,const void * eventData)149 void App::handleEvent(uint32_t senderInstanceId, uint16_t eventType,
150 const void *eventData) {
151 // TODO: When we get an API that fixes the reservedMessageType,
152 // then we should only use our adjustedData hack on APIs
153 // prior to it being fixed. Eventually, we should remove
154 // this altogether.
155 chreMessageFromHostData adjustedData;
156 if (eventType == CHRE_EVENT_MESSAGE_FROM_HOST) {
157 auto data = static_cast<const chreMessageFromHostData *>(eventData);
158 adjustedData = adjustHostMessageForNYC(data);
159 eventData = &adjustedData;
160 }
161
162 if (mCurrentTest != nullptr) {
163 // Our test is in progress, so let it take control.
164 mCurrentTest->testHandleEvent(senderInstanceId, eventType, eventData);
165 return;
166 }
167
168 // No test in progress, so we expect this message to be the host telling
169 // us which test to run. We fail if it's anything else.
170 if (eventType != CHRE_EVENT_MESSAGE_FROM_HOST) {
171 uint32_t localEventType = eventType;
172 sendFatalFailureToHost("Unexpected event type with no established test:",
173 &localEventType);
174 }
175 if (senderInstanceId != CHRE_INSTANCE_ID) {
176 sendFatalFailureToHost("Got MESSAGE_FROM_HOST not from CHRE_INSTANCE_ID:",
177 &senderInstanceId);
178 }
179 createTest(eventData);
180 }
181
createTest(const void * eventData)182 void App::createTest(const void *eventData) {
183 if (mCurrentTest != nullptr) {
184 sendInternalFailureToHost("Got to createTest() with non-null mCurrentTest");
185 }
186
187 auto data = static_cast<const chreMessageFromHostData *>(eventData);
188
189 switch (static_cast<TestNames>(data->reservedMessageType)) {
190 using namespace general_test;
191
192 #define CASE(testName, className) \
193 case TestNames::testName: \
194 mCurrentTest = getNew<className>(); \
195 break;
196
197 CASE(kHelloWorld, HelloWorldTest);
198 CASE(kSimpleHeapAlloc, SimpleHeapAllocTest);
199 CASE(kHeapAllocStress, HeapAllocStressTest);
200 CASE(kGetTime, GetTimeTest);
201 CASE(kEventBetweenApps0, EventBetweenApps0);
202 CASE(kEventBetweenApps1, EventBetweenApps1);
203 CASE(kSendEvent, SendEventTest);
204 CASE(kBasicAccelerometer, BasicAccelerometerTest);
205 CASE(kBasicInstantMotionDetect, BasicInstantMotionDetectTest);
206 CASE(kBasicStationaryDetect, BasicStationaryDetectTest);
207 CASE(kBasicGyroscope, BasicGyroscopeTest);
208 CASE(kBasicMagnetometer, BasicMagnetometerTest);
209 CASE(kBasicBarometer, BasicBarometerTest);
210 CASE(kBasicLightSensor, BasicLightSensorTest);
211 CASE(kBasicProximity, BasicProximityTest);
212 CASE(kVersionConsistency, VersionConsistencyTest);
213 CASE(kLoggingConsistency, LoggingConsistencyTest);
214 CASE(kSendMessageToHost, SendMessageToHostTest);
215 CASE(kTimerSet, TimerSetTest);
216 CASE(kTimerCancel, TimerCancelTest);
217 CASE(kTimerStress, TimerStressTest);
218 CASE(kSendEventStress, SendEventStressTest);
219 CASE(kHeapExhaustionStability, HeapExhaustionStabilityTest);
220 CASE(kGnssCapabilities, GnssCapabilitiesTest);
221 CASE(kWifiCapabilities, WifiCapabilitiesTest);
222 CASE(kWwanCapabilities, WwanCapabilitiesTest);
223 CASE(kSensorInfo, SensorInfoTest);
224 CASE(kWwanCellInfoTest, WwanCellInfoTest);
225 CASE(kEstimatedHostTime, EstimatedHostTimeTest);
226 CASE(kNanoappInfoByAppId, NanoappInfoByAppIdTest);
227 CASE(kNanoappInfoByInstanceId, NanoappInfoByInstanceIdTest);
228 CASE(kNanoAppInfoEventsPerformer, NanoAppInfoEventsTestPerformer);
229 CASE(kNanoAppInfoEventsObserver, NanoAppInfoEventsTestObserver);
230 CASE(kBasicAudioTest, BasicAudioTest);
231 CASE(kHostAwakeSuspend, HostAwakeSuspendTest);
232 CASE(kBasicGnssTest, BasicGnssTest);
233 CASE(kBasicWifiTest, BasicWifiTest);
234 CASE(kBasicSensorFlushAsyncTest, BasicSensorFlushAsyncTest);
235
236 #undef CASE
237
238 default:
239 sendFatalFailureToHost("Unexpected message type:",
240 &(data->reservedMessageType));
241 }
242
243 if (mCurrentTest != nullptr) {
244 mCurrentTest->testSetUp(data->messageSize, data->message);
245 } else {
246 sendInternalFailureToHost("createTest() ended with null mCurrentTest");
247 }
248 }
249
freeTest()250 void App::freeTest() {
251 if (mCurrentTest == nullptr) {
252 sendInternalFailureToHost("Nanoapp unloading without running any test");
253 }
254 mCurrentTest->~Test();
255 }
256
257 } // namespace general_test
258
259 static general_test::App gApp;
260
nanoappHandleEvent(uint32_t senderInstanceId,uint16_t eventType,const void * eventData)261 extern "C" void nanoappHandleEvent(uint32_t senderInstanceId,
262 uint16_t eventType, const void *eventData) {
263 gApp.handleEvent(senderInstanceId, eventType, eventData);
264 }
265
266 static uint32_t zeroedBytes[13];
267
nanoappStart(void)268 extern "C" bool nanoappStart(void) {
269 // zeroedBytes is in the BSS and needs to be zero'd out.
270 for (size_t i = 0; i < sizeof(zeroedBytes) / sizeof(zeroedBytes[0]); i++) {
271 if (zeroedBytes[i] != 0) {
272 return false;
273 }
274 }
275
276 // A CHRE is required to call the constructor of our class prior to
277 // reaching this point.
278 return gApp.wasConstructed();
279 }
280
nanoappEnd(void)281 extern "C" void nanoappEnd(void) {
282 if (gApp.wasDestructed()) {
283 // It's not legal for us to send a message from here. The best
284 // we can do is abort, although there's no means for the end user
285 // to see such a failure.
286 // TODO: Figure out how to have this failure noticed.
287 nanoapp_testing::abort(AbortBlame::kChreInNanoappEnd);
288 }
289 gApp.freeTest();
290
291 // TODO: Unclear how we can test the global destructor being called,
292 // but that would be good to test. Since it's supposed to happen
293 // after this call completes, it's difficult to test.
294 }
295