Skip to content

Commit

Permalink
add tests for toAddress() and toPubKey()
Browse files Browse the repository at this point in the history
  • Loading branch information
kroggen committed Oct 18, 2023
1 parent ffc79be commit 3aa380a
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 1 deletion.
2 changes: 1 addition & 1 deletion contract/system_module.c
Original file line number Diff line number Diff line change
Expand Up @@ -545,7 +545,7 @@ static const luaL_Reg system_lib_v4[] = {
{"time", os_time},
{"difftime", os_difftime},
{"random", lua_random},
{"toPubkey", toPubkey},
{"toPubKey", toPubkey},
{"toAddress", toAddress},
{"isContract", is_contract},
{"isFeeDelegation", is_fee_delegation},
Expand Down
11 changes: 11 additions & 0 deletions contract/vm_dummy/test_files/contract_system.lua
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,14 @@ function testState()
end

abi.register(testState)


function to_address(pubkey)
return system.toAddress(pubkey)
end

function to_pubkey(address)
return system.toPubKey(address)
end

abi.register_view(to_address, to_pubkey)
47 changes: 47 additions & 0 deletions contract/vm_dummy/vm_dummy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -393,6 +393,53 @@ func TestContractSystem(t *testing.T) {
exRv := fmt.Sprintf(`["%s","6FbDRScGruVdATaNWzD51xJkTfYCVwxSZDb7gzqCLzwf","AmhNNBNY7XFk4p5ym4CJf8nTcRTEHjWzAeXJfhP71244CjBCAQU3",%d,3,999]`, StrToAddress("user1"), bc.cBlock.Header.Timestamp/1e9)
assert.Equal(t, exRv, receipt.GetRet(), "receipt ret error")

if version >= 4 {

// system.toPubKey()

err = bc.Query("system", `{"Name":"to_pubkey", "Args":["AmgKtCaGjH4XkXwny2Jb1YH5gdsJGJh78ibWEgLmRWBS5LMfQuTf"]}`, "", `"0x0c3270bb25fea5bf0029b57e78581647a143265810b84940dd24e543ddc618ab91"`)
require.NoErrorf(t, err, "failed to query")

err = bc.Query("system", `{"Name":"to_pubkey", "Args":["Amhmj6kKZz7mPstBAPJWRe1e8RHP7bZ5pV35XatqTHMWeAVSyMkc"]}`, "", `"0x0cf0d0fd04f44db75d66409346102167d67c40a5d76d46748fc4533f0265d0f83f"`)
require.NoErrorf(t, err, "failed to query")

err = bc.Query("system", `{"Name":"to_pubkey", "Args":["6FbDRScGruVdATaNWzD51xJkTfYCVwxSZDb7gzqCLzwf"]}`, "invalid address length", "")
require.NoErrorf(t, err, "failed to query")

err = bc.Query("system", `{"Name":"to_pubkey", "Args":["0x0c3270bb25fea5bf0029b57e78581647a143265810b84940dd24e543ddc618ab91"]}`, "invalid address length", "")
require.NoErrorf(t, err, "failed to query")

err = bc.Query("system", `{"Name":"to_pubkey", "Args":[""]}`, "invalid address length", "")
require.NoErrorf(t, err, "failed to query")

err = bc.Query("system", `{"Name":"to_pubkey", "Args":[]}`, "string expected, got nil", "")
require.NoErrorf(t, err, "failed to query")

// system.toAddress()

err = bc.Query("system", `{"Name":"to_address", "Args":["0x0c3270bb25fea5bf0029b57e78581647a143265810b84940dd24e543ddc618ab91"]}`, "", `"AmgKtCaGjH4XkXwny2Jb1YH5gdsJGJh78ibWEgLmRWBS5LMfQuTf"`)
require.NoErrorf(t, err, "failed to query")

err = bc.Query("system", `{"Name":"to_address", "Args":["0x0cf0d0fd04f44db75d66409346102167d67c40a5d76d46748fc4533f0265d0f83f"]}`, "", `"Amhmj6kKZz7mPstBAPJWRe1e8RHP7bZ5pV35XatqTHMWeAVSyMkc"`)
require.NoErrorf(t, err, "failed to query")

err = bc.Query("system", `{"Name":"to_address", "Args":["0cf0d0fd04f44db75d66409346102167d67c40a5d76d46748fc4533f0265d0f83f"]}`, "", `"Amhmj6kKZz7mPstBAPJWRe1e8RHP7bZ5pV35XatqTHMWeAVSyMkc"`)
require.NoErrorf(t, err, "failed to query")

err = bc.Query("system", `{"Name":"to_address", "Args":["AmhNNBNY7XFk4p5ym4CJf8nTcRTEHjWzAeXJfhP71244CjBCAQU3"]}`, "invalid public key", "")
require.NoErrorf(t, err, "failed to query")

err = bc.Query("system", `{"Name":"to_address", "Args":["6FbDRScGruVdATaNWzD51xJkTfYCVwxSZDb7gzqCLzwf"]}`, "invalid public key", "")
require.NoErrorf(t, err, "failed to query")

err = bc.Query("system", `{"Name":"to_address", "Args":[""]}`, "invalid public key", "")
require.NoErrorf(t, err, "failed to query")

err = bc.Query("system", `{"Name":"to_address", "Args":[]}`, "string expected, got nil", "")
require.NoErrorf(t, err, "failed to query")

}

}
}

Expand Down

0 comments on commit 3aa380a

Please sign in to comment.