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 "TextStubHelpers.h"
10 #include "llvm/TextAPI/InterfaceFile.h"
11 #include "llvm/TextAPI/TextAPIReader.h"
12 #include "llvm/TextAPI/TextAPIWriter.h"
13 #include "gtest/gtest.h"
18 using namespace llvm::MachO
;
20 static ExportedSymbol TBDv1Symbols
[] = {
21 {SymbolKind::GlobalSymbol
, "$ld$hide$os9.0$_sym1", false, false},
22 {SymbolKind::GlobalSymbol
, "_sym1", false, false},
23 {SymbolKind::GlobalSymbol
, "_sym2", false, false},
24 {SymbolKind::GlobalSymbol
, "_sym3", false, false},
25 {SymbolKind::GlobalSymbol
, "_sym4", false, false},
26 {SymbolKind::GlobalSymbol
, "_sym5", false, false},
27 {SymbolKind::GlobalSymbol
, "_tlv1", false, true},
28 {SymbolKind::GlobalSymbol
, "_tlv2", false, true},
29 {SymbolKind::GlobalSymbol
, "_tlv3", false, true},
30 {SymbolKind::GlobalSymbol
, "_weak1", true, false},
31 {SymbolKind::GlobalSymbol
, "_weak2", true, false},
32 {SymbolKind::GlobalSymbol
, "_weak3", true, false},
33 {SymbolKind::ObjectiveCClass
, "class1", false, false},
34 {SymbolKind::ObjectiveCClass
, "class2", false, false},
35 {SymbolKind::ObjectiveCClass
, "class3", false, false},
36 {SymbolKind::ObjectiveCInstanceVariable
, "class1._ivar1", false, false},
37 {SymbolKind::ObjectiveCInstanceVariable
, "class1._ivar2", false, false},
38 {SymbolKind::ObjectiveCInstanceVariable
, "class1._ivar3", false, false},
43 TEST(TBDv1
, ReadFile
) {
44 static const char TBDv1File1
[] =
46 "archs: [ armv7, armv7s, armv7k, arm64 ]\n"
48 "install-name: Test.dylib\n"
49 "current-version: 2.3.4\n"
50 "compatibility-version: 1.0\n"
51 "swift-version: 1.1\n"
53 " - archs: [ armv7, armv7s, armv7k, arm64 ]\n"
54 " allowed-clients: [ clientA ]\n"
55 " re-exports: [ /usr/lib/libfoo.dylib ]\n"
56 " symbols: [ _sym1, _sym2, _sym3, _sym4, $ld$hide$os9.0$_sym1 ]\n"
57 " objc-classes: [ _class1, _class2 ]\n"
58 " objc-ivars: [ _class1._ivar1, _class1._ivar2 ]\n"
59 " weak-def-symbols: [ _weak1, _weak2 ]\n"
60 " thread-local-symbols: [ _tlv1, _tlv2 ]\n"
61 " - archs: [ armv7, armv7s, armv7k ]\n"
62 " symbols: [ _sym5 ]\n"
63 " objc-classes: [ _class3 ]\n"
64 " objc-ivars: [ _class1._ivar3 ]\n"
65 " weak-def-symbols: [ _weak3 ]\n"
66 " thread-local-symbols: [ _tlv3 ]\n"
69 Expected
<TBDFile
> Result
=
70 TextAPIReader::get(MemoryBufferRef(TBDv1File1
, "Test.tbd"));
71 EXPECT_TRUE(!!Result
);
72 TBDFile File
= std::move(Result
.get());
73 EXPECT_EQ(FileType::TBD_V1
, File
->getFileType());
74 auto Archs
= AK_armv7
| AK_armv7s
| AK_armv7k
| AK_arm64
;
75 auto Platform
= PLATFORM_IOS
;
77 for (auto &&arch
: Archs
)
78 Targets
.emplace_back(Target(arch
, Platform
));
79 EXPECT_EQ(Archs
, File
->getArchitectures());
80 EXPECT_EQ(File
->getPlatforms().size(), 1U);
81 EXPECT_EQ(Platform
, *File
->getPlatforms().begin());
82 EXPECT_EQ(std::string("Test.dylib"), File
->getInstallName());
83 EXPECT_EQ(PackedVersion(2, 3, 4), File
->getCurrentVersion());
84 EXPECT_EQ(PackedVersion(1, 0, 0), File
->getCompatibilityVersion());
85 EXPECT_EQ(2U, File
->getSwiftABIVersion());
86 EXPECT_EQ(ObjCConstraintType::None
, File
->getObjCConstraint());
87 EXPECT_TRUE(File
->isTwoLevelNamespace());
88 EXPECT_TRUE(File
->isApplicationExtensionSafe());
89 InterfaceFileRef
client("clientA", Targets
);
90 InterfaceFileRef
reexport("/usr/lib/libfoo.dylib", Targets
);
91 EXPECT_EQ(1U, File
->allowableClients().size());
92 EXPECT_EQ(client
, File
->allowableClients().front());
93 EXPECT_EQ(1U, File
->reexportedLibraries().size());
94 EXPECT_EQ(reexport
, File
->reexportedLibraries().front());
96 ExportedSymbolSeq Exports
;
97 for (const auto *Sym
: File
->symbols()) {
98 EXPECT_FALSE(Sym
->isWeakReferenced());
99 EXPECT_FALSE(Sym
->isUndefined());
100 Exports
.emplace_back(
101 ExportedSymbol
{Sym
->getKind(), std::string(Sym
->getName()),
102 Sym
->isWeakDefined(), Sym
->isThreadLocalValue()});
106 EXPECT_EQ(std::size(TBDv1Symbols
), Exports
.size());
108 std::equal(Exports
.begin(), Exports
.end(), std::begin(TBDv1Symbols
)));
110 File
->addSymbol(SymbolKind::ObjectiveCClassEHType
, "Class1", {Targets
[1]});
111 File
->addSymbol(SymbolKind::ObjectiveCInstanceVariable
, "Class1._ivar1",
115 TEST(TBDv1
, ReadFile2
) {
116 static const char TBDv1File2
[] = "--- !tapi-tbd-v1\n"
117 "archs: [ armv7, armv7s, armv7k, arm64 ]\n"
119 "install-name: Test.dylib\n"
122 Expected
<TBDFile
> Result
=
123 TextAPIReader::get(MemoryBufferRef(TBDv1File2
, "Test.tbd"));
124 EXPECT_TRUE(!!Result
);
125 TBDFile File
= std::move(Result
.get());
126 EXPECT_EQ(FileType::TBD_V1
, File
->getFileType());
127 auto Archs
= AK_armv7
| AK_armv7s
| AK_armv7k
| AK_arm64
;
128 auto Platform
= PLATFORM_IOS
;
130 for (auto &&arch
: Archs
)
131 Targets
.emplace_back(Target(arch
, Platform
));
132 EXPECT_EQ(Archs
, File
->getArchitectures());
133 EXPECT_EQ(File
->getPlatforms().size(), 1U);
134 EXPECT_EQ(Platform
, *File
->getPlatforms().begin());
135 EXPECT_EQ(std::string("Test.dylib"), File
->getInstallName());
136 EXPECT_EQ(PackedVersion(1, 0, 0), File
->getCurrentVersion());
137 EXPECT_EQ(PackedVersion(1, 0, 0), File
->getCompatibilityVersion());
138 EXPECT_EQ(0U, File
->getSwiftABIVersion());
139 EXPECT_EQ(ObjCConstraintType::None
, File
->getObjCConstraint());
140 EXPECT_TRUE(File
->isTwoLevelNamespace());
141 EXPECT_TRUE(File
->isApplicationExtensionSafe());
142 EXPECT_EQ(0U, File
->allowableClients().size());
143 EXPECT_EQ(0U, File
->reexportedLibraries().size());
146 TEST(TBDv1
, WriteFile
) {
147 static const char TBDv1File3
[] =
149 "archs: [ i386, x86_64 ]\n"
151 "install-name: '/usr/lib/libfoo.dylib'\n"
152 "current-version: 1.2.3\n"
153 "compatibility-version: 0\n"
155 "objc-constraint: retain_release\n"
157 " - archs: [ i386 ]\n"
158 " symbols: [ _sym1 ]\n"
159 " weak-def-symbols: [ _sym2 ]\n"
160 " thread-local-symbols: [ _sym3 ]\n"
161 " - archs: [ x86_64 ]\n"
162 " allowed-clients: [ clientA ]\n"
163 " re-exports: [ '/usr/lib/libfoo.dylib' ]\n"
164 " symbols: [ '_OBJC_EHTYPE_$_Class1' ]\n"
165 " objc-classes: [ _Class1 ]\n"
166 " objc-ivars: [ _Class1._ivar1 ]\n"
171 for (auto &&arch
: AK_i386
| AK_x86_64
)
172 Targets
.emplace_back(Target(arch
, PLATFORM_MACOS
));
173 File
.setPath("libfoo.dylib");
174 File
.setInstallName("/usr/lib/libfoo.dylib");
175 File
.setFileType(FileType::TBD_V1
);
176 File
.addTargets(Targets
);
177 File
.setCurrentVersion(PackedVersion(1, 2, 3));
178 File
.setSwiftABIVersion(5);
179 File
.setObjCConstraint(ObjCConstraintType::Retain_Release
);
180 File
.addAllowableClient("clientA", Targets
[1]);
181 File
.addReexportedLibrary("/usr/lib/libfoo.dylib", Targets
[1]);
182 File
.addSymbol(SymbolKind::GlobalSymbol
, "_sym1", {Targets
[0]});
183 File
.addSymbol(SymbolKind::GlobalSymbol
, "_sym2", {Targets
[0]},
184 SymbolFlags::WeakDefined
);
185 File
.addSymbol(SymbolKind::GlobalSymbol
, "_sym3", {Targets
[0]},
186 SymbolFlags::ThreadLocalValue
);
187 File
.addSymbol(SymbolKind::ObjectiveCClass
, "Class1", {Targets
[1]});
188 File
.addSymbol(SymbolKind::ObjectiveCClassEHType
, "Class1", {Targets
[1]});
189 File
.addSymbol(SymbolKind::ObjectiveCInstanceVariable
, "Class1._ivar1",
192 SmallString
<4096> Buffer
;
193 raw_svector_ostream
OS(Buffer
);
194 Error Result
= TextAPIWriter::writeToStream(OS
, File
);
195 EXPECT_FALSE(Result
);
196 EXPECT_STREQ(TBDv1File3
, Buffer
.c_str());
199 TEST(TBDv1
, Platform_macOS
) {
200 static const char TBDv1PlatformMacOS
[] = "---\n"
201 "archs: [ x86_64 ]\n"
203 "install-name: Test.dylib\n"
206 Expected
<TBDFile
> Result
=
207 TextAPIReader::get(MemoryBufferRef(TBDv1PlatformMacOS
, "Test.tbd"));
208 EXPECT_TRUE(!!Result
);
209 auto Platform
= PLATFORM_MACOS
;
210 TBDFile File
= std::move(Result
.get());
211 EXPECT_EQ(FileType::TBD_V1
, File
->getFileType());
212 EXPECT_EQ(File
->getPlatforms().size(), 1U);
213 EXPECT_EQ(Platform
, *File
->getPlatforms().begin());
216 TEST(TBDv1
, Platform_iOS
) {
217 static const char TBDv1PlatformiOS
[] = "---\n"
220 "install-name: Test.dylib\n"
223 Expected
<TBDFile
> Result
=
224 TextAPIReader::get(MemoryBufferRef(TBDv1PlatformiOS
, "Test.tbd"));
225 EXPECT_TRUE(!!Result
);
226 auto Platform
= PLATFORM_IOS
;
227 TBDFile File
= std::move(Result
.get());
228 EXPECT_EQ(FileType::TBD_V1
, File
->getFileType());
229 EXPECT_EQ(File
->getPlatforms().size(), 1U);
230 EXPECT_EQ(Platform
, *File
->getPlatforms().begin());
233 TEST(TBDv1
, Platform_watchOS
) {
234 static const char TBDv1PlatformWatchOS
[] = "---\n"
235 "archs: [ armv7k ]\n"
236 "platform: watchos\n"
237 "install-name: Test.dylib\n"
240 Expected
<TBDFile
> Result
=
241 TextAPIReader::get(MemoryBufferRef(TBDv1PlatformWatchOS
, "Test.tbd"));
242 EXPECT_TRUE(!!Result
);
243 auto Platform
= PLATFORM_WATCHOS
;
244 TBDFile File
= std::move(Result
.get());
245 EXPECT_EQ(FileType::TBD_V1
, File
->getFileType());
246 EXPECT_EQ(File
->getPlatforms().size(), 1U);
247 EXPECT_EQ(Platform
, *File
->getPlatforms().begin());
250 TEST(TBDv1
, Platform_tvOS
) {
251 static const char TBDv1PlatformtvOS
[] = "---\n"
254 "install-name: Test.dylib\n"
257 Expected
<TBDFile
> Result
=
258 TextAPIReader::get(MemoryBufferRef(TBDv1PlatformtvOS
, "Test.tbd"));
259 EXPECT_TRUE(!!Result
);
260 auto Platform
= PLATFORM_TVOS
;
261 TBDFile File
= std::move(Result
.get());
262 EXPECT_EQ(FileType::TBD_V1
, File
->getFileType());
263 EXPECT_EQ(File
->getPlatforms().size(), 1U);
264 EXPECT_EQ(Platform
, *File
->getPlatforms().begin());
267 TEST(TBDv1
, Platform_bridgeOS
) {
268 static const char TBDv1BridgeOS
[] = "---\n"
269 "archs: [ armv7k ]\n"
270 "platform: bridgeos\n"
271 "install-name: Test.dylib\n"
274 Expected
<TBDFile
> Result
=
275 TextAPIReader::get(MemoryBufferRef(TBDv1BridgeOS
, "Test.tbd"));
276 EXPECT_TRUE(!!Result
);
277 auto Platform
= PLATFORM_BRIDGEOS
;
278 TBDFile File
= std::move(Result
.get());
279 EXPECT_EQ(FileType::TBD_V1
, File
->getFileType());
280 EXPECT_EQ(File
->getPlatforms().size(), 1U);
281 EXPECT_EQ(Platform
, *File
->getPlatforms().begin());
284 TEST(TBDv1
, Swift_1_0
) {
285 static const char TBDv1Swift1
[] = "---\n"
288 "install-name: Test.dylib\n"
289 "swift-version: 1.0\n"
292 Expected
<TBDFile
> Result
=
293 TextAPIReader::get(MemoryBufferRef(TBDv1Swift1
, "Test.tbd"));
294 EXPECT_TRUE(!!Result
);
295 TBDFile File
= std::move(Result
.get());
296 EXPECT_EQ(FileType::TBD_V1
, File
->getFileType());
297 EXPECT_EQ(1U, File
->getSwiftABIVersion());
300 TEST(TBDv1
, Swift_1_1
) {
301 static const char TBDv1Swift1dot
[] = "---\n"
304 "install-name: Test.dylib\n"
305 "swift-version: 1.1\n"
308 Expected
<TBDFile
> Result
=
309 TextAPIReader::get(MemoryBufferRef(TBDv1Swift1dot
, "Test.tbd"));
310 EXPECT_TRUE(!!Result
);
311 TBDFile File
= std::move(Result
.get());
312 EXPECT_EQ(FileType::TBD_V1
, File
->getFileType());
313 EXPECT_EQ(2U, File
->getSwiftABIVersion());
316 TEST(TBDv1
, Swift_2_0
) {
317 static const char TBDv1Swift2
[] = "---\n"
320 "install-name: Test.dylib\n"
321 "swift-version: 2.0\n"
324 Expected
<TBDFile
> Result
=
325 TextAPIReader::get(MemoryBufferRef(TBDv1Swift2
, "Test.tbd"));
326 EXPECT_TRUE(!!Result
);
327 TBDFile File
= std::move(Result
.get());
328 EXPECT_EQ(FileType::TBD_V1
, File
->getFileType());
329 EXPECT_EQ(3U, File
->getSwiftABIVersion());
332 TEST(TBDv1
, Swift_3_0
) {
333 static const char TBDv1Swift3
[] = "---\n"
336 "install-name: Test.dylib\n"
337 "swift-version: 3.0\n"
340 Expected
<TBDFile
> Result
=
341 TextAPIReader::get(MemoryBufferRef(TBDv1Swift3
, "Test.tbd"));
342 EXPECT_TRUE(!!Result
);
343 TBDFile File
= std::move(Result
.get());
344 EXPECT_EQ(FileType::TBD_V1
, File
->getFileType());
345 EXPECT_EQ(4U, File
->getSwiftABIVersion());
348 TEST(TBDv1
, Swift_4_0
) {
349 static const char TBDv1Swift4
[] = "---\n"
352 "install-name: Test.dylib\n"
353 "swift-version: 4.0\n"
356 Expected
<TBDFile
> Result
=
357 TextAPIReader::get(MemoryBufferRef(TBDv1Swift4
, "Test.tbd"));
358 EXPECT_FALSE(!!Result
);
359 std::string ErrorMessage
= toString(Result
.takeError());
360 EXPECT_EQ("malformed file\nTest.tbd:5:16: error: invalid Swift ABI "
361 "version.\nswift-version: 4.0\n ^~~\n",
365 TEST(TBDv1
, Swift_5
) {
366 static const char TBDv1Swift5
[] = "---\n"
369 "install-name: Test.dylib\n"
373 Expected
<TBDFile
> Result
=
374 TextAPIReader::get(MemoryBufferRef(TBDv1Swift5
, "Test.tbd"));
375 EXPECT_TRUE(!!Result
);
376 TBDFile File
= std::move(Result
.get());
377 EXPECT_EQ(FileType::TBD_V1
, File
->getFileType());
378 EXPECT_EQ(5U, File
->getSwiftABIVersion());
381 TEST(TBDv1
, Swift_99
) {
382 static const char TBDv1Swift99
[] = "---\n"
385 "install-name: Test.dylib\n"
386 "swift-version: 99\n"
389 Expected
<TBDFile
> Result
=
390 TextAPIReader::get(MemoryBufferRef(TBDv1Swift99
, "Test.tbd"));
391 EXPECT_TRUE(!!Result
);
392 TBDFile File
= std::move(Result
.get());
393 EXPECT_EQ(FileType::TBD_V1
, File
->getFileType());
394 EXPECT_EQ(99U, File
->getSwiftABIVersion());
397 TEST(TBDv1
, UnknownArchitecture
) {
398 static const char TBDv1FileUnknownArch
[] = "---\n"
401 "install-name: Test.dylib\n"
404 Expected
<TBDFile
> Result
=
405 TextAPIReader::get(MemoryBufferRef(TBDv1FileUnknownArch
, "Test.tbd"));
406 EXPECT_TRUE(!!Result
);
409 TEST(TBDv1
, UnknownPlatform
) {
410 static const char TBDv1FileUnknownPlatform
[] = "---\n"
415 Expected
<TBDFile
> Result
=
416 TextAPIReader::get(MemoryBufferRef(TBDv1FileUnknownPlatform
, "Test.tbd"));
417 EXPECT_FALSE(!!Result
);
418 std::string ErrorMessage
= toString(Result
.takeError());
419 EXPECT_EQ("malformed file\nTest.tbd:3:11: error: unknown platform\nplatform: "
424 TEST(TBDv1
, MalformedFile1
) {
425 static const char TBDv1FileMalformed1
[] = "---\n"
427 "foobar: \"Unsupported key\"\n"
430 Expected
<TBDFile
> Result
=
431 TextAPIReader::get(MemoryBufferRef(TBDv1FileMalformed1
, "Test.tbd"));
432 EXPECT_FALSE(!!Result
);
433 std::string ErrorMessage
= toString(Result
.takeError());
434 ASSERT_EQ("malformed file\nTest.tbd:2:1: error: missing required key "
435 "'platform'\narchs: [ arm64 ]\n^\n",
439 TEST(TBDv1
, MalformedFile2
) {
440 static const char TBDv1FileMalformed2
[] = "---\n"
443 "install-name: Test.dylib\n"
444 "foobar: \"Unsupported key\"\n"
447 Expected
<TBDFile
> Result
=
448 TextAPIReader::get(MemoryBufferRef(TBDv1FileMalformed2
, "Test.tbd"));
449 EXPECT_FALSE(!!Result
);
450 std::string ErrorMessage
= toString(Result
.takeError());
452 "malformed file\nTest.tbd:5:1: error: unknown key 'foobar'\nfoobar: "
453 "\"Unsupported key\"\n^~~~~~\n",
457 } // end namespace TBDv1.