Skip to content

Commit

Permalink
use underscored api
Browse files Browse the repository at this point in the history
  • Loading branch information
glbrntt committed Dec 11, 2024
1 parent 99d59a8 commit 812c2a1
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 19 deletions.
4 changes: 2 additions & 2 deletions Sources/NIOCore/ByteBuffer-aux.swift
Original file line number Diff line number Diff line change
Expand Up @@ -733,13 +733,13 @@ extension ByteBuffer: Codable {
public init(from decoder: Decoder) throws {
let container = try decoder.singleValueContainer()
let base64String = try container.decode(String.self)
self = try ByteBuffer(bytes: base64String.decodeBase64())
self = try ByteBuffer(bytes: base64String._base64Decoded())
}

/// Encodes this buffer as a base64 string in a single value container.
public func encode(to encoder: Encoder) throws {
var container = encoder.singleValueContainer()
let base64String = String(encodingAsBase64: self.readableBytesView)
let base64String = String(_base64Encoding: self.readableBytesView)
try container.encode(base64String)
}
}
Expand Down
4 changes: 2 additions & 2 deletions Sources/NIOWebSocket/NIOWebSocketClientUpgrader.swift
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ extension NIOWebSocketClientUpgrader {
UInt64.random(in: UInt64.min...UInt64.max, using: &generator),
UInt64.random(in: UInt64.min...UInt64.max, using: &generator)
)
return String(encodingAsBase64: buffer.readableBytesView)
return String(_base64Encoding: buffer.readableBytesView)
}
/// Generates a random WebSocket Request Key by generating 16 bytes randomly using the `SystemRandomNumberGenerator` and encoding them as a base64 string as defined in RFC6455 https://tools.ietf.org/html/rfc6455#section-4.1.
/// - Returns: base64 encoded request key
Expand Down Expand Up @@ -179,7 +179,7 @@ private func _shouldAllowUpgrade(upgradeResponse: HTTPResponseHead, requestKey:
var hasher = SHA1()
hasher.update(string: requestKey)
hasher.update(string: magicWebSocketGUID)
let expectedAcceptValue = String(encodingAsBase64: hasher.finish())
let expectedAcceptValue = String(_base64Encoding: hasher.finish())

return expectedAcceptValue == acceptValueHeader[0]
}
Expand Down
3 changes: 2 additions & 1 deletion Sources/NIOWebSocket/NIOWebSocketServerUpgrader.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import CNIOSHA1
import NIOCore
import NIOHTTP1
import _NIOBase64

let magicWebSocketGUID = "258EAFA5-E914-47DA-95CA-C5AB0DC85B11"

Expand Down Expand Up @@ -303,7 +304,7 @@ private func _buildUpgradeResponse(
var hasher = SHA1()
hasher.update(string: key)
hasher.update(string: magicWebSocketGUID)
acceptValue = String(encodingAsBase64: hasher.finish())
acceptValue = String(_base64Encoding: hasher.finish())
}

extraHeaders.replaceOrAdd(name: "Upgrade", value: "websocket")
Expand Down
8 changes: 4 additions & 4 deletions Sources/_NIOBase64/Base64.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,23 +20,23 @@ extension String {
@available(*, deprecated, message: "This API was unintentionally made public.")
@inlinable
public init<Buffer: Collection>(base64Encoding bytes: Buffer) where Buffer.Element == UInt8 {
self.init(encodingAsBase64: bytes)
self.init(_base64Encoding: bytes)
}

@available(*, deprecated, message: "This API was unintentionally made public.")
@inlinable
public func base64Decoded() throws -> [UInt8] {
try self.decodeBase64()
try self._base64Decoded()
}

/// Base64 encode a collection of UInt8 to a string, without the use of Foundation.
@inlinable
package init<Buffer: Collection>(encodingAsBase64 bytes: Buffer) where Buffer.Element == UInt8 {
public init<Buffer: Collection>(_base64Encoding bytes: Buffer) where Buffer.Element == UInt8 {
self = Base64.encode(bytes: bytes)
}

@inlinable
package func decodeBase64() throws -> [UInt8] {
public func _base64Decoded() throws -> [UInt8] {
try Base64.decode(string: self)
}
}
Expand Down
22 changes: 12 additions & 10 deletions Tests/NIOBase64Tests/Base64Test.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,58 +13,60 @@
//===----------------------------------------------------------------------===//

import XCTest

@_spi(Package)
import _NIOBase64

class Base64Test: XCTestCase {
func testEncodeEmptyData() throws {
let data = [UInt8]()
let encodedData = String(encodingAsBase64: data)
let encodedData = String(_base64Encoding: data)
XCTAssertEqual(encodedData.count, 0)
}

func testBase64EncodingOfEmptyString() throws {
let string = ""
let encoded = String(encodingAsBase64: string.utf8)
let encoded = String(_base64Encoding: string.utf8)
XCTAssertEqual(encoded, "")
}

func testBase64DecodingOfEmptyString() throws {
let encoded = ""
XCTAssertNoThrow {
let decoded = try encoded.decodeBase64()
let decoded = try encoded._base64Decoded()
XCTAssertEqual(decoded, [UInt8]())
}
}

func testBase64EncodingArrayOfNulls() throws {
let data = Array(repeating: UInt8(0), count: 10)
let encodedData = String(encodingAsBase64: data)
let encodedData = String(_base64Encoding: data)
XCTAssertEqual(encodedData, "AAAAAAAAAAAAAA==")
}

func testBase64DecodeArrayOfNulls() throws {
let encoded = "AAAAAAAAAAAAAA=="
let decoded = try! encoded.decodeBase64()
let decoded = try! encoded._base64Decoded()
let expected = Array(repeating: UInt8(0), count: 10)
XCTAssertEqual(decoded, expected)
}

func testBase64EncodeingHelloWorld() throws {
let string = "Hello, world!"
let encoded = String(encodingAsBase64: string.utf8)
let encoded = String(_base64Encoding: string.utf8)
let expected = "SGVsbG8sIHdvcmxkIQ=="
XCTAssertEqual(encoded, expected)
}

func testBase64DecodeHelloWorld() throws {
let encoded = "SGVsbG8sIHdvcmxkIQ=="
let decoded = try! encoded.decodeBase64()
let decoded = try! encoded._base64Decoded()
XCTAssertEqual(decoded, "Hello, world!".utf8.map { UInt8($0) })
}

func testBase64EncodingAllTheBytesSequentially() throws {
let data = Array(UInt8(0)...UInt8(255))
let encodedData = String(encodingAsBase64: data)
let encodedData = String(_base64Encoding: data)
XCTAssertEqual(
encodedData,
"AAECAwQFBgcICQoLDA0ODxAREhMUFRYXGBkaGxwdHh8gISIjJCUmJygpKissLS4vMDEyMzQ1Njc4OTo7PD0+P0BBQkNERUZHSElKS0xNTk9QUVJTVFVWV1hZWltcXV5fYGFiY2RlZmdoaWprbG1ub3BxcnN0dXZ3eHl6e3x9fn+AgYKDhIWGh4iJiouMjY6PkJGSk5SVlpeYmZqbnJ2en6ChoqOkpaanqKmqq6ytrq+wsbKztLW2t7i5uru8vb6/wMHCw8TFxsfIycrLzM3Oz9DR0tPU1dbX2Nna29zd3t/g4eLj5OXm5+jp6uvs7e7v8PHy8/T19vf4+fr7/P3+/w=="
Expand All @@ -73,14 +75,14 @@ class Base64Test: XCTestCase {

func testBase64DecodingWithInvalidLength() {
let encoded = "dGVzbA!=="
XCTAssertThrowsError(try encoded.decodeBase64()) { error in
XCTAssertThrowsError(try encoded._base64Decoded()) { error in
XCTAssertEqual(error as? Base64Error, .invalidLength)
}
}

func testBase64DecodeWithInvalidCharacter() throws {
let encoded = "SGVsbG8sI_dvcmxkIQ=="
XCTAssertThrowsError(try encoded.decodeBase64()) { error in
XCTAssertThrowsError(try encoded._base64Decoded()) { error in
XCTAssertEqual(error as? Base64Error, .invalidCharacter)
}
}
Expand Down

0 comments on commit 812c2a1

Please sign in to comment.