Skip to content

Commit

Permalink
Add Unit Tests (#295)
Browse files Browse the repository at this point in the history
  • Loading branch information
RickyLB authored Oct 3, 2023
1 parent ebf40e0 commit 48fdb78
Show file tree
Hide file tree
Showing 28 changed files with 877 additions and 1 deletion.
106 changes: 106 additions & 0 deletions Tests/HederaTests/CryptoPemTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
/*
* ‌
* 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 CryptoPemTests: XCTestCase {
internal func testLabelType() throws {
let pemString =
"""
-----BEGIN PRIVATE KEY-----
MIGbMFcGCSqGSIb3DQEFDTBKMCkGCSqGSIb3DQEFDDAcBAjeB6TNNQX+1gICCAAw
-----END PRIVATE KEY-----
"""

let doc = try Crypto.Pem.decode(pemString)

XCTAssertEqual(doc.typeLabel, "PRIVATE KEY")
}

internal func testExceedsLineLimitFail() throws {
let pemString =
"""
-----BEGIN PRIVATE KEY-----
MIGbMFcGCSqGSIb3DQEFDTBKMCkGCSqGSIb3DQEFDDAcBAjeB6TNNQX+1gICCAAwMIGb
-----END PRIVATE KEY-----
"""

XCTAssertThrowsError(try Crypto.Pem.decode(pemString))
}

internal func testShortLineFail() throws {
let pemString =
"""
-----BEGIN PRIVATE KEY-----
MIGbMFcGCSqGSIb3DQEFDTBKMCkGCSqGSIb3DQEFD
-----END PRIVATE KEY-----
"""

XCTAssertThrowsError(try Crypto.Pem.decode(pemString))
}

internal func testNonBase64CharacterFail() throws {
let pemString =
"""
-----BEGIN PRIVATE KEY-----
≈MIGbMFcGCSqGSIb3DQEFDTBKMCkGCSqGSIb3DQEFDDAcBAjeB6TNNQX+1gICCAAw
-----END PRIVATE KEY-----
"""

XCTAssertThrowsError(try Crypto.Pem.decode(pemString))
}

internal func testBadHeaderFail() throws {
let pemString =
"""
-----BEGIN PRIVATE KEYS-----
MIGbMFcGCSqGSIb3DQEFDSTBKMCkGCSqGSIb3DQEFDDAcBAjeB6TNNQX+1gICCAAw
-----END PRIVATE KEY-----
"""

XCTAssertThrowsError(try Crypto.Pem.decode(pemString))
}

internal func testBadFooterFail() throws {
let pemString =
"""
-----BEGIN PRIVATE KEY-----
MIGbMFcGCSqGSIb3DQEFDSTBKMCkGCSqGSIb3DQEFDDAcBAjeB6TNNQX+1gICCAAw
-----END PRIVATE KEYS-----
"""

XCTAssertThrowsError(try Crypto.Pem.decode(pemString))
}

internal func testBase64CharacterFail() throws {
let pemString =
"""
-----BEGIN PRIVATE KEY-----
@IGbMFcGCSqGSIb3DQEFDTBKMCkGCSqGSIb3DQEFDDAcBAjeB6TNNQX+1gICCAAw
-----END PRIVATE KEY-----
"""

XCTAssertThrowsError(try Crypto.Pem.decode(pemString))
}
}
106 changes: 106 additions & 0 deletions Tests/HederaTests/CustomFeeTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
/*
* ‌
* 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 CustomFeeTests: XCTestCase {
private static let customFixedFee = Proto_CustomFee.with { proto in
proto.feeCollectorAccountID = AccountId(4322).toProtobuf()
proto.fee = .fixedFee(
Proto_FixedFee.with { proto in
proto.amount = 10
proto.denominatingTokenID = TokenId(483902).toProtobuf()
})
}

private static let customFractionalFee = Proto_CustomFee.with { proto in
proto.feeCollectorAccountID = AccountId(389042).toProtobuf()
proto.fee = .fractionalFee(
Proto_FractionalFee.with { proto in
proto.fractionalAmount = .with { proto in
proto.numerator = 3
proto.denominator = 7
}
proto.minimumAmount = 3
proto.maximumAmount = 100
})
}

private static let customRoyaltyFee = Proto_CustomFee.with { proto in
proto.feeCollectorAccountID = AccountId(23423).toProtobuf()
proto.fee = .royaltyFee(
Proto_RoyaltyFee.with { proto in
proto.fallbackFee = .with { proto in
proto.amount = 10
proto.denominatingTokenID = TokenId(483902).toProtobuf()
}
proto.exchangeValueFraction = .with { proto in
proto.numerator = 5
proto.denominator = 8
}
})
}

internal func testSerializeFixed() throws {
assertSnapshot(matching: Self.customFixedFee, as: .description)
}

internal func testSerializeFractional() throws {
assertSnapshot(matching: Self.customFractionalFee, as: .description)
}

internal func testSerializeRoyalty() throws {
assertSnapshot(matching: Self.customRoyaltyFee, as: .description)
}

internal func testFixedFromToBytes() throws {
let fixed = try AnyCustomFee.fromProtobuf(Self.customFixedFee)
let bytes = fixed.toBytes()
XCTAssertEqual(try AnyCustomFee.fromBytes(bytes), fixed)
}

internal func testFractionalFromToBytes() throws {
let fractional = try AnyCustomFee.fromProtobuf(Self.customFractionalFee)
let bytes = fractional.toBytes()
XCTAssertEqual(try AnyCustomFee.fromBytes(bytes), fractional)
}

internal func testRoyaltyFromToBytes() throws {
let royalty = try AnyCustomFee.fromProtobuf(Self.customRoyaltyFee)
let bytes = royalty.toBytes()
XCTAssertEqual(try AnyCustomFee.fromBytes(bytes), royalty)
}

internal func testFixedFromProtobuf() throws {
assertSnapshot(matching: try AnyCustomFee.fromProtobuf(Self.customFixedFee), as: .description)
}

internal func testFractionalFromProtobuf() throws {
assertSnapshot(matching: try AnyCustomFee.fromProtobuf(Self.customFractionalFee), as: .description)
}

internal func testRoyaltyFromProtobuf() throws {
assertSnapshot(matching: try AnyCustomFee.fromProtobuf(Self.customRoyaltyFee), as: .description)
}
}
64 changes: 64 additions & 0 deletions Tests/HederaTests/FeeScheduleTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/*
* ‌
* 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 FeeScheduleTests: XCTestCase {
private static func makeFeeComponent(_ min: UInt64, _ max: UInt64) -> FeeComponents {
FeeComponents(
min: min, max: max, constant: 0, bandwidthByte: 0, verification: 0, storageByteHour: 0, ramByteHour: 0,
contractTransactionGas: 0, transferVolumeHbar: 0, responseMemoryByte: 0, responseDiskByte: 0)
}

private let feeSchedule: FeeSchedule =
FeeSchedule.init(
transactionFeeSchedules: [
TransactionFeeSchedule(
requestType: nil,
fees: [
FeeData.init(
node: makeFeeComponent(0, 0), network: makeFeeComponent(2, 5),
service: makeFeeComponent(0, 0), kind: FeeDataType.default)
])
], expirationTime: Timestamp(seconds: 1_554_158_542, subSecondNanos: 0))

internal func testSerialize() throws {
assertSnapshot(matching: feeSchedule, as: .description)
}

internal func testToFromBytes() throws {
assertSnapshot(matching: try FeeSchedule.fromBytes(feeSchedule.toBytes()), as: .description)
}

internal func testFromProtobuf() throws {
let feeSchedule = try FeeSchedule.fromProtobuf(feeSchedule.toProtobuf())

assertSnapshot(matching: feeSchedule, as: .description)
}

internal func testToProtobuf() throws {
let protoFeeSchedule = feeSchedule.toProtobuf()

assertSnapshot(matching: protoFeeSchedule, as: .description)
}
}
48 changes: 48 additions & 0 deletions Tests/HederaTests/LedgerIdTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*
* ‌
* 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 SnapshotTesting
import XCTest

import struct HederaProtobufs.Proto_Key

@testable import Hedera

internal final class LedgerIdTests: XCTestCase {
internal func testToString() throws {
XCTAssertEqual(LedgerId.mainnet.toString(), "mainnet")
XCTAssertEqual(LedgerId.testnet.toString(), "testnet")
XCTAssertEqual(LedgerId.previewnet.toString(), "previewnet")
XCTAssertEqual(LedgerId.fromBytes(Data([0x00, 0xFF, 0x00, 0xFF])), "00FF00FF")
}

internal func testFromString() throws {
XCTAssertEqual(LedgerId.fromString("mainnet"), LedgerId.mainnet)
XCTAssertEqual(LedgerId.fromString("testnet"), LedgerId.testnet)
XCTAssertEqual(LedgerId.fromString("previewnet"), LedgerId.previewnet)
XCTAssertEqual(LedgerId.fromString("00ff00ff"), LedgerId.fromBytes(Data([0x00, 0xFF, 0x00, 0xFF])))
XCTAssertEqual(LedgerId.fromString("00FF00FF"), LedgerId.fromBytes(Data([0x00, 0xFF, 0x00, 0xFF])))
}

internal func testToBytes() throws {
let bytes = Data([0x00, 0xFF, 0x00, 0xFF])
XCTAssertEqual(LedgerId.fromBytes(Data([0x00, 0xFF, 0x00, 0xFF])).bytes, bytes)
}
}
1 change: 0 additions & 1 deletion Tests/HederaTests/StakingInfoTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ import XCTest
@testable import Hedera

internal final class StakingInfoTests: XCTestCase {

private static let stakingInfoAccount: Proto_StakingInfo = .with { proto in
proto.declineReward = true
proto.stakePeriodStart = Resources.validStart.toProtobuf()
Expand Down
42 changes: 42 additions & 0 deletions Tests/HederaTests/TokenNftTransferTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
* ‌
* 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 HederaProtobufs
import SnapshotTesting
import XCTest

@testable import Hedera

internal class TokenNftTransferTests: XCTestCase {
internal static let testReceiver = AccountId("0.0.5008")
internal static let testSerialNumber = 4

private func makeTransfer() throws -> TokenNftTransfer {
TokenNftTransfer.init(
tokenId: Resources.tokenId, sender: Resources.accountId, receiver: Self.testReceiver, serial: 4,
isApproved: true)
}

internal func testSerialize() throws {
let transfer = try makeTransfer()

assertSnapshot(of: transfer, as: .description)
}
}
Loading

0 comments on commit 48fdb78

Please sign in to comment.