Skip to content

Commit 2de7109

Browse files
committed
tar: Add unit tests for helper functions
Test the underlying helper methods used to write tar headers.
1 parent 1639f45 commit 2de7109

File tree

2 files changed

+79
-0
lines changed

2 files changed

+79
-0
lines changed

Package.swift

+1
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ let package = Package(
8787
dependencies: [.target(name: "ContainerRegistry")],
8888
resources: [.process("Resources")]
8989
), .testTarget(name: "containertoolTests", dependencies: [.target(name: "containertool")]),
90+
.testTarget(name: "TarTests", dependencies: [.target(name: "Tar")]),
9091
],
9192
swiftLanguageModes: [.v6]
9293
)

Tests/TarTests/TarUnitTests.swift

+78
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// This source file is part of the SwiftContainerPlugin open source project
4+
//
5+
// Copyright (c) 2024 Apple Inc. and the SwiftContainerPlugin project authors
6+
// Licensed under Apache License v2.0
7+
//
8+
// See LICENSE.txt for license information
9+
// See CONTRIBUTORS.txt for the list of SwiftContainerPlugin project authors
10+
//
11+
// SPDX-License-Identifier: Apache-2.0
12+
//
13+
//===----------------------------------------------------------------------===//
14+
15+
import Foundation
16+
import Testing
17+
18+
@testable import Tar
19+
20+
let blocksize = 512
21+
let headerLen = blocksize
22+
let trailerLen = 2 * blocksize
23+
24+
@Suite struct TarUnitTests {
25+
@Test(arguments: [
26+
(input: 0o000, expected: "000000"),
27+
(input: 0o555, expected: "000555"),
28+
(input: 0o750, expected: "000750"),
29+
(input: 0o777, expected: "000777"),
30+
(input: 0o1777, expected: "001777"),
31+
])
32+
func testOctal6(input: Int, expected: String) async throws {
33+
#expect(octal6(input) == expected)
34+
}
35+
36+
@Test(arguments: [
37+
(input: 0, expected: "00000000000"),
38+
(input: 1024, expected: "00000002000"),
39+
(input: 0o2000, expected: "00000002000"),
40+
(input: 1024 * 1024, expected: "00004000000"),
41+
])
42+
func testOctal11(input: Int, expected: String) async throws {
43+
#expect(octal11(input) == expected)
44+
}
45+
46+
@Test func testUInt8writeString() async throws {
47+
// Fill the buffer with 0xFF to show null termination
48+
var hdr = [UInt8](repeating: 255, count: 21)
49+
50+
// The typechecker timed out when these test cases were passed as arguments, in the style of the octal tests
51+
hdr.writeString("abc", inField: 0..<5, withTermination: .none)
52+
#expect(
53+
hdr == [
54+
97, 98, 99, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
55+
]
56+
)
57+
58+
hdr.writeString("def", inField: 3..<7, withTermination: .null)
59+
#expect(
60+
hdr == [97, 98, 99, 100, 101, 102, 0, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255]
61+
)
62+
63+
hdr.writeString("ghi", inField: 7..<11, withTermination: .space)
64+
#expect(
65+
hdr == [97, 98, 99, 100, 101, 102, 0, 103, 104, 105, 32, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255]
66+
)
67+
68+
hdr.writeString("jkl", inField: 11..<16, withTermination: .nullAndSpace)
69+
#expect(
70+
hdr == [97, 98, 99, 100, 101, 102, 0, 103, 104, 105, 32, 106, 107, 108, 0, 32, 255, 255, 255, 255, 255]
71+
)
72+
73+
hdr.writeString("mno", inField: 16..<21, withTermination: .spaceAndNull)
74+
#expect(
75+
hdr == [97, 98, 99, 100, 101, 102, 0, 103, 104, 105, 32, 106, 107, 108, 0, 32, 109, 110, 111, 32, 0]
76+
)
77+
}
78+
}

0 commit comments

Comments
 (0)