TextStubV2Tests.cpp 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510
  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 Result = TextAPIReader::get(MemoryBufferRef(tbd_v2_file1, "Test.tbd"));
  79. EXPECT_TRUE(!!Result);
  80. auto File = std::move(Result.get());
  81. EXPECT_EQ(FileType::TBD_V2, File->getFileType());
  82. auto Archs = AK_armv7 | AK_armv7s | AK_armv7k | AK_arm64;
  83. auto Platform = PlatformKind::iOS;
  84. TargetList Targets;
  85. for (auto &&arch : Archs)
  86. Targets.emplace_back(Target(arch, Platform));
  87. EXPECT_EQ(Archs, File->getArchitectures());
  88. EXPECT_EQ(File->getPlatforms().size(), 1U);
  89. EXPECT_EQ(Platform, *File->getPlatforms().begin());
  90. EXPECT_EQ(std::string("Test.dylib"), File->getInstallName());
  91. EXPECT_EQ(PackedVersion(2, 3, 4), File->getCurrentVersion());
  92. EXPECT_EQ(PackedVersion(1, 0, 0), File->getCompatibilityVersion());
  93. EXPECT_EQ(2U, File->getSwiftABIVersion());
  94. EXPECT_EQ(ObjCConstraintType::Retain_Release, File->getObjCConstraint());
  95. EXPECT_TRUE(File->isTwoLevelNamespace());
  96. EXPECT_TRUE(File->isApplicationExtensionSafe());
  97. EXPECT_TRUE(File->isInstallAPI());
  98. InterfaceFileRef client("clientA", Targets);
  99. InterfaceFileRef reexport("/usr/lib/libfoo.dylib", Targets);
  100. EXPECT_EQ(1U, File->allowableClients().size());
  101. EXPECT_EQ(client, File->allowableClients().front());
  102. EXPECT_EQ(1U, File->reexportedLibraries().size());
  103. EXPECT_EQ(reexport, File->reexportedLibraries().front());
  104. ExportedSymbolSeq Exports;
  105. for (const auto *Sym : File->symbols()) {
  106. EXPECT_FALSE(Sym->isWeakReferenced());
  107. EXPECT_FALSE(Sym->isUndefined());
  108. Exports.emplace_back(ExportedSymbol{Sym->getKind(), Sym->getName(),
  109. Sym->isWeakDefined(),
  110. Sym->isThreadLocalValue()});
  111. }
  112. llvm::sort(Exports.begin(), Exports.end());
  113. EXPECT_EQ(sizeof(TBDv2Symbols) / sizeof(ExportedSymbol), Exports.size());
  114. EXPECT_TRUE(
  115. std::equal(Exports.begin(), Exports.end(), std::begin(TBDv2Symbols)));
  116. }
  117. TEST(TBDv2, ReadFile2) {
  118. static const char tbd_v2_file2[] =
  119. "--- !tapi-tbd-v2\n"
  120. "archs: [ armv7, armv7s, armv7k, arm64 ]\n"
  121. "platform: ios\n"
  122. "flags: [ flat_namespace, not_app_extension_safe ]\n"
  123. "install-name: Test.dylib\n"
  124. "swift-version: 1.1\n"
  125. "exports:\n"
  126. " - archs: [ armv7, armv7s, armv7k, arm64 ]\n"
  127. " symbols: [ _sym1, _sym2, _sym3, _sym4, $ld$hide$os9.0$_sym1 ]\n"
  128. " objc-classes: [ _class1, _class2 ]\n"
  129. " objc-ivars: [ _class1._ivar1, _class1._ivar2 ]\n"
  130. " weak-def-symbols: [ _weak1, _weak2 ]\n"
  131. " thread-local-symbols: [ _tlv1, _tlv2 ]\n"
  132. " - archs: [ armv7, armv7s, armv7k ]\n"
  133. " symbols: [ _sym5 ]\n"
  134. " objc-classes: [ _class3 ]\n"
  135. " objc-ivars: [ _class1._ivar3 ]\n"
  136. " weak-def-symbols: [ _weak3 ]\n"
  137. " thread-local-symbols: [ _tlv3 ]\n"
  138. "undefineds:\n"
  139. " - archs: [ armv7, armv7s, armv7k, arm64 ]\n"
  140. " symbols: [ _undefSym1, _undefSym2, _undefSym3 ]\n"
  141. " objc-classes: [ _undefClass1, _undefClass2 ]\n"
  142. " objc-ivars: [ _undefClass1._ivar1, _undefClass1._ivar2 ]\n"
  143. " weak-ref-symbols: [ _undefWeak1, _undefWeak2 ]\n"
  144. "...\n";
  145. auto Result = TextAPIReader::get(MemoryBufferRef(tbd_v2_file2, "Test.tbd"));
  146. EXPECT_TRUE(!!Result);
  147. auto File = std::move(Result.get());
  148. EXPECT_EQ(FileType::TBD_V2, File->getFileType());
  149. auto Archs = AK_armv7 | AK_armv7s | AK_armv7k | AK_arm64;
  150. auto Platform = PlatformKind::iOS;
  151. TargetList Targets;
  152. for (auto &&arch : Archs)
  153. Targets.emplace_back(Target(arch, Platform));
  154. EXPECT_EQ(Archs, File->getArchitectures());
  155. EXPECT_EQ(File->getPlatforms().size(), 1U);
  156. EXPECT_EQ(Platform, *File->getPlatforms().begin());
  157. EXPECT_EQ(std::string("Test.dylib"), File->getInstallName());
  158. EXPECT_EQ(PackedVersion(1, 0, 0), File->getCurrentVersion());
  159. EXPECT_EQ(PackedVersion(1, 0, 0), File->getCompatibilityVersion());
  160. EXPECT_EQ(2U, File->getSwiftABIVersion());
  161. EXPECT_EQ(ObjCConstraintType::Retain_Release, File->getObjCConstraint());
  162. EXPECT_FALSE(File->isTwoLevelNamespace());
  163. EXPECT_FALSE(File->isApplicationExtensionSafe());
  164. EXPECT_FALSE(File->isInstallAPI());
  165. EXPECT_EQ(0U, File->allowableClients().size());
  166. EXPECT_EQ(0U, File->reexportedLibraries().size());
  167. }
  168. TEST(TBDv2, WriteFile) {
  169. static const char tbd_v2_file3[] =
  170. "--- !tapi-tbd-v2\n"
  171. "archs: [ i386, x86_64 ]\n"
  172. "platform: macosx\n"
  173. "install-name: '/usr/lib/libfoo.dylib'\n"
  174. "current-version: 1.2.3\n"
  175. "compatibility-version: 0\n"
  176. "swift-version: 5\n"
  177. "exports:\n"
  178. " - archs: [ i386 ]\n"
  179. " symbols: [ _sym1 ]\n"
  180. " weak-def-symbols: [ _sym2 ]\n"
  181. " thread-local-symbols: [ _sym3 ]\n"
  182. " - archs: [ x86_64 ]\n"
  183. " allowable-clients: [ clientA ]\n"
  184. " re-exports: [ '/usr/lib/libfoo.dylib' ]\n"
  185. " symbols: [ '_OBJC_EHTYPE_$_Class1' ]\n"
  186. " objc-classes: [ _Class1 ]\n"
  187. " objc-ivars: [ _Class1._ivar1 ]\n"
  188. "...\n";
  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. SmallString<4096> Buffer;
  214. raw_svector_ostream OS(Buffer);
  215. auto Result = TextAPIWriter::writeToStream(OS, File);
  216. EXPECT_FALSE(Result);
  217. EXPECT_STREQ(tbd_v2_file3, Buffer.c_str());
  218. }
  219. TEST(TBDv2, Platform_macOS) {
  220. static const char tbd_v1_platform_macos[] = "--- !tapi-tbd-v2\n"
  221. "archs: [ x86_64 ]\n"
  222. "platform: macosx\n"
  223. "install-name: Test.dylib\n"
  224. "...\n";
  225. auto Result =
  226. TextAPIReader::get(MemoryBufferRef(tbd_v1_platform_macos, "Test.tbd"));
  227. EXPECT_TRUE(!!Result);
  228. auto File = std::move(Result.get());
  229. auto Platform = PlatformKind::macOS;
  230. EXPECT_EQ(FileType::TBD_V2, File->getFileType());
  231. EXPECT_EQ(File->getPlatforms().size(), 1U);
  232. EXPECT_EQ(Platform, *File->getPlatforms().begin());
  233. }
  234. TEST(TBDv2, Platform_iOS) {
  235. static const char tbd_v1_platform_ios[] = "--- !tapi-tbd-v2\n"
  236. "archs: [ arm64 ]\n"
  237. "platform: ios\n"
  238. "install-name: Test.dylib\n"
  239. "...\n";
  240. auto Result =
  241. TextAPIReader::get(MemoryBufferRef(tbd_v1_platform_ios, "Test.tbd"));
  242. EXPECT_TRUE(!!Result);
  243. auto Platform = PlatformKind::iOS;
  244. auto File = std::move(Result.get());
  245. EXPECT_EQ(FileType::TBD_V2, File->getFileType());
  246. EXPECT_EQ(File->getPlatforms().size(), 1U);
  247. EXPECT_EQ(Platform, *File->getPlatforms().begin());
  248. }
  249. TEST(TBDv2, Platform_watchOS) {
  250. static const char tbd_v1_platform_watchos[] = "--- !tapi-tbd-v2\n"
  251. "archs: [ armv7k ]\n"
  252. "platform: watchos\n"
  253. "install-name: Test.dylib\n"
  254. "...\n";
  255. auto Result =
  256. TextAPIReader::get(MemoryBufferRef(tbd_v1_platform_watchos, "Test.tbd"));
  257. EXPECT_TRUE(!!Result);
  258. auto Platform = PlatformKind::watchOS;
  259. auto File = std::move(Result.get());
  260. EXPECT_EQ(FileType::TBD_V2, File->getFileType());
  261. EXPECT_EQ(File->getPlatforms().size(), 1U);
  262. EXPECT_EQ(Platform, *File->getPlatforms().begin());
  263. }
  264. TEST(TBDv2, Platform_tvOS) {
  265. static const char tbd_v1_platform_tvos[] = "--- !tapi-tbd-v2\n"
  266. "archs: [ arm64 ]\n"
  267. "platform: tvos\n"
  268. "install-name: Test.dylib\n"
  269. "...\n";
  270. auto Result =
  271. TextAPIReader::get(MemoryBufferRef(tbd_v1_platform_tvos, "Test.tbd"));
  272. EXPECT_TRUE(!!Result);
  273. auto Platform = PlatformKind::tvOS;
  274. auto File = std::move(Result.get());
  275. EXPECT_EQ(FileType::TBD_V2, File->getFileType());
  276. EXPECT_EQ(File->getPlatforms().size(), 1U);
  277. EXPECT_EQ(Platform, *File->getPlatforms().begin());
  278. }
  279. TEST(TBDv2, Platform_bridgeOS) {
  280. static const char tbd_v1_platform_bridgeos[] = "--- !tapi-tbd-v2\n"
  281. "archs: [ armv7k ]\n"
  282. "platform: bridgeos\n"
  283. "install-name: Test.dylib\n"
  284. "...\n";
  285. auto Result =
  286. TextAPIReader::get(MemoryBufferRef(tbd_v1_platform_bridgeos, "Test.tbd"));
  287. EXPECT_TRUE(!!Result);
  288. auto Platform = PlatformKind::bridgeOS;
  289. auto File = std::move(Result.get());
  290. EXPECT_EQ(FileType::TBD_V2, File->getFileType());
  291. EXPECT_EQ(File->getPlatforms().size(), 1U);
  292. EXPECT_EQ(Platform, *File->getPlatforms().begin());
  293. }
  294. TEST(TBDv2, Swift_1_0) {
  295. static const char tbd_v1_swift_1_0[] = "--- !tapi-tbd-v2\n"
  296. "archs: [ arm64 ]\n"
  297. "platform: ios\n"
  298. "install-name: Test.dylib\n"
  299. "swift-version: 1.0\n"
  300. "...\n";
  301. auto Result =
  302. TextAPIReader::get(MemoryBufferRef(tbd_v1_swift_1_0, "Test.tbd"));
  303. EXPECT_TRUE(!!Result);
  304. auto File = std::move(Result.get());
  305. EXPECT_EQ(FileType::TBD_V2, File->getFileType());
  306. EXPECT_EQ(1U, File->getSwiftABIVersion());
  307. }
  308. TEST(TBDv2, Swift_1_1) {
  309. static const char tbd_v1_swift_1_1[] = "--- !tapi-tbd-v2\n"
  310. "archs: [ arm64 ]\n"
  311. "platform: ios\n"
  312. "install-name: Test.dylib\n"
  313. "swift-version: 1.1\n"
  314. "...\n";
  315. auto Result =
  316. TextAPIReader::get(MemoryBufferRef(tbd_v1_swift_1_1, "Test.tbd"));
  317. EXPECT_TRUE(!!Result);
  318. auto File = std::move(Result.get());
  319. EXPECT_EQ(FileType::TBD_V2, File->getFileType());
  320. EXPECT_EQ(2U, File->getSwiftABIVersion());
  321. }
  322. TEST(TBDv2, Swift_2_0) {
  323. static const char tbd_v1_swift_2_0[] = "--- !tapi-tbd-v2\n"
  324. "archs: [ arm64 ]\n"
  325. "platform: ios\n"
  326. "install-name: Test.dylib\n"
  327. "swift-version: 2.0\n"
  328. "...\n";
  329. auto Result =
  330. TextAPIReader::get(MemoryBufferRef(tbd_v1_swift_2_0, "Test.tbd"));
  331. EXPECT_TRUE(!!Result);
  332. auto File = std::move(Result.get());
  333. EXPECT_EQ(FileType::TBD_V2, File->getFileType());
  334. EXPECT_EQ(3U, File->getSwiftABIVersion());
  335. }
  336. TEST(TBDv2, Swift_3_0) {
  337. static const char tbd_v1_swift_3_0[] = "--- !tapi-tbd-v2\n"
  338. "archs: [ arm64 ]\n"
  339. "platform: ios\n"
  340. "install-name: Test.dylib\n"
  341. "swift-version: 3.0\n"
  342. "...\n";
  343. auto Result =
  344. TextAPIReader::get(MemoryBufferRef(tbd_v1_swift_3_0, "Test.tbd"));
  345. EXPECT_TRUE(!!Result);
  346. auto File = std::move(Result.get());
  347. EXPECT_EQ(FileType::TBD_V2, File->getFileType());
  348. EXPECT_EQ(4U, File->getSwiftABIVersion());
  349. }
  350. TEST(TBDv2, Swift_4_0) {
  351. static const char tbd_v1_swift_4_0[] = "--- !tapi-tbd-v2\n"
  352. "archs: [ arm64 ]\n"
  353. "platform: ios\n"
  354. "install-name: Test.dylib\n"
  355. "swift-version: 4.0\n"
  356. "...\n";
  357. auto Result =
  358. TextAPIReader::get(MemoryBufferRef(tbd_v1_swift_4_0, "Test.tbd"));
  359. EXPECT_FALSE(!!Result);
  360. auto errorMessage = toString(Result.takeError());
  361. EXPECT_EQ("malformed file\nTest.tbd:5:16: error: invalid Swift ABI "
  362. "version.\nswift-version: 4.0\n ^~~\n",
  363. errorMessage);
  364. }
  365. TEST(TBDv2, Swift_5) {
  366. static const char tbd_v1_swift_5[] = "--- !tapi-tbd-v2\n"
  367. "archs: [ arm64 ]\n"
  368. "platform: ios\n"
  369. "install-name: Test.dylib\n"
  370. "swift-version: 5\n"
  371. "...\n";
  372. auto Result = TextAPIReader::get(MemoryBufferRef(tbd_v1_swift_5, "Test.tbd"));
  373. EXPECT_TRUE(!!Result);
  374. auto File = std::move(Result.get());
  375. EXPECT_EQ(FileType::TBD_V2, File->getFileType());
  376. EXPECT_EQ(5U, File->getSwiftABIVersion());
  377. }
  378. TEST(TBDv2, Swift_99) {
  379. static const char tbd_v1_swift_99[] = "--- !tapi-tbd-v2\n"
  380. "archs: [ arm64 ]\n"
  381. "platform: ios\n"
  382. "install-name: Test.dylib\n"
  383. "swift-version: 99\n"
  384. "...\n";
  385. auto Result =
  386. TextAPIReader::get(MemoryBufferRef(tbd_v1_swift_99, "Test.tbd"));
  387. EXPECT_TRUE(!!Result);
  388. auto File = std::move(Result.get());
  389. EXPECT_EQ(FileType::TBD_V2, File->getFileType());
  390. EXPECT_EQ(99U, File->getSwiftABIVersion());
  391. }
  392. TEST(TBDv2, UnknownArchitecture) {
  393. static const char tbd_v2_file_unknown_architecture[] =
  394. "--- !tapi-tbd-v2\n"
  395. "archs: [ foo ]\n"
  396. "platform: macosx\n"
  397. "install-name: Test.dylib\n"
  398. "...\n";
  399. auto Result = TextAPIReader::get(
  400. MemoryBufferRef(tbd_v2_file_unknown_architecture, "Test.tbd"));
  401. EXPECT_TRUE(!!Result);
  402. }
  403. TEST(TBDv2, UnknownPlatform) {
  404. static const char tbd_v2_file_unknown_platform[] = "--- !tapi-tbd-v2\n"
  405. "archs: [ i386 ]\n"
  406. "platform: newOS\n"
  407. "...\n";
  408. auto Result = TextAPIReader::get(
  409. MemoryBufferRef(tbd_v2_file_unknown_platform, "Test.tbd"));
  410. EXPECT_FALSE(!!Result);
  411. auto errorMessage = toString(Result.takeError());
  412. EXPECT_EQ("malformed file\nTest.tbd:3:11: error: unknown platform\nplatform: "
  413. "newOS\n ^~~~~\n",
  414. errorMessage);
  415. }
  416. TEST(TBDv2, InvalidPlatform) {
  417. static const char tbd_v2_file_invalid_platform[] =
  418. "--- !tapi-tbd-v2\n"
  419. "archs: [ i386 ]\n"
  420. "platform: iosmac\n"
  421. "install-name: Test.dylib\n"
  422. "...\n";
  423. auto Result = TextAPIReader::get(
  424. MemoryBufferRef(tbd_v2_file_invalid_platform, "Test.tbd"));
  425. EXPECT_FALSE(!!Result);
  426. auto errorMessage = toString(Result.takeError());
  427. EXPECT_EQ("malformed file\nTest.tbd:3:11: error: invalid platform\nplatform: "
  428. "iosmac\n ^~~~~~\n",
  429. errorMessage);
  430. }
  431. TEST(TBDv2, MalformedFile1) {
  432. static const char malformed_file1[] = "--- !tapi-tbd-v2\n"
  433. "archs: [ arm64 ]\n"
  434. "foobar: \"Unsupported key\"\n"
  435. "...\n";
  436. auto Result =
  437. TextAPIReader::get(MemoryBufferRef(malformed_file1, "Test.tbd"));
  438. EXPECT_FALSE(!!Result);
  439. auto errorMessage = toString(Result.takeError());
  440. ASSERT_EQ("malformed file\nTest.tbd:2:1: error: missing required key "
  441. "'platform'\narchs: [ arm64 ]\n^\n",
  442. errorMessage);
  443. }
  444. TEST(TBDv2, MalformedFile2) {
  445. static const char malformed_file2[] = "--- !tapi-tbd-v2\n"
  446. "archs: [ arm64 ]\n"
  447. "platform: ios\n"
  448. "install-name: Test.dylib\n"
  449. "foobar: \"Unsupported key\"\n"
  450. "...\n";
  451. auto Result =
  452. TextAPIReader::get(MemoryBufferRef(malformed_file2, "Test.tbd"));
  453. EXPECT_FALSE(!!Result);
  454. auto errorMessage = toString(Result.takeError());
  455. ASSERT_EQ(
  456. "malformed file\nTest.tbd:5:9: error: unknown key 'foobar'\nfoobar: "
  457. "\"Unsupported key\"\n ^~~~~~~~~~~~~~~~~\n",
  458. errorMessage);
  459. }
  460. } // namespace TBDv2