Repository: apple/swift-nio
File: Tests/NIOFoundationCompatTests/ByteBuffer+UUIDTests.swift (Swift)

Code before:
// Copyright (c) 2022 Apple Inc. and the SwiftNIO project authors
// Licensed under Apache License v2.0
//
// See LICENSE.txt for license information
// See CONTRIBUTORS.txt for the list of SwiftNIO project authors
//
// SPDX-License-Identifier: Apache-2.0
//
//===----------------------------------------------------------------------===//

import Foundation
import NIOCore
import NIOFoundationCompat
import XCTest

final class ByteBufferUUIDTests: XCTestCase {
    func testSetUUID() {
        let uuid = UUID()
        var buffer = ByteBuffer()

        XCTAssertEqual(buffer.storageCapacity, 0)
        XCTAssertEqual(buffer.setUUID(uuid, at: 0), 16)
        XCTAssertEqual(buffer.writerIndex, 0)
        XCTAssertEqual(buffer.readableBytes, 0)
        XCTAssertGreaterThanOrEqual(buffer.storageCapacity, 16)
    }

    func testGetUUIDEmptyBuffer() {
        let buffer = ByteBuffer()
        XCTAssertNil(buffer.getUUID(at: 0))
    }

    func testGetUUIDAfterSet() {
        let uuid = UUID()
        var buffer = ByteBuffer()
        XCTAssertEqual(buffer.setUUID(uuid, at: 0), 16)
        // nil because there are no bytes to read
        XCTAssertNil(buffer.getUUID(at: 0))
    }

    func testWriteUUID() {
        let uuid = UUID()
        var buffer = ByteBuffer()

        XCTAssertEqual(buffer.writeUUID(uuid), 16)
        XCTAssertEqual(buffer.readableBytes, 16)
        XCTAssertEqual(buffer.writerIndex, 16)

        XCTAssertEqual(buffer.writeUUID(uuid), 16)
        XCTAssertEqual(buffer.readableBytes, 32)
        XCTAssertEqual(buffer.writerIndex, 32)

Review comment (suggestion):
(drive-by comment):

None of these tests check the actual contents of the buffer after a UUID has been written to it. There is only one test that ensures the same machine can reconstruct a UUID from the buffer.

IMO, this test should also check that each byte in the buffer matches the byte of the UUID at the same position. 

Code after:
// Copyright (c) 2022 Apple Inc. and the SwiftNIO project authors
// Licensed under Apache License v2.0
//
// See LICENSE.txt for license information
// See CONTRIBUTORS.txt for the list of SwiftNIO project authors
//
// SPDX-License-Identifier: Apache-2.0
//
//===----------------------------------------------------------------------===//

import Foundation
import NIOCore
import NIOFoundationCompat
import XCTest

final class ByteBufferUUIDTests: XCTestCase {
    func testSetUUIDBytes() {
        let uuid = UUID(uuid: (0x0, 0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7,
                               0x8, 0x9, 0xa, 0xb, 0xc, 0xd, 0xe, 0xf))
        var buffer = ByteBuffer()

        XCTAssertEqual(buffer.storageCapacity, 0)
        XCTAssertEqual(buffer.setUUIDBytes(uuid, at: 0), 16)
        XCTAssertEqual(buffer.writerIndex, 0)
        XCTAssertEqual(buffer.readableBytes, 0)
        XCTAssertGreaterThanOrEqual(buffer.storageCapacity, 16)

        buffer.moveWriterIndex(forwardBy: 16)
        let bytes = buffer.getBytes(at: buffer.readerIndex, length: 16)
        XCTAssertEqual(bytes, Array(0..<16))
    }

    func testSetUUIDBytesBlatsExistingBytes() {
        var buffer = ByteBuffer()
        buffer.writeRepeatingByte(.max, count: 32)

        let uuid = UUID(uuid: (0x0, 0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7,
                               0x8, 0x9, 0xa, 0xb, 0xc, 0xd, 0xe, 0xf))
        buffer.setUUIDBytes(uuid, at: buffer.readerIndex + 4)

        XCTAssertEqual(buffer.readBytes(length: 4), Array(repeating: .max, count: 4))
        XCTAssertEqual(buffer.readBytes(length: 16), Array(0..<16))
        XCTAssertEqual(buffer.readBytes(length: 12), Array(repeating: .max, count: 12))
        XCTAssertEqual(buffer.readableBytes, 0)
    }

    func testGetUUIDEmptyBuffer() {
        let buffer = ByteBuffer()
        XCTAssertNil(buffer.getUUIDBytes(at: 0))
    }