Skip to content

Commit

Permalink
tar: Add unit tests for helper functions
Browse files Browse the repository at this point in the history
Test the underlying helper methods used to write tar headers.
  • Loading branch information
euanh committed Dec 20, 2024
1 parent 1639f45 commit 04f26b8
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 0 deletions.
1 change: 1 addition & 0 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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]
)
78 changes: 78 additions & 0 deletions Tests/TarTests/TarTests.swift
Original file line number Diff line number Diff line change
@@ -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]
)
}
}

0 comments on commit 04f26b8

Please sign in to comment.