llvm-project
457 строк · 18.2 Кб
1//===-- TextStubV1Tests.cpp - TBD V1 File Test ----------------------------===//
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 "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"
14#include <string>
15#include <vector>
16
17using namespace llvm;
18using namespace llvm::MachO;
19
20static ExportedSymbol TBDv1Symbols[] = {
21{EncodeKind::GlobalSymbol, "$ld$hide$os9.0$_sym1", false, false},
22{EncodeKind::GlobalSymbol, "_sym1", false, false},
23{EncodeKind::GlobalSymbol, "_sym2", false, false},
24{EncodeKind::GlobalSymbol, "_sym3", false, false},
25{EncodeKind::GlobalSymbol, "_sym4", false, false},
26{EncodeKind::GlobalSymbol, "_sym5", false, false},
27{EncodeKind::GlobalSymbol, "_tlv1", false, true},
28{EncodeKind::GlobalSymbol, "_tlv2", false, true},
29{EncodeKind::GlobalSymbol, "_tlv3", false, true},
30{EncodeKind::GlobalSymbol, "_weak1", true, false},
31{EncodeKind::GlobalSymbol, "_weak2", true, false},
32{EncodeKind::GlobalSymbol, "_weak3", true, false},
33{EncodeKind::ObjectiveCClass, "class1", false, false},
34{EncodeKind::ObjectiveCClass, "class2", false, false},
35{EncodeKind::ObjectiveCClass, "class3", false, false},
36{EncodeKind::ObjectiveCInstanceVariable, "class1._ivar1", false, false},
37{EncodeKind::ObjectiveCInstanceVariable, "class1._ivar2", false, false},
38{EncodeKind::ObjectiveCInstanceVariable, "class1._ivar3", false, false},
39};
40
41namespace TBDv1 {
42
43TEST(TBDv1, ReadFile) {
44static const char TBDv1File1[] =
45"---\n"
46"archs: [ armv7, armv7s, armv7k, arm64 ]\n"
47"platform: ios\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"
52"exports:\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"
67"...\n";
68
69Expected<TBDFile> Result =
70TextAPIReader::get(MemoryBufferRef(TBDv1File1, "Test.tbd"));
71EXPECT_TRUE(!!Result);
72TBDFile File = std::move(Result.get());
73EXPECT_EQ(FileType::TBD_V1, File->getFileType());
74auto Archs = AK_armv7 | AK_armv7s | AK_armv7k | AK_arm64;
75auto Platform = PLATFORM_IOS;
76TargetList Targets;
77for (auto &&arch : Archs)
78Targets.emplace_back(Target(arch, Platform));
79EXPECT_EQ(Archs, File->getArchitectures());
80EXPECT_EQ(File->getPlatforms().size(), 1U);
81EXPECT_EQ(Platform, *File->getPlatforms().begin());
82EXPECT_EQ(std::string("Test.dylib"), File->getInstallName());
83EXPECT_EQ(PackedVersion(2, 3, 4), File->getCurrentVersion());
84EXPECT_EQ(PackedVersion(1, 0, 0), File->getCompatibilityVersion());
85EXPECT_EQ(2U, File->getSwiftABIVersion());
86EXPECT_EQ(ObjCConstraintType::None, File->getObjCConstraint());
87EXPECT_TRUE(File->isTwoLevelNamespace());
88EXPECT_TRUE(File->isApplicationExtensionSafe());
89InterfaceFileRef client("clientA", Targets);
90InterfaceFileRef reexport("/usr/lib/libfoo.dylib", Targets);
91EXPECT_EQ(1U, File->allowableClients().size());
92EXPECT_EQ(client, File->allowableClients().front());
93EXPECT_EQ(1U, File->reexportedLibraries().size());
94EXPECT_EQ(reexport, File->reexportedLibraries().front());
95
96ExportedSymbolSeq Exports;
97for (const auto *Sym : File->symbols()) {
98EXPECT_FALSE(Sym->isWeakReferenced());
99EXPECT_FALSE(Sym->isUndefined());
100Exports.emplace_back(
101ExportedSymbol{Sym->getKind(), std::string(Sym->getName()),
102Sym->isWeakDefined(), Sym->isThreadLocalValue()});
103}
104llvm::sort(Exports);
105
106EXPECT_EQ(std::size(TBDv1Symbols), Exports.size());
107EXPECT_TRUE(
108std::equal(Exports.begin(), Exports.end(), std::begin(TBDv1Symbols)));
109
110File->addSymbol(EncodeKind::ObjectiveCClassEHType, "Class1", {Targets[1]});
111File->addSymbol(EncodeKind::ObjectiveCInstanceVariable, "Class1._ivar1",
112{Targets[1]});
113}
114
115TEST(TBDv1, ReadFile2) {
116static const char TBDv1File2[] = "--- !tapi-tbd-v1\n"
117"archs: [ armv7, armv7s, armv7k, arm64 ]\n"
118"platform: ios\n"
119"install-name: Test.dylib\n"
120"...\n";
121
122Expected<TBDFile> Result =
123TextAPIReader::get(MemoryBufferRef(TBDv1File2, "Test.tbd"));
124EXPECT_TRUE(!!Result);
125TBDFile File = std::move(Result.get());
126EXPECT_EQ(FileType::TBD_V1, File->getFileType());
127auto Archs = AK_armv7 | AK_armv7s | AK_armv7k | AK_arm64;
128auto Platform = PLATFORM_IOS;
129TargetList Targets;
130for (auto &&arch : Archs)
131Targets.emplace_back(Target(arch, Platform));
132EXPECT_EQ(Archs, File->getArchitectures());
133EXPECT_EQ(File->getPlatforms().size(), 1U);
134EXPECT_EQ(Platform, *File->getPlatforms().begin());
135EXPECT_EQ(std::string("Test.dylib"), File->getInstallName());
136EXPECT_EQ(PackedVersion(1, 0, 0), File->getCurrentVersion());
137EXPECT_EQ(PackedVersion(1, 0, 0), File->getCompatibilityVersion());
138EXPECT_EQ(0U, File->getSwiftABIVersion());
139EXPECT_EQ(ObjCConstraintType::None, File->getObjCConstraint());
140EXPECT_TRUE(File->isTwoLevelNamespace());
141EXPECT_TRUE(File->isApplicationExtensionSafe());
142EXPECT_EQ(0U, File->allowableClients().size());
143EXPECT_EQ(0U, File->reexportedLibraries().size());
144}
145
146TEST(TBDv1, WriteFile) {
147static const char TBDv1File3[] =
148"---\n"
149"archs: [ i386, x86_64 ]\n"
150"platform: macosx\n"
151"install-name: '/usr/lib/libfoo.dylib'\n"
152"current-version: 1.2.3\n"
153"compatibility-version: 0\n"
154"swift-version: 5\n"
155"objc-constraint: retain_release\n"
156"exports:\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"
167"...\n";
168
169InterfaceFile File;
170TargetList Targets;
171for (auto &&arch : AK_i386 | AK_x86_64)
172Targets.emplace_back(Target(arch, PLATFORM_MACOS));
173File.setPath("libfoo.dylib");
174File.setInstallName("/usr/lib/libfoo.dylib");
175File.setFileType(FileType::TBD_V1);
176File.addTargets(Targets);
177File.setCurrentVersion(PackedVersion(1, 2, 3));
178File.setSwiftABIVersion(5);
179File.setObjCConstraint(ObjCConstraintType::Retain_Release);
180File.addAllowableClient("clientA", Targets[1]);
181File.addReexportedLibrary("/usr/lib/libfoo.dylib", Targets[1]);
182File.addSymbol(EncodeKind::GlobalSymbol, "_sym1", {Targets[0]});
183File.addSymbol(EncodeKind::GlobalSymbol, "_sym2", {Targets[0]},
184SymbolFlags::WeakDefined);
185File.addSymbol(EncodeKind::GlobalSymbol, "_sym3", {Targets[0]},
186SymbolFlags::ThreadLocalValue);
187File.addSymbol(EncodeKind::ObjectiveCClass, "Class1", {Targets[1]});
188File.addSymbol(EncodeKind::ObjectiveCClassEHType, "Class1", {Targets[1]});
189File.addSymbol(EncodeKind::ObjectiveCInstanceVariable, "Class1._ivar1",
190{Targets[1]});
191
192SmallString<4096> Buffer;
193raw_svector_ostream OS(Buffer);
194Error Result = TextAPIWriter::writeToStream(OS, File);
195EXPECT_FALSE(Result);
196EXPECT_STREQ(TBDv1File3, Buffer.c_str());
197}
198
199TEST(TBDv1, Platform_macOS) {
200static const char TBDv1PlatformMacOS[] = "---\n"
201"archs: [ x86_64 ]\n"
202"platform: macosx\n"
203"install-name: Test.dylib\n"
204"...\n";
205
206Expected<TBDFile> Result =
207TextAPIReader::get(MemoryBufferRef(TBDv1PlatformMacOS, "Test.tbd"));
208EXPECT_TRUE(!!Result);
209auto Platform = PLATFORM_MACOS;
210TBDFile File = std::move(Result.get());
211EXPECT_EQ(FileType::TBD_V1, File->getFileType());
212EXPECT_EQ(File->getPlatforms().size(), 1U);
213EXPECT_EQ(Platform, *File->getPlatforms().begin());
214}
215
216TEST(TBDv1, Platform_iOS) {
217static const char TBDv1PlatformiOS[] = "---\n"
218"archs: [ arm64 ]\n"
219"platform: ios\n"
220"install-name: Test.dylib\n"
221"...\n";
222
223Expected<TBDFile> Result =
224TextAPIReader::get(MemoryBufferRef(TBDv1PlatformiOS, "Test.tbd"));
225EXPECT_TRUE(!!Result);
226auto Platform = PLATFORM_IOS;
227TBDFile File = std::move(Result.get());
228EXPECT_EQ(FileType::TBD_V1, File->getFileType());
229EXPECT_EQ(File->getPlatforms().size(), 1U);
230EXPECT_EQ(Platform, *File->getPlatforms().begin());
231}
232
233TEST(TBDv1, Platform_watchOS) {
234static const char TBDv1PlatformWatchOS[] = "---\n"
235"archs: [ armv7k ]\n"
236"platform: watchos\n"
237"install-name: Test.dylib\n"
238"...\n";
239
240Expected<TBDFile> Result =
241TextAPIReader::get(MemoryBufferRef(TBDv1PlatformWatchOS, "Test.tbd"));
242EXPECT_TRUE(!!Result);
243auto Platform = PLATFORM_WATCHOS;
244TBDFile File = std::move(Result.get());
245EXPECT_EQ(FileType::TBD_V1, File->getFileType());
246EXPECT_EQ(File->getPlatforms().size(), 1U);
247EXPECT_EQ(Platform, *File->getPlatforms().begin());
248}
249
250TEST(TBDv1, Platform_tvOS) {
251static const char TBDv1PlatformtvOS[] = "---\n"
252"archs: [ arm64 ]\n"
253"platform: tvos\n"
254"install-name: Test.dylib\n"
255"...\n";
256
257Expected<TBDFile> Result =
258TextAPIReader::get(MemoryBufferRef(TBDv1PlatformtvOS, "Test.tbd"));
259EXPECT_TRUE(!!Result);
260auto Platform = PLATFORM_TVOS;
261TBDFile File = std::move(Result.get());
262EXPECT_EQ(FileType::TBD_V1, File->getFileType());
263EXPECT_EQ(File->getPlatforms().size(), 1U);
264EXPECT_EQ(Platform, *File->getPlatforms().begin());
265}
266
267TEST(TBDv1, Platform_bridgeOS) {
268static const char TBDv1BridgeOS[] = "---\n"
269"archs: [ armv7k ]\n"
270"platform: bridgeos\n"
271"install-name: Test.dylib\n"
272"...\n";
273
274Expected<TBDFile> Result =
275TextAPIReader::get(MemoryBufferRef(TBDv1BridgeOS, "Test.tbd"));
276EXPECT_TRUE(!!Result);
277auto Platform = PLATFORM_BRIDGEOS;
278TBDFile File = std::move(Result.get());
279EXPECT_EQ(FileType::TBD_V1, File->getFileType());
280EXPECT_EQ(File->getPlatforms().size(), 1U);
281EXPECT_EQ(Platform, *File->getPlatforms().begin());
282}
283
284TEST(TBDv1, Swift_1_0) {
285static const char TBDv1Swift1[] = "---\n"
286"archs: [ arm64 ]\n"
287"platform: ios\n"
288"install-name: Test.dylib\n"
289"swift-version: 1.0\n"
290"...\n";
291
292Expected<TBDFile> Result =
293TextAPIReader::get(MemoryBufferRef(TBDv1Swift1, "Test.tbd"));
294EXPECT_TRUE(!!Result);
295TBDFile File = std::move(Result.get());
296EXPECT_EQ(FileType::TBD_V1, File->getFileType());
297EXPECT_EQ(1U, File->getSwiftABIVersion());
298}
299
300TEST(TBDv1, Swift_1_1) {
301static const char TBDv1Swift1dot[] = "---\n"
302"archs: [ arm64 ]\n"
303"platform: ios\n"
304"install-name: Test.dylib\n"
305"swift-version: 1.1\n"
306"...\n";
307
308Expected<TBDFile> Result =
309TextAPIReader::get(MemoryBufferRef(TBDv1Swift1dot, "Test.tbd"));
310EXPECT_TRUE(!!Result);
311TBDFile File = std::move(Result.get());
312EXPECT_EQ(FileType::TBD_V1, File->getFileType());
313EXPECT_EQ(2U, File->getSwiftABIVersion());
314}
315
316TEST(TBDv1, Swift_2_0) {
317static const char TBDv1Swift2[] = "---\n"
318"archs: [ arm64 ]\n"
319"platform: ios\n"
320"install-name: Test.dylib\n"
321"swift-version: 2.0\n"
322"...\n";
323
324Expected<TBDFile> Result =
325TextAPIReader::get(MemoryBufferRef(TBDv1Swift2, "Test.tbd"));
326EXPECT_TRUE(!!Result);
327TBDFile File = std::move(Result.get());
328EXPECT_EQ(FileType::TBD_V1, File->getFileType());
329EXPECT_EQ(3U, File->getSwiftABIVersion());
330}
331
332TEST(TBDv1, Swift_3_0) {
333static const char TBDv1Swift3[] = "---\n"
334"archs: [ arm64 ]\n"
335"platform: ios\n"
336"install-name: Test.dylib\n"
337"swift-version: 3.0\n"
338"...\n";
339
340Expected<TBDFile> Result =
341TextAPIReader::get(MemoryBufferRef(TBDv1Swift3, "Test.tbd"));
342EXPECT_TRUE(!!Result);
343TBDFile File = std::move(Result.get());
344EXPECT_EQ(FileType::TBD_V1, File->getFileType());
345EXPECT_EQ(4U, File->getSwiftABIVersion());
346}
347
348TEST(TBDv1, Swift_4_0) {
349static const char TBDv1Swift4[] = "---\n"
350"archs: [ arm64 ]\n"
351"platform: ios\n"
352"install-name: Test.dylib\n"
353"swift-version: 4.0\n"
354"...\n";
355
356Expected<TBDFile> Result =
357TextAPIReader::get(MemoryBufferRef(TBDv1Swift4, "Test.tbd"));
358EXPECT_FALSE(!!Result);
359std::string ErrorMessage = toString(Result.takeError());
360EXPECT_EQ("malformed file\nTest.tbd:5:16: error: invalid Swift ABI "
361"version.\nswift-version: 4.0\n ^~~\n",
362ErrorMessage);
363}
364
365TEST(TBDv1, Swift_5) {
366static const char TBDv1Swift5[] = "---\n"
367"archs: [ arm64 ]\n"
368"platform: ios\n"
369"install-name: Test.dylib\n"
370"swift-version: 5\n"
371"...\n";
372
373Expected<TBDFile> Result =
374TextAPIReader::get(MemoryBufferRef(TBDv1Swift5, "Test.tbd"));
375EXPECT_TRUE(!!Result);
376TBDFile File = std::move(Result.get());
377EXPECT_EQ(FileType::TBD_V1, File->getFileType());
378EXPECT_EQ(5U, File->getSwiftABIVersion());
379}
380
381TEST(TBDv1, Swift_99) {
382static const char TBDv1Swift99[] = "---\n"
383"archs: [ arm64 ]\n"
384"platform: ios\n"
385"install-name: Test.dylib\n"
386"swift-version: 99\n"
387"...\n";
388
389Expected<TBDFile> Result =
390TextAPIReader::get(MemoryBufferRef(TBDv1Swift99, "Test.tbd"));
391EXPECT_TRUE(!!Result);
392TBDFile File = std::move(Result.get());
393EXPECT_EQ(FileType::TBD_V1, File->getFileType());
394EXPECT_EQ(99U, File->getSwiftABIVersion());
395}
396
397TEST(TBDv1, UnknownArchitecture) {
398static const char TBDv1FileUnknownArch[] = "---\n"
399"archs: [ foo ]\n"
400"platform: macosx\n"
401"install-name: Test.dylib\n"
402"...\n";
403
404Expected<TBDFile> Result =
405TextAPIReader::get(MemoryBufferRef(TBDv1FileUnknownArch, "Test.tbd"));
406EXPECT_TRUE(!!Result);
407}
408
409TEST(TBDv1, UnknownPlatform) {
410static const char TBDv1FileUnknownPlatform[] = "---\n"
411"archs: [ i386 ]\n"
412"platform: newOS\n"
413"...\n";
414
415Expected<TBDFile> Result =
416TextAPIReader::get(MemoryBufferRef(TBDv1FileUnknownPlatform, "Test.tbd"));
417EXPECT_FALSE(!!Result);
418std::string ErrorMessage = toString(Result.takeError());
419EXPECT_EQ("malformed file\nTest.tbd:3:11: error: unknown platform\nplatform: "
420"newOS\n ^~~~~\n",
421ErrorMessage);
422}
423
424TEST(TBDv1, MalformedFile1) {
425static const char TBDv1FileMalformed1[] = "---\n"
426"archs: [ arm64 ]\n"
427"foobar: \"Unsupported key\"\n"
428"...\n";
429
430Expected<TBDFile> Result =
431TextAPIReader::get(MemoryBufferRef(TBDv1FileMalformed1, "Test.tbd"));
432EXPECT_FALSE(!!Result);
433std::string ErrorMessage = toString(Result.takeError());
434ASSERT_EQ("malformed file\nTest.tbd:2:1: error: missing required key "
435"'platform'\narchs: [ arm64 ]\n^\n",
436ErrorMessage);
437}
438
439TEST(TBDv1, MalformedFile2) {
440static const char TBDv1FileMalformed2[] = "---\n"
441"archs: [ arm64 ]\n"
442"platform: ios\n"
443"install-name: Test.dylib\n"
444"foobar: \"Unsupported key\"\n"
445"...\n";
446
447Expected<TBDFile> Result =
448TextAPIReader::get(MemoryBufferRef(TBDv1FileMalformed2, "Test.tbd"));
449EXPECT_FALSE(!!Result);
450std::string ErrorMessage = toString(Result.takeError());
451ASSERT_EQ(
452"malformed file\nTest.tbd:5:1: error: unknown key 'foobar'\nfoobar: "
453"\"Unsupported key\"\n^~~~~~\n",
454ErrorMessage);
455}
456
457} // end namespace TBDv1.
458