• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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, seq, 0);
38   CHECK_OFFSET(InputMessage::Body::Key, eventId, 4);
39   CHECK_OFFSET(InputMessage::Body::Key, eventTime, 8);
40   CHECK_OFFSET(InputMessage::Body::Key, deviceId, 16);
41   CHECK_OFFSET(InputMessage::Body::Key, source, 20);
42   CHECK_OFFSET(InputMessage::Body::Key, displayId, 24);
43   CHECK_OFFSET(InputMessage::Body::Key, hmac, 28);
44   CHECK_OFFSET(InputMessage::Body::Key, action, 60);
45   CHECK_OFFSET(InputMessage::Body::Key, flags, 64);
46   CHECK_OFFSET(InputMessage::Body::Key, keyCode, 68);
47   CHECK_OFFSET(InputMessage::Body::Key, scanCode, 72);
48   CHECK_OFFSET(InputMessage::Body::Key, metaState, 76);
49   CHECK_OFFSET(InputMessage::Body::Key, repeatCount, 80);
50   CHECK_OFFSET(InputMessage::Body::Key, downTime, 88);
51 
52   CHECK_OFFSET(InputMessage::Body::Motion, seq, 0);
53   CHECK_OFFSET(InputMessage::Body::Motion, eventId, 4);
54   CHECK_OFFSET(InputMessage::Body::Motion, eventTime, 8);
55   CHECK_OFFSET(InputMessage::Body::Motion, deviceId, 16);
56   CHECK_OFFSET(InputMessage::Body::Motion, source, 20);
57   CHECK_OFFSET(InputMessage::Body::Motion, displayId, 24);
58   CHECK_OFFSET(InputMessage::Body::Motion, hmac, 28);
59   CHECK_OFFSET(InputMessage::Body::Motion, action, 60);
60   CHECK_OFFSET(InputMessage::Body::Motion, actionButton, 64);
61   CHECK_OFFSET(InputMessage::Body::Motion, flags, 68);
62   CHECK_OFFSET(InputMessage::Body::Motion, metaState, 72);
63   CHECK_OFFSET(InputMessage::Body::Motion, buttonState, 76);
64   CHECK_OFFSET(InputMessage::Body::Motion, classification, 80);
65   CHECK_OFFSET(InputMessage::Body::Motion, edgeFlags, 84);
66   CHECK_OFFSET(InputMessage::Body::Motion, downTime, 88);
67   CHECK_OFFSET(InputMessage::Body::Motion, xScale, 96);
68   CHECK_OFFSET(InputMessage::Body::Motion, yScale, 100);
69   CHECK_OFFSET(InputMessage::Body::Motion, xOffset, 104);
70   CHECK_OFFSET(InputMessage::Body::Motion, yOffset, 108);
71   CHECK_OFFSET(InputMessage::Body::Motion, xPrecision, 112);
72   CHECK_OFFSET(InputMessage::Body::Motion, yPrecision, 116);
73   CHECK_OFFSET(InputMessage::Body::Motion, xCursorPosition, 120);
74   CHECK_OFFSET(InputMessage::Body::Motion, yCursorPosition, 124);
75   CHECK_OFFSET(InputMessage::Body::Motion, pointerCount, 128);
76   CHECK_OFFSET(InputMessage::Body::Motion, pointers, 136);
77 
78   CHECK_OFFSET(InputMessage::Body::Focus, seq, 0);
79   CHECK_OFFSET(InputMessage::Body::Focus, eventId, 4);
80   CHECK_OFFSET(InputMessage::Body::Focus, hasFocus, 12);
81   CHECK_OFFSET(InputMessage::Body::Focus, inTouchMode, 14);
82 
83   CHECK_OFFSET(InputMessage::Body::Finished, seq, 0);
84   CHECK_OFFSET(InputMessage::Body::Finished, handled, 4);
85 }
86 
TestHeaderSize()87 void TestHeaderSize() {
88     static_assert(sizeof(InputMessage::Header) == 8);
89 }
90 
91 /**
92  * We cannot use the Body::size() method here because it is not static for
93  * the Motion type, where "pointerCount" variable affects the size and can change at runtime.
94  */
TestBodySize()95 void TestBodySize() {
96     static_assert(sizeof(InputMessage::Body::Key) == 96);
97     static_assert(sizeof(InputMessage::Body::Motion) ==
98                   offsetof(InputMessage::Body::Motion, pointers) +
99                           sizeof(InputMessage::Body::Motion::Pointer) * MAX_POINTERS);
100     static_assert(sizeof(InputMessage::Body::Finished) == 8);
101     static_assert(sizeof(InputMessage::Body::Focus) == 16);
102 }
103 
104 // --- VerifiedInputEvent ---
105 // Ensure that VerifiedInputEvent, VerifiedKeyEvent, VerifiedMotionEvent are packed.
106 // We will treat them as byte collections when signing them. There should not be any uninitialized
107 // data in-between fields. Otherwise, the padded data will affect the hmac value and verifications
108 // will fail.
109 
TestVerifiedEventSize()110 void TestVerifiedEventSize() {
111     // VerifiedInputEvent
112     constexpr size_t VERIFIED_INPUT_EVENT_SIZE = sizeof(VerifiedInputEvent::type) +
113             sizeof(VerifiedInputEvent::deviceId) + sizeof(VerifiedInputEvent::eventTimeNanos) +
114             sizeof(VerifiedInputEvent::source) + sizeof(VerifiedInputEvent::displayId);
115     static_assert(sizeof(VerifiedInputEvent) == VERIFIED_INPUT_EVENT_SIZE);
116 
117     // VerifiedKeyEvent
118     constexpr size_t VERIFIED_KEY_EVENT_SIZE = VERIFIED_INPUT_EVENT_SIZE +
119             sizeof(VerifiedKeyEvent::action) + sizeof(VerifiedKeyEvent::downTimeNanos) +
120             sizeof(VerifiedKeyEvent::flags) + sizeof(VerifiedKeyEvent::keyCode) +
121             sizeof(VerifiedKeyEvent::scanCode) + sizeof(VerifiedKeyEvent::metaState) +
122             sizeof(VerifiedKeyEvent::repeatCount);
123     static_assert(sizeof(VerifiedKeyEvent) == VERIFIED_KEY_EVENT_SIZE);
124 
125     // VerifiedMotionEvent
126     constexpr size_t VERIFIED_MOTION_EVENT_SIZE = VERIFIED_INPUT_EVENT_SIZE +
127             sizeof(VerifiedMotionEvent::rawX) + sizeof(VerifiedMotionEvent::rawY) +
128             sizeof(VerifiedMotionEvent::actionMasked) + sizeof(VerifiedMotionEvent::downTimeNanos) +
129             sizeof(VerifiedMotionEvent::flags) + sizeof(VerifiedMotionEvent::metaState) +
130             sizeof(VerifiedMotionEvent::buttonState);
131     static_assert(sizeof(VerifiedMotionEvent) == VERIFIED_MOTION_EVENT_SIZE);
132 }
133 
134 } // namespace android
135