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/version_consistency_test.h>
18
19 #include <cstddef>
20
21 #include <shared/send_message.h>
22
23 #include <chre.h>
24
25 #include "chre/util/macros.h"
26
27 using nanoapp_testing::sendFatalFailureToHost;
28 using nanoapp_testing::sendSuccessToHost;
29
30 namespace general_test {
31
VersionConsistencyTest()32 VersionConsistencyTest::VersionConsistencyTest() : Test(CHRE_API_VERSION_1_0) {}
33
setUp(uint32_t messageSize,const void *)34 void VersionConsistencyTest::setUp(uint32_t messageSize,
35 const void * /* message */) {
36 if (messageSize != 0) {
37 sendFatalFailureToHost(
38 "VersionConsistency message expects 0 additional bytes, got ",
39 &messageSize);
40 }
41
42 if (mApiVersion < CHRE_API_VERSION_1_0) {
43 sendFatalFailureToHost("API version less than 1.0", &mApiVersion);
44 }
45 if ((mApiVersion & UINT32_C(0xFFFF)) != 0) {
46 sendFatalFailureToHost("API version has two LSB set", &mApiVersion);
47 }
48 uint32_t platformVersion = chreGetVersion();
49 constexpr uint32_t kMajorMinorMask = UINT32_C(0xFFFF0000);
50 // Both mApiVersion and platformVersion refer to what the CHRE was
51 // built against, so they must agree in major and minor version.
52 if ((platformVersion & kMajorMinorMask) != (mApiVersion & kMajorMinorMask)) {
53 sendFatalFailureToHost("API and platform version mismatch",
54 &platformVersion);
55 }
56
57 // Confirm that our app (CHRE_API_VERSION) and CHRE (mApiVersion) were
58 // both compiled against the same major version. They're allowed to
59 // differ in minor version.
60 constexpr uint32_t kMajorMask = UINT32_C(0xFF000000);
61 if ((mApiVersion & kMajorMask) != (CHRE_API_VERSION & kMajorMask)) {
62 uint32_t appVersion = CHRE_API_VERSION;
63 sendFatalFailureToHost("App built against different major version",
64 &appVersion);
65 }
66
67 uint64_t platformId = chreGetPlatformId();
68 if ((platformId == UINT64_C(0)) || (platformId == UINT64_C(-1))) {
69 sendFatalFailureToHost("Bogus platform ID");
70 }
71 // TODO(b/30077401): We should send the Platform ID back to the Host, and
72 // have the Host confirm this is the Platform ID we used for loading
73 // the nanoapp.
74
75 sendSuccessToHost();
76 }
77
handleEvent(uint32_t senderInstanceId,uint16_t eventType,const void * eventData)78 void VersionConsistencyTest::handleEvent(uint32_t senderInstanceId,
79 uint16_t eventType,
80 const void *eventData) {
81 UNUSED_VAR(senderInstanceId);
82 UNUSED_VAR(eventData);
83
84 unexpectedEvent(eventType);
85 }
86
87 } // namespace general_test
88