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 Result
= TextAPIReader::get(MemoryBufferRef(tbd_v1_file1
, "Test.tbd"));
86 EXPECT_TRUE(!!Result
);
87 auto File
= std::move(Result
.get());
88 EXPECT_EQ(FileType::TBD_V1
, File
->getFileType());
89 auto Archs
= AK_armv7
| AK_armv7s
| AK_armv7k
| AK_arm64
;
90 auto Platform
= PlatformKind::iOS
;
92 for (auto &&arch
: Archs
)
93 Targets
.emplace_back(Target(arch
, Platform
));
94 EXPECT_EQ(Archs
, File
->getArchitectures());
95 EXPECT_EQ(File
->getPlatforms().size(), 1U);
96 EXPECT_EQ(Platform
, *File
->getPlatforms().begin());
97 EXPECT_EQ(std::string("Test.dylib"), File
->getInstallName());
98 EXPECT_EQ(PackedVersion(2, 3, 4), File
->getCurrentVersion());
99 EXPECT_EQ(PackedVersion(1, 0, 0), File
->getCompatibilityVersion());
100 EXPECT_EQ(2U, File
->getSwiftABIVersion());
101 EXPECT_EQ(ObjCConstraintType::None
, File
->getObjCConstraint());
102 EXPECT_TRUE(File
->isTwoLevelNamespace());
103 EXPECT_TRUE(File
->isApplicationExtensionSafe());
104 EXPECT_FALSE(File
->isInstallAPI());
105 InterfaceFileRef
client("clientA", Targets
);
106 InterfaceFileRef
reexport("/usr/lib/libfoo.dylib", Targets
);
107 EXPECT_EQ(1U, File
->allowableClients().size());
108 EXPECT_EQ(client
, File
->allowableClients().front());
109 EXPECT_EQ(1U, File
->reexportedLibraries().size());
110 EXPECT_EQ(reexport
, File
->reexportedLibraries().front());
112 ExportedSymbolSeq Exports
;
113 for (const auto *Sym
: File
->symbols()) {
114 EXPECT_FALSE(Sym
->isWeakReferenced());
115 EXPECT_FALSE(Sym
->isUndefined());
116 Exports
.emplace_back(ExportedSymbol
{Sym
->getKind(), Sym
->getName(),
117 Sym
->isWeakDefined(),
118 Sym
->isThreadLocalValue()});
120 llvm::sort(Exports
.begin(), Exports
.end());
122 EXPECT_EQ(sizeof(TBDv1Symbols
) / sizeof(ExportedSymbol
), Exports
.size());
124 std::equal(Exports
.begin(), Exports
.end(), std::begin(TBDv1Symbols
)));
126 File
->addSymbol(SymbolKind::ObjectiveCClassEHType
, "Class1", {Targets
[1]});
127 File
->addSymbol(SymbolKind::ObjectiveCInstanceVariable
, "Class1._ivar1",
131 TEST(TBDv1
, ReadFile2
) {
132 static const char tbd_v1_file2
[] = "--- !tapi-tbd-v1\n"
133 "archs: [ armv7, armv7s, armv7k, arm64 ]\n"
135 "install-name: Test.dylib\n"
138 auto Result
= TextAPIReader::get(MemoryBufferRef(tbd_v1_file2
, "Test.tbd"));
139 EXPECT_TRUE(!!Result
);
140 auto File
= std::move(Result
.get());
141 EXPECT_EQ(FileType::TBD_V1
, File
->getFileType());
142 auto Archs
= AK_armv7
| AK_armv7s
| AK_armv7k
| AK_arm64
;
143 auto Platform
= PlatformKind::iOS
;
145 for (auto &&arch
: Archs
)
146 Targets
.emplace_back(Target(arch
, Platform
));
147 EXPECT_EQ(Archs
, File
->getArchitectures());
148 EXPECT_EQ(File
->getPlatforms().size(), 1U);
149 EXPECT_EQ(Platform
, *File
->getPlatforms().begin());
150 EXPECT_EQ(std::string("Test.dylib"), File
->getInstallName());
151 EXPECT_EQ(PackedVersion(1, 0, 0), File
->getCurrentVersion());
152 EXPECT_EQ(PackedVersion(1, 0, 0), File
->getCompatibilityVersion());
153 EXPECT_EQ(0U, File
->getSwiftABIVersion());
154 EXPECT_EQ(ObjCConstraintType::None
, File
->getObjCConstraint());
155 EXPECT_TRUE(File
->isTwoLevelNamespace());
156 EXPECT_TRUE(File
->isApplicationExtensionSafe());
157 EXPECT_FALSE(File
->isInstallAPI());
158 EXPECT_EQ(0U, File
->allowableClients().size());
159 EXPECT_EQ(0U, File
->reexportedLibraries().size());
162 TEST(TBDv1
, WriteFile
) {
163 static const char tbd_v1_file3
[] =
165 "archs: [ i386, x86_64 ]\n"
167 "install-name: '/usr/lib/libfoo.dylib'\n"
168 "current-version: 1.2.3\n"
169 "compatibility-version: 0\n"
171 "objc-constraint: retain_release\n"
173 " - archs: [ i386 ]\n"
174 " symbols: [ _sym1 ]\n"
175 " weak-def-symbols: [ _sym2 ]\n"
176 " thread-local-symbols: [ _sym3 ]\n"
177 " - archs: [ x86_64 ]\n"
178 " allowed-clients: [ clientA ]\n"
179 " re-exports: [ '/usr/lib/libfoo.dylib' ]\n"
180 " symbols: [ '_OBJC_EHTYPE_$_Class1' ]\n"
181 " objc-classes: [ _Class1 ]\n"
182 " objc-ivars: [ _Class1._ivar1 ]\n"
187 for (auto &&arch
: AK_i386
| AK_x86_64
)
188 Targets
.emplace_back(Target(arch
, PlatformKind::macOS
));
189 File
.setPath("libfoo.dylib");
190 File
.setInstallName("/usr/lib/libfoo.dylib");
191 File
.setFileType(FileType::TBD_V1
);
192 File
.addTargets(Targets
);
193 File
.setCurrentVersion(PackedVersion(1, 2, 3));
194 File
.setSwiftABIVersion(5);
195 File
.setObjCConstraint(ObjCConstraintType::Retain_Release
);
196 File
.addAllowableClient("clientA", Targets
[1]);
197 File
.addReexportedLibrary("/usr/lib/libfoo.dylib", Targets
[1]);
198 File
.addSymbol(SymbolKind::GlobalSymbol
, "_sym1", {Targets
[0]});
199 File
.addSymbol(SymbolKind::GlobalSymbol
, "_sym2", {Targets
[0]},
200 SymbolFlags::WeakDefined
);
201 File
.addSymbol(SymbolKind::GlobalSymbol
, "_sym3", {Targets
[0]},
202 SymbolFlags::ThreadLocalValue
);
203 File
.addSymbol(SymbolKind::ObjectiveCClass
, "Class1", {Targets
[1]});
204 File
.addSymbol(SymbolKind::ObjectiveCClassEHType
, "Class1", {Targets
[1]});
205 File
.addSymbol(SymbolKind::ObjectiveCInstanceVariable
, "Class1._ivar1",
208 SmallString
<4096> Buffer
;
209 raw_svector_ostream
OS(Buffer
);
210 auto Result
= TextAPIWriter::writeToStream(OS
, File
);
211 EXPECT_FALSE(Result
);
212 EXPECT_STREQ(tbd_v1_file3
, Buffer
.c_str());
215 TEST(TBDv1
, Platform_macOS
) {
216 static const char tbd_v1_platform_macos
[] = "---\n"
217 "archs: [ x86_64 ]\n"
219 "install-name: Test.dylib\n"
223 TextAPIReader::get(MemoryBufferRef(tbd_v1_platform_macos
, "Test.tbd"));
224 EXPECT_TRUE(!!Result
);
225 auto Platform
= PlatformKind::macOS
;
226 auto File
= std::move(Result
.get());
227 EXPECT_EQ(FileType::TBD_V1
, File
->getFileType());
228 EXPECT_EQ(File
->getPlatforms().size(), 1U);
229 EXPECT_EQ(Platform
, *File
->getPlatforms().begin());
232 TEST(TBDv1
, Platform_iOS
) {
233 static const char tbd_v1_platform_ios
[] = "---\n"
236 "install-name: Test.dylib\n"
240 TextAPIReader::get(MemoryBufferRef(tbd_v1_platform_ios
, "Test.tbd"));
241 EXPECT_TRUE(!!Result
);
242 auto Platform
= PlatformKind::iOS
;
243 auto File
= std::move(Result
.get());
244 EXPECT_EQ(FileType::TBD_V1
, File
->getFileType());
245 EXPECT_EQ(File
->getPlatforms().size(), 1U);
246 EXPECT_EQ(Platform
, *File
->getPlatforms().begin());
249 TEST(TBDv1
, Platform_watchOS
) {
250 static const char tbd_v1_platform_watchos
[] = "---\n"
251 "archs: [ armv7k ]\n"
252 "platform: watchos\n"
253 "install-name: Test.dylib\n"
257 TextAPIReader::get(MemoryBufferRef(tbd_v1_platform_watchos
, "Test.tbd"));
258 EXPECT_TRUE(!!Result
);
259 auto Platform
= PlatformKind::watchOS
;
260 auto File
= std::move(Result
.get());
261 EXPECT_EQ(FileType::TBD_V1
, File
->getFileType());
262 EXPECT_EQ(File
->getPlatforms().size(), 1U);
263 EXPECT_EQ(Platform
, *File
->getPlatforms().begin());
266 TEST(TBDv1
, Platform_tvOS
) {
267 static const char tbd_v1_platform_tvos
[] = "---\n"
270 "install-name: Test.dylib\n"
274 TextAPIReader::get(MemoryBufferRef(tbd_v1_platform_tvos
, "Test.tbd"));
275 EXPECT_TRUE(!!Result
);
276 auto Platform
= PlatformKind::tvOS
;
277 auto File
= std::move(Result
.get());
278 EXPECT_EQ(FileType::TBD_V1
, File
->getFileType());
279 EXPECT_EQ(File
->getPlatforms().size(), 1U);
280 EXPECT_EQ(Platform
, *File
->getPlatforms().begin());
283 TEST(TBDv1
, Platform_bridgeOS
) {
284 static const char tbd_v1_platform_bridgeos
[] = "---\n"
285 "archs: [ armv7k ]\n"
286 "platform: bridgeos\n"
287 "install-name: Test.dylib\n"
291 TextAPIReader::get(MemoryBufferRef(tbd_v1_platform_bridgeos
, "Test.tbd"));
292 EXPECT_TRUE(!!Result
);
293 auto Platform
= PlatformKind::bridgeOS
;
294 auto File
= std::move(Result
.get());
295 EXPECT_EQ(FileType::TBD_V1
, File
->getFileType());
296 EXPECT_EQ(File
->getPlatforms().size(), 1U);
297 EXPECT_EQ(Platform
, *File
->getPlatforms().begin());
300 TEST(TBDv1
, Swift_1_0
) {
301 static const char tbd_v1_swift_1_0
[] = "---\n"
304 "install-name: Test.dylib\n"
305 "swift-version: 1.0\n"
309 TextAPIReader::get(MemoryBufferRef(tbd_v1_swift_1_0
, "Test.tbd"));
310 EXPECT_TRUE(!!Result
);
311 auto File
= std::move(Result
.get());
312 EXPECT_EQ(FileType::TBD_V1
, File
->getFileType());
313 EXPECT_EQ(1U, File
->getSwiftABIVersion());
316 TEST(TBDv1
, Swift_1_1
) {
317 static const char tbd_v1_swift_1_1
[] = "---\n"
320 "install-name: Test.dylib\n"
321 "swift-version: 1.1\n"
325 TextAPIReader::get(MemoryBufferRef(tbd_v1_swift_1_1
, "Test.tbd"));
326 EXPECT_TRUE(!!Result
);
327 auto File
= std::move(Result
.get());
328 EXPECT_EQ(FileType::TBD_V1
, File
->getFileType());
329 EXPECT_EQ(2U, File
->getSwiftABIVersion());
332 TEST(TBDv1
, Swift_2_0
) {
333 static const char tbd_v1_swift_2_0
[] = "---\n"
336 "install-name: Test.dylib\n"
337 "swift-version: 2.0\n"
341 TextAPIReader::get(MemoryBufferRef(tbd_v1_swift_2_0
, "Test.tbd"));
342 EXPECT_TRUE(!!Result
);
343 auto File
= std::move(Result
.get());
344 EXPECT_EQ(FileType::TBD_V1
, File
->getFileType());
345 EXPECT_EQ(3U, File
->getSwiftABIVersion());
348 TEST(TBDv1
, Swift_3_0
) {
349 static const char tbd_v1_swift_3_0
[] = "---\n"
352 "install-name: Test.dylib\n"
353 "swift-version: 3.0\n"
357 TextAPIReader::get(MemoryBufferRef(tbd_v1_swift_3_0
, "Test.tbd"));
358 EXPECT_TRUE(!!Result
);
359 auto File
= std::move(Result
.get());
360 EXPECT_EQ(FileType::TBD_V1
, File
->getFileType());
361 EXPECT_EQ(4U, File
->getSwiftABIVersion());
364 TEST(TBDv1
, Swift_4_0
) {
365 static const char tbd_v1_swift_4_0
[] = "---\n"
368 "install-name: Test.dylib\n"
369 "swift-version: 4.0\n"
373 TextAPIReader::get(MemoryBufferRef(tbd_v1_swift_4_0
, "Test.tbd"));
374 EXPECT_FALSE(!!Result
);
375 auto errorMessage
= toString(Result
.takeError());
376 EXPECT_EQ("malformed file\nTest.tbd:5:16: error: invalid Swift ABI "
377 "version.\nswift-version: 4.0\n ^~~\n",
381 TEST(TBDv1
, Swift_5
) {
382 static const char tbd_v1_swift_5
[] = "---\n"
385 "install-name: Test.dylib\n"
389 auto Result
= TextAPIReader::get(MemoryBufferRef(tbd_v1_swift_5
, "Test.tbd"));
390 EXPECT_TRUE(!!Result
);
391 auto File
= std::move(Result
.get());
392 EXPECT_EQ(FileType::TBD_V1
, File
->getFileType());
393 EXPECT_EQ(5U, File
->getSwiftABIVersion());
396 TEST(TBDv1
, Swift_99
) {
397 static const char tbd_v1_swift_99
[] = "---\n"
400 "install-name: Test.dylib\n"
401 "swift-version: 99\n"
405 TextAPIReader::get(MemoryBufferRef(tbd_v1_swift_99
, "Test.tbd"));
406 EXPECT_TRUE(!!Result
);
407 auto File
= std::move(Result
.get());
408 EXPECT_EQ(FileType::TBD_V1
, File
->getFileType());
409 EXPECT_EQ(99U, File
->getSwiftABIVersion());
412 TEST(TBDv1
, UnknownArchitecture
) {
413 static const char tbd_v1_file_unknown_architecture
[] =
417 "install-name: Test.dylib\n"
420 auto Result
= TextAPIReader::get(
421 MemoryBufferRef(tbd_v1_file_unknown_architecture
, "Test.tbd"));
422 EXPECT_TRUE(!!Result
);
425 TEST(TBDv1
, UnknownPlatform
) {
426 static const char tbd_v1_file_unknown_platform
[] = "---\n"
431 auto Result
= TextAPIReader::get(
432 MemoryBufferRef(tbd_v1_file_unknown_platform
, "Test.tbd"));
433 EXPECT_FALSE(!!Result
);
434 auto errorMessage
= toString(Result
.takeError());
435 EXPECT_EQ("malformed file\nTest.tbd:3:11: error: unknown platform\nplatform: "
440 TEST(TBDv1
, MalformedFile1
) {
441 static const char malformed_file1
[] = "---\n"
443 "foobar: \"Unsupported key\"\n"
447 TextAPIReader::get(MemoryBufferRef(malformed_file1
, "Test.tbd"));
448 EXPECT_FALSE(!!Result
);
449 auto errorMessage
= toString(Result
.takeError());
450 ASSERT_EQ("malformed file\nTest.tbd:2:1: error: missing required key "
451 "'platform'\narchs: [ arm64 ]\n^\n",
455 TEST(TBDv1
, MalformedFile2
) {
456 static const char malformed_file2
[] = "---\n"
459 "install-name: Test.dylib\n"
460 "foobar: \"Unsupported key\"\n"
464 TextAPIReader::get(MemoryBufferRef(malformed_file2
, "Test.tbd"));
465 EXPECT_FALSE(!!Result
);
466 auto errorMessage
= toString(Result
.takeError());
468 "malformed file\nTest.tbd:5:9: error: unknown key 'foobar'\nfoobar: "
469 "\"Unsupported key\"\n ^~~~~~~~~~~~~~~~~\n",
473 } // end namespace TBDv1.