From 04f26b8b627fbc88e75e7b0e95ccb87898c84f15 Mon Sep 17 00:00:00 2001 From: Euan Harris Date: Thu, 19 Dec 2024 10:10:15 +0000 Subject: [PATCH] tar: Add unit tests for helper functions Test the underlying helper methods used to write tar headers. --- Package.swift | 1 + Tests/TarTests/TarTests.swift | 78 +++++++++++++++++++++++++++++++++++ 2 files changed, 79 insertions(+) create mode 100644 Tests/TarTests/TarTests.swift diff --git a/Package.swift b/Package.swift index 2328623..195c639 100644 --- a/Package.swift +++ b/Package.swift @@ -87,6 +87,7 @@ let package = Package( dependencies: [.target(name: "ContainerRegistry")], resources: [.process("Resources")] ), .testTarget(name: "containertoolTests", dependencies: [.target(name: "containertool")]), + .testTarget(name: "TarTests", dependencies: [.target(name: "Tar")]), ], swiftLanguageModes: [.v6] ) diff --git a/Tests/TarTests/TarTests.swift b/Tests/TarTests/TarTests.swift new file mode 100644 index 0000000..04a6ad8 --- /dev/null +++ b/Tests/TarTests/TarTests.swift @@ -0,0 +1,78 @@ +//===----------------------------------------------------------------------===// +// +// This source file is part of the SwiftContainerPlugin open source project +// +// Copyright (c) 2024 Apple Inc. and the SwiftContainerPlugin project authors +// Licensed under Apache License v2.0 +// +// See LICENSE.txt for license information +// See CONTRIBUTORS.txt for the list of SwiftContainerPlugin project authors +// +// SPDX-License-Identifier: Apache-2.0 +// +//===----------------------------------------------------------------------===// + +import Foundation +import Testing + +@testable import Tar + +let blocksize = 512 +let headerLen = blocksize +let trailerLen = 2 * blocksize + +@Suite struct TarUnitTests { + @Test(arguments: [ + (input: 0o000, expected: "000000"), + (input: 0o555, expected: "000555"), + (input: 0o750, expected: "000750"), + (input: 0o777, expected: "000777"), + (input: 0o1777, expected: "001777"), + ]) + func testOctal6(input: Int, expected: String) async throws { + #expect(octal6(input) == expected) + } + + @Test(arguments: [ + (input: 0, expected: "00000000000"), + (input: 1024, expected: "00000002000"), + (input: 0o2000, expected: "00000002000"), + (input: 1024 * 1024, expected: "00004000000"), + ]) + func testOctal11(input: Int, expected: String) async throws { + #expect(octal11(input) == expected) + } + + @Test func testUInt8writeString() async throws { + // Fill the buffer with 0xFF to show null termination + var hdr = [UInt8](repeating: 255, count: 21) + + // The typechecker timed out when these test cases were passed as arguments, in the style of the octal tests + hdr.writeString("abc", inField: 0..<5, withTermination: .none) + #expect( + hdr == [ + 97, 98, 99, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, + ] + ) + + hdr.writeString("def", inField: 3..<7, withTermination: .null) + #expect( + hdr == [97, 98, 99, 100, 101, 102, 0, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255] + ) + + hdr.writeString("ghi", inField: 7..<11, withTermination: .space) + #expect( + hdr == [97, 98, 99, 100, 101, 102, 0, 103, 104, 105, 32, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255] + ) + + hdr.writeString("jkl", inField: 11..<16, withTermination: .nullAndSpace) + #expect( + hdr == [97, 98, 99, 100, 101, 102, 0, 103, 104, 105, 32, 106, 107, 108, 0, 32, 255, 255, 255, 255, 255] + ) + + hdr.writeString("mno", inField: 16..<21, withTermination: .spaceAndNull) + #expect( + hdr == [97, 98, 99, 100, 101, 102, 0, 103, 104, 105, 32, 106, 107, 108, 0, 32, 109, 110, 111, 32, 0] + ) + } +}