TextStubV2Tests.cpp 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481
  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 "llvm/TextAPI/MachO/InterfaceFile.h"
  9. #include "llvm/TextAPI/MachO/TextAPIReader.h"
  10. #include "llvm/TextAPI/MachO/TextAPIWriter.h"
  11. #include "gtest/gtest.h"
  12. #include <string>
  13. #include <vector>
  14. using namespace llvm;
  15. using namespace llvm::MachO;
  16. struct ExportedSymbol {
  17. SymbolKind Kind;
  18. std::string Name;
  19. bool WeakDefined;
  20. bool ThreadLocalValue;
  21. };
  22. using ExportedSymbolSeq = std::vector<ExportedSymbol>;
  23. inline bool operator<(const ExportedSymbol &lhs, const ExportedSymbol &rhs) {
  24. return std::tie(lhs.Kind, lhs.Name) < std::tie(rhs.Kind, rhs.Name);
  25. }
  26. inline bool operator==(const ExportedSymbol &lhs, const ExportedSymbol &rhs) {
  27. return std::tie(lhs.Kind, lhs.Name, lhs.WeakDefined, lhs.ThreadLocalValue) ==
  28. std::tie(rhs.Kind, rhs.Name, rhs.WeakDefined, rhs.ThreadLocalValue);
  29. }
  30. static ExportedSymbol TBDv2Symbols[] = {
  31. {SymbolKind::GlobalSymbol, "$ld$hide$os9.0$_sym1", false, false},
  32. {SymbolKind::GlobalSymbol, "_sym1", false, false},
  33. {SymbolKind::GlobalSymbol, "_sym2", false, false},
  34. {SymbolKind::GlobalSymbol, "_sym3", false, false},
  35. {SymbolKind::GlobalSymbol, "_sym4", false, false},
  36. {SymbolKind::GlobalSymbol, "_sym5", false, false},
  37. {SymbolKind::GlobalSymbol, "_tlv1", false, true},
  38. {SymbolKind::GlobalSymbol, "_tlv2", false, true},
  39. {SymbolKind::GlobalSymbol, "_tlv3", false, true},
  40. {SymbolKind::GlobalSymbol, "_weak1", true, false},
  41. {SymbolKind::GlobalSymbol, "_weak2", true, false},
  42. {SymbolKind::GlobalSymbol, "_weak3", true, false},
  43. {SymbolKind::ObjectiveCClass, "class1", false, false},
  44. {SymbolKind::ObjectiveCClass, "class2", false, false},
  45. {SymbolKind::ObjectiveCClass, "class3", false, false},
  46. {SymbolKind::ObjectiveCInstanceVariable, "class1._ivar1", false, false},
  47. {SymbolKind::ObjectiveCInstanceVariable, "class1._ivar2", false, false},
  48. {SymbolKind::ObjectiveCInstanceVariable, "class1._ivar3", false, false},
  49. };
  50. namespace TBDv2 {
  51. TEST(TBDv2, ReadFile) {
  52. static const char tbd_v2_file1[] =
  53. "--- !tapi-tbd-v2\n"
  54. "archs: [ armv7, armv7s, armv7k, arm64 ]\n"
  55. "platform: ios\n"
  56. "flags: [ installapi ]\n"
  57. "install-name: Test.dylib\n"
  58. "current-version: 2.3.4\n"
  59. "compatibility-version: 1.0\n"
  60. "swift-version: 1.1\n"
  61. "parent-umbrella: Umbrella.dylib\n"
  62. "exports:\n"
  63. " - archs: [ armv7, armv7s, armv7k, arm64 ]\n"
  64. " allowable-clients: [ clientA ]\n"
  65. " re-exports: [ /usr/lib/libfoo.dylib ]\n"
  66. " symbols: [ _sym1, _sym2, _sym3, _sym4, $ld$hide$os9.0$_sym1 ]\n"
  67. " objc-classes: [ _class1, _class2 ]\n"
  68. " objc-ivars: [ _class1._ivar1, _class1._ivar2 ]\n"
  69. " weak-def-symbols: [ _weak1, _weak2 ]\n"
  70. " thread-local-symbols: [ _tlv1, _tlv2 ]\n"
  71. " - archs: [ armv7, armv7s, armv7k ]\n"
  72. " symbols: [ _sym5 ]\n"
  73. " objc-classes: [ _class3 ]\n"
  74. " objc-ivars: [ _class1._ivar3 ]\n"
  75. " weak-def-symbols: [ _weak3 ]\n"
  76. " thread-local-symbols: [ _tlv3 ]\n"
  77. "...\n";
  78. auto Buffer = MemoryBuffer::getMemBuffer(tbd_v2_file1, "Test.tbd");
  79. auto Result = TextAPIReader::get(std::move(Buffer));
  80. EXPECT_TRUE(!!Result);
  81. auto File = std::move(Result.get());
  82. EXPECT_EQ(FileType::TBD_V2, File->getFileType());
  83. auto Archs = AK_armv7 | AK_armv7s | AK_armv7k | AK_arm64;
  84. EXPECT_EQ(Archs, File->getArchitectures());
  85. EXPECT_EQ(PlatformKind::iOS, File->getPlatform());
  86. EXPECT_EQ(std::string("Test.dylib"), File->getInstallName());
  87. EXPECT_EQ(PackedVersion(2, 3, 4), File->getCurrentVersion());
  88. EXPECT_EQ(PackedVersion(1, 0, 0), File->getCompatibilityVersion());
  89. EXPECT_EQ(2U, File->getSwiftABIVersion());
  90. EXPECT_EQ(ObjCConstraintType::Retain_Release, File->getObjCConstraint());
  91. EXPECT_TRUE(File->isTwoLevelNamespace());
  92. EXPECT_TRUE(File->isApplicationExtensionSafe());
  93. EXPECT_TRUE(File->isInstallAPI());
  94. InterfaceFileRef client("clientA", Archs);
  95. InterfaceFileRef reexport("/usr/lib/libfoo.dylib", Archs);
  96. EXPECT_EQ(1U, File->allowableClients().size());
  97. EXPECT_EQ(client, File->allowableClients().front());
  98. EXPECT_EQ(1U, File->reexportedLibraries().size());
  99. EXPECT_EQ(reexport, File->reexportedLibraries().front());
  100. ExportedSymbolSeq Exports;
  101. for (const auto *Sym : File->symbols()) {
  102. EXPECT_FALSE(Sym->isWeakReferenced());
  103. EXPECT_FALSE(Sym->isUndefined());
  104. Exports.emplace_back(ExportedSymbol{Sym->getKind(), Sym->getName(),
  105. Sym->isWeakDefined(),
  106. Sym->isThreadLocalValue()});
  107. }
  108. llvm::sort(Exports.begin(), Exports.end());
  109. EXPECT_EQ(sizeof(TBDv2Symbols) / sizeof(ExportedSymbol), Exports.size());
  110. EXPECT_TRUE(
  111. std::equal(Exports.begin(), Exports.end(), std::begin(TBDv2Symbols)));
  112. }
  113. TEST(TBDv2, ReadFile2) {
  114. static const char tbd_v2_file2[] =
  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. auto Buffer = MemoryBuffer::getMemBuffer(tbd_v2_file2, "Test.tbd");
  142. auto Result = TextAPIReader::get(std::move(Buffer));
  143. EXPECT_TRUE(!!Result);
  144. auto File = std::move(Result.get());
  145. EXPECT_EQ(FileType::TBD_V2, File->getFileType());
  146. auto Archs = AK_armv7 | AK_armv7s | AK_armv7k | AK_arm64;
  147. EXPECT_EQ(Archs, File->getArchitectures());
  148. EXPECT_EQ(PlatformKind::iOS, File->getPlatform());
  149. EXPECT_EQ(std::string("Test.dylib"), File->getInstallName());
  150. EXPECT_EQ(PackedVersion(1, 0, 0), File->getCurrentVersion());
  151. EXPECT_EQ(PackedVersion(1, 0, 0), File->getCompatibilityVersion());
  152. EXPECT_EQ(2U, File->getSwiftABIVersion());
  153. EXPECT_EQ(ObjCConstraintType::Retain_Release, File->getObjCConstraint());
  154. EXPECT_FALSE(File->isTwoLevelNamespace());
  155. EXPECT_FALSE(File->isApplicationExtensionSafe());
  156. EXPECT_FALSE(File->isInstallAPI());
  157. EXPECT_EQ(0U, File->allowableClients().size());
  158. EXPECT_EQ(0U, File->reexportedLibraries().size());
  159. }
  160. // Disable test for windows.
  161. #ifndef _WIN32
  162. TEST(TBDv2, WriteFile) {
  163. static const char tbd_v2_file3[] =
  164. "--- !tapi-tbd-v2\n"
  165. "archs: [ i386, x86_64 ]\n"
  166. "platform: macosx\n"
  167. "install-name: '/usr/lib/libfoo.dylib'\n"
  168. "current-version: 1.2.3\n"
  169. "compatibility-version: 0\n"
  170. "swift-version: 5\n"
  171. "exports:\n"
  172. " - archs: [ i386 ]\n"
  173. " symbols: [ _sym1 ]\n"
  174. " weak-def-symbols: [ _sym2 ]\n"
  175. " thread-local-symbols: [ _sym3 ]\n"
  176. " - archs: [ x86_64 ]\n"
  177. " allowable-clients: [ clientA ]\n"
  178. " re-exports: [ '/usr/lib/libfoo.dylib' ]\n"
  179. " symbols: [ '_OBJC_EHTYPE_$_Class1' ]\n"
  180. " objc-classes: [ _Class1 ]\n"
  181. " objc-ivars: [ _Class1._ivar1 ]\n"
  182. "...\n";
  183. InterfaceFile File;
  184. File.setPath("libfoo.dylib");
  185. File.setInstallName("/usr/lib/libfoo.dylib");
  186. File.setFileType(FileType::TBD_V2);
  187. File.setArchitectures(AK_i386 | AK_x86_64);
  188. File.setPlatform(PlatformKind::macOS);
  189. File.setCurrentVersion(PackedVersion(1, 2, 3));
  190. File.setTwoLevelNamespace();
  191. File.setApplicationExtensionSafe();
  192. File.setSwiftABIVersion(5);
  193. File.setObjCConstraint(ObjCConstraintType::Retain_Release);
  194. File.addAllowableClient("clientA", AK_x86_64);
  195. File.addReexportedLibrary("/usr/lib/libfoo.dylib", AK_x86_64);
  196. File.addSymbol(SymbolKind::GlobalSymbol, "_sym1", AK_i386);
  197. File.addSymbol(SymbolKind::GlobalSymbol, "_sym2", AK_i386,
  198. SymbolFlags::WeakDefined);
  199. File.addSymbol(SymbolKind::GlobalSymbol, "_sym3", AK_i386,
  200. SymbolFlags::ThreadLocalValue);
  201. File.addSymbol(SymbolKind::ObjectiveCClass, "Class1", AK_x86_64);
  202. File.addSymbol(SymbolKind::ObjectiveCClassEHType, "Class1", AK_x86_64);
  203. File.addSymbol(SymbolKind::ObjectiveCInstanceVariable, "Class1._ivar1",
  204. AK_x86_64);
  205. SmallString<4096> Buffer;
  206. raw_svector_ostream OS(Buffer);
  207. auto Result = TextAPIWriter::writeToStream(OS, File);
  208. EXPECT_FALSE(Result);
  209. EXPECT_STREQ(tbd_v2_file3, Buffer.c_str());
  210. }
  211. #endif // _WIN32
  212. TEST(TBDv2, Platform_macOS) {
  213. static const char tbd_v1_platform_macos[] = "--- !tapi-tbd-v2\n"
  214. "archs: [ x86_64 ]\n"
  215. "platform: macosx\n"
  216. "install-name: Test.dylib\n"
  217. "...\n";
  218. auto Buffer = MemoryBuffer::getMemBuffer(tbd_v1_platform_macos, "Test.tbd");
  219. auto Result = TextAPIReader::get(std::move(Buffer));
  220. EXPECT_TRUE(!!Result);
  221. auto File = std::move(Result.get());
  222. EXPECT_EQ(FileType::TBD_V2, File->getFileType());
  223. EXPECT_EQ(PlatformKind::macOS, File->getPlatform());
  224. }
  225. TEST(TBDv2, Platform_iOS) {
  226. static const char tbd_v1_platform_ios[] = "--- !tapi-tbd-v2\n"
  227. "archs: [ arm64 ]\n"
  228. "platform: ios\n"
  229. "install-name: Test.dylib\n"
  230. "...\n";
  231. auto Buffer = MemoryBuffer::getMemBuffer(tbd_v1_platform_ios, "Test.tbd");
  232. auto Result = TextAPIReader::get(std::move(Buffer));
  233. EXPECT_TRUE(!!Result);
  234. auto File = std::move(Result.get());
  235. EXPECT_EQ(FileType::TBD_V2, File->getFileType());
  236. EXPECT_EQ(PlatformKind::iOS, File->getPlatform());
  237. }
  238. TEST(TBDv2, Platform_watchOS) {
  239. static const char tbd_v1_platform_watchos[] = "--- !tapi-tbd-v2\n"
  240. "archs: [ armv7k ]\n"
  241. "platform: watchos\n"
  242. "install-name: Test.dylib\n"
  243. "...\n";
  244. auto Buffer = MemoryBuffer::getMemBuffer(tbd_v1_platform_watchos, "Test.tbd");
  245. auto Result = TextAPIReader::get(std::move(Buffer));
  246. EXPECT_TRUE(!!Result);
  247. auto File = std::move(Result.get());
  248. EXPECT_EQ(FileType::TBD_V2, File->getFileType());
  249. EXPECT_EQ(PlatformKind::watchOS, File->getPlatform());
  250. }
  251. TEST(TBDv2, Platform_tvOS) {
  252. static const char tbd_v1_platform_tvos[] = "--- !tapi-tbd-v2\n"
  253. "archs: [ arm64 ]\n"
  254. "platform: tvos\n"
  255. "install-name: Test.dylib\n"
  256. "...\n";
  257. auto Buffer = MemoryBuffer::getMemBuffer(tbd_v1_platform_tvos, "Test.tbd");
  258. auto Result = TextAPIReader::get(std::move(Buffer));
  259. EXPECT_TRUE(!!Result);
  260. auto File = std::move(Result.get());
  261. EXPECT_EQ(FileType::TBD_V2, File->getFileType());
  262. EXPECT_EQ(PlatformKind::tvOS, File->getPlatform());
  263. }
  264. TEST(TBDv2, Platform_bridgeOS) {
  265. static const char tbd_v1_platform_bridgeos[] = "--- !tapi-tbd-v2\n"
  266. "archs: [ armv7k ]\n"
  267. "platform: bridgeos\n"
  268. "install-name: Test.dylib\n"
  269. "...\n";
  270. auto Buffer =
  271. MemoryBuffer::getMemBuffer(tbd_v1_platform_bridgeos, "Test.tbd");
  272. auto Result = TextAPIReader::get(std::move(Buffer));
  273. EXPECT_TRUE(!!Result);
  274. auto File = std::move(Result.get());
  275. EXPECT_EQ(FileType::TBD_V2, File->getFileType());
  276. EXPECT_EQ(PlatformKind::bridgeOS, File->getPlatform());
  277. }
  278. TEST(TBDv2, Swift_1_0) {
  279. static const char tbd_v1_swift_1_0[] = "--- !tapi-tbd-v2\n"
  280. "archs: [ arm64 ]\n"
  281. "platform: ios\n"
  282. "install-name: Test.dylib\n"
  283. "swift-version: 1.0\n"
  284. "...\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_V2, File->getFileType());
  290. EXPECT_EQ(1U, File->getSwiftABIVersion());
  291. }
  292. TEST(TBDv2, Swift_1_1) {
  293. static const char tbd_v1_swift_1_1[] = "--- !tapi-tbd-v2\n"
  294. "archs: [ arm64 ]\n"
  295. "platform: ios\n"
  296. "install-name: Test.dylib\n"
  297. "swift-version: 1.1\n"
  298. "...\n";
  299. auto Buffer = MemoryBuffer::getMemBuffer(tbd_v1_swift_1_1, "Test.tbd");
  300. auto Result = TextAPIReader::get(std::move(Buffer));
  301. EXPECT_TRUE(!!Result);
  302. auto File = std::move(Result.get());
  303. EXPECT_EQ(FileType::TBD_V2, File->getFileType());
  304. EXPECT_EQ(2U, File->getSwiftABIVersion());
  305. }
  306. TEST(TBDv2, Swift_2_0) {
  307. static const char tbd_v1_swift_2_0[] = "--- !tapi-tbd-v2\n"
  308. "archs: [ arm64 ]\n"
  309. "platform: ios\n"
  310. "install-name: Test.dylib\n"
  311. "swift-version: 2.0\n"
  312. "...\n";
  313. auto Buffer = MemoryBuffer::getMemBuffer(tbd_v1_swift_2_0, "Test.tbd");
  314. auto Result = TextAPIReader::get(std::move(Buffer));
  315. EXPECT_TRUE(!!Result);
  316. auto File = std::move(Result.get());
  317. EXPECT_EQ(FileType::TBD_V2, File->getFileType());
  318. EXPECT_EQ(3U, File->getSwiftABIVersion());
  319. }
  320. TEST(TBDv2, Swift_3_0) {
  321. static const char tbd_v1_swift_3_0[] = "--- !tapi-tbd-v2\n"
  322. "archs: [ arm64 ]\n"
  323. "platform: ios\n"
  324. "install-name: Test.dylib\n"
  325. "swift-version: 3.0\n"
  326. "...\n";
  327. auto Buffer = MemoryBuffer::getMemBuffer(tbd_v1_swift_3_0, "Test.tbd");
  328. auto Result = TextAPIReader::get(std::move(Buffer));
  329. EXPECT_TRUE(!!Result);
  330. auto File = std::move(Result.get());
  331. EXPECT_EQ(FileType::TBD_V2, File->getFileType());
  332. EXPECT_EQ(4U, File->getSwiftABIVersion());
  333. }
  334. TEST(TBDv2, Swift_4_0) {
  335. static const char tbd_v1_swift_4_0[] = "--- !tapi-tbd-v2\n"
  336. "archs: [ arm64 ]\n"
  337. "platform: ios\n"
  338. "install-name: Test.dylib\n"
  339. "swift-version: 4.0\n"
  340. "...\n";
  341. auto Buffer = MemoryBuffer::getMemBuffer(tbd_v1_swift_4_0, "Test.tbd");
  342. auto Result = TextAPIReader::get(std::move(Buffer));
  343. EXPECT_FALSE(!!Result);
  344. auto errorMessage = toString(Result.takeError());
  345. EXPECT_EQ("malformed file\nTest.tbd:5:16: error: invalid Swift ABI "
  346. "version.\nswift-version: 4.0\n ^~~\n",
  347. errorMessage);
  348. }
  349. TEST(TBDv2, Swift_5) {
  350. static const char tbd_v1_swift_5[] = "--- !tapi-tbd-v2\n"
  351. "archs: [ arm64 ]\n"
  352. "platform: ios\n"
  353. "install-name: Test.dylib\n"
  354. "swift-version: 5\n"
  355. "...\n";
  356. auto Buffer = MemoryBuffer::getMemBuffer(tbd_v1_swift_5, "Test.tbd");
  357. auto Result = TextAPIReader::get(std::move(Buffer));
  358. EXPECT_TRUE(!!Result);
  359. auto File = std::move(Result.get());
  360. EXPECT_EQ(FileType::TBD_V2, File->getFileType());
  361. EXPECT_EQ(5U, File->getSwiftABIVersion());
  362. }
  363. TEST(TBDv2, Swift_99) {
  364. static const char tbd_v1_swift_99[] = "--- !tapi-tbd-v2\n"
  365. "archs: [ arm64 ]\n"
  366. "platform: ios\n"
  367. "install-name: Test.dylib\n"
  368. "swift-version: 99\n"
  369. "...\n";
  370. auto Buffer = MemoryBuffer::getMemBuffer(tbd_v1_swift_99, "Test.tbd");
  371. auto Result = TextAPIReader::get(std::move(Buffer));
  372. EXPECT_TRUE(!!Result);
  373. auto File = std::move(Result.get());
  374. EXPECT_EQ(FileType::TBD_V2, File->getFileType());
  375. EXPECT_EQ(99U, File->getSwiftABIVersion());
  376. }
  377. TEST(TBDv2, UnknownArchitecture) {
  378. static const char tbd_v2_file_unknown_architecture[] =
  379. "--- !tapi-tbd-v2\n"
  380. "archs: [ foo ]\n"
  381. "platform: macosx\n"
  382. "install-name: Test.dylib\n"
  383. "...\n";
  384. auto Buffer =
  385. MemoryBuffer::getMemBuffer(tbd_v2_file_unknown_architecture, "Test.tbd");
  386. auto Result = TextAPIReader::get(std::move(Buffer));
  387. EXPECT_TRUE(!!Result);
  388. }
  389. TEST(TBDv2, UnknownPlatform) {
  390. static const char tbd_v2_file_unknown_platform[] = "--- !tapi-tbd-v2\n"
  391. "archs: [ i386 ]\n"
  392. "platform: newOS\n"
  393. "...\n";
  394. auto Buffer =
  395. MemoryBuffer::getMemBuffer(tbd_v2_file_unknown_platform, "Test.tbd");
  396. auto Result = TextAPIReader::get(std::move(Buffer));
  397. EXPECT_FALSE(!!Result);
  398. auto errorMessage = toString(Result.takeError());
  399. EXPECT_EQ("malformed file\nTest.tbd:3:11: error: unknown platform\nplatform: "
  400. "newOS\n ^~~~~\n",
  401. errorMessage);
  402. }
  403. TEST(TBDv2, MalformedFile1) {
  404. static const char malformed_file1[] = "--- !tapi-tbd-v2\n"
  405. "archs: [ arm64 ]\n"
  406. "foobar: \"Unsupported key\"\n"
  407. "...\n";
  408. auto Buffer = MemoryBuffer::getMemBuffer(malformed_file1, "Test.tbd");
  409. auto Result = TextAPIReader::get(std::move(Buffer));
  410. EXPECT_FALSE(!!Result);
  411. auto errorMessage = toString(Result.takeError());
  412. ASSERT_EQ("malformed file\nTest.tbd:2:1: error: missing required key "
  413. "'platform'\narchs: [ arm64 ]\n^\n",
  414. errorMessage);
  415. }
  416. TEST(TBDv2, MalformedFile2) {
  417. static const char malformed_file2[] = "--- !tapi-tbd-v2\n"
  418. "archs: [ arm64 ]\n"
  419. "platform: ios\n"
  420. "install-name: Test.dylib\n"
  421. "foobar: \"Unsupported key\"\n"
  422. "...\n";
  423. auto Buffer = MemoryBuffer::getMemBuffer(malformed_file2, "Test.tbd");
  424. auto Result = TextAPIReader::get(std::move(Buffer));
  425. EXPECT_FALSE(!!Result);
  426. auto errorMessage = toString(Result.takeError());
  427. ASSERT_EQ(
  428. "malformed file\nTest.tbd:5:9: error: unknown key 'foobar'\nfoobar: "
  429. "\"Unsupported key\"\n ^~~~~~~~~~~~~~~~~\n",
  430. errorMessage);
  431. }
  432. } // namespace TBDv2