Skip to content

Commit e79038b

Browse files
authored
More unit tests (#313)
1 parent 6b41e4e commit e79038b

File tree

8 files changed

+348
-0
lines changed

8 files changed

+348
-0
lines changed
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/*
2+
* ‌
3+
* Hedera Swift SDK
4+
* ​
5+
* Copyright (C) 2022 - 2023 Hedera Hashgraph, LLC
6+
* ​
7+
* Licensed under the Apache License, Version 2.0 (the "License");
8+
* you may not use this file except in compliance with the License.
9+
* You may obtain a copy of the License at
10+
*
11+
* http://www.apache.org/licenses/LICENSE-2.0
12+
*
13+
* Unless required by applicable law or agreed to in writing, software
14+
* distributed under the License is distributed on an "AS IS" BASIS,
15+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
* See the License for the specific language governing permissions and
17+
* limitations under the License.
18+
* ‍
19+
*/
20+
21+
import CommonCrypto
22+
import CryptoKit
23+
import SwiftASN1
24+
import XCTest
25+
26+
@testable import Hedera
27+
28+
internal final class CryptoAesTests: XCTestCase {
29+
internal static var testPassphrase = "testpassphrase13d14"
30+
31+
internal func testAesDecryption() throws {
32+
let bytesDer = PrivateKey.toBytesDer(Resources.privateKey)
33+
let iv = Data(hexEncoded: "0046A9EED8D16BE8BD6F0CAA6A197CE8")!
34+
35+
var hash = CryptoKit.Insecure.MD5()
36+
37+
hash.update(data: Self.testPassphrase.data(using: .utf8)!)
38+
hash.update(data: iv[slicing: ..<8]!)
39+
40+
let password = Data(hash.finalize().bytes)
41+
42+
let decrypted = try Crypto.Aes.aes128CbcPadDecrypt(key: password, iv: iv, message: bytesDer())
43+
44+
XCTAssertEqual(
45+
decrypted.hexStringEncoded(),
46+
"d8ea1c72c322bc67ad533333a0b1a9e2215e34e466c913bed40c5a301a3f7fa9d376b475781c326b91e02599dc7a4412"
47+
)
48+
}
49+
}
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
/*
2+
* ‌
3+
* Hedera Swift SDK
4+
* ​
5+
* Copyright (C) 2022 - 2023 Hedera Hashgraph, LLC
6+
* ​
7+
* Licensed under the Apache License, Version 2.0 (the "License");
8+
* you may not use this file except in compliance with the License.
9+
* You may obtain a copy of the License at
10+
*
11+
* http://www.apache.org/licenses/LICENSE-2.0
12+
*
13+
* Unless required by applicable law or agreed to in writing, software
14+
* distributed under the License is distributed on an "AS IS" BASIS,
15+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
* See the License for the specific language governing permissions and
17+
* limitations under the License.
18+
* ‍
19+
*/
20+
21+
import XCTest
22+
23+
@testable import Hedera
24+
25+
internal final class CryptoSha2Tests: XCTestCase {
26+
internal func testSha256Hash() throws {
27+
let input = "testingSha256".data(using: .utf8)!
28+
29+
let sha = Crypto.Sha2.sha256(input)
30+
31+
XCTAssertEqual(sha.hexStringEncoded(), "635cd23293b70af14655d9de9b84c403ab2668d5acd0bd38b5c8e79b50e5992a")
32+
XCTAssertEqual(sha.count, 32)
33+
}
34+
35+
internal func testSha384Hash() throws {
36+
let input = "testingSha384".data(using: .utf8)!
37+
38+
let sha = Crypto.Sha2.sha384(input)
39+
40+
XCTAssertEqual(
41+
sha.hexStringEncoded(),
42+
"3192e2d18a6cbf87971dffb52cf5661f3eab1a682a41e878108a83e87f7621dcb0dc45bca09776db710ac5806272414e")
43+
XCTAssertEqual(sha.count, 48)
44+
}
45+
46+
internal func testSha256HashDigest() throws {
47+
let input = "testingSha256digest".data(using: .utf8)!
48+
49+
let sha = Crypto.Sha2.digest(Crypto.Sha2.sha256, input)
50+
51+
XCTAssertEqual(
52+
sha.hexStringEncoded(),
53+
"c06923f6c6b92625b9e1822930ddda782f2602f55a90b7c621ab8ac6e30e1655")
54+
XCTAssertEqual(sha.count, 32)
55+
}
56+
57+
internal func testSha384HashDigest() throws {
58+
let input = "testingSha384digest".data(using: .utf8)!
59+
60+
let sha = Crypto.Sha2.digest(Crypto.Sha2.sha384, input)
61+
62+
XCTAssertEqual(
63+
sha.hexStringEncoded(),
64+
"093efec585a6221172036f291263eede21b43e3240976320a2232d728ca9f9b16a0927260493b6310a761e913441ed10")
65+
XCTAssertEqual(sha.count, 48)
66+
}
67+
68+
internal func testSha512HashDigest() throws {
69+
let input = "testingSha512digest".data(using: .utf8)!
70+
71+
let sha = Crypto.Sha2.digest(Crypto.Sha2.sha512, input)
72+
73+
XCTAssertEqual(
74+
sha.hexStringEncoded(),
75+
"6e96845f64768cca16294f269bfcdd086afc942e072ab952c33ad90c782182fa260db70978d8306abedbf7d91979f0e8d2d65a1c53e4b36dfbd98707939e32d7"
76+
)
77+
XCTAssertEqual(sha.count, 64)
78+
}
79+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/*
2+
* ‌
3+
* Hedera Swift SDK
4+
* ​
5+
* Copyright (C) 2022 - 2023 Hedera Hashgraph, LLC
6+
* ​
7+
* Licensed under the Apache License, Version 2.0 (the "License");
8+
* you may not use this file except in compliance with the License.
9+
* You may obtain a copy of the License at
10+
*
11+
* http://www.apache.org/licenses/LICENSE-2.0
12+
*
13+
* Unless required by applicable law or agreed to in writing, software
14+
* distributed under the License is distributed on an "AS IS" BASIS,
15+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
* See the License for the specific language governing permissions and
17+
* limitations under the License.
18+
* ‍
19+
*/
20+
21+
import XCTest
22+
23+
@testable import Hedera
24+
25+
internal final class CryptoSha3Tests: XCTestCase {
26+
internal func testKeccak256Hash() throws {
27+
let input = "testingKeccak256".data(using: .utf8)!
28+
29+
let sha = Crypto.Sha3.keccak256(input)
30+
31+
XCTAssertEqual(sha.hexStringEncoded(), "e1ab2907c85b96939eba66d57102166b98b590e6d50711473c16886f96ddfe9a")
32+
XCTAssertEqual(sha.count, 32)
33+
}
34+
35+
internal func testKeccak256HashDigest() throws {
36+
let input = "testingKeccak256Digest".data(using: .utf8)!
37+
38+
let sha = Crypto.Sha3.digest(Crypto.Sha3.keccak256, input)
39+
40+
XCTAssertEqual(sha.hexStringEncoded(), "01d49c057038debea7a86616abfd86d76ac9fdfdb15536831d26e94a60d95562")
41+
XCTAssertEqual(sha.count, 32)
42+
}
43+
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
/*
2+
* ‌
3+
* Hedera Swift SDK
4+
* ​
5+
* Copyright (C) 2023 - 2023 Hedera Hashgraph, LLC
6+
* ​
7+
* Licensed under the Apache License, Version 2.0 (the "License");
8+
* you may not use this file except in compliance with the License.
9+
* You may obtain a copy of the License at
10+
*
11+
* http://www.apache.org/licenses/LICENSE-2.0
12+
*
13+
* Unless required by applicable law or agreed to in writing, software
14+
* distributed under the License is distributed on an "AS IS" BASIS,
15+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
* See the License for the specific language governing permissions and
17+
* limitations under the License.
18+
* ‍
19+
*/
20+
21+
import SnapshotTesting
22+
import XCTest
23+
24+
@testable import Hedera
25+
26+
internal final class DurationTests: XCTestCase {
27+
private static let seconds: UInt64 = 1_554_158_542
28+
internal func testSeconds() throws {
29+
let duration = Duration(seconds: Self.seconds)
30+
31+
XCTAssertEqual(duration.seconds, Self.seconds)
32+
}
33+
34+
internal func testMinutes() throws {
35+
let duration = Duration.minutes(Self.seconds)
36+
37+
XCTAssertEqual(duration.seconds, Self.seconds * 60)
38+
}
39+
40+
internal func testHours() throws {
41+
let duration = Duration.hours(Self.seconds)
42+
43+
XCTAssertEqual(duration.seconds, Self.seconds * 60 * 60)
44+
}
45+
46+
internal func testDays() throws {
47+
let duration = Duration.days(Self.seconds)
48+
49+
XCTAssertEqual(duration.seconds, Self.seconds * 60 * 60 * 24)
50+
}
51+
52+
internal func testToFromProtobuf() throws {
53+
let durationProto = Duration(seconds: Self.seconds).toProtobuf()
54+
55+
let duration = Duration.fromProtobuf(durationProto)
56+
57+
assertSnapshot(matching: duration, as: .description)
58+
}
59+
}
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
/*
2+
* ‌
3+
* Hedera Swift SDK
4+
* ​
5+
* Copyright (C) 2023 - 2023 Hedera Hashgraph, LLC
6+
* ​
7+
* Licensed under the Apache License, Version 2.0 (the "License");
8+
* you may not use this file except in compliance with the License.
9+
* You may obtain a copy of the License at
10+
*
11+
* http://www.apache.org/licenses/LICENSE-2.0
12+
*
13+
* Unless required by applicable law or agreed to in writing, software
14+
* distributed under the License is distributed on an "AS IS" BASIS,
15+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
* See the License for the specific language governing permissions and
17+
* limitations under the License.
18+
* ‍
19+
*/
20+
21+
import HederaProtobufs
22+
import SnapshotTesting
23+
import XCTest
24+
25+
@testable import Hedera
26+
27+
internal final class FileAppendTransactionTests: XCTestCase {
28+
private static let fileId = FileId("0.0.10")
29+
private static let contents = "{foo: 231}".data(using: .utf8)!
30+
31+
private static func makeTransaction() throws -> FileAppendTransaction {
32+
try FileAppendTransaction()
33+
.nodeAccountIds(Resources.nodeAccountIds)
34+
.transactionId(Resources.txId)
35+
.maxTransactionFee(Hbar(2))
36+
.sign(Resources.privateKey)
37+
.fileId(fileId)
38+
.contents(contents)
39+
.freeze()
40+
}
41+
42+
internal func testSerialize() throws {
43+
let tx = try Self.makeTransaction()
44+
45+
// Unlike most transactions, this iteration makes sure the chunked data is properly handled.
46+
// NOTE: Without a client, dealing with chunked data is cumbersome.
47+
let bodyBytes = try tx.makeSources().signedTransactions.makeIterator().map { signed in
48+
try Proto_TransactionBody.init(contiguousBytes: signed.bodyBytes)
49+
}
50+
51+
let txes = try bodyBytes.makeIterator().map { bytes in
52+
try Resources.checkTransactionBody(body: bytes)
53+
}
54+
55+
assertSnapshot(of: txes, as: .description)
56+
}
57+
58+
internal func testToFromBytes() throws {
59+
let tx = try Self.makeTransaction()
60+
61+
let tx2 = try Transaction.fromBytes(try tx.toBytes())
62+
63+
// As stated above, this assignment properly handles the possibilty of the data being chunked.
64+
let txBody = try tx.makeSources().signedTransactions.makeIterator().map { signed in
65+
try Proto_TransactionBody.init(contiguousBytes: signed.bodyBytes)
66+
}
67+
68+
let txBody2 = try tx2.makeSources().signedTransactions.makeIterator().map { signed in
69+
try Proto_TransactionBody.init(contiguousBytes: signed.bodyBytes)
70+
}
71+
72+
XCTAssertEqual(txBody, txBody2)
73+
}
74+
75+
internal func testGetSetFileId() throws {
76+
let tx = FileAppendTransaction.init()
77+
tx.fileId(Self.fileId)
78+
79+
XCTAssertEqual(tx.fileId, Self.fileId)
80+
}
81+
82+
internal func testGetSetContents() throws {
83+
let tx = FileAppendTransaction.init()
84+
tx.contents(Self.contents)
85+
86+
XCTAssertEqual(tx.contents, Self.contents)
87+
}
88+
}

Tests/HederaTests/Resources.swift

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,4 +42,22 @@ internal enum Resources {
4242
privateKey.publicKey
4343
}
4444

45+
internal static func checkTransactionBody(body: Proto_TransactionBody) throws -> Proto_TransactionBody.OneOf_Data {
46+
let txBody = body
47+
48+
let nodeAccountId = txBody.nodeAccountID
49+
50+
XCTAssertEqual(txBody.transactionID, Self.txId.toProtobuf())
51+
52+
XCTAssert(Resources.nodeAccountIds.contains(try AccountId.fromProtobuf(nodeAccountId)))
53+
54+
XCTAssertEqual(txBody.transactionFee, UInt64(Hbar(2).toTinybars()))
55+
56+
XCTAssertEqual(txBody.transactionValidDuration, Duration.seconds(120).toProtobuf())
57+
XCTAssertEqual(txBody.generateRecord, false)
58+
XCTAssertEqual(txBody.memo, "")
59+
60+
return txBody.data!
61+
}
62+
4563
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Duration(seconds: 1554158542)
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
[HederaProtobufs.Proto_TransactionBody.OneOf_Data.fileAppend(HederaProtobufs.Proto_FileAppendTransactionBody:
2+
fileID {
3+
fileNum: 10
4+
}
5+
contents: "{foo: 231}"
6+
), HederaProtobufs.Proto_TransactionBody.OneOf_Data.fileAppend(HederaProtobufs.Proto_FileAppendTransactionBody:
7+
fileID {
8+
fileNum: 10
9+
}
10+
contents: "{foo: 231}"
11+
)]

0 commit comments

Comments
 (0)