mirror of
https://github.com/apple/container.git
synced 2026-09-12 18:55:43 +00:00
+6








3407cc3f16
Co-authored-by: Aditya Ramani <a_ramani@apple.com> Co-authored-by: Agam Dua <agam_dua@apple.com> Co-authored-by: Danny Canter <danny_canter@apple.com> Co-authored-by: Dmitry Kovba <dkovba@apple.com> Co-authored-by: Eric Ernst <eric_ernst@apple.com> Co-authored-by: Evan Hazlett <ehazlett@apple.com> Co-authored-by: Gilbert Song <gilbertsong@apple.com> Co-authored-by: Hugh Bussell <hbussell@apple.com> Co-authored-by: John Logan <john_logan@apple.com> Co-authored-by: Kathryn Baldauf <k_baldauf@apple.com> Co-authored-by: Madhu Venugopal <mvenugopal@apple.com> Co-authored-by: Michael Crosby <michael_crosby@apple.com> Co-authored-by: Sidhartha Mani <sidhartha_mani@apple.com> Co-authored-by: Tanweer Noor <tnoor@apple.com> Co-authored-by: Ximena Perez Diaz <xperez528@gmail.com> Co-authored-by: Yibo Zhuang <yzhuang@apple.com>
93 lines
3.6 KiB
Swift
93 lines
3.6 KiB
Swift
//===----------------------------------------------------------------------===//
|
|
// Copyright © 2025 Apple Inc. and the containerization project authors. All rights reserved.
|
|
//
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
// you may not use this file except in compliance with the License.
|
|
// You may obtain a copy of the License at
|
|
//
|
|
// https://www.apache.org/licenses/LICENSE-2.0
|
|
//
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
// See the License for the specific language governing permissions and
|
|
// limitations under the License.
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
import Testing
|
|
|
|
@testable import ContainerizationNetlink
|
|
|
|
struct BufferTest {
|
|
@Test func testBufferBind() throws {
|
|
let expectedValue: UInt64 = 0x0102_0304_0506_0708
|
|
let expectedBuffer: [UInt8] = [
|
|
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
|
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
|
0x08, 0x07, 0x06, 0x05, 0x04, 0x03, 0x02, 0x01,
|
|
]
|
|
var buffer = [UInt8](repeating: 0, count: 3 * MemoryLayout<UInt64>.size)
|
|
guard let ptr = buffer.bind(as: UInt64.self, offset: 2 * MemoryLayout<UInt64>.size) else {
|
|
// NOTE: This does not work:
|
|
// let ptr: UnsafeMutablePointer<UInt64> = #require(buffer.bind(as: UInt64.self, offset: MemoryLayout<UInt64>.size), "could not bind value to buffer")
|
|
// it fails with the error:
|
|
// cannot use mutating member on immutable value: '$0' is immutable
|
|
// $0.bind(as: $1, offset: $2)
|
|
#expect(Bool(false), "could not bind value to buffer")
|
|
return
|
|
}
|
|
|
|
ptr.pointee = expectedValue
|
|
#expect(buffer == expectedBuffer)
|
|
}
|
|
|
|
@Test func testBufferBindRangeError() throws {
|
|
var buffer = [UInt8](repeating: 0, count: 3 * MemoryLayout<UInt64>.size)
|
|
#expect(buffer.bind(as: UInt64.self, offset: 2 * MemoryLayout<UInt64>.size + 1) == nil)
|
|
}
|
|
|
|
@Test func testBufferCopy() throws {
|
|
let inputBuffer: [UInt8] = [0x01, 0x02, 0x03]
|
|
var buffer = [UInt8](repeating: 0, count: 9)
|
|
|
|
guard let offset = buffer.copyIn(buffer: inputBuffer, offset: 4) else {
|
|
#expect(Bool(false), "could not copy to buffer")
|
|
return
|
|
}
|
|
#expect(offset == 7)
|
|
|
|
guard let offset = buffer.copyIn(buffer: inputBuffer, offset: 6) else {
|
|
#expect(Bool(false), "could not copy to buffer")
|
|
return
|
|
}
|
|
#expect(offset == 9)
|
|
|
|
let expectedBuffer: [UInt8] = [
|
|
0x00, 0x00, 0x00, 0x00, 0x01, 0x02, 0x01, 0x02, 0x03,
|
|
]
|
|
#expect(expectedBuffer == buffer)
|
|
|
|
var outputBuffer = [UInt8](repeating: 0, count: 3)
|
|
guard let offset = buffer.copyOut(buffer: &outputBuffer, offset: 6) else {
|
|
#expect(Bool(false), "could not copy to buffer")
|
|
return
|
|
}
|
|
#expect(offset == 9)
|
|
|
|
let expectedOutputBuffer: [UInt8] = [
|
|
0x01, 0x02, 0x03,
|
|
]
|
|
#expect(expectedOutputBuffer == outputBuffer)
|
|
}
|
|
|
|
@Test func testBufferCopyRangeError() throws {
|
|
let inputBuffer: [UInt8] = [0x01, 0x02, 0x03]
|
|
var buffer = [UInt8](repeating: 0, count: 9)
|
|
|
|
#expect(buffer.copyIn(buffer: inputBuffer, offset: 7) == nil)
|
|
|
|
var outputBuffer = [UInt8](repeating: 0, count: 3)
|
|
#expect(buffer.copyOut(buffer: &outputBuffer, offset: 7) == nil)
|
|
}
|
|
}
|