1//===-- sanitizer_ioctl_test.cc -------------------------------------------===//
2//
3//                     The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// Tests for ioctl interceptor implementation in sanitizer_common.
11//
12//===----------------------------------------------------------------------===//
13
14#include "sanitizer_common/sanitizer_platform.h"
15#if SANITIZER_LINUX
16
17#include <linux/input.h>
18#include <vector>
19
20#include "interception/interception.h"
21#include "sanitizer_test_utils.h"
22#include "sanitizer_common/sanitizer_platform_limits_posix.h"
23#include "sanitizer_common/sanitizer_common.h"
24#include "gtest/gtest.h"
25
26
27using namespace __sanitizer;
28
29#define COMMON_INTERCEPTOR_READ_RANGE(ctx, ptr, sz) \
30  do {                                              \
31    (void) ctx;                                     \
32    (void) ptr;                                     \
33    (void) sz;                                      \
34  } while (0)
35#define COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ptr, sz) \
36  do {                                               \
37    (void) ctx;                                      \
38    (void) ptr;                                      \
39    (void) sz;                                       \
40  } while (0)
41
42#include "sanitizer_common/sanitizer_common_interceptors_ioctl.inc"
43
44static struct IoctlInit {
45  IoctlInit() {
46    ioctl_init();
47    // Avoid unused function warnings.
48    (void)&ioctl_common_pre;
49    (void)&ioctl_common_post;
50  }
51} ioctl_static_initializer;
52
53TEST(SanitizerIoctl, Fixup) {
54  EXPECT_EQ((unsigned)FIONBIO, ioctl_request_fixup(FIONBIO));
55
56  EXPECT_EQ(EVIOCGBIT(0, 0), ioctl_request_fixup(EVIOCGBIT(0, 16)));
57  EXPECT_EQ(EVIOCGBIT(0, 0), ioctl_request_fixup(EVIOCGBIT(1, 16)));
58  EXPECT_EQ(EVIOCGBIT(0, 0), ioctl_request_fixup(EVIOCGBIT(1, 17)));
59  EXPECT_EQ(EVIOCGBIT(0, 0), ioctl_request_fixup(EVIOCGBIT(31, 16)));
60  EXPECT_NE(EVIOCGBIT(0, 0), ioctl_request_fixup(EVIOCGBIT(32, 16)));
61
62  EXPECT_EQ(EVIOCGABS(0), ioctl_request_fixup(EVIOCGABS(0)));
63  EXPECT_EQ(EVIOCGABS(0), ioctl_request_fixup(EVIOCGABS(5)));
64  EXPECT_EQ(EVIOCGABS(0), ioctl_request_fixup(EVIOCGABS(63)));
65  EXPECT_NE(EVIOCGABS(0), ioctl_request_fixup(EVIOCGABS(64)));
66
67  EXPECT_EQ(EVIOCSABS(0), ioctl_request_fixup(EVIOCSABS(0)));
68  EXPECT_EQ(EVIOCSABS(0), ioctl_request_fixup(EVIOCSABS(5)));
69  EXPECT_EQ(EVIOCSABS(0), ioctl_request_fixup(EVIOCSABS(63)));
70  EXPECT_NE(EVIOCSABS(0), ioctl_request_fixup(EVIOCSABS(64)));
71
72  const ioctl_desc *desc = ioctl_lookup(EVIOCGKEY(16));
73  EXPECT_NE((void *)0, desc);
74  EXPECT_EQ(EVIOCGKEY(0), desc->req);
75}
76
77#endif // SANITIZER_LINUX
78