• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_MAC
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 
TEST(SanitizerMac,ParseVersion)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
TEST(SanitizerMac,GetMacosAlignedVersion)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
TEST(SanitizerMac,GetMacosAlignedVersion)57 TEST(SanitizerMac, GetMacosAlignedVersion) {
58   MacosVersion vers = GetMacosAlignedVersion();
59   u16 kernel_major = GetDarwinKernelVersion().major;
60   bool macos_11 = (kernel_major >= 20);
61   u16 expected_major = macos_11 ? (kernel_major - 9) : 10;
62   u16 expected_minor = macos_11 ? 0 : (kernel_major - 4);
63   EXPECT_EQ(vers.major, expected_major);
64   EXPECT_EQ(vers.minor, expected_minor);
65 }
66 #endif
67 
TEST(SanitizerMac,GetDarwinKernelVersion)68 TEST(SanitizerMac, GetDarwinKernelVersion) {
69   DarwinKernelVersion vers = GetDarwinKernelVersion();
70   std::ostringstream oss;
71   oss << vers.major << '.' << vers.minor;
72   std::string actual = oss.str();
73 
74   char buf[100];
75   size_t len = sizeof(buf);
76   int res = sysctlbyname("kern.osrelease", buf, &len, nullptr, 0);
77   ASSERT_EQ(res, KERN_SUCCESS);
78   std::string expected(buf);
79 
80   // Prefix match
81   ASSERT_EQ(expected.compare(0, actual.size(), actual), 0);
82 }
83 
84 }  // namespace __sanitizer
85 
86 #endif  // SANITIZER_MAC
87