-
Notifications
You must be signed in to change notification settings - Fork 652
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add utilties for reading and writing UUIDs (#2045)
Motivation: UUIDs are often sent over the wire but writing and reading their bytes to/from a buffer is a bit of a pain. Modifications: - Add utilties to 'NIOFoundationCompat' for reading/writing and getting/setting a UUID on a `ByteBuffer`. Result: Easier to write/read UUIDs to/from a buffer. Co-authored-by: Cory Benfield <[email protected]>
- Loading branch information
Showing
4 changed files
with
252 additions
and
2 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
41 changes: 41 additions & 0 deletions
41
Tests/NIOFoundationCompatTests/ByteBuffer+UUIDTests+XCTest.swift
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,41 @@ | ||
//===----------------------------------------------------------------------===// | ||
// | ||
// This source file is part of the SwiftNIO open source project | ||
// | ||
// Copyright (c) 2017-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 | ||
// | ||
//===----------------------------------------------------------------------===// | ||
// | ||
// ByteBuffer+UUIDTests+XCTest.swift | ||
// | ||
import XCTest | ||
|
||
/// | ||
/// NOTE: This file was generated by generate_linux_tests.rb | ||
/// | ||
/// Do NOT edit this file directly as it will be regenerated automatically when needed. | ||
/// | ||
|
||
extension ByteBufferUUIDTests { | ||
|
||
@available(*, deprecated, message: "not actually deprecated. Just deprecated to allow deprecated tests (which test deprecated functionality) without warnings") | ||
static var allTests : [(String, (ByteBufferUUIDTests) -> () throws -> Void)] { | ||
return [ | ||
("testSetUUIDBytes", testSetUUIDBytes), | ||
("testSetUUIDBytesBlatsExistingBytes", testSetUUIDBytesBlatsExistingBytes), | ||
("testGetUUIDEmptyBuffer", testGetUUIDEmptyBuffer), | ||
("testGetUUIDAfterSet", testGetUUIDAfterSet), | ||
("testWriteUUIDBytesIntoEmptyBuffer", testWriteUUIDBytesIntoEmptyBuffer), | ||
("testWriteUUIDBytesIntoNonEmptyBuffer", testWriteUUIDBytesIntoNonEmptyBuffer), | ||
("testReadUUID", testReadUUID), | ||
("testReadUUIDNotEnoughBytes", testReadUUIDNotEnoughBytes), | ||
] | ||
} | ||
} | ||
|
113 changes: 113 additions & 0 deletions
113
Tests/NIOFoundationCompatTests/ByteBuffer+UUIDTests.swift
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,113 @@ | ||
//===----------------------------------------------------------------------===// | ||
// | ||
// This source file is part of the SwiftNIO open source project | ||
// | ||
// 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)) | ||
} | ||
|
||
func testGetUUIDAfterSet() { | ||
let uuid = UUID() | ||
var buffer = ByteBuffer() | ||
XCTAssertEqual(buffer.setUUIDBytes(uuid, at: 0), 16) | ||
// nil because there are no bytes to read | ||
XCTAssertNil(buffer.getUUIDBytes(at: 0)) | ||
} | ||
|
||
func testWriteUUIDBytesIntoEmptyBuffer() { | ||
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.writeUUIDBytes(uuid), 16) | ||
XCTAssertEqual(buffer.readableBytesView, [0x0, 0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7, | ||
0x8, 0x9, 0xa, 0xb, 0xc, 0xd, 0xe, 0xf]) | ||
XCTAssertEqual(buffer.readableBytes, 16) | ||
XCTAssertEqual(buffer.writerIndex, 16) | ||
} | ||
|
||
func testWriteUUIDBytesIntoNonEmptyBuffer() { | ||
let uuid = UUID(uuid: (0x0, 0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7, | ||
0x8, 0x9, 0xa, 0xb, 0xc, 0xd, 0xe, 0xf)) | ||
|
||
var buffer = ByteBuffer() | ||
buffer.writeRepeatingByte(42, count: 10) | ||
XCTAssertEqual(buffer.writeUUIDBytes(uuid), 16) | ||
XCTAssertEqual(buffer.readableBytes, 26) | ||
XCTAssertEqual(buffer.writerIndex, 26) | ||
|
||
XCTAssertEqual(buffer.readableBytesView.dropFirst(10), | ||
[0x0, 0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7, 0x8, 0x9, 0xa, 0xb, 0xc, 0xd, 0xe, 0xf]) | ||
} | ||
|
||
func testReadUUID() { | ||
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.writeUUIDBytes(uuid), 16) | ||
XCTAssertEqual(buffer.readUUIDBytes(), uuid) | ||
XCTAssertEqual(buffer.readableBytes, 0) | ||
} | ||
|
||
func testReadUUIDNotEnoughBytes() { | ||
var buffer = ByteBuffer() | ||
XCTAssertNil(buffer.readUUIDBytes()) | ||
XCTAssertEqual(buffer.readerIndex, 0) | ||
|
||
buffer.writeRepeatingByte(0, count: 8) | ||
XCTAssertNil(buffer.readUUIDBytes()) | ||
XCTAssertEqual(buffer.readerIndex, 0) | ||
|
||
buffer.writeRepeatingByte(0, count: 8) | ||
XCTAssertEqual(buffer.readUUIDBytes(), | ||
UUID(uuid: (0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0))) | ||
XCTAssertEqual(buffer.readerIndex, 16) | ||
} | ||
} |