Skip to content

Commit

Permalink
add tests and fix soundness
Browse files Browse the repository at this point in the history
  • Loading branch information
lovetodream committed Nov 13, 2024
1 parent 40dc61b commit c1b222f
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 10 deletions.
Original file line number Diff line number Diff line change
@@ -1,3 +1,17 @@
//===----------------------------------------------------------------------===//
//
// This source file is part of the OracleNIO open source project
//
// Copyright (c) 2024 Timo Zacherl and the OracleNIO project authors
// Licensed under Apache License v2.0
//
// See LICENSE for license information
// See CONTRIBUTORS.md for the list of OracleNIO project authors
//
// SPDX-License-Identifier: Apache-2.0
//
//===----------------------------------------------------------------------===//

import NIOCore

extension ByteBuffer {
Expand Down
22 changes: 12 additions & 10 deletions Sources/OracleNIO/Messages/OracleBackendMessage+BackendError.swift
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,12 @@ extension OracleBackendMessage {
let length = try buffer.throwingReadInteger(as: UInt16.self)
// length of error message
try buffer.throwingMoveReaderIndex(forwardBy: 2) // skip flags
let errorMessage: String? = if number != 0 && length > 0 {
try buffer.throwingReadString(length: Int(length))
} else {
nil
}
let errorMessage: String? =
if number != 0 && length > 0 {
try buffer.throwingReadString(length: Int(length))
} else {
nil
}

Check warning on line 42 in Sources/OracleNIO/Messages/OracleBackendMessage+BackendError.swift

View check run for this annotation

Codecov / codecov/patch

Sources/OracleNIO/Messages/OracleBackendMessage+BackendError.swift#L36-L42

Added lines #L36 - L42 were not covered by tests
return .init(
number: UInt32(number),
isWarning: true,
Expand Down Expand Up @@ -133,11 +134,12 @@ extension OracleBackendMessage {
try buffer.throwingSkipUB4() // server checksum
}

let errorMessage: String? = if number != 0 {
try buffer.readString().trimmingCharacters(in: .whitespaces)
} else {
nil
}
let errorMessage: String? =
if number != 0 {
try buffer.readString().trimmingCharacters(in: .whitespaces)
} else {
nil
}

return .init(
number: number,
Expand Down
15 changes: 15 additions & 0 deletions Tests/OracleNIOTests/Extensions/ByteBufferExtensionTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -152,4 +152,19 @@ final class ByteBufferExtensionTests: XCTestCase {
var buffer = ByteBuffer()
XCTAssertEqual(buffer.readOracleSlice(), nil)
}

func testThrowingSkipUBShouldThrowOnMissingBytes() {
var buffer = ByteBuffer(bytes: [1])
XCTAssertThrowsError(
try buffer.throwingSkipUB4(),
expected: OraclePartialDecodingError.expectedAtLeastNRemainingBytes(1, actual: 0)
)
}

func testReadOSONFailsAppropriately() {
var sliceMissingBuffer = ByteBuffer(bytes: [1, 40, 0, 0])
XCTAssertNil(try? sliceMissingBuffer.throwingReadOSON())
var locatorMissingBuffer = ByteBuffer(bytes: [1, 40, 0, 0, 0])
XCTAssertNil(try? locatorMissingBuffer.throwingReadOSON())
}
}

0 comments on commit c1b222f

Please sign in to comment.