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