1 //===-- PlatformDarwinTest.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 #include "gtest/gtest.h"
10 
11 #include "Plugins/Platform/MacOSX/PlatformDarwin.h"
12 
13 #include "llvm/ADT/StringRef.h"
14 
15 #include <tuple>
16 
17 using namespace lldb;
18 using namespace lldb_private;
19 
20 struct PlatformDarwinTester : public PlatformDarwin {
21 public:
22   using PlatformDarwin::FindComponentInPath;
23   using PlatformDarwin::FindXcodeContentsDirectoryInPath;
24   static bool SDKSupportsModules(SDKType desired_type,
25                                  const lldb_private::FileSpec &sdk_path) {
26     return PlatformDarwin::SDKSupportsModules(desired_type, sdk_path);
27   }
28 };
29 
30 TEST(PlatformDarwinTest, TestParseVersionBuildDir) {
31   llvm::VersionTuple V;
32   llvm::StringRef D;
33 
34   std::tie(V, D) = PlatformDarwin::ParseVersionBuildDir("1.2.3 (test1)");
35   EXPECT_EQ(llvm::VersionTuple(1, 2, 3), V);
36   EXPECT_EQ("test1", D);
37 
38   std::tie(V, D) = PlatformDarwin::ParseVersionBuildDir("2.3 (test2)");
39   EXPECT_EQ(llvm::VersionTuple(2, 3), V);
40   EXPECT_EQ("test2", D);
41 
42   std::tie(V, D) = PlatformDarwin::ParseVersionBuildDir("3 (test3)");
43   EXPECT_EQ(llvm::VersionTuple(3), V);
44   EXPECT_EQ("test3", D);
45 
46   std::tie(V, D) = PlatformDarwin::ParseVersionBuildDir("1.2.3 (test");
47   EXPECT_EQ(llvm::VersionTuple(1, 2, 3), V);
48   EXPECT_EQ("test", D);
49 
50   std::tie(V, D) = PlatformDarwin::ParseVersionBuildDir("2.3.4 test");
51   EXPECT_EQ(llvm::VersionTuple(2, 3, 4), V);
52   EXPECT_EQ("", D);
53 
54   std::tie(V, D) = PlatformDarwin::ParseVersionBuildDir("3.4.5");
55   EXPECT_EQ(llvm::VersionTuple(3, 4, 5), V);
56 
57   std::string base = "/Applications/Xcode.app/Contents/Developer/Platforms/";
58   EXPECT_TRUE(PlatformDarwinTester::SDKSupportsModules(
59       PlatformDarwin::SDKType::iPhoneSimulator,
60       FileSpec(
61           base +
62           "iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator12.0.sdk")));
63   EXPECT_FALSE(PlatformDarwinTester::SDKSupportsModules(
64       PlatformDarwin::SDKType::iPhoneSimulator,
65       FileSpec(
66           base +
67           "iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator7.2.sdk")));
68   EXPECT_TRUE(PlatformDarwinTester::SDKSupportsModules(
69       PlatformDarwin::SDKType::MacOSX,
70       FileSpec(base + "MacOSX.platform/Developer/SDKs/MacOSX10.10.sdk")));
71   EXPECT_FALSE(PlatformDarwinTester::SDKSupportsModules(
72       PlatformDarwin::SDKType::MacOSX,
73       FileSpec(base + "MacOSX.platform/Developer/SDKs/MacOSX10.9.sdk")));
74 }
75 
76 TEST(PlatformDarwinTest, FindXcodeContentsDirectoryInPath) {
77   std::string standard =
78       "/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/"
79       "Developer/SDKs/MacOSX.sdk";
80   EXPECT_EQ("/Applications/Xcode.app/Contents",
81             PlatformDarwinTester::FindXcodeContentsDirectoryInPath(standard));
82 
83   std::string standard_version =
84       "/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/"
85       "Developer/SDKs/MacOSX10.15.sdk";
86   EXPECT_EQ(
87       "/Applications/Xcode.app/Contents",
88       PlatformDarwinTester::FindXcodeContentsDirectoryInPath(standard_version));
89 
90   std::string beta = "/Applications/Xcode-beta.app/Contents/Developer/"
91                      "Platforms/MacOSX.platform/"
92                      "Developer/SDKs/MacOSX10.15.sdk";
93   EXPECT_EQ("/Applications/Xcode-beta.app/Contents",
94             PlatformDarwinTester::FindXcodeContentsDirectoryInPath(beta));
95 
96   std::string no_app =
97       "/Applications/Xcode/Contents/Developer/Platforms/MacOSX.platform/"
98       "Developer/SDKs/MacOSX10.15.sdk";
99   EXPECT_EQ("", PlatformDarwinTester::FindXcodeContentsDirectoryInPath(no_app));
100 
101   std::string no_contents =
102       "/Applications/Xcode.app/Developer/Platforms/MacOSX.platform/"
103       "Developer/SDKs/MacOSX10.15.sdk";
104   EXPECT_EQ(
105       "", PlatformDarwinTester::FindXcodeContentsDirectoryInPath(no_contents));
106 
107   std::string no_capitalization =
108       "/Applications/Xcode.app/contents/Developer/Platforms/MacOSX.platform/"
109       "Developer/SDKs/MacOSX10.15.sdk";
110   EXPECT_EQ("", PlatformDarwinTester::FindXcodeContentsDirectoryInPath(
111                     no_capitalization));
112 }
113 
114 TEST(PlatformDarwinTest, GetSDKNameForType) {
115   EXPECT_EQ("macosx",
116             PlatformDarwin::GetSDKNameForType(PlatformDarwin::SDKType::MacOSX));
117   EXPECT_EQ("iphonesimulator", PlatformDarwin::GetSDKNameForType(
118                                    PlatformDarwin::SDKType::iPhoneSimulator));
119   EXPECT_EQ("iphoneos", PlatformDarwin::GetSDKNameForType(
120                             PlatformDarwin::SDKType::iPhoneOS));
121   EXPECT_EQ("appletvsimulator", PlatformDarwin::GetSDKNameForType(
122                                     PlatformDarwin::SDKType::AppleTVSimulator));
123   EXPECT_EQ("appletvos", PlatformDarwin::GetSDKNameForType(
124                              PlatformDarwin::SDKType::AppleTVOS));
125   EXPECT_EQ("watchsimulator", PlatformDarwin::GetSDKNameForType(
126                                   PlatformDarwin::SDKType::WatchSimulator));
127   EXPECT_EQ("watchos", PlatformDarwin::GetSDKNameForType(
128                            PlatformDarwin::SDKType::watchOS));
129   EXPECT_EQ("linux",
130             PlatformDarwin::GetSDKNameForType(PlatformDarwin::SDKType::Linux));
131   EXPECT_EQ("", PlatformDarwin::GetSDKNameForType(
132                     PlatformDarwin::SDKType::numSDKTypes));
133   EXPECT_EQ(
134       "", PlatformDarwin::GetSDKNameForType(PlatformDarwin::SDKType::unknown));
135 }
136 
137 TEST(PlatformDarwinTest, FindComponentInPath) {
138   EXPECT_EQ("/path/to/foo",
139             PlatformDarwinTester::FindComponentInPath("/path/to/foo/", "foo"));
140 
141   EXPECT_EQ("/path/to/foo",
142             PlatformDarwinTester::FindComponentInPath("/path/to/foo", "foo"));
143 
144   EXPECT_EQ("/path/to/foobar", PlatformDarwinTester::FindComponentInPath(
145                                    "/path/to/foobar", "foo"));
146 
147   EXPECT_EQ("/path/to/foobar", PlatformDarwinTester::FindComponentInPath(
148                                    "/path/to/foobar", "bar"));
149 
150   EXPECT_EQ("",
151             PlatformDarwinTester::FindComponentInPath("/path/to/foo", "bar"));
152 }
153