-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
tar: Add unit tests for helper functions
Test the underlying helper methods used to write tar headers.
- Loading branch information
Showing
2 changed files
with
79 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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] | ||
) | ||
} | ||
} |