1 //===-- TextStubV1Tests.cpp - TBD V1 File Test ----------------------------===//
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
7 //===-----------------------------------------------------------------------===/
9 #include "llvm/TextAPI/MachO/InterfaceFile.h"
10 #include "llvm/TextAPI/MachO/TextAPIReader.h"
11 #include "llvm/TextAPI/MachO/TextAPIWriter.h"
12 #include "gtest/gtest.h"
17 using namespace llvm::MachO
;
19 struct ExportedSymbol
{
23 bool ThreadLocalValue
;
25 using ExportedSymbolSeq
= std::vector
<ExportedSymbol
>;
27 inline bool operator<(const ExportedSymbol
&lhs
, const ExportedSymbol
&rhs
) {
28 return std::tie(lhs
.Kind
, lhs
.Name
) < std::tie(rhs
.Kind
, rhs
.Name
);
31 inline bool operator==(const ExportedSymbol
&lhs
, const ExportedSymbol
&rhs
) {
32 return std::tie(lhs
.Kind
, lhs
.Name
, lhs
.WeakDefined
, lhs
.ThreadLocalValue
) ==
33 std::tie(rhs
.Kind
, rhs
.Name
, rhs
.WeakDefined
, rhs
.ThreadLocalValue
);
36 static ExportedSymbol TBDv1Symbols
[] = {
37 {SymbolKind::GlobalSymbol
, "$ld$hide$os9.0$_sym1", false, false},
38 {SymbolKind::GlobalSymbol
, "_sym1", false, false},
39 {SymbolKind::GlobalSymbol
, "_sym2", false, false},
40 {SymbolKind::GlobalSymbol
, "_sym3", false, false},
41 {SymbolKind::GlobalSymbol
, "_sym4", false, false},
42 {SymbolKind::GlobalSymbol
, "_sym5", false, false},
43 {SymbolKind::GlobalSymbol
, "_tlv1", false, true},
44 {SymbolKind::GlobalSymbol
, "_tlv2", false, true},
45 {SymbolKind::GlobalSymbol
, "_tlv3", false, true},
46 {SymbolKind::GlobalSymbol
, "_weak1", true, false},
47 {SymbolKind::GlobalSymbol
, "_weak2", true, false},
48 {SymbolKind::GlobalSymbol
, "_weak3", true, false},
49 {SymbolKind::ObjectiveCClass
, "class1", false, false},
50 {SymbolKind::ObjectiveCClass
, "class2", false, false},
51 {SymbolKind::ObjectiveCClass
, "class3", false, false},
52 {SymbolKind::ObjectiveCInstanceVariable
, "class1._ivar1", false, false},
53 {SymbolKind::ObjectiveCInstanceVariable
, "class1._ivar2", false, false},
54 {SymbolKind::ObjectiveCInstanceVariable
, "class1._ivar3", false, false},
59 TEST(TBDv1
, ReadFile
) {
60 static const char tbd_v1_file1
[] =
62 "archs: [ armv7, armv7s, armv7k, arm64 ]\n"
64 "install-name: Test.dylib\n"
65 "current-version: 2.3.4\n"
66 "compatibility-version: 1.0\n"
67 "swift-version: 1.1\n"
69 " - archs: [ armv7, armv7s, armv7k, arm64 ]\n"
70 " allowed-clients: [ clientA ]\n"
71 " re-exports: [ /usr/lib/libfoo.dylib ]\n"
72 " symbols: [ _sym1, _sym2, _sym3, _sym4, $ld$hide$os9.0$_sym1 ]\n"
73 " objc-classes: [ _class1, _class2 ]\n"
74 " objc-ivars: [ _class1._ivar1, _class1._ivar2 ]\n"
75 " weak-def-symbols: [ _weak1, _weak2 ]\n"
76 " thread-local-symbols: [ _tlv1, _tlv2 ]\n"
77 " - archs: [ armv7, armv7s, armv7k ]\n"
78 " symbols: [ _sym5 ]\n"
79 " objc-classes: [ _class3 ]\n"
80 " objc-ivars: [ _class1._ivar3 ]\n"
81 " weak-def-symbols: [ _weak3 ]\n"
82 " thread-local-symbols: [ _tlv3 ]\n"
85 auto Buffer
= MemoryBuffer::getMemBuffer(tbd_v1_file1
, "Test.tbd");
86 auto Result
= TextAPIReader::get(std::move(Buffer
));
87 EXPECT_TRUE(!!Result
);
88 auto File
= std::move(Result
.get());
89 EXPECT_EQ(FileType::TBD_V1
, File
->getFileType());
90 auto Archs
= AK_armv7
| AK_armv7s
| AK_armv7k
| AK_arm64
;
91 EXPECT_EQ(Archs
, File
->getArchitectures());
92 EXPECT_EQ(PlatformKind::iOS
, File
->getPlatform());
93 EXPECT_EQ(std::string("Test.dylib"), File
->getInstallName());
94 EXPECT_EQ(PackedVersion(2, 3, 4), File
->getCurrentVersion());
95 EXPECT_EQ(PackedVersion(1, 0, 0), File
->getCompatibilityVersion());
96 EXPECT_EQ(2U, File
->getSwiftABIVersion());
97 EXPECT_EQ(ObjCConstraintType::None
, File
->getObjCConstraint());
98 EXPECT_TRUE(File
->isTwoLevelNamespace());
99 EXPECT_TRUE(File
->isApplicationExtensionSafe());
100 EXPECT_FALSE(File
->isInstallAPI());
101 InterfaceFileRef
client("clientA", Archs
);
102 InterfaceFileRef
reexport("/usr/lib/libfoo.dylib", Archs
);
103 EXPECT_EQ(1U, File
->allowableClients().size());
104 EXPECT_EQ(client
, File
->allowableClients().front());
105 EXPECT_EQ(1U, File
->reexportedLibraries().size());
106 EXPECT_EQ(reexport
, File
->reexportedLibraries().front());
108 ExportedSymbolSeq Exports
;
109 for (const auto *Sym
: File
->symbols()) {
110 EXPECT_FALSE(Sym
->isWeakReferenced());
111 EXPECT_FALSE(Sym
->isUndefined());
112 Exports
.emplace_back(ExportedSymbol
{Sym
->getKind(), Sym
->getName(),
113 Sym
->isWeakDefined(),
114 Sym
->isThreadLocalValue()});
116 llvm::sort(Exports
.begin(), Exports
.end());
118 EXPECT_EQ(sizeof(TBDv1Symbols
) / sizeof(ExportedSymbol
), Exports
.size());
120 std::equal(Exports
.begin(), Exports
.end(), std::begin(TBDv1Symbols
)));
123 TEST(TBDv1
, ReadFile2
) {
124 static const char tbd_v1_file2
[] = "--- !tapi-tbd-v1\n"
125 "archs: [ armv7, armv7s, armv7k, arm64 ]\n"
127 "install-name: Test.dylib\n"
130 auto Buffer
= MemoryBuffer::getMemBuffer(tbd_v1_file2
, "Test.tbd");
131 auto Result
= TextAPIReader::get(std::move(Buffer
));
132 EXPECT_TRUE(!!Result
);
133 auto File
= std::move(Result
.get());
134 EXPECT_EQ(FileType::TBD_V1
, File
->getFileType());
135 auto Archs
= AK_armv7
| AK_armv7s
| AK_armv7k
| AK_arm64
;
136 EXPECT_EQ(Archs
, File
->getArchitectures());
137 EXPECT_EQ(PlatformKind::iOS
, File
->getPlatform());
138 EXPECT_EQ(std::string("Test.dylib"), File
->getInstallName());
139 EXPECT_EQ(PackedVersion(1, 0, 0), File
->getCurrentVersion());
140 EXPECT_EQ(PackedVersion(1, 0, 0), File
->getCompatibilityVersion());
141 EXPECT_EQ(0U, File
->getSwiftABIVersion());
142 EXPECT_EQ(ObjCConstraintType::None
, File
->getObjCConstraint());
143 EXPECT_TRUE(File
->isTwoLevelNamespace());
144 EXPECT_TRUE(File
->isApplicationExtensionSafe());
145 EXPECT_FALSE(File
->isInstallAPI());
146 EXPECT_EQ(0U, File
->allowableClients().size());
147 EXPECT_EQ(0U, File
->reexportedLibraries().size());
150 TEST(TBDv1
, WriteFile
) {
151 static const char tbd_v1_file3
[] =
153 "archs: [ i386, x86_64 ]\n"
155 "install-name: '/usr/lib/libfoo.dylib'\n"
156 "current-version: 1.2.3\n"
157 "compatibility-version: 0\n"
159 "objc-constraint: retain_release\n"
161 " - archs: [ i386 ]\n"
162 " symbols: [ _sym1 ]\n"
163 " weak-def-symbols: [ _sym2 ]\n"
164 " thread-local-symbols: [ _sym3 ]\n"
165 " - archs: [ x86_64 ]\n"
166 " allowed-clients: [ clientA ]\n"
167 " re-exports: [ '/usr/lib/libfoo.dylib' ]\n"
168 " symbols: [ '_OBJC_EHTYPE_$_Class1' ]\n"
169 " objc-classes: [ _Class1 ]\n"
170 " objc-ivars: [ _Class1._ivar1 ]\n"
174 File
.setPath("libfoo.dylib");
175 File
.setInstallName("/usr/lib/libfoo.dylib");
176 File
.setFileType(FileType::TBD_V1
);
177 File
.setArchitectures(AK_i386
| AK_x86_64
);
178 File
.setPlatform(PlatformKind::macOS
);
179 File
.setCurrentVersion(PackedVersion(1, 2, 3));
180 File
.setSwiftABIVersion(5);
181 File
.setObjCConstraint(ObjCConstraintType::Retain_Release
);
182 File
.addAllowableClient("clientA", AK_x86_64
);
183 File
.addReexportedLibrary("/usr/lib/libfoo.dylib", AK_x86_64
);
184 File
.addSymbol(SymbolKind::GlobalSymbol
, "_sym1", AK_i386
);
185 File
.addSymbol(SymbolKind::GlobalSymbol
, "_sym2", AK_i386
,
186 SymbolFlags::WeakDefined
);
187 File
.addSymbol(SymbolKind::GlobalSymbol
, "_sym3", AK_i386
,
188 SymbolFlags::ThreadLocalValue
);
189 File
.addSymbol(SymbolKind::ObjectiveCClass
, "Class1", AK_x86_64
);
190 File
.addSymbol(SymbolKind::ObjectiveCClassEHType
, "Class1", AK_x86_64
);
191 File
.addSymbol(SymbolKind::ObjectiveCInstanceVariable
, "Class1._ivar1",
194 SmallString
<4096> Buffer
;
195 raw_svector_ostream
OS(Buffer
);
196 auto Result
= TextAPIWriter::writeToStream(OS
, File
);
197 EXPECT_FALSE(Result
);
198 EXPECT_STREQ(tbd_v1_file3
, Buffer
.c_str());
201 TEST(TBDv1
, Platform_macOS
) {
202 static const char tbd_v1_platform_macos
[] = "---\n"
203 "archs: [ x86_64 ]\n"
205 "install-name: Test.dylib\n"
208 auto Buffer
= MemoryBuffer::getMemBuffer(tbd_v1_platform_macos
, "Test.tbd");
209 auto Result
= TextAPIReader::get(std::move(Buffer
));
210 EXPECT_TRUE(!!Result
);
211 auto File
= std::move(Result
.get());
212 EXPECT_EQ(FileType::TBD_V1
, File
->getFileType());
213 EXPECT_EQ(PlatformKind::macOS
, File
->getPlatform());
216 TEST(TBDv1
, Platform_iOS
) {
217 static const char tbd_v1_platform_ios
[] = "---\n"
220 "install-name: Test.dylib\n"
223 auto Buffer
= MemoryBuffer::getMemBuffer(tbd_v1_platform_ios
, "Test.tbd");
224 auto Result
= TextAPIReader::get(std::move(Buffer
));
225 EXPECT_TRUE(!!Result
);
226 auto File
= std::move(Result
.get());
227 EXPECT_EQ(FileType::TBD_V1
, File
->getFileType());
228 EXPECT_EQ(PlatformKind::iOS
, File
->getPlatform());
231 TEST(TBDv1
, Platform_watchOS
) {
232 static const char tbd_v1_platform_watchos
[] = "---\n"
233 "archs: [ armv7k ]\n"
234 "platform: watchos\n"
235 "install-name: Test.dylib\n"
238 auto Buffer
= MemoryBuffer::getMemBuffer(tbd_v1_platform_watchos
, "Test.tbd");
239 auto Result
= TextAPIReader::get(std::move(Buffer
));
240 EXPECT_TRUE(!!Result
);
241 auto File
= std::move(Result
.get());
242 EXPECT_EQ(FileType::TBD_V1
, File
->getFileType());
243 EXPECT_EQ(PlatformKind::watchOS
, File
->getPlatform());
246 TEST(TBDv1
, Platform_tvOS
) {
247 static const char tbd_v1_platform_tvos
[] = "---\n"
250 "install-name: Test.dylib\n"
253 auto Buffer
= MemoryBuffer::getMemBuffer(tbd_v1_platform_tvos
, "Test.tbd");
254 auto Result
= TextAPIReader::get(std::move(Buffer
));
255 EXPECT_TRUE(!!Result
);
256 auto File
= std::move(Result
.get());
257 EXPECT_EQ(FileType::TBD_V1
, File
->getFileType());
258 EXPECT_EQ(PlatformKind::tvOS
, File
->getPlatform());
261 TEST(TBDv1
, Platform_bridgeOS
) {
262 static const char tbd_v1_platform_bridgeos
[] = "---\n"
263 "archs: [ armv7k ]\n"
264 "platform: bridgeos\n"
265 "install-name: Test.dylib\n"
269 MemoryBuffer::getMemBuffer(tbd_v1_platform_bridgeos
, "Test.tbd");
270 auto Result
= TextAPIReader::get(std::move(Buffer
));
271 EXPECT_TRUE(!!Result
);
272 auto File
= std::move(Result
.get());
273 EXPECT_EQ(FileType::TBD_V1
, File
->getFileType());
274 EXPECT_EQ(PlatformKind::bridgeOS
, File
->getPlatform());
277 TEST(TBDv1
, Swift_1_0
) {
278 static const char tbd_v1_swift_1_0
[] = "---\n"
281 "install-name: Test.dylib\n"
282 "swift-version: 1.0\n"
285 auto Buffer
= MemoryBuffer::getMemBuffer(tbd_v1_swift_1_0
, "Test.tbd");
286 auto Result
= TextAPIReader::get(std::move(Buffer
));
287 EXPECT_TRUE(!!Result
);
288 auto File
= std::move(Result
.get());
289 EXPECT_EQ(FileType::TBD_V1
, File
->getFileType());
290 EXPECT_EQ(1U, File
->getSwiftABIVersion());
293 TEST(TBDv1
, Swift_1_1
) {
294 static const char tbd_v1_swift_1_1
[] = "---\n"
297 "install-name: Test.dylib\n"
298 "swift-version: 1.1\n"
301 auto Buffer
= MemoryBuffer::getMemBuffer(tbd_v1_swift_1_1
, "Test.tbd");
302 auto Result
= TextAPIReader::get(std::move(Buffer
));
303 EXPECT_TRUE(!!Result
);
304 auto File
= std::move(Result
.get());
305 EXPECT_EQ(FileType::TBD_V1
, File
->getFileType());
306 EXPECT_EQ(2U, File
->getSwiftABIVersion());
309 TEST(TBDv1
, Swift_2_0
) {
310 static const char tbd_v1_swift_2_0
[] = "---\n"
313 "install-name: Test.dylib\n"
314 "swift-version: 2.0\n"
317 auto Buffer
= MemoryBuffer::getMemBuffer(tbd_v1_swift_2_0
, "Test.tbd");
318 auto Result
= TextAPIReader::get(std::move(Buffer
));
319 EXPECT_TRUE(!!Result
);
320 auto File
= std::move(Result
.get());
321 EXPECT_EQ(FileType::TBD_V1
, File
->getFileType());
322 EXPECT_EQ(3U, File
->getSwiftABIVersion());
325 TEST(TBDv1
, Swift_3_0
) {
326 static const char tbd_v1_swift_3_0
[] = "---\n"
329 "install-name: Test.dylib\n"
330 "swift-version: 3.0\n"
333 auto Buffer
= MemoryBuffer::getMemBuffer(tbd_v1_swift_3_0
, "Test.tbd");
334 auto Result
= TextAPIReader::get(std::move(Buffer
));
335 EXPECT_TRUE(!!Result
);
336 auto File
= std::move(Result
.get());
337 EXPECT_EQ(FileType::TBD_V1
, File
->getFileType());
338 EXPECT_EQ(4U, File
->getSwiftABIVersion());
341 TEST(TBDv1
, Swift_4_0
) {
342 static const char tbd_v1_swift_4_0
[] = "---\n"
345 "install-name: Test.dylib\n"
346 "swift-version: 4.0\n"
349 auto Buffer
= MemoryBuffer::getMemBuffer(tbd_v1_swift_4_0
, "Test.tbd");
350 auto Result
= TextAPIReader::get(std::move(Buffer
));
351 EXPECT_FALSE(!!Result
);
352 auto errorMessage
= toString(Result
.takeError());
353 EXPECT_EQ("malformed file\nTest.tbd:5:16: error: invalid Swift ABI "
354 "version.\nswift-version: 4.0\n ^~~\n",
358 TEST(TBDv1
, Swift_5
) {
359 static const char tbd_v1_swift_5
[] = "---\n"
362 "install-name: Test.dylib\n"
366 auto Buffer
= MemoryBuffer::getMemBuffer(tbd_v1_swift_5
, "Test.tbd");
367 auto Result
= TextAPIReader::get(std::move(Buffer
));
368 EXPECT_TRUE(!!Result
);
369 auto File
= std::move(Result
.get());
370 EXPECT_EQ(FileType::TBD_V1
, File
->getFileType());
371 EXPECT_EQ(5U, File
->getSwiftABIVersion());
374 TEST(TBDv1
, Swift_99
) {
375 static const char tbd_v1_swift_99
[] = "---\n"
378 "install-name: Test.dylib\n"
379 "swift-version: 99\n"
382 auto Buffer
= MemoryBuffer::getMemBuffer(tbd_v1_swift_99
, "Test.tbd");
383 auto Result
= TextAPIReader::get(std::move(Buffer
));
384 EXPECT_TRUE(!!Result
);
385 auto File
= std::move(Result
.get());
386 EXPECT_EQ(FileType::TBD_V1
, File
->getFileType());
387 EXPECT_EQ(99U, File
->getSwiftABIVersion());
390 TEST(TBDv1
, UnknownArchitecture
) {
391 static const char tbd_v1_file_unknown_architecture
[] =
395 "install-name: Test.dylib\n"
399 MemoryBuffer::getMemBuffer(tbd_v1_file_unknown_architecture
, "Test.tbd");
400 auto Result
= TextAPIReader::get(std::move(Buffer
));
401 EXPECT_TRUE(!!Result
);
404 TEST(TBDv1
, UnknownPlatform
) {
405 static const char tbd_v1_file_unknown_platform
[] = "---\n"
411 MemoryBuffer::getMemBuffer(tbd_v1_file_unknown_platform
, "Test.tbd");
412 auto Result
= TextAPIReader::get(std::move(Buffer
));
413 EXPECT_FALSE(!!Result
);
414 auto errorMessage
= toString(Result
.takeError());
415 EXPECT_EQ("malformed file\nTest.tbd:3:11: error: unknown platform\nplatform: "
420 TEST(TBDv1
, MalformedFile1
) {
421 static const char malformed_file1
[] = "---\n"
423 "foobar: \"Unsupported key\"\n"
426 auto Buffer
= MemoryBuffer::getMemBuffer(malformed_file1
, "Test.tbd");
427 auto Result
= TextAPIReader::get(std::move(Buffer
));
428 EXPECT_FALSE(!!Result
);
429 auto errorMessage
= toString(Result
.takeError());
430 ASSERT_EQ("malformed file\nTest.tbd:2:1: error: missing required key "
431 "'platform'\narchs: [ arm64 ]\n^\n",
435 TEST(TBDv1
, MalformedFile2
) {
436 static const char malformed_file2
[] = "---\n"
439 "install-name: Test.dylib\n"
440 "foobar: \"Unsupported key\"\n"
443 auto Buffer
= MemoryBuffer::getMemBuffer(malformed_file2
, "Test.tbd");
444 auto Result
= TextAPIReader::get(std::move(Buffer
));
445 EXPECT_FALSE(!!Result
);
446 auto errorMessage
= toString(Result
.takeError());
448 "malformed file\nTest.tbd:5:9: error: unknown key 'foobar'\nfoobar: "
449 "\"Unsupported key\"\n ^~~~~~~~~~~~~~~~~\n",
453 } // end namespace TBDv1.