|
| 1 | +//===----------------------------------------------------------------------===// |
| 2 | +// |
| 3 | +// This source file is part of the Swift W3C Trace Context open source project |
| 4 | +// |
| 5 | +// Copyright (c) YEARS Moritz Lang and the Swift W3C Trace Context project authors |
| 6 | +// Licensed under Apache License v2.0 |
| 7 | +// |
| 8 | +// See LICENSE.txt for license information |
| 9 | +// |
| 10 | +// SPDX-License-Identifier: Apache-2.0 |
| 11 | +// |
| 12 | +//===----------------------------------------------------------------------===// |
| 13 | + |
| 14 | +@testable import W3CTraceContext |
| 15 | +import XCTest |
| 16 | + |
| 17 | +final class TraceParentRawRepresentableTests: XCTestCase { |
| 18 | + // MARK: - Trace Parent - |
| 19 | + |
| 20 | + func testDecodeValidTraceParentStringWithSampledFlag() { |
| 21 | + let rawValue = "00-0af7651916cd43dd8448eb211c80319c-b7ad6b7169203331-01" |
| 22 | + guard let traceParent = W3C.TraceParent(rawValue: rawValue) else { |
| 23 | + XCTFail("Could not decode valid trace parent") |
| 24 | + return |
| 25 | + } |
| 26 | + |
| 27 | + XCTAssertEqual( |
| 28 | + traceParent, |
| 29 | + W3C.TraceParent(traceID: "0af7651916cd43dd8448eb211c80319c", parentID: "b7ad6b7169203331", traceFlags: "01") |
| 30 | + ) |
| 31 | + |
| 32 | + XCTAssert(traceParent.sampled) |
| 33 | + } |
| 34 | + |
| 35 | + func testDecodeValidTraceParentStringWithoutSampledFlag() { |
| 36 | + let rawValue = "00-0af7651916cd43dd8448eb211c80319c-b7ad6b7169203331-00" |
| 37 | + guard let traceParent = W3C.TraceParent(rawValue: rawValue) else { |
| 38 | + XCTFail("Could not decode valid trace parent") |
| 39 | + return |
| 40 | + } |
| 41 | + |
| 42 | + XCTAssertEqual( |
| 43 | + traceParent, |
| 44 | + W3C.TraceParent(traceID: "0af7651916cd43dd8448eb211c80319c", parentID: "b7ad6b7169203331", traceFlags: "00") |
| 45 | + ) |
| 46 | + |
| 47 | + XCTAssertFalse(traceParent.sampled) |
| 48 | + } |
| 49 | + |
| 50 | + func testEncodesToValidRawValue() { |
| 51 | + let traceParent = W3C.TraceParent( |
| 52 | + traceID: "0af7651916cd43dd8448eb211c80319c", |
| 53 | + parentID: "b7ad6b7169203331", |
| 54 | + traceFlags: "01" |
| 55 | + ) |
| 56 | + |
| 57 | + XCTAssertEqual(traceParent.rawValue, "00-0af7651916cd43dd8448eb211c80319c-b7ad6b7169203331-01") |
| 58 | + } |
| 59 | + |
| 60 | + func testDecodeFailsWithTooManyComponents() { |
| 61 | + let rawValue = "00-0af7651916cd43dd8448eb211c80319c-b7ad6b7169203331-01-additional-components" |
| 62 | + XCTAssertUninitializedTraceParent(rawValue) |
| 63 | + } |
| 64 | + |
| 65 | + func testDecodeFailsWithTooFewComponents() { |
| 66 | + let rawValue = "00-0af7651916cd43dd8448eb211c80319c" |
| 67 | + XCTAssertUninitializedTraceParent(rawValue) |
| 68 | + } |
| 69 | + |
| 70 | + func testDecodeFailsWithInvalidVersionComponent() { |
| 71 | + let rawValue = "ff-0af7651916cd43dd8448eb211c80319c-b7ad6b7169203331-01" |
| 72 | + XCTAssertUninitializedTraceParent(rawValue) |
| 73 | + } |
| 74 | + |
| 75 | + func testDecodeFailsWithTooLongTraceID() { |
| 76 | + let rawValue = "00-0af7651916cd43dd8448eb211c80319cclearlylongerthan16bytes-b7ad6b7169203331-01" |
| 77 | + XCTAssertUninitializedTraceParent(rawValue) |
| 78 | + } |
| 79 | + |
| 80 | + func testDecodeFailsWithTooShortTraceID() { |
| 81 | + let rawValue = "00-tooshort-b7ad6b7169203331-01" |
| 82 | + XCTAssertUninitializedTraceParent(rawValue) |
| 83 | + } |
| 84 | + |
| 85 | + func testDecodeFailsWithAllZeroesTraceID() { |
| 86 | + let rawValue = "00-00000000000000000000000000000000-b7ad6b7169203331-01" |
| 87 | + XCTAssertUninitializedTraceParent(rawValue) |
| 88 | + } |
| 89 | + |
| 90 | + func testDecodeFailsWithTooShortParentID() { |
| 91 | + let rawValue = "00-0af7651916cd43dd8448eb211c80319c-tooshort-01" |
| 92 | + XCTAssertUninitializedTraceParent(rawValue) |
| 93 | + } |
| 94 | + |
| 95 | + func testDecodeFailsWithAllZeroesParentID() { |
| 96 | + let rawValue = "00-0af7651916cd43dd8448eb211c80319c-0000000000000000-01" |
| 97 | + XCTAssertUninitializedTraceParent(rawValue) |
| 98 | + } |
| 99 | + |
| 100 | + func testDecodeFailsWithTooLongTraceFlags() { |
| 101 | + let rawValue = "00-0af7651916cd43dd8448eb211c80319c-b7ad6b7169203331-toolong" |
| 102 | + XCTAssertUninitializedTraceParent(rawValue) |
| 103 | + } |
| 104 | + |
| 105 | + func testDecodeFailsWithTooShortTraceFlags() { |
| 106 | + let rawValue = "00-0af7651916cd43dd8448eb211c80319c-b7ad6b7169203331-0" |
| 107 | + XCTAssertUninitializedTraceParent(rawValue) |
| 108 | + } |
| 109 | + |
| 110 | + // TODO: Trace State |
| 111 | +} |
| 112 | + |
| 113 | +private func XCTAssertUninitializedTraceParent(_ rawValue: String, file: StaticString = #file, line: UInt = #line) { |
| 114 | + if let traceParent = W3C.TraceParent(rawValue: rawValue) { |
| 115 | + XCTFail( |
| 116 | + "Expected trace parent not to be initialized from invalid raw value: \(traceParent)", |
| 117 | + file: file, |
| 118 | + line: line |
| 119 | + ) |
| 120 | + } |
| 121 | +} |
0 commit comments