|
| 1 | +/****************************************************************************** |
| 2 | + * Copyright (C) 2018-2021 aitos.io |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + *****************************************************************************/ |
| 16 | +#include "tcase_platon.h" |
| 17 | +#define EXCEED_STR_MAX_LEN 4097 |
| 18 | + |
| 19 | +#define TEST_EIP155_COMPATIBILITY BOAT_TRUE |
| 20 | +#define TEST_PLATON_CHAIN_ID 210309 |
| 21 | +#define TEST_GAS_LIMIT "0x6691B7" |
| 22 | +#define TEST_GAS_PRICE "0x4A817C800" |
| 23 | +#define TEST_IS_SYNC_TX BOAT_TRUE |
| 24 | +#define TEST_RECIPIENT_ADDRESS "lat159js9hw63x2y8m65mhgprm3eu9f4c7xmv3lym4" |
| 25 | + |
| 26 | +const BCHAR *hrp = "lat"; |
| 27 | + |
| 28 | +extern BoatPlatONWalletConfig get_platon_wallet_settings(); |
| 29 | + |
| 30 | +START_TEST(test_006GetBalance_0001GetSuccess) |
| 31 | +{ |
| 32 | + BOAT_RESULT result; |
| 33 | + BoatPlatONTx tx_ctx; |
| 34 | + BCHAR *cur_balance_wei = NULL; |
| 35 | + BoatFieldVariable parse_result = {NULL, 0}; |
| 36 | + |
| 37 | + BoatIotSdkInit(); |
| 38 | + |
| 39 | + result = platonWalletPrepare(); |
| 40 | + ck_assert(result == BOAT_SUCCESS); |
| 41 | + |
| 42 | + result = BoatPlatONTxInit(g_platon_wallet_ptr, &tx_ctx, BOAT_TRUE, NULL, |
| 43 | + "0x333333", |
| 44 | + (BCHAR *)TEST_RECIPIENT_ADDRESS); |
| 45 | + ck_assert_int_eq(result, BOAT_SUCCESS); |
| 46 | + |
| 47 | + |
| 48 | + cur_balance_wei = BoatPlatONWalletGetBalance(g_platon_wallet_ptr, TEST_RECIPIENT_ADDRESS); |
| 49 | + result = BoatPlatONParseRpcResponseStringResult(cur_balance_wei, &parse_result); |
| 50 | + |
| 51 | + ck_assert_int_eq(result, BOAT_SUCCESS); |
| 52 | + ck_assert_ptr_ne(parse_result.field_ptr, NULL); |
| 53 | + |
| 54 | + BoatFree(parse_result.field_ptr); |
| 55 | + |
| 56 | + BoatIotSdkDeInit(); |
| 57 | +} |
| 58 | +END_TEST |
| 59 | + |
| 60 | +START_TEST(test_006GetBalance_0002GetWalletDefaultAddressSuccess) |
| 61 | +{ |
| 62 | + BOAT_RESULT result; |
| 63 | + BoatPlatONTx tx_ctx; |
| 64 | + BCHAR *cur_balance_wei = NULL; |
| 65 | + BoatFieldVariable parse_result = {NULL, 0}; |
| 66 | + |
| 67 | + BoatIotSdkInit(); |
| 68 | + |
| 69 | + result = platonWalletPrepare(); |
| 70 | + ck_assert(result == BOAT_SUCCESS); |
| 71 | + |
| 72 | + result = BoatPlatONTxInit(g_platon_wallet_ptr, &tx_ctx, BOAT_TRUE, NULL, |
| 73 | + "0x333333", |
| 74 | + (BCHAR *)TEST_RECIPIENT_ADDRESS); |
| 75 | + ck_assert_int_eq(result, BOAT_SUCCESS); |
| 76 | + |
| 77 | + |
| 78 | + cur_balance_wei = BoatPlatONWalletGetBalance(g_platon_wallet_ptr, NULL); |
| 79 | + result = BoatPlatONParseRpcResponseStringResult(cur_balance_wei, &parse_result); |
| 80 | + |
| 81 | + ck_assert_ptr_ne(parse_result.field_ptr, NULL); |
| 82 | + ck_assert_int_eq(result, BOAT_SUCCESS); |
| 83 | + |
| 84 | + BoatFree(parse_result.field_ptr); |
| 85 | + |
| 86 | + BoatIotSdkDeInit(); |
| 87 | +} |
| 88 | +END_TEST |
| 89 | + |
| 90 | +START_TEST(test_006GetBalance_0003GetFailureNullWallet) |
| 91 | +{ |
| 92 | + BCHAR *cur_balance_wei = NULL; |
| 93 | + |
| 94 | + BoatIotSdkInit(); |
| 95 | + |
| 96 | + cur_balance_wei = BoatPlatONWalletGetBalance(NULL, NULL); |
| 97 | + |
| 98 | + ck_assert_ptr_eq(cur_balance_wei, NULL); |
| 99 | + |
| 100 | + BoatIotSdkDeInit(); |
| 101 | +} |
| 102 | +END_TEST |
| 103 | + |
| 104 | +START_TEST(test_007Transfer_0001TransferSuccess) |
| 105 | +{ |
| 106 | + BOAT_RESULT result; |
| 107 | + BoatPlatONTx tx_ctx; |
| 108 | + |
| 109 | + BoatIotSdkInit(); |
| 110 | + |
| 111 | + result = platonWalletPrepare(); |
| 112 | + ck_assert(result == BOAT_SUCCESS); |
| 113 | + |
| 114 | + result = BoatPlatONTxInit(g_platon_wallet_ptr, &tx_ctx, BOAT_TRUE, NULL, |
| 115 | + "0x333333", |
| 116 | + (BCHAR *)TEST_RECIPIENT_ADDRESS, hrp); |
| 117 | + ck_assert_int_eq(result, BOAT_SUCCESS); |
| 118 | + |
| 119 | + result = BoatPlatONTransfer(&tx_ctx, "0x1"); |
| 120 | + ck_assert_int_eq(result, BOAT_SUCCESS); |
| 121 | + |
| 122 | + BoatIotSdkDeInit(); |
| 123 | +} |
| 124 | +END_TEST |
| 125 | + |
| 126 | +START_TEST(test_007Transfer_0002TransferFailureNullParam) |
| 127 | +{ |
| 128 | + BOAT_RESULT result; |
| 129 | + BoatPlatONTx tx_ctx; |
| 130 | + |
| 131 | + BoatIotSdkInit(); |
| 132 | + |
| 133 | + result = platonWalletPrepare(); |
| 134 | + ck_assert(result == BOAT_SUCCESS); |
| 135 | + |
| 136 | + result = BoatPlatONTxInit(g_platon_wallet_ptr, &tx_ctx, BOAT_TRUE, NULL, |
| 137 | + "0x333333", |
| 138 | + (BCHAR *)TEST_RECIPIENT_ADDRESS); |
| 139 | + ck_assert_int_eq(result, BOAT_SUCCESS); |
| 140 | + |
| 141 | + result = BoatPlatONTransfer(&tx_ctx, NULL); |
| 142 | + ck_assert_int_eq(result, BOAT_ERROR_COMMON_INVALID_ARGUMENT); |
| 143 | + |
| 144 | + BoatIotSdkDeInit(); |
| 145 | +} |
| 146 | +END_TEST |
| 147 | + |
| 148 | +START_TEST(test_007Transfer_0003TransferWithSpecifyChainIDSuccess) |
| 149 | +{ |
| 150 | + BSINT32 rtnVal; |
| 151 | + BOAT_RESULT result; |
| 152 | + BoatPlatONTx tx_ctx; |
| 153 | + |
| 154 | + BoatIotSdkInit(); |
| 155 | + |
| 156 | + BoatPlatONWalletConfig wallet = get_platon_wallet_settings(); |
| 157 | + |
| 158 | + wallet.eip155_compatibility = BOAT_TRUE; |
| 159 | + wallet.chain_id = TEST_EPLATON_CHAIN_ID; |
| 160 | + /* 1. execute unit test */ |
| 161 | + rtnVal = BoatWalletCreate(BOAT_PROTOCOL_PLATON, NULL, &wallet, sizeof(BoatPlatONWalletConfig)); |
| 162 | + ck_assert_int_eq(rtnVal, 0); |
| 163 | + |
| 164 | + g_platon_wallet_ptr = BoatGetWalletByIndex(rtnVal); |
| 165 | + |
| 166 | + result = BoatPlatONTxInit(g_platon_wallet_ptr, &tx_ctx, BOAT_TRUE, NULL, |
| 167 | + "0x333333", |
| 168 | + (BCHAR *)TEST_RECIPIENT_ADDRESS, hrp); |
| 169 | + ck_assert_int_eq(result, BOAT_SUCCESS); |
| 170 | + |
| 171 | + result = BoatPlatONTransfer(&tx_ctx, "0x1"); |
| 172 | + ck_assert_int_eq(result, BOAT_SUCCESS); |
| 173 | + |
| 174 | + BoatIotSdkDeInit(); |
| 175 | +} |
| 176 | +END_TEST |
| 177 | + |
| 178 | +Suite *make_transactions_suite(void) |
| 179 | +{ |
| 180 | + /* Create Suite */ |
| 181 | + Suite *s_transaction = suite_create("transaction"); |
| 182 | + |
| 183 | + |
| 184 | + /* Create test cases */ |
| 185 | + TCase *tc_transaction_api = tcase_create("transaction_api"); |
| 186 | + |
| 187 | + |
| 188 | + /* Add a test case to the Suite */ |
| 189 | + suite_add_tcase(s_transaction, tc_transaction_api); |
| 190 | + |
| 191 | + /* Test cases are added to the test set */ |
| 192 | + tcase_add_test(tc_transaction_api, test_006GetBalance_0001GetSuccess); |
| 193 | + tcase_add_test(tc_transaction_api, test_006GetBalance_0002GetWalletDefaultAddressSuccess); |
| 194 | + tcase_add_test(tc_transaction_api, test_006GetBalance_0003GetFailureNullWallet); |
| 195 | + |
| 196 | + tcase_add_test(tc_transaction_api, test_007Transfer_0001TransferSuccess); |
| 197 | + tcase_add_test(tc_transaction_api, test_007Transfer_0002TransferFailureNullParam); |
| 198 | + tcase_add_test(tc_transaction_api, test_007Transfer_0003TransferWithSpecifyChainIDSuccess); |
| 199 | + |
| 200 | + return s_transaction; |
| 201 | +} |
0 commit comments