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