blob: 585779e472b9d2e2d8ff96857407cb8109017fd6 [file] [log] [blame]
Fengwei Yin83e0e422014-05-24 05:32:09 +08001/*
2 * Copyright (C) 2014 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 <input/InputTransport.h>
18#include <input/Input.h>
19
20namespace android {
21
22#define CHECK_OFFSET(type, member, expected_offset) \
Chih-Hung Hsieh4a186d42016-05-20 11:33:26 -070023 static_assert((offsetof(type, member) == (expected_offset)), "")
Fengwei Yin83e0e422014-05-24 05:32:09 +080024
25struct Foo {
26 uint32_t dummy;
27 PointerCoords coords;
28};
29
30void TestPointerCoordsAlignment() {
31 CHECK_OFFSET(Foo, coords, 8);
32}
33
34void TestInputMessageAlignment() {
35 CHECK_OFFSET(InputMessage, body, 8);
36
Siarhei Vishniakoua64c1592020-06-22 12:02:29 -050037 CHECK_OFFSET(InputMessage::Body::Key, eventId, 0);
Fengwei Yin83e0e422014-05-24 05:32:09 +080038 CHECK_OFFSET(InputMessage::Body::Key, eventTime, 8);
39 CHECK_OFFSET(InputMessage::Body::Key, deviceId, 16);
40 CHECK_OFFSET(InputMessage::Body::Key, source, 20);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +010041 CHECK_OFFSET(InputMessage::Body::Key, displayId, 24);
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -060042 CHECK_OFFSET(InputMessage::Body::Key, hmac, 28);
43 CHECK_OFFSET(InputMessage::Body::Key, action, 60);
44 CHECK_OFFSET(InputMessage::Body::Key, flags, 64);
45 CHECK_OFFSET(InputMessage::Body::Key, keyCode, 68);
46 CHECK_OFFSET(InputMessage::Body::Key, scanCode, 72);
47 CHECK_OFFSET(InputMessage::Body::Key, metaState, 76);
48 CHECK_OFFSET(InputMessage::Body::Key, repeatCount, 80);
49 CHECK_OFFSET(InputMessage::Body::Key, downTime, 88);
Fengwei Yin83e0e422014-05-24 05:32:09 +080050
Siarhei Vishniakoua64c1592020-06-22 12:02:29 -050051 CHECK_OFFSET(InputMessage::Body::Motion, eventId, 0);
Siarhei Vishniakou38b7f7f2021-03-05 01:57:08 +000052 CHECK_OFFSET(InputMessage::Body::Motion, empty1, 4);
Fengwei Yin83e0e422014-05-24 05:32:09 +080053 CHECK_OFFSET(InputMessage::Body::Motion, eventTime, 8);
54 CHECK_OFFSET(InputMessage::Body::Motion, deviceId, 16);
55 CHECK_OFFSET(InputMessage::Body::Motion, source, 20);
Tarandeep Singh58641502017-07-31 10:51:54 -070056 CHECK_OFFSET(InputMessage::Body::Motion, displayId, 24);
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -060057 CHECK_OFFSET(InputMessage::Body::Motion, hmac, 28);
58 CHECK_OFFSET(InputMessage::Body::Motion, action, 60);
59 CHECK_OFFSET(InputMessage::Body::Motion, actionButton, 64);
60 CHECK_OFFSET(InputMessage::Body::Motion, flags, 68);
61 CHECK_OFFSET(InputMessage::Body::Motion, metaState, 72);
62 CHECK_OFFSET(InputMessage::Body::Motion, buttonState, 76);
63 CHECK_OFFSET(InputMessage::Body::Motion, classification, 80);
Siarhei Vishniakou38b7f7f2021-03-05 01:57:08 +000064 CHECK_OFFSET(InputMessage::Body::Motion, empty2, 81);
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -060065 CHECK_OFFSET(InputMessage::Body::Motion, edgeFlags, 84);
66 CHECK_OFFSET(InputMessage::Body::Motion, downTime, 88);
chaviw9eaa22c2020-07-01 16:21:27 -070067 CHECK_OFFSET(InputMessage::Body::Motion, dsdx, 96);
68 CHECK_OFFSET(InputMessage::Body::Motion, dtdx, 100);
69 CHECK_OFFSET(InputMessage::Body::Motion, dtdy, 104);
70 CHECK_OFFSET(InputMessage::Body::Motion, dsdy, 108);
71 CHECK_OFFSET(InputMessage::Body::Motion, tx, 112);
72 CHECK_OFFSET(InputMessage::Body::Motion, ty, 116);
73 CHECK_OFFSET(InputMessage::Body::Motion, xPrecision, 120);
74 CHECK_OFFSET(InputMessage::Body::Motion, yPrecision, 124);
75 CHECK_OFFSET(InputMessage::Body::Motion, xCursorPosition, 128);
76 CHECK_OFFSET(InputMessage::Body::Motion, yCursorPosition, 132);
77 CHECK_OFFSET(InputMessage::Body::Motion, pointerCount, 136);
Siarhei Vishniakou38b7f7f2021-03-05 01:57:08 +000078 CHECK_OFFSET(InputMessage::Body::Motion, empty3, 140);
chaviw9eaa22c2020-07-01 16:21:27 -070079 CHECK_OFFSET(InputMessage::Body::Motion, pointers, 144);
Siarhei Vishniakou1f7c0e42018-11-16 22:18:53 -080080
Siarhei Vishniakoua64c1592020-06-22 12:02:29 -050081 CHECK_OFFSET(InputMessage::Body::Focus, eventId, 0);
82 CHECK_OFFSET(InputMessage::Body::Focus, hasFocus, 4);
Siarhei Vishniakou38b7f7f2021-03-05 01:57:08 +000083 CHECK_OFFSET(InputMessage::Body::Focus, inTouchMode, 5);
84 CHECK_OFFSET(InputMessage::Body::Focus, empty, 6);
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -080085
Prabir Pradhan3f37b7b2020-11-10 16:50:18 -080086 CHECK_OFFSET(InputMessage::Body::Capture, eventId, 0);
87 CHECK_OFFSET(InputMessage::Body::Capture, pointerCaptureEnabled, 4);
Siarhei Vishniakou38b7f7f2021-03-05 01:57:08 +000088 CHECK_OFFSET(InputMessage::Body::Capture, empty, 5);
Prabir Pradhan3f37b7b2020-11-10 16:50:18 -080089
arthurhung7632c332020-12-30 16:58:01 +080090 CHECK_OFFSET(InputMessage::Body::Drag, eventId, 0);
91 CHECK_OFFSET(InputMessage::Body::Drag, x, 4);
92 CHECK_OFFSET(InputMessage::Body::Drag, y, 8);
93 CHECK_OFFSET(InputMessage::Body::Drag, isExiting, 12);
94 CHECK_OFFSET(InputMessage::Body::Drag, empty, 13);
95
Siarhei Vishniakou38b7f7f2021-03-05 01:57:08 +000096 CHECK_OFFSET(InputMessage::Body::Finished, handled, 0);
97 CHECK_OFFSET(InputMessage::Body::Finished, empty, 1);
Siarhei Vishniakou3531ae72021-02-02 12:12:27 -100098 CHECK_OFFSET(InputMessage::Body::Finished, consumeTime, 8);
Siarhei Vishniakouf94ae022021-02-04 01:23:17 +000099
100 CHECK_OFFSET(InputMessage::Body::Timeline, eventId, 0);
101 CHECK_OFFSET(InputMessage::Body::Timeline, empty, 4);
102 CHECK_OFFSET(InputMessage::Body::Timeline, graphicsTimeline, 8);
Fengwei Yin83e0e422014-05-24 05:32:09 +0800103}
104
Siarhei Vishniakou10fe6762019-11-25 11:44:11 -0800105void TestHeaderSize() {
Siarhei Vishniakoua64c1592020-06-22 12:02:29 -0500106 CHECK_OFFSET(InputMessage::Header, type, 0);
107 CHECK_OFFSET(InputMessage::Header, seq, 4);
Siarhei Vishniakou10fe6762019-11-25 11:44:11 -0800108 static_assert(sizeof(InputMessage::Header) == 8);
109}
110
111/**
112 * We cannot use the Body::size() method here because it is not static for
113 * the Motion type, where "pointerCount" variable affects the size and can change at runtime.
114 */
115void TestBodySize() {
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600116 static_assert(sizeof(InputMessage::Body::Key) == 96);
Siarhei Vishniakou10fe6762019-11-25 11:44:11 -0800117 static_assert(sizeof(InputMessage::Body::Motion) ==
118 offsetof(InputMessage::Body::Motion, pointers) +
119 sizeof(InputMessage::Body::Motion::Pointer) * MAX_POINTERS);
Siarhei Vishniakou3531ae72021-02-02 12:12:27 -1000120 static_assert(sizeof(InputMessage::Body::Finished) == 16);
Siarhei Vishniakoua64c1592020-06-22 12:02:29 -0500121 static_assert(sizeof(InputMessage::Body::Focus) == 8);
Prabir Pradhan3f37b7b2020-11-10 16:50:18 -0800122 static_assert(sizeof(InputMessage::Body::Capture) == 8);
arthurhung7632c332020-12-30 16:58:01 +0800123 static_assert(sizeof(InputMessage::Body::Drag) == 16);
Siarhei Vishniakouf94ae022021-02-04 01:23:17 +0000124 // Timeline
125 static_assert(GraphicsTimeline::SIZE == 2);
126 static_assert(sizeof(InputMessage::Body::Timeline) == 24);
Siarhei Vishniakou10fe6762019-11-25 11:44:11 -0800127}
128
Siarhei Vishniakou54d3e182020-01-15 17:38:38 -0800129// --- VerifiedInputEvent ---
130// Ensure that VerifiedInputEvent, VerifiedKeyEvent, VerifiedMotionEvent are packed.
131// We will treat them as byte collections when signing them. There should not be any uninitialized
132// data in-between fields. Otherwise, the padded data will affect the hmac value and verifications
133// will fail.
134
135void TestVerifiedEventSize() {
136 // VerifiedInputEvent
137 constexpr size_t VERIFIED_INPUT_EVENT_SIZE = sizeof(VerifiedInputEvent::type) +
138 sizeof(VerifiedInputEvent::deviceId) + sizeof(VerifiedInputEvent::eventTimeNanos) +
139 sizeof(VerifiedInputEvent::source) + sizeof(VerifiedInputEvent::displayId);
140 static_assert(sizeof(VerifiedInputEvent) == VERIFIED_INPUT_EVENT_SIZE);
141
142 // VerifiedKeyEvent
143 constexpr size_t VERIFIED_KEY_EVENT_SIZE = VERIFIED_INPUT_EVENT_SIZE +
144 sizeof(VerifiedKeyEvent::action) + sizeof(VerifiedKeyEvent::downTimeNanos) +
145 sizeof(VerifiedKeyEvent::flags) + sizeof(VerifiedKeyEvent::keyCode) +
146 sizeof(VerifiedKeyEvent::scanCode) + sizeof(VerifiedKeyEvent::metaState) +
147 sizeof(VerifiedKeyEvent::repeatCount);
148 static_assert(sizeof(VerifiedKeyEvent) == VERIFIED_KEY_EVENT_SIZE);
149
150 // VerifiedMotionEvent
151 constexpr size_t VERIFIED_MOTION_EVENT_SIZE = VERIFIED_INPUT_EVENT_SIZE +
152 sizeof(VerifiedMotionEvent::rawX) + sizeof(VerifiedMotionEvent::rawY) +
153 sizeof(VerifiedMotionEvent::actionMasked) + sizeof(VerifiedMotionEvent::downTimeNanos) +
154 sizeof(VerifiedMotionEvent::flags) + sizeof(VerifiedMotionEvent::metaState) +
155 sizeof(VerifiedMotionEvent::buttonState);
156 static_assert(sizeof(VerifiedMotionEvent) == VERIFIED_MOTION_EVENT_SIZE);
157}
158
Fengwei Yin83e0e422014-05-24 05:32:09 +0800159} // namespace android