1 //===-- TextStubV2Tests.cpp - TBD V2 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 #include "TextStubHelpers.h"
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"
13 #include <string>
14 #include <vector>
15 
16 using namespace llvm;
17 using namespace llvm::MachO;
18 
19 static ExportedSymbol TBDv2Symbols[] = {
20     {SymbolKind::GlobalSymbol, "$ld$hide$os9.0$_sym1", false, false},
21     {SymbolKind::GlobalSymbol, "_sym1", false, false},
22     {SymbolKind::GlobalSymbol, "_sym2", false, false},
23     {SymbolKind::GlobalSymbol, "_sym3", false, false},
24     {SymbolKind::GlobalSymbol, "_sym4", false, false},
25     {SymbolKind::GlobalSymbol, "_sym5", false, false},
26     {SymbolKind::GlobalSymbol, "_tlv1", false, true},
27     {SymbolKind::GlobalSymbol, "_tlv2", false, true},
28     {SymbolKind::GlobalSymbol, "_tlv3", false, true},
29     {SymbolKind::GlobalSymbol, "_weak1", true, false},
30     {SymbolKind::GlobalSymbol, "_weak2", true, false},
31     {SymbolKind::GlobalSymbol, "_weak3", true, false},
32     {SymbolKind::ObjectiveCClass, "class1", false, false},
33     {SymbolKind::ObjectiveCClass, "class2", false, false},
34     {SymbolKind::ObjectiveCClass, "class3", false, false},
35     {SymbolKind::ObjectiveCInstanceVariable, "class1._ivar1", false, false},
36     {SymbolKind::ObjectiveCInstanceVariable, "class1._ivar2", false, false},
37     {SymbolKind::ObjectiveCInstanceVariable, "class1._ivar3", false, false},
38 };
39 
40 namespace TBDv2 {
41 
TEST(TBDv2,ReadFile)42 TEST(TBDv2, ReadFile) {
43   static const char TBDv2File1[] =
44       "--- !tapi-tbd-v2\n"
45       "archs: [ armv7, armv7s, armv7k, arm64 ]\n"
46       "platform: ios\n"
47       "flags: [ installapi ]\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       "parent-umbrella: Umbrella.dylib\n"
53       "exports:\n"
54       "  - archs: [ armv7, armv7s, armv7k, arm64 ]\n"
55       "    allowable-clients: [ clientA ]\n"
56       "    re-exports: [ /usr/lib/libfoo.dylib ]\n"
57       "    symbols: [ _sym1, _sym2, _sym3, _sym4, $ld$hide$os9.0$_sym1 ]\n"
58       "    objc-classes: [ _class1, _class2 ]\n"
59       "    objc-ivars: [ _class1._ivar1, _class1._ivar2 ]\n"
60       "    weak-def-symbols: [ _weak1, _weak2 ]\n"
61       "    thread-local-symbols: [ _tlv1, _tlv2 ]\n"
62       "  - archs: [ armv7, armv7s, armv7k ]\n"
63       "    symbols: [ _sym5 ]\n"
64       "    objc-classes: [ _class3 ]\n"
65       "    objc-ivars: [ _class1._ivar3 ]\n"
66       "    weak-def-symbols: [ _weak3 ]\n"
67       "    thread-local-symbols: [ _tlv3 ]\n"
68       "...\n";
69 
70   Expected<TBDFile> Result =
71       TextAPIReader::get(MemoryBufferRef(TBDv2File1, "Test.tbd"));
72   EXPECT_TRUE(!!Result);
73   TBDFile File = std::move(Result.get());
74   EXPECT_EQ(FileType::TBD_V2, File->getFileType());
75   auto Archs = AK_armv7 | AK_armv7s | AK_armv7k | AK_arm64;
76   auto Platform = PlatformKind::iOS;
77   TargetList Targets;
78   for (auto &&arch : Archs)
79     Targets.emplace_back(Target(arch, Platform));
80   EXPECT_EQ(Archs, File->getArchitectures());
81   EXPECT_EQ(File->getPlatforms().size(), 1U);
82   EXPECT_EQ(Platform, *File->getPlatforms().begin());
83   EXPECT_EQ(std::string("Test.dylib"), File->getInstallName());
84   EXPECT_EQ(PackedVersion(2, 3, 4), File->getCurrentVersion());
85   EXPECT_EQ(PackedVersion(1, 0, 0), File->getCompatibilityVersion());
86   EXPECT_EQ(2U, File->getSwiftABIVersion());
87   EXPECT_EQ(ObjCConstraintType::Retain_Release, File->getObjCConstraint());
88   EXPECT_TRUE(File->isTwoLevelNamespace());
89   EXPECT_TRUE(File->isApplicationExtensionSafe());
90   EXPECT_TRUE(File->isInstallAPI());
91   InterfaceFileRef client("clientA", Targets);
92   InterfaceFileRef reexport("/usr/lib/libfoo.dylib", Targets);
93   EXPECT_EQ(1U, File->allowableClients().size());
94   EXPECT_EQ(client, File->allowableClients().front());
95   EXPECT_EQ(1U, File->reexportedLibraries().size());
96   EXPECT_EQ(reexport, File->reexportedLibraries().front());
97 
98   ExportedSymbolSeq Exports;
99   for (const auto *Sym : File->symbols()) {
100     EXPECT_FALSE(Sym->isWeakReferenced());
101     EXPECT_FALSE(Sym->isUndefined());
102     Exports.emplace_back(
103         ExportedSymbol{Sym->getKind(), std::string(Sym->getName()),
104                        Sym->isWeakDefined(), Sym->isThreadLocalValue()});
105   }
106   llvm::sort(Exports.begin(), Exports.end());
107 
108   EXPECT_EQ(sizeof(TBDv2Symbols) / sizeof(ExportedSymbol), Exports.size());
109   EXPECT_TRUE(
110       std::equal(Exports.begin(), Exports.end(), std::begin(TBDv2Symbols)));
111 }
112 
TEST(TBDv2,ReadFile2)113 TEST(TBDv2, ReadFile2) {
114   static const char TBDv2File2[] =
115       "--- !tapi-tbd-v2\n"
116       "archs: [ armv7, armv7s, armv7k, arm64 ]\n"
117       "platform: ios\n"
118       "flags: [ flat_namespace, not_app_extension_safe ]\n"
119       "install-name: Test.dylib\n"
120       "swift-version: 1.1\n"
121       "exports:\n"
122       "  - archs: [ armv7, armv7s, armv7k, arm64 ]\n"
123       "    symbols: [ _sym1, _sym2, _sym3, _sym4, $ld$hide$os9.0$_sym1 ]\n"
124       "    objc-classes: [ _class1, _class2 ]\n"
125       "    objc-ivars: [ _class1._ivar1, _class1._ivar2 ]\n"
126       "    weak-def-symbols: [ _weak1, _weak2 ]\n"
127       "    thread-local-symbols: [ _tlv1, _tlv2 ]\n"
128       "  - archs: [ armv7, armv7s, armv7k ]\n"
129       "    symbols: [ _sym5 ]\n"
130       "    objc-classes: [ _class3 ]\n"
131       "    objc-ivars: [ _class1._ivar3 ]\n"
132       "    weak-def-symbols: [ _weak3 ]\n"
133       "    thread-local-symbols: [ _tlv3 ]\n"
134       "undefineds:\n"
135       "  - archs: [ armv7, armv7s, armv7k, arm64 ]\n"
136       "    symbols: [ _undefSym1, _undefSym2, _undefSym3 ]\n"
137       "    objc-classes: [ _undefClass1, _undefClass2 ]\n"
138       "    objc-ivars: [ _undefClass1._ivar1, _undefClass1._ivar2 ]\n"
139       "    weak-ref-symbols: [ _undefWeak1, _undefWeak2 ]\n"
140       "...\n";
141 
142   Expected<TBDFile> Result =
143       TextAPIReader::get(MemoryBufferRef(TBDv2File2, "Test.tbd"));
144   EXPECT_TRUE(!!Result);
145   TBDFile File = std::move(Result.get());
146   EXPECT_EQ(FileType::TBD_V2, File->getFileType());
147   auto Archs = AK_armv7 | AK_armv7s | AK_armv7k | AK_arm64;
148   auto Platform = PlatformKind::iOS;
149   TargetList Targets;
150   for (auto &&arch : Archs)
151     Targets.emplace_back(Target(arch, Platform));
152   EXPECT_EQ(Archs, File->getArchitectures());
153   EXPECT_EQ(File->getPlatforms().size(), 1U);
154   EXPECT_EQ(Platform, *File->getPlatforms().begin());
155   EXPECT_EQ(std::string("Test.dylib"), File->getInstallName());
156   EXPECT_EQ(PackedVersion(1, 0, 0), File->getCurrentVersion());
157   EXPECT_EQ(PackedVersion(1, 0, 0), File->getCompatibilityVersion());
158   EXPECT_EQ(2U, File->getSwiftABIVersion());
159   EXPECT_EQ(ObjCConstraintType::Retain_Release, File->getObjCConstraint());
160   EXPECT_FALSE(File->isTwoLevelNamespace());
161   EXPECT_FALSE(File->isApplicationExtensionSafe());
162   EXPECT_FALSE(File->isInstallAPI());
163   EXPECT_EQ(0U, File->allowableClients().size());
164   EXPECT_EQ(0U, File->reexportedLibraries().size());
165 }
166 
TEST(TBDv2,WriteFile)167 TEST(TBDv2, WriteFile) {
168   static const char TBDv2File3[] =
169       "--- !tapi-tbd-v2\n"
170       "archs:           [ i386, x86_64 ]\n"
171       "platform:        macosx\n"
172       "install-name:    '/usr/lib/libfoo.dylib'\n"
173       "current-version: 1.2.3\n"
174       "compatibility-version: 0\n"
175       "swift-version:   5\n"
176       "exports:\n"
177       "  - archs:           [ i386 ]\n"
178       "    symbols:         [ _sym1 ]\n"
179       "    weak-def-symbols: [ _sym2 ]\n"
180       "    thread-local-symbols: [ _sym3 ]\n"
181       "  - archs:           [ x86_64 ]\n"
182       "    allowable-clients: [ clientA ]\n"
183       "    re-exports:      [ '/usr/lib/libfoo.dylib' ]\n"
184       "    symbols:         [ '_OBJC_EHTYPE_$_Class1' ]\n"
185       "    objc-classes:    [ _Class1 ]\n"
186       "    objc-ivars:      [ _Class1._ivar1 ]\n"
187       "...\n";
188 
189   InterfaceFile File;
190   TargetList Targets;
191   for (auto &&arch : AK_i386 | AK_x86_64)
192     Targets.emplace_back(Target(arch, PlatformKind::macOS));
193   File.setPath("libfoo.dylib");
194   File.setInstallName("/usr/lib/libfoo.dylib");
195   File.setFileType(FileType::TBD_V2);
196   File.addTargets(Targets);
197   File.setCurrentVersion(PackedVersion(1, 2, 3));
198   File.setTwoLevelNamespace();
199   File.setApplicationExtensionSafe();
200   File.setSwiftABIVersion(5);
201   File.setObjCConstraint(ObjCConstraintType::Retain_Release);
202   File.addAllowableClient("clientA", Targets[1]);
203   File.addReexportedLibrary("/usr/lib/libfoo.dylib", Targets[1]);
204   File.addSymbol(SymbolKind::GlobalSymbol, "_sym1", {Targets[0]});
205   File.addSymbol(SymbolKind::GlobalSymbol, "_sym2", {Targets[0]},
206                  SymbolFlags::WeakDefined);
207   File.addSymbol(SymbolKind::GlobalSymbol, "_sym3", {Targets[0]},
208                  SymbolFlags::ThreadLocalValue);
209   File.addSymbol(SymbolKind::ObjectiveCClass, "Class1", {Targets[1]});
210   File.addSymbol(SymbolKind::ObjectiveCClassEHType, "Class1", {Targets[1]});
211   File.addSymbol(SymbolKind::ObjectiveCInstanceVariable, "Class1._ivar1",
212                  {Targets[1]});
213 
214   SmallString<4096> Buffer;
215   raw_svector_ostream OS(Buffer);
216   Error Result = TextAPIWriter::writeToStream(OS, File);
217   EXPECT_FALSE(Result);
218   EXPECT_STREQ(TBDv2File3, Buffer.c_str());
219 }
220 
TEST(TBDv2,Platform_macOS)221 TEST(TBDv2, Platform_macOS) {
222   static const char TBDv2PlatformMacOS[] = "--- !tapi-tbd-v2\n"
223                                            "archs: [ x86_64 ]\n"
224                                            "platform: macosx\n"
225                                            "install-name: Test.dylib\n"
226                                            "...\n";
227 
228   Expected<TBDFile> Result =
229       TextAPIReader::get(MemoryBufferRef(TBDv2PlatformMacOS, "Test.tbd"));
230   EXPECT_TRUE(!!Result);
231   TBDFile File = std::move(Result.get());
232   auto Platform = PlatformKind::macOS;
233   EXPECT_EQ(FileType::TBD_V2, File->getFileType());
234   EXPECT_EQ(File->getPlatforms().size(), 1U);
235   EXPECT_EQ(Platform, *File->getPlatforms().begin());
236 }
237 
TEST(TBDv2,Platform_iOS)238 TEST(TBDv2, Platform_iOS) {
239   static const char TBDv2PlatformiOS[] = "--- !tapi-tbd-v2\n"
240                                          "archs: [ arm64 ]\n"
241                                          "platform: ios\n"
242                                          "install-name: Test.dylib\n"
243                                          "...\n";
244 
245   Expected<TBDFile> Result =
246       TextAPIReader::get(MemoryBufferRef(TBDv2PlatformiOS, "Test.tbd"));
247   EXPECT_TRUE(!!Result);
248   auto Platform = PlatformKind::iOS;
249   TBDFile File = std::move(Result.get());
250   EXPECT_EQ(FileType::TBD_V2, File->getFileType());
251   EXPECT_EQ(File->getPlatforms().size(), 1U);
252   EXPECT_EQ(Platform, *File->getPlatforms().begin());
253 }
254 
TEST(TBDv2,Platform_watchOS)255 TEST(TBDv2, Platform_watchOS) {
256   static const char TBDv2PlatformWatchOS[] = "--- !tapi-tbd-v2\n"
257                                              "archs: [ armv7k ]\n"
258                                              "platform: watchos\n"
259                                              "install-name: Test.dylib\n"
260                                              "...\n";
261 
262   Expected<TBDFile> Result =
263       TextAPIReader::get(MemoryBufferRef(TBDv2PlatformWatchOS, "Test.tbd"));
264   EXPECT_TRUE(!!Result);
265   auto Platform = PlatformKind::watchOS;
266   TBDFile File = std::move(Result.get());
267   EXPECT_EQ(FileType::TBD_V2, File->getFileType());
268   EXPECT_EQ(File->getPlatforms().size(), 1U);
269   EXPECT_EQ(Platform, *File->getPlatforms().begin());
270 }
271 
TEST(TBDv2,Platform_tvOS)272 TEST(TBDv2, Platform_tvOS) {
273   static const char TBDv2PlatformtvOS[] = "--- !tapi-tbd-v2\n"
274                                           "archs: [ arm64 ]\n"
275                                           "platform: tvos\n"
276                                           "install-name: Test.dylib\n"
277                                           "...\n";
278 
279   Expected<TBDFile> Result =
280       TextAPIReader::get(MemoryBufferRef(TBDv2PlatformtvOS, "Test.tbd"));
281   EXPECT_TRUE(!!Result);
282   auto Platform = PlatformKind::tvOS;
283   TBDFile File = std::move(Result.get());
284   EXPECT_EQ(FileType::TBD_V2, File->getFileType());
285   EXPECT_EQ(File->getPlatforms().size(), 1U);
286   EXPECT_EQ(Platform, *File->getPlatforms().begin());
287 }
288 
TEST(TBDv2,Platform_bridgeOS)289 TEST(TBDv2, Platform_bridgeOS) {
290   static const char TBDv2BridgeOS[] = "--- !tapi-tbd-v2\n"
291                                       "archs: [ armv7k ]\n"
292                                       "platform: bridgeos\n"
293                                       "install-name: Test.dylib\n"
294                                       "...\n";
295 
296   Expected<TBDFile> Result =
297       TextAPIReader::get(MemoryBufferRef(TBDv2BridgeOS, "Test.tbd"));
298   EXPECT_TRUE(!!Result);
299   auto Platform = PlatformKind::bridgeOS;
300   TBDFile File = std::move(Result.get());
301   EXPECT_EQ(FileType::TBD_V2, File->getFileType());
302   EXPECT_EQ(File->getPlatforms().size(), 1U);
303   EXPECT_EQ(Platform, *File->getPlatforms().begin());
304 }
305 
TEST(TBDv2,Swift_1_0)306 TEST(TBDv2, Swift_1_0) {
307   static const char TBDv2Swift1[] = "--- !tapi-tbd-v2\n"
308                                     "archs: [ arm64 ]\n"
309                                     "platform: ios\n"
310                                     "install-name: Test.dylib\n"
311                                     "swift-version: 1.0\n"
312                                     "...\n";
313 
314   Expected<TBDFile> Result =
315       TextAPIReader::get(MemoryBufferRef(TBDv2Swift1, "Test.tbd"));
316   EXPECT_TRUE(!!Result);
317   TBDFile File = std::move(Result.get());
318   EXPECT_EQ(FileType::TBD_V2, File->getFileType());
319   EXPECT_EQ(1U, File->getSwiftABIVersion());
320 }
321 
TEST(TBDv2,Swift_1_1)322 TEST(TBDv2, Swift_1_1) {
323   static const char TBDv2Swift1dot[] = "--- !tapi-tbd-v2\n"
324                                        "archs: [ arm64 ]\n"
325                                        "platform: ios\n"
326                                        "install-name: Test.dylib\n"
327                                        "swift-version: 1.1\n"
328                                        "...\n";
329 
330   Expected<TBDFile> Result =
331       TextAPIReader::get(MemoryBufferRef(TBDv2Swift1dot, "Test.tbd"));
332   EXPECT_TRUE(!!Result);
333   TBDFile File = std::move(Result.get());
334   EXPECT_EQ(FileType::TBD_V2, File->getFileType());
335   EXPECT_EQ(2U, File->getSwiftABIVersion());
336 }
337 
TEST(TBDv2,Swift_2_0)338 TEST(TBDv2, Swift_2_0) {
339   static const char tbd_v2_swift_2_0[] = "--- !tapi-tbd-v2\n"
340                                          "archs: [ arm64 ]\n"
341                                          "platform: ios\n"
342                                          "install-name: Test.dylib\n"
343                                          "swift-version: 2.0\n"
344                                          "...\n";
345 
346   Expected<TBDFile> Result =
347       TextAPIReader::get(MemoryBufferRef(tbd_v2_swift_2_0, "Test.tbd"));
348   EXPECT_TRUE(!!Result);
349   TBDFile File = std::move(Result.get());
350   EXPECT_EQ(FileType::TBD_V2, File->getFileType());
351   EXPECT_EQ(3U, File->getSwiftABIVersion());
352 }
353 
TEST(TBDv2,Swift_3_0)354 TEST(TBDv2, Swift_3_0) {
355   static const char TBDv2Swift3[] = "--- !tapi-tbd-v2\n"
356                                     "archs: [ arm64 ]\n"
357                                     "platform: ios\n"
358                                     "install-name: Test.dylib\n"
359                                     "swift-version: 3.0\n"
360                                     "...\n";
361 
362   Expected<TBDFile> Result =
363       TextAPIReader::get(MemoryBufferRef(TBDv2Swift3, "Test.tbd"));
364   EXPECT_TRUE(!!Result);
365   TBDFile File = std::move(Result.get());
366   EXPECT_EQ(FileType::TBD_V2, File->getFileType());
367   EXPECT_EQ(4U, File->getSwiftABIVersion());
368 }
369 
TEST(TBDv2,Swift_4_0)370 TEST(TBDv2, Swift_4_0) {
371   static const char TBDv2Swift4[] = "--- !tapi-tbd-v2\n"
372                                     "archs: [ arm64 ]\n"
373                                     "platform: ios\n"
374                                     "install-name: Test.dylib\n"
375                                     "swift-version: 4.0\n"
376                                     "...\n";
377 
378   Expected<TBDFile> Result =
379       TextAPIReader::get(MemoryBufferRef(TBDv2Swift4, "Test.tbd"));
380   EXPECT_FALSE(!!Result);
381   std::string ErrorMessage = toString(Result.takeError());
382   EXPECT_EQ("malformed file\nTest.tbd:5:16: error: invalid Swift ABI "
383             "version.\nswift-version: 4.0\n               ^~~\n",
384             ErrorMessage);
385 }
386 
TEST(TBDv2,Swift_5)387 TEST(TBDv2, Swift_5) {
388   static const char TBDv2Swift5[] = "--- !tapi-tbd-v2\n"
389                                     "archs: [ arm64 ]\n"
390                                     "platform: ios\n"
391                                     "install-name: Test.dylib\n"
392                                     "swift-version: 5\n"
393                                     "...\n";
394 
395   Expected<TBDFile> Result =
396       TextAPIReader::get(MemoryBufferRef(TBDv2Swift5, "Test.tbd"));
397   EXPECT_TRUE(!!Result);
398   TBDFile File = std::move(Result.get());
399   EXPECT_EQ(FileType::TBD_V2, File->getFileType());
400   EXPECT_EQ(5U, File->getSwiftABIVersion());
401 }
402 
TEST(TBDv2,Swift_99)403 TEST(TBDv2, Swift_99) {
404   static const char TBDv2Swift99[] = "--- !tapi-tbd-v2\n"
405                                      "archs: [ arm64 ]\n"
406                                      "platform: ios\n"
407                                      "install-name: Test.dylib\n"
408                                      "swift-version: 99\n"
409                                      "...\n";
410 
411   Expected<TBDFile> Result =
412       TextAPIReader::get(MemoryBufferRef(TBDv2Swift99, "Test.tbd"));
413   EXPECT_TRUE(!!Result);
414   TBDFile File = std::move(Result.get());
415   EXPECT_EQ(FileType::TBD_V2, File->getFileType());
416   EXPECT_EQ(99U, File->getSwiftABIVersion());
417 }
418 
TEST(TBDv2,UnknownArchitecture)419 TEST(TBDv2, UnknownArchitecture) {
420   static const char TBDv2FileUnknownArch[] = "--- !tapi-tbd-v2\n"
421                                              "archs: [ foo ]\n"
422                                              "platform: macosx\n"
423                                              "install-name: Test.dylib\n"
424                                              "...\n";
425   Expected<TBDFile> Result =
426       TextAPIReader::get(MemoryBufferRef(TBDv2FileUnknownArch, "Test.tbd"));
427   EXPECT_TRUE(!!Result);
428 }
429 
TEST(TBDv2,UnknownPlatform)430 TEST(TBDv2, UnknownPlatform) {
431   static const char TBDv2FileUnknownPlatform[] = "--- !tapi-tbd-v2\n"
432                                                  "archs: [ i386 ]\n"
433                                                  "platform: newOS\n"
434                                                  "...\n";
435 
436   Expected<TBDFile> Result =
437       TextAPIReader::get(MemoryBufferRef(TBDv2FileUnknownPlatform, "Test.tbd"));
438   EXPECT_FALSE(!!Result);
439   std::string ErrorMessage = toString(Result.takeError());
440   EXPECT_EQ("malformed file\nTest.tbd:3:11: error: unknown platform\nplatform: "
441             "newOS\n          ^~~~~\n",
442             ErrorMessage);
443 }
444 
TEST(TBDv2,InvalidPlatform)445 TEST(TBDv2, InvalidPlatform) {
446   static const char TBDv2FileInvalidPlatform[] = "--- !tapi-tbd-v2\n"
447                                                  "archs: [ i386 ]\n"
448                                                  "platform: iosmac\n"
449                                                  "install-name: Test.dylib\n"
450                                                  "...\n";
451 
452   Expected<TBDFile> Result =
453       TextAPIReader::get(MemoryBufferRef(TBDv2FileInvalidPlatform, "Test.tbd"));
454   EXPECT_FALSE(!!Result);
455   std::string ErrorMessage = toString(Result.takeError());
456   EXPECT_EQ("malformed file\nTest.tbd:3:11: error: invalid platform\nplatform: "
457             "iosmac\n          ^~~~~~\n",
458             ErrorMessage);
459 }
460 
TEST(TBDv2,MalformedFile1)461 TEST(TBDv2, MalformedFile1) {
462   static const char TBDv2FileMalformed1[] = "--- !tapi-tbd-v2\n"
463                                             "archs: [ arm64 ]\n"
464                                             "foobar: \"Unsupported key\"\n"
465                                             "...\n";
466 
467   Expected<TBDFile> Result =
468       TextAPIReader::get(MemoryBufferRef(TBDv2FileMalformed1, "Test.tbd"));
469   EXPECT_FALSE(!!Result);
470   std::string ErrorMessage = toString(Result.takeError());
471   ASSERT_EQ("malformed file\nTest.tbd:2:1: error: missing required key "
472             "'platform'\narchs: [ arm64 ]\n^\n",
473             ErrorMessage);
474 }
475 
TEST(TBDv2,MalformedFile2)476 TEST(TBDv2, MalformedFile2) {
477   static const char TBDv2FileMalformed2[] = "--- !tapi-tbd-v2\n"
478                                             "archs: [ arm64 ]\n"
479                                             "platform: ios\n"
480                                             "install-name: Test.dylib\n"
481                                             "foobar: \"Unsupported key\"\n"
482                                             "...\n";
483 
484   Expected<TBDFile> Result =
485       TextAPIReader::get(MemoryBufferRef(TBDv2FileMalformed2, "Test.tbd"));
486   EXPECT_FALSE(!!Result);
487   std::string ErrorMessage = toString(Result.takeError());
488   ASSERT_EQ(
489       "malformed file\nTest.tbd:5:9: error: unknown key 'foobar'\nfoobar: "
490       "\"Unsupported key\"\n        ^~~~~~~~~~~~~~~~~\n",
491       ErrorMessage);
492 }
493 
494 } // namespace TBDv2
495