Skip to content

Commit

Permalink
Add testComputeTreeHash
Browse files Browse the repository at this point in the history
  • Loading branch information
adam-fowler committed Nov 22, 2022
1 parent 2d30816 commit 84a53ea
Showing 1 changed file with 34 additions and 0 deletions.
34 changes: 34 additions & 0 deletions Tests/SotoCoreTests/MiddlewareTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -141,4 +141,38 @@ class MiddlewareTests: XCTestCase {
XCTAssertEqual(request.url.absoluteString, "https://bucket.s3-accelerate.dualstack.amazonaws.com/file")
}
}

// create a buffer of random values. Will always create the same given you supply the same z and w values
// Random number generator from https://www.codeproject.com/Articles/25172/Simple-Random-Number-Generation
func createRandomBuffer(_ w: UInt, _ z: UInt, size: Int) -> [UInt8] {
var z = z
var w = w
func getUInt8() -> UInt8 {
z = 36969 * (z & 65535) + (z >> 16)
w = 18000 * (w & 65535) + (w >> 16)
return UInt8(((z << 16) + w) & 0xFF)
}
var data = [UInt8](repeating: 0, count: size)
for i in 0..<size {
data[i] = getUInt8()
}
return data
}

func testComputeTreeHash() throws {
// create buffer full of random data, use the same seeds to ensure we get the same buffer everytime
let data = self.createRandomBuffer(23, 4, size: 7 * 1024 * 1024 + 258)

// create byte buffer
var byteBuffer = ByteBufferAllocator().buffer(capacity: data.count)
byteBuffer.writeBytes(data)

let middleware = TreeHashMiddleware(header: "tree-hash")
let treeHash = try middleware.computeTreeHash(byteBuffer)

XCTAssertEqual(
treeHash,
[210, 50, 5, 126, 16, 6, 59, 6, 21, 40, 186, 74, 192, 56, 39, 85, 210, 25, 238, 54, 4, 252, 221, 238, 107, 127, 76, 118, 245, 76, 22, 45]
)
}
}

0 comments on commit 84a53ea

Please sign in to comment.