-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
348 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1 change: 1 addition & 0 deletions
1
Tests/HederaTests/__Snapshots__/DurationTests/testToFromProtobuf.1.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Duration(seconds: 1554158542) |
11 changes: 11 additions & 0 deletions
11
Tests/HederaTests/__Snapshots__/FileAppendTransactionTests/testSerialize.1.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}" | ||
)] |