|
| 1 | +@_spi(Private) @testable import Sentry |
| 2 | +@_spi(Private) import SentryTestUtils |
| 3 | +import XCTest |
| 4 | + |
| 5 | +class SentryMobileProvisionParserTests: XCTestCase { |
| 6 | + // MARK: - Valid Content Tests |
| 7 | + |
| 8 | + func testInitWithValidEnterpriseContent() throws { |
| 9 | + let content = """ |
| 10 | + <?xml version="1.0" encoding="UTF-8"?> |
| 11 | + <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"> |
| 12 | + <plist version="1.0"> |
| 13 | + <dict> |
| 14 | + <key>ProvisionsAllDevices</key> |
| 15 | + <true/> |
| 16 | + <key>AppIDName</key> |
| 17 | + <string>Enterprise App</string> |
| 18 | + </dict> |
| 19 | + </plist> |
| 20 | + """ |
| 21 | + try createFileAndAssert(content) { path in |
| 22 | + let parser = SentryMobileProvisionParser(path) |
| 23 | + XCTAssertTrue(parser.mobileProvisionProfileProvisionsAllDevices) |
| 24 | + } |
| 25 | + } |
| 26 | + |
| 27 | + func testInitWithValidAdhocContent() throws { |
| 28 | + let content = """ |
| 29 | + <?xml version="1.0" encoding="UTF-8"?> |
| 30 | + <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"> |
| 31 | + <plist version="1.0"> |
| 32 | + <dict> |
| 33 | + <key>ProvisionsAllDevices</key> |
| 34 | + <false/> |
| 35 | + <key>AppIDName</key> |
| 36 | + <string>Adhoc App</string> |
| 37 | + <ProvisionedDevices> |
| 38 | + </ProvisionedDevices> |
| 39 | + </dict> |
| 40 | + </plist> |
| 41 | + """ |
| 42 | + |
| 43 | + try createFileAndAssert(content) { path in |
| 44 | + let parser = SentryMobileProvisionParser(path) |
| 45 | + XCTAssertFalse(parser.mobileProvisionProfileProvisionsAllDevices) |
| 46 | + } |
| 47 | + } |
| 48 | + |
| 49 | + func testInitWithContentWithoutProvisionsAllDevicesKey() throws { |
| 50 | + let content = """ |
| 51 | + <?xml version="1.0" encoding="UTF-8"?> |
| 52 | + <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"> |
| 53 | + <plist version="1.0"> |
| 54 | + <dict> |
| 55 | + <key>AppIDName</key> |
| 56 | + <string>No Provisions All Devices Key</string> |
| 57 | + <ProvisionedDevices> |
| 58 | + </ProvisionedDevices> |
| 59 | + </dict> |
| 60 | + </plist> |
| 61 | + """ |
| 62 | + |
| 63 | + try createFileAndAssert(content) { path in |
| 64 | + let parser = SentryMobileProvisionParser(path) |
| 65 | + XCTAssertFalse(parser.mobileProvisionProfileProvisionsAllDevices) |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + func testInitWithExtraDataInMobileprovisionfile() throws { |
| 70 | + let content = """ |
| 71 | + This is not a valid plist content |
| 72 | + <plist> |
| 73 | + <dict> |
| 74 | + <key>ProvisionsAllDevices</key> |
| 75 | + <true/> |
| 76 | + </dict> |
| 77 | + </plist> |
| 78 | + This isn't valid content either |
| 79 | + """ |
| 80 | + |
| 81 | + // The parser scans for the plist inside the profile, so it should find it |
| 82 | + try createFileAndAssert(content) { path in |
| 83 | + let parser = SentryMobileProvisionParser(path) |
| 84 | + XCTAssertTrue(parser.mobileProvisionProfileProvisionsAllDevices) |
| 85 | + } |
| 86 | + } |
| 87 | + |
| 88 | + // MARK: - Invalid Content Tests |
| 89 | + |
| 90 | + func testInitWithEmptyContent() throws { |
| 91 | + try createFileAndAssert("") { path in |
| 92 | + let parser = SentryMobileProvisionParser(path) |
| 93 | + XCTAssertFalse(parser.mobileProvisionProfileProvisionsAllDevices) // Should default to false when parsing fails |
| 94 | + } |
| 95 | + } |
| 96 | + |
| 97 | + func testInitWithMalformedPlistContent() throws { |
| 98 | + let content = """ |
| 99 | + <plist> |
| 100 | + <dict> |
| 101 | + <key>ProvisionsAllDevices</key> |
| 102 | + <true/> |
| 103 | + </dict> |
| 104 | + """ |
| 105 | + |
| 106 | + try createFileAndAssert(content) { path in |
| 107 | + let parser = SentryMobileProvisionParser(path) |
| 108 | + XCTAssertFalse(parser.mobileProvisionProfileProvisionsAllDevices) // Should default to false when parsing fails |
| 109 | + } |
| 110 | + } |
| 111 | + |
| 112 | + func testInitWithMissingPlistTags() throws { |
| 113 | + let content = """ |
| 114 | + <dict> |
| 115 | + <key>ProvisionsAllDevices</key> |
| 116 | + <true/> |
| 117 | + </dict> |
| 118 | + """ |
| 119 | + |
| 120 | + try createFileAndAssert(content) { path in |
| 121 | + let parser = SentryMobileProvisionParser(path) |
| 122 | + XCTAssertFalse(parser.mobileProvisionProfileProvisionsAllDevices) // Should default to false when parsing fails |
| 123 | + } |
| 124 | + } |
| 125 | + |
| 126 | + // MARK: - Edge Cases |
| 127 | + |
| 128 | + func testProvisionsAllDevicesWithNonBooleanValue() throws { |
| 129 | + let content = """ |
| 130 | + <?xml version="1.0" encoding="UTF-8"?> |
| 131 | + <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"> |
| 132 | + <plist version="1.0"> |
| 133 | + <dict> |
| 134 | + <key>ProvisionsAllDevices</key> |
| 135 | + <string>true</string> |
| 136 | + </dict> |
| 137 | + </plist> |
| 138 | + """ |
| 139 | + |
| 140 | + try createFileAndAssert(content) { path in |
| 141 | + let parser = SentryMobileProvisionParser(path) |
| 142 | + XCTAssertFalse(parser.mobileProvisionProfileProvisionsAllDevices) // Should default to false when value is not boolean |
| 143 | + } |
| 144 | + } |
| 145 | + |
| 146 | + func testProvisionsAllDevicesWithArrayValue() throws { |
| 147 | + let content = """ |
| 148 | + <?xml version="1.0" encoding="UTF-8"?> |
| 149 | + <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"> |
| 150 | + <plist version="1.0"> |
| 151 | + <dict> |
| 152 | + <key>ProvisionsAllDevices</key> |
| 153 | + <array> |
| 154 | + <string>true</string> |
| 155 | + </array> |
| 156 | + </dict> |
| 157 | + </plist> |
| 158 | + """ |
| 159 | + |
| 160 | + try createFileAndAssert(content) { path in |
| 161 | + let parser = SentryMobileProvisionParser(path) |
| 162 | + XCTAssertFalse(parser.mobileProvisionProfileProvisionsAllDevices) // Should default to false when value is not boolean |
| 163 | + } |
| 164 | + } |
| 165 | + |
| 166 | + func testPathDoesNotExist() throws { |
| 167 | + let parser = SentryMobileProvisionParser("/randomPath.xml") |
| 168 | + XCTAssertFalse(parser.mobileProvisionProfileProvisionsAllDevices) |
| 169 | + } |
| 170 | + |
| 171 | + func testContentWithEmojiAndJapaneseCharacters() throws { |
| 172 | + // Create content with UTF-8 characters that cannot be represented in Latin-1 |
| 173 | + // These include emojis, Chinese characters, and other Unicode characters |
| 174 | + let content = """ |
| 175 | + <?xml version="1.0" encoding="UTF-8"?> |
| 176 | + <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"> |
| 177 | + <plist version="1.0"> |
| 178 | + <dict> |
| 179 | + <key>ProvisionsAllDevices</key> |
| 180 | + <true/> |
| 181 | + <key>AppIDName</key> |
| 182 | + <string>Test App with 中文 characters and 🚀 emojis</string> |
| 183 | + </dict> |
| 184 | + </plist> |
| 185 | + """ |
| 186 | + |
| 187 | + try createFileAndAssert(content) { path in |
| 188 | + let parser = SentryMobileProvisionParser(path) |
| 189 | + // Should default to false when Latin-1 conversion fails and plist cannot be extracted |
| 190 | + XCTAssertTrue(parser.mobileProvisionProfileProvisionsAllDevices) |
| 191 | + } |
| 192 | + } |
| 193 | + |
| 194 | + // MARK: - Utils |
| 195 | + private func createFileAndAssert(_ content: String, at fileName: String = #function, assertBlock: ((String) throws -> Void)) throws { |
| 196 | + let tmpPath = FileManager.default.temporaryDirectory.path |
| 197 | + let path = "\(tmpPath)\(fileName).tmp" |
| 198 | + try content.write(toFile: path, atomically: true, encoding: .utf8) |
| 199 | + |
| 200 | + try assertBlock(path) |
| 201 | + |
| 202 | + try FileManager.default.removeItem(atPath: path) |
| 203 | + } |
| 204 | +} |
0 commit comments