1 | //===-- sanitizer_mac_test.cpp --------------------------------------------===// |
2 | // |
3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
4 | // See https://llvm.org/LICENSE.txt for license information. |
5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
6 | // |
7 | //===----------------------------------------------------------------------===// |
8 | // |
9 | // Tests for sanitizer_mac.{h,cpp} |
10 | // |
11 | //===----------------------------------------------------------------------===// |
12 | |
13 | #include "sanitizer_common/sanitizer_platform.h" |
14 | #if SANITIZER_APPLE |
15 | |
16 | #include "sanitizer_common/sanitizer_mac.h" |
17 | |
18 | #include "gtest/gtest.h" |
19 | |
20 | #include <sys/sysctl.h> // sysctlbyname |
21 | #include <mach/kern_return.h> // KERN_SUCCESS |
22 | |
23 | namespace __sanitizer { |
24 | |
25 | void ParseVersion(const char *vers, u16 *major, u16 *minor); |
26 | |
27 | TEST(SanitizerMac, ParseVersion) { |
28 | u16 major, minor; |
29 | |
30 | ParseVersion("11.22.33" , &major, &minor); |
31 | EXPECT_EQ(major, 11); |
32 | EXPECT_EQ(minor, 22); |
33 | |
34 | ParseVersion("1.2" , &major, &minor); |
35 | EXPECT_EQ(major, 1); |
36 | EXPECT_EQ(minor, 2); |
37 | } |
38 | |
39 | // TODO(yln): Run sanitizer unit tests for the simulators (rdar://65680742) |
40 | #if SANITIZER_IOSSIM |
41 | TEST(SanitizerMac, GetMacosAlignedVersion) { |
42 | const char *vers_str; |
43 | if (SANITIZER_IOS || SANITIZER_TVOS) { |
44 | vers_str = "13.0" ; |
45 | } else if (SANITIZER_WATCHOS) { |
46 | vers_str = "6.5" ; |
47 | } else { |
48 | FAIL() << "unsupported simulator runtime" ; |
49 | } |
50 | setenv("SIMULATOR_RUNTIME_VERSION" , vers_str, /*overwrite=*/1); |
51 | |
52 | MacosVersion vers = GetMacosAlignedVersion(); |
53 | EXPECT_EQ(vers.major, 10); |
54 | EXPECT_EQ(vers.minor, 15); |
55 | } |
56 | #else |
57 | TEST(SanitizerMac, GetMacosAlignedVersion) { |
58 | MacosVersion vers = GetMacosAlignedVersion(); |
59 | std::ostringstream oss; |
60 | oss << vers.major << '.' << vers.minor; |
61 | std::string actual = oss.str(); |
62 | |
63 | char buf[100]; |
64 | size_t len = sizeof(buf); |
65 | int res = sysctlbyname("kern.osproductversion" , buf, &len, nullptr, 0); |
66 | ASSERT_EQ(res, KERN_SUCCESS); |
67 | std::string expected(buf); |
68 | |
69 | // Prefix match |
70 | ASSERT_EQ(expected.compare(0, actual.size(), actual), 0); |
71 | } |
72 | #endif |
73 | |
74 | TEST(SanitizerMac, GetDarwinKernelVersion) { |
75 | DarwinKernelVersion vers = GetDarwinKernelVersion(); |
76 | std::ostringstream oss; |
77 | oss << vers.major << '.' << vers.minor; |
78 | std::string actual = oss.str(); |
79 | |
80 | char buf[100]; |
81 | size_t len = sizeof(buf); |
82 | int res = sysctlbyname("kern.osrelease" , buf, &len, nullptr, 0); |
83 | ASSERT_EQ(res, KERN_SUCCESS); |
84 | std::string expected(buf); |
85 | |
86 | // Prefix match |
87 | ASSERT_EQ(expected.compare(0, actual.size(), actual), 0); |
88 | } |
89 | |
90 | } // namespace __sanitizer |
91 | |
92 | #endif // SANITIZER_APPLE |
93 | |