Skip to content

Commit

Permalink
More unit tests (#313)
Browse files Browse the repository at this point in the history
  • Loading branch information
RickyLB authored Nov 3, 2023
1 parent 6b41e4e commit e79038b
Show file tree
Hide file tree
Showing 8 changed files with 348 additions and 0 deletions.
49 changes: 49 additions & 0 deletions Tests/HederaTests/CryptoAesTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/*
* ‌
* Hedera Swift SDK
* ​
* Copyright (C) 2022 - 2023 Hedera Hashgraph, LLC
* ​
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* ‍
*/

import CommonCrypto
import CryptoKit
import SwiftASN1
import XCTest

@testable import Hedera

internal final class CryptoAesTests: XCTestCase {
internal static var testPassphrase = "testpassphrase13d14"

internal func testAesDecryption() throws {
let bytesDer = PrivateKey.toBytesDer(Resources.privateKey)
let iv = Data(hexEncoded: "0046A9EED8D16BE8BD6F0CAA6A197CE8")!

var hash = CryptoKit.Insecure.MD5()

hash.update(data: Self.testPassphrase.data(using: .utf8)!)
hash.update(data: iv[slicing: ..<8]!)

let password = Data(hash.finalize().bytes)

let decrypted = try Crypto.Aes.aes128CbcPadDecrypt(key: password, iv: iv, message: bytesDer())

XCTAssertEqual(
decrypted.hexStringEncoded(),
"d8ea1c72c322bc67ad533333a0b1a9e2215e34e466c913bed40c5a301a3f7fa9d376b475781c326b91e02599dc7a4412"
)
}
}
79 changes: 79 additions & 0 deletions Tests/HederaTests/CryptoSha2Tests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
/*
* ‌
* Hedera Swift SDK
* ​
* Copyright (C) 2022 - 2023 Hedera Hashgraph, LLC
* ​
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* ‍
*/

import XCTest

@testable import Hedera

internal final class CryptoSha2Tests: XCTestCase {
internal func testSha256Hash() throws {
let input = "testingSha256".data(using: .utf8)!

let sha = Crypto.Sha2.sha256(input)

XCTAssertEqual(sha.hexStringEncoded(), "635cd23293b70af14655d9de9b84c403ab2668d5acd0bd38b5c8e79b50e5992a")
XCTAssertEqual(sha.count, 32)
}

internal func testSha384Hash() throws {
let input = "testingSha384".data(using: .utf8)!

let sha = Crypto.Sha2.sha384(input)

XCTAssertEqual(
sha.hexStringEncoded(),
"3192e2d18a6cbf87971dffb52cf5661f3eab1a682a41e878108a83e87f7621dcb0dc45bca09776db710ac5806272414e")
XCTAssertEqual(sha.count, 48)
}

internal func testSha256HashDigest() throws {
let input = "testingSha256digest".data(using: .utf8)!

let sha = Crypto.Sha2.digest(Crypto.Sha2.sha256, input)

XCTAssertEqual(
sha.hexStringEncoded(),
"c06923f6c6b92625b9e1822930ddda782f2602f55a90b7c621ab8ac6e30e1655")
XCTAssertEqual(sha.count, 32)
}

internal func testSha384HashDigest() throws {
let input = "testingSha384digest".data(using: .utf8)!

let sha = Crypto.Sha2.digest(Crypto.Sha2.sha384, input)

XCTAssertEqual(
sha.hexStringEncoded(),
"093efec585a6221172036f291263eede21b43e3240976320a2232d728ca9f9b16a0927260493b6310a761e913441ed10")
XCTAssertEqual(sha.count, 48)
}

internal func testSha512HashDigest() throws {
let input = "testingSha512digest".data(using: .utf8)!

let sha = Crypto.Sha2.digest(Crypto.Sha2.sha512, input)

XCTAssertEqual(
sha.hexStringEncoded(),
"6e96845f64768cca16294f269bfcdd086afc942e072ab952c33ad90c782182fa260db70978d8306abedbf7d91979f0e8d2d65a1c53e4b36dfbd98707939e32d7"
)
XCTAssertEqual(sha.count, 64)
}
}
43 changes: 43 additions & 0 deletions Tests/HederaTests/CryptoSha3Tests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*
* ‌
* Hedera Swift SDK
* ​
* Copyright (C) 2022 - 2023 Hedera Hashgraph, LLC
* ​
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* ‍
*/

import XCTest

@testable import Hedera

internal final class CryptoSha3Tests: XCTestCase {
internal func testKeccak256Hash() throws {
let input = "testingKeccak256".data(using: .utf8)!

let sha = Crypto.Sha3.keccak256(input)

XCTAssertEqual(sha.hexStringEncoded(), "e1ab2907c85b96939eba66d57102166b98b590e6d50711473c16886f96ddfe9a")
XCTAssertEqual(sha.count, 32)
}

internal func testKeccak256HashDigest() throws {
let input = "testingKeccak256Digest".data(using: .utf8)!

let sha = Crypto.Sha3.digest(Crypto.Sha3.keccak256, input)

XCTAssertEqual(sha.hexStringEncoded(), "01d49c057038debea7a86616abfd86d76ac9fdfdb15536831d26e94a60d95562")
XCTAssertEqual(sha.count, 32)
}
}
59 changes: 59 additions & 0 deletions Tests/HederaTests/DurationTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/*
* ‌
* Hedera Swift SDK
* ​
* Copyright (C) 2023 - 2023 Hedera Hashgraph, LLC
* ​
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* ‍
*/

import SnapshotTesting
import XCTest

@testable import Hedera

internal final class DurationTests: XCTestCase {
private static let seconds: UInt64 = 1_554_158_542
internal func testSeconds() throws {
let duration = Duration(seconds: Self.seconds)

XCTAssertEqual(duration.seconds, Self.seconds)
}

internal func testMinutes() throws {
let duration = Duration.minutes(Self.seconds)

XCTAssertEqual(duration.seconds, Self.seconds * 60)
}

internal func testHours() throws {
let duration = Duration.hours(Self.seconds)

XCTAssertEqual(duration.seconds, Self.seconds * 60 * 60)
}

internal func testDays() throws {
let duration = Duration.days(Self.seconds)

XCTAssertEqual(duration.seconds, Self.seconds * 60 * 60 * 24)
}

internal func testToFromProtobuf() throws {
let durationProto = Duration(seconds: Self.seconds).toProtobuf()

let duration = Duration.fromProtobuf(durationProto)

assertSnapshot(matching: duration, as: .description)
}
}
88 changes: 88 additions & 0 deletions Tests/HederaTests/FileAppendTransactionTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
/*
* ‌
* Hedera Swift SDK
* ​
* Copyright (C) 2023 - 2023 Hedera Hashgraph, LLC
* ​
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* ‍
*/

import HederaProtobufs
import SnapshotTesting
import XCTest

@testable import Hedera

internal final class FileAppendTransactionTests: XCTestCase {
private static let fileId = FileId("0.0.10")
private static let contents = "{foo: 231}".data(using: .utf8)!

private static func makeTransaction() throws -> FileAppendTransaction {
try FileAppendTransaction()
.nodeAccountIds(Resources.nodeAccountIds)
.transactionId(Resources.txId)
.maxTransactionFee(Hbar(2))
.sign(Resources.privateKey)
.fileId(fileId)
.contents(contents)
.freeze()
}

internal func testSerialize() throws {
let tx = try Self.makeTransaction()

// Unlike most transactions, this iteration makes sure the chunked data is properly handled.
// NOTE: Without a client, dealing with chunked data is cumbersome.
let bodyBytes = try tx.makeSources().signedTransactions.makeIterator().map { signed in
try Proto_TransactionBody.init(contiguousBytes: signed.bodyBytes)
}

let txes = try bodyBytes.makeIterator().map { bytes in
try Resources.checkTransactionBody(body: bytes)
}

assertSnapshot(of: txes, as: .description)
}

internal func testToFromBytes() throws {
let tx = try Self.makeTransaction()

let tx2 = try Transaction.fromBytes(try tx.toBytes())

// As stated above, this assignment properly handles the possibilty of the data being chunked.
let txBody = try tx.makeSources().signedTransactions.makeIterator().map { signed in
try Proto_TransactionBody.init(contiguousBytes: signed.bodyBytes)
}

let txBody2 = try tx2.makeSources().signedTransactions.makeIterator().map { signed in
try Proto_TransactionBody.init(contiguousBytes: signed.bodyBytes)
}

XCTAssertEqual(txBody, txBody2)
}

internal func testGetSetFileId() throws {
let tx = FileAppendTransaction.init()
tx.fileId(Self.fileId)

XCTAssertEqual(tx.fileId, Self.fileId)
}

internal func testGetSetContents() throws {
let tx = FileAppendTransaction.init()
tx.contents(Self.contents)

XCTAssertEqual(tx.contents, Self.contents)
}
}
18 changes: 18 additions & 0 deletions Tests/HederaTests/Resources.swift
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,22 @@ internal enum Resources {
privateKey.publicKey
}

internal static func checkTransactionBody(body: Proto_TransactionBody) throws -> Proto_TransactionBody.OneOf_Data {
let txBody = body

let nodeAccountId = txBody.nodeAccountID

XCTAssertEqual(txBody.transactionID, Self.txId.toProtobuf())

XCTAssert(Resources.nodeAccountIds.contains(try AccountId.fromProtobuf(nodeAccountId)))

XCTAssertEqual(txBody.transactionFee, UInt64(Hbar(2).toTinybars()))

XCTAssertEqual(txBody.transactionValidDuration, Duration.seconds(120).toProtobuf())
XCTAssertEqual(txBody.generateRecord, false)
XCTAssertEqual(txBody.memo, "")

return txBody.data!
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Duration(seconds: 1554158542)
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
[HederaProtobufs.Proto_TransactionBody.OneOf_Data.fileAppend(HederaProtobufs.Proto_FileAppendTransactionBody:
fileID {
fileNum: 10
}
contents: "{foo: 231}"
), HederaProtobufs.Proto_TransactionBody.OneOf_Data.fileAppend(HederaProtobufs.Proto_FileAppendTransactionBody:
fileID {
fileNum: 10
}
contents: "{foo: 231}"
)]

0 comments on commit e79038b

Please sign in to comment.