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 TEST(SanitizerMac, GetMacosAlignedVersion) { 26 MacosVersion vers = GetMacosAlignedVersion(); 27 u16 kernel_major = GetDarwinKernelVersion().major; 28 bool macos_11 = (kernel_major >= 20); 29 u16 expected_major = macos_11 ? (kernel_major - 9) : 10; 30 u16 expected_minor = macos_11 ? 0 : (kernel_major - 4); 31 EXPECT_EQ(vers.major, expected_major); 32 EXPECT_EQ(vers.minor, expected_minor); 33 } 34 35 void ParseVersion(const char *vers, u16 *major, u16 *minor); 36 37 TEST(SanitizerMac, ParseVersion) { 38 u16 major, minor; 39 ParseVersion("11.22.33", &major, &minor); 40 EXPECT_EQ(major, 11); 41 EXPECT_EQ(minor, 22); 42 } 43 44 TEST(SanitizerMac, GetDarwinKernelVersion) { 45 DarwinKernelVersion vers = GetDarwinKernelVersion(); 46 std::ostringstream oss; 47 oss << vers.major << '.' << vers.minor; 48 std::string actual = oss.str(); 49 50 char buf[100]; 51 size_t len = sizeof(buf); 52 int res = sysctlbyname("kern.osrelease", buf, &len, nullptr, 0); 53 ASSERT_EQ(res, KERN_SUCCESS); 54 std::string expected(buf); 55 56 // Prefix match 57 ASSERT_EQ(expected.compare(0, actual.size(), actual), 0); 58 } 59 60 } // namespace __sanitizer 61 62 #endif // SANITIZER_MAC 63