1 /*
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
20 namespace android {
21
22 #define CHECK_OFFSET(type, member, expected_offset) \
23 static_assert((offsetof(type, member) == (expected_offset)), "")
24
25 struct Foo {
26 uint32_t dummy;
27 PointerCoords coords;
28 };
29
TestPointerCoordsAlignment()30 void TestPointerCoordsAlignment() {
31 CHECK_OFFSET(Foo, coords, 8);
32 }
33
TestInputMessageAlignment()34 void TestInputMessageAlignment() {
35 CHECK_OFFSET(InputMessage, body, 8);
36
37 CHECK_OFFSET(InputMessage::Body::Key, eventId, 0);
38 CHECK_OFFSET(InputMessage::Body::Key, eventTime, 8);
39 CHECK_OFFSET(InputMessage::Body::Key, deviceId, 16);
40 CHECK_OFFSET(InputMessage::Body::Key, source, 20);
41 CHECK_OFFSET(InputMessage::Body::Key, displayId, 24);
42 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);
50
51 CHECK_OFFSET(InputMessage::Body::Motion, eventId, 0);
52 CHECK_OFFSET(InputMessage::Body::Motion, pointerCount, 4);
53 CHECK_OFFSET(InputMessage::Body::Motion, eventTime, 8);
54 CHECK_OFFSET(InputMessage::Body::Motion, deviceId, 16);
55 CHECK_OFFSET(InputMessage::Body::Motion, source, 20);
56 CHECK_OFFSET(InputMessage::Body::Motion, displayId, 24);
57 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);
64 CHECK_OFFSET(InputMessage::Body::Motion, empty2, 81);
65 CHECK_OFFSET(InputMessage::Body::Motion, edgeFlags, 84);
66 CHECK_OFFSET(InputMessage::Body::Motion, downTime, 88);
67 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, dsdxRaw, 136);
78 CHECK_OFFSET(InputMessage::Body::Motion, dtdxRaw, 140);
79 CHECK_OFFSET(InputMessage::Body::Motion, dtdyRaw, 144);
80 CHECK_OFFSET(InputMessage::Body::Motion, dsdyRaw, 148);
81 CHECK_OFFSET(InputMessage::Body::Motion, txRaw, 152);
82 CHECK_OFFSET(InputMessage::Body::Motion, tyRaw, 156);
83 CHECK_OFFSET(InputMessage::Body::Motion, pointers, 160);
84
85 CHECK_OFFSET(InputMessage::Body::Focus, eventId, 0);
86 CHECK_OFFSET(InputMessage::Body::Focus, hasFocus, 4);
87 CHECK_OFFSET(InputMessage::Body::Focus, empty, 5);
88
89 CHECK_OFFSET(InputMessage::Body::Capture, eventId, 0);
90 CHECK_OFFSET(InputMessage::Body::Capture, pointerCaptureEnabled, 4);
91 CHECK_OFFSET(InputMessage::Body::Capture, empty, 5);
92
93 CHECK_OFFSET(InputMessage::Body::Drag, eventId, 0);
94 CHECK_OFFSET(InputMessage::Body::Drag, x, 4);
95 CHECK_OFFSET(InputMessage::Body::Drag, y, 8);
96 CHECK_OFFSET(InputMessage::Body::Drag, isExiting, 12);
97 CHECK_OFFSET(InputMessage::Body::Drag, empty, 13);
98
99 CHECK_OFFSET(InputMessage::Body::Finished, handled, 0);
100 CHECK_OFFSET(InputMessage::Body::Finished, empty, 1);
101 CHECK_OFFSET(InputMessage::Body::Finished, consumeTime, 8);
102
103 CHECK_OFFSET(InputMessage::Body::Timeline, eventId, 0);
104 CHECK_OFFSET(InputMessage::Body::Timeline, empty, 4);
105 CHECK_OFFSET(InputMessage::Body::Timeline, graphicsTimeline, 8);
106
107 CHECK_OFFSET(InputMessage::Body::TouchMode, eventId, 0);
108 CHECK_OFFSET(InputMessage::Body::TouchMode, isInTouchMode, 4);
109 CHECK_OFFSET(InputMessage::Body::TouchMode, empty, 5);
110 }
111
TestHeaderSize()112 void TestHeaderSize() {
113 CHECK_OFFSET(InputMessage::Header, type, 0);
114 CHECK_OFFSET(InputMessage::Header, seq, 4);
115 static_assert(sizeof(InputMessage::Header) == 8);
116 }
117
TestBodySize()118 void TestBodySize() {
119 static_assert(sizeof(InputMessage::Body::Key) == 96);
120 static_assert(sizeof(InputMessage::Body::Motion::Pointer) == 144);
121 static_assert(sizeof(InputMessage::Body::Motion) ==
122 offsetof(InputMessage::Body::Motion, pointers) +
123 sizeof(InputMessage::Body::Motion::Pointer) * MAX_POINTERS);
124 static_assert(sizeof(InputMessage::Body::Finished) == 16);
125 static_assert(sizeof(InputMessage::Body::Focus) == 8);
126 static_assert(sizeof(InputMessage::Body::Capture) == 8);
127 static_assert(sizeof(InputMessage::Body::Drag) == 16);
128 static_assert(sizeof(InputMessage::Body::TouchMode) == 8);
129 // Timeline
130 static_assert(GraphicsTimeline::SIZE == 2);
131 static_assert(sizeof(InputMessage::Body::Timeline) == 24);
132
133 /**
134 * We cannot use the Body::size() method here because it is not static for
135 * the Motion type, where "pointerCount" variable affects the size and can change at runtime.
136 */
137 static_assert(sizeof(InputMessage::Body) ==
138 offsetof(InputMessage::Body::Motion, pointers) +
139 sizeof(InputMessage::Body::Motion::Pointer) * MAX_POINTERS);
140 static_assert(sizeof(InputMessage::Body) == 160 + 144 * 16);
141 static_assert(sizeof(InputMessage::Body) == 2464);
142 }
143
144 /**
145 * In general, we are sending a variable-length message across the socket, because the number of
146 * pointers varies. When we receive the message, we still need to allocate enough memory for the
147 * entire InputMessage struct. This size is, therefore, the worst case scenario. However, it is
148 * still helpful to compute to get an idea of the sizes that are involved.
149 */
TestWorstCaseInputMessageSize()150 void TestWorstCaseInputMessageSize() {
151 static_assert(sizeof(InputMessage) == /*header*/ 8 + /*body*/ 2464);
152 static_assert(sizeof(InputMessage) == 2472);
153 }
154
155 /**
156 * Assuming a single pointer, how big is the message that we are sending across the socket?
157 */
CalculateSinglePointerInputMessageSize()158 void CalculateSinglePointerInputMessageSize() {
159 constexpr size_t pointerCount = 1;
160 constexpr size_t bodySize = offsetof(InputMessage::Body::Motion, pointers) +
161 sizeof(InputMessage::Body::Motion::Pointer) * pointerCount;
162 static_assert(bodySize == 160 + 144);
163 static_assert(bodySize == 304); // For the total message size, add the small header
164 }
165
166 // --- VerifiedInputEvent ---
167 // Ensure that VerifiedInputEvent, VerifiedKeyEvent, VerifiedMotionEvent are packed.
168 // We will treat them as byte collections when signing them. There should not be any uninitialized
169 // data in-between fields. Otherwise, the padded data will affect the hmac value and verifications
170 // will fail.
171
TestVerifiedEventSize()172 void TestVerifiedEventSize() {
173 // VerifiedInputEvent
174 constexpr size_t VERIFIED_INPUT_EVENT_SIZE = sizeof(VerifiedInputEvent::type) +
175 sizeof(VerifiedInputEvent::deviceId) + sizeof(VerifiedInputEvent::eventTimeNanos) +
176 sizeof(VerifiedInputEvent::source) + sizeof(VerifiedInputEvent::displayId);
177 static_assert(sizeof(VerifiedInputEvent) == VERIFIED_INPUT_EVENT_SIZE);
178
179 // VerifiedKeyEvent
180 constexpr size_t VERIFIED_KEY_EVENT_SIZE = VERIFIED_INPUT_EVENT_SIZE +
181 sizeof(VerifiedKeyEvent::action) + sizeof(VerifiedKeyEvent::downTimeNanos) +
182 sizeof(VerifiedKeyEvent::flags) + sizeof(VerifiedKeyEvent::keyCode) +
183 sizeof(VerifiedKeyEvent::scanCode) + sizeof(VerifiedKeyEvent::metaState) +
184 sizeof(VerifiedKeyEvent::repeatCount);
185 static_assert(sizeof(VerifiedKeyEvent) == VERIFIED_KEY_EVENT_SIZE);
186
187 // VerifiedMotionEvent
188 constexpr size_t VERIFIED_MOTION_EVENT_SIZE = VERIFIED_INPUT_EVENT_SIZE +
189 sizeof(VerifiedMotionEvent::rawX) + sizeof(VerifiedMotionEvent::rawY) +
190 sizeof(VerifiedMotionEvent::actionMasked) + sizeof(VerifiedMotionEvent::downTimeNanos) +
191 sizeof(VerifiedMotionEvent::flags) + sizeof(VerifiedMotionEvent::metaState) +
192 sizeof(VerifiedMotionEvent::buttonState);
193 static_assert(sizeof(VerifiedMotionEvent) == VERIFIED_MOTION_EVENT_SIZE);
194 }
195
196 } // namespace android
197