|
| 1 | +//===----------------------------------------------------------------------===// |
| 2 | +// |
| 3 | +// This source file is part of the Swift Binary Parsing open source project |
| 4 | +// |
| 5 | +// Copyright (c) 2025 Apple Inc. and the Swift project authors |
| 6 | +// Licensed under Apache License v2.0 with Runtime Library Exception |
| 7 | +// |
| 8 | +// See https://swift.org/LICENSE.txt for license information |
| 9 | +// |
| 10 | +//===----------------------------------------------------------------------===// |
| 11 | + |
| 12 | +import BinaryParsing |
| 13 | +import BinaryParsingMacros |
| 14 | +import MacroTesting |
| 15 | +import Testing |
| 16 | + |
| 17 | +@Suite( |
| 18 | + .macros(macros: ["magic": MagicMacro.self]) |
| 19 | +) |
| 20 | +struct MagicMacroTests { |
| 21 | + // MARK: Macro expansion tests |
| 22 | + @Test |
| 23 | + func magicStringAsciiOnly() { |
| 24 | + assertMacro { |
| 25 | + #"try #magic("test", parsing: &data)"# |
| 26 | + } expansion: { |
| 27 | + "try _loadAndCheckInlineArrayBytes(parsing: &data, expectedBytes: [116, 101, 115, 116])" |
| 28 | + } |
| 29 | + } |
| 30 | + |
| 31 | + @Test |
| 32 | + func magicStringLong() { |
| 33 | + assertMacro { |
| 34 | + #"try #magic("hello world", parsing: &data)"# |
| 35 | + } expansion: { |
| 36 | + "try _loadAndCheckInlineArrayBytes(parsing: &data, expectedBytes: [104, 101, 108, 108, 111, 32, 119, 111, 114, 108, 100])" |
| 37 | + } |
| 38 | + } |
| 39 | + |
| 40 | + @Test |
| 41 | + func magicStringSingleByte() { |
| 42 | + assertMacro { |
| 43 | + #"try #magic("A", parsing: &data)"# |
| 44 | + } expansion: { |
| 45 | + "try _loadAndCheckInlineArrayBytes(parsing: &data, expectedBytes: [65])" |
| 46 | + } |
| 47 | + } |
| 48 | + |
| 49 | + @Test |
| 50 | + func magicStringWithLiteralBackslashN() { |
| 51 | + // Note: \n is treated as literal backslash + n characters, not a newline |
| 52 | + assertMacro { |
| 53 | + #"try #magic("hello\nworld", parsing: &data)"# |
| 54 | + } expansion: { |
| 55 | + "try _loadAndCheckInlineArrayBytes(parsing: &data, expectedBytes: [104, 101, 108, 108, 111, 92, 110, 119, 111, 114, 108, 100])" |
| 56 | + } |
| 57 | + } |
| 58 | + |
| 59 | + @Test |
| 60 | + func magicStringEmpty() { |
| 61 | + assertMacro { |
| 62 | + #"try #magic("", parsing: &data)"# |
| 63 | + } diagnostics: { |
| 64 | + """ |
| 65 | + try #magic("", parsing: &data) |
| 66 | + ┬───────────────────────── |
| 67 | + ╰─ 🛑 Magic bytes string cannot be empty. |
| 68 | + """ |
| 69 | + } |
| 70 | + } |
| 71 | + |
| 72 | + @Test |
| 73 | + func magicCustomParsingArgument() { |
| 74 | + assertMacro { |
| 75 | + #"try #magic("test", parsing: &mySpan)"# |
| 76 | + } expansion: { |
| 77 | + "try _loadAndCheckInlineArrayBytes(parsing: &mySpan, expectedBytes: [116, 101, 115, 116])" |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + // MARK: End-to-end runtime tests |
| 82 | + @available(macOS 26, iOS 26, watchOS 26, tvOS 26, visionOS 26, *) |
| 83 | + @Test |
| 84 | + func magicEndToEndMatching() throws { |
| 85 | + let testBytes: [UInt8] = [116, 101, 115, 116] // "test" |
| 86 | + |
| 87 | + try testBytes.withParserSpan { span in |
| 88 | + // This should succeed - bytes match |
| 89 | + try #magic("test", parsing: &span) |
| 90 | + #expect(span.count == 0) |
| 91 | + } |
| 92 | + } |
| 93 | + |
| 94 | + @available(macOS 26, iOS 26, watchOS 26, tvOS 26, visionOS 26, *) |
| 95 | + @Test |
| 96 | + func magicEndToEndMismatched() throws { |
| 97 | + let wrongBytes: [UInt8] = [74, 80, 69, 71] // "JPEG" |
| 98 | + |
| 99 | + wrongBytes.withParserSpan { span in |
| 100 | + // This should fail - bytes don't match |
| 101 | + #expect(throws: ParsingError.self) { |
| 102 | + try #magic("test", parsing: &span) |
| 103 | + } |
| 104 | + // Span should still be consumed even though comparison failed |
| 105 | + #expect(span.count == 0) |
| 106 | + } |
| 107 | + } |
| 108 | + |
| 109 | + @available(macOS 26, iOS 26, watchOS 26, tvOS 26, visionOS 26, *) |
| 110 | + @Test |
| 111 | + func magicEndToEndLongString() throws { |
| 112 | + let longTestBytes: [UInt8] = Array("hello world".utf8) |
| 113 | + |
| 114 | + try longTestBytes.withParserSpan { span in |
| 115 | + // Test arbitrary length support |
| 116 | + try #magic("hello world", parsing: &span) |
| 117 | + #expect(span.count == 0) |
| 118 | + } |
| 119 | + } |
| 120 | + |
| 121 | + @available(macOS 26, iOS 26, watchOS 26, tvOS 26, visionOS 26, *) |
| 122 | + @Test |
| 123 | + func magicEndToEndInsufficientBytes() throws { |
| 124 | + let shortBytes: [UInt8] = [116, 101] // "te" (only 2 bytes) |
| 125 | + |
| 126 | + shortBytes.withParserSpan { span in |
| 127 | + // This should fail - not enough bytes |
| 128 | + #expect(throws: ParsingError.self) { |
| 129 | + try #magic("test", parsing: &span) // needs 4 bytes |
| 130 | + } |
| 131 | + } |
| 132 | + } |
| 133 | +} |
0 commit comments