You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

178 lines
5.2 KiB

  1. package types
  2. import (
  3. "testing"
  4. acm "github.com/tendermint/tendermint/account"
  5. . "github.com/tendermint/tendermint/common"
  6. _ "github.com/tendermint/tendermint/config/tendermint_test"
  7. ptypes "github.com/tendermint/tendermint/permission/types"
  8. )
  9. var chainID string
  10. func init() {
  11. chainID = config.GetString("chain_id")
  12. }
  13. func TestSendTxSignable(t *testing.T) {
  14. sendTx := &SendTx{
  15. Inputs: []*TxInput{
  16. &TxInput{
  17. Address: []byte("input1"),
  18. Amount: 12345,
  19. Sequence: 67890,
  20. },
  21. &TxInput{
  22. Address: []byte("input2"),
  23. Amount: 111,
  24. Sequence: 222,
  25. },
  26. },
  27. Outputs: []*TxOutput{
  28. &TxOutput{
  29. Address: []byte("output1"),
  30. Amount: 333,
  31. },
  32. &TxOutput{
  33. Address: []byte("output2"),
  34. Amount: 444,
  35. },
  36. },
  37. }
  38. signBytes := acm.SignBytes(chainID, sendTx)
  39. signStr := string(signBytes)
  40. expected := Fmt(`{"chain_id":"%s","tx":[1,{"inputs":[{"address":"696E70757431","amount":12345,"sequence":67890},{"address":"696E70757432","amount":111,"sequence":222}],"outputs":[{"address":"6F757470757431","amount":333},{"address":"6F757470757432","amount":444}]}]}`,
  41. config.GetString("chain_id"))
  42. if signStr != expected {
  43. t.Errorf("Got unexpected sign string for SendTx. Expected:\n%v\nGot:\n%v", expected, signStr)
  44. }
  45. }
  46. func TestCallTxSignable(t *testing.T) {
  47. callTx := &CallTx{
  48. Input: &TxInput{
  49. Address: []byte("input1"),
  50. Amount: 12345,
  51. Sequence: 67890,
  52. },
  53. Address: []byte("contract1"),
  54. GasLimit: 111,
  55. Fee: 222,
  56. Data: []byte("data1"),
  57. }
  58. signBytes := acm.SignBytes(chainID, callTx)
  59. signStr := string(signBytes)
  60. expected := Fmt(`{"chain_id":"%s","tx":[2,{"address":"636F6E747261637431","data":"6461746131","fee":222,"gas_limit":111,"input":{"address":"696E70757431","amount":12345,"sequence":67890}}]}`,
  61. config.GetString("chain_id"))
  62. if signStr != expected {
  63. t.Errorf("Got unexpected sign string for CallTx. Expected:\n%v\nGot:\n%v", expected, signStr)
  64. }
  65. }
  66. func TestNameTxSignable(t *testing.T) {
  67. nameTx := &NameTx{
  68. Input: &TxInput{
  69. Address: []byte("input1"),
  70. Amount: 12345,
  71. Sequence: 250,
  72. },
  73. Name: "google.com",
  74. Data: "secretly.not.google.com",
  75. Fee: 1000,
  76. }
  77. signBytes := acm.SignBytes(chainID, nameTx)
  78. signStr := string(signBytes)
  79. expected := Fmt(`{"chain_id":"%s","tx":[3,{"data":"secretly.not.google.com","fee":1000,"input":{"address":"696E70757431","amount":12345,"sequence":250},"name":"google.com"}]}`,
  80. config.GetString("chain_id"))
  81. if signStr != expected {
  82. t.Errorf("Got unexpected sign string for CallTx. Expected:\n%v\nGot:\n%v", expected, signStr)
  83. }
  84. }
  85. func TestBondTxSignable(t *testing.T) {
  86. privKeyBytes := make([]byte, 64)
  87. privAccount := acm.GenPrivAccountFromPrivKeyBytes(privKeyBytes)
  88. bondTx := &BondTx{
  89. PubKey: privAccount.PubKey.(acm.PubKeyEd25519),
  90. Inputs: []*TxInput{
  91. &TxInput{
  92. Address: []byte("input1"),
  93. Amount: 12345,
  94. Sequence: 67890,
  95. },
  96. &TxInput{
  97. Address: []byte("input2"),
  98. Amount: 111,
  99. Sequence: 222,
  100. },
  101. },
  102. UnbondTo: []*TxOutput{
  103. &TxOutput{
  104. Address: []byte("output1"),
  105. Amount: 333,
  106. },
  107. &TxOutput{
  108. Address: []byte("output2"),
  109. Amount: 444,
  110. },
  111. },
  112. }
  113. signBytes := acm.SignBytes(chainID, bondTx)
  114. signStr := string(signBytes)
  115. expected := Fmt(`{"chain_id":"%s","tx":[17,{"inputs":[{"address":"696E70757431","amount":12345,"sequence":67890},{"address":"696E70757432","amount":111,"sequence":222}],"pub_key":[1,"3B6A27BCCEB6A42D62A3A8D02A6F0D73653215771DE243A63AC048A18B59DA29"],"unbond_to":[{"address":"6F757470757431","amount":333},{"address":"6F757470757432","amount":444}]}]}`,
  116. config.GetString("chain_id"))
  117. if signStr != expected {
  118. t.Errorf("Unexpected sign string for BondTx. \nGot %s\nExpected %s", signStr, expected)
  119. }
  120. }
  121. func TestUnbondTxSignable(t *testing.T) {
  122. unbondTx := &UnbondTx{
  123. Address: []byte("address1"),
  124. Height: 111,
  125. }
  126. signBytes := acm.SignBytes(chainID, unbondTx)
  127. signStr := string(signBytes)
  128. expected := Fmt(`{"chain_id":"%s","tx":[18,{"address":"6164647265737331","height":111}]}`,
  129. config.GetString("chain_id"))
  130. if signStr != expected {
  131. t.Errorf("Got unexpected sign string for UnbondTx")
  132. }
  133. }
  134. func TestRebondTxSignable(t *testing.T) {
  135. rebondTx := &RebondTx{
  136. Address: []byte("address1"),
  137. Height: 111,
  138. }
  139. signBytes := acm.SignBytes(chainID, rebondTx)
  140. signStr := string(signBytes)
  141. expected := Fmt(`{"chain_id":"%s","tx":[19,{"address":"6164647265737331","height":111}]}`,
  142. config.GetString("chain_id"))
  143. if signStr != expected {
  144. t.Errorf("Got unexpected sign string for RebondTx")
  145. }
  146. }
  147. func TestPermissionsTxSignable(t *testing.T) {
  148. permsTx := &PermissionsTx{
  149. Input: &TxInput{
  150. Address: []byte("input1"),
  151. Amount: 12345,
  152. Sequence: 250,
  153. },
  154. PermArgs: &ptypes.SetBaseArgs{
  155. Address: []byte("address1"),
  156. Permission: 1,
  157. Value: true,
  158. },
  159. }
  160. signBytes := acm.SignBytes(chainID, permsTx)
  161. signStr := string(signBytes)
  162. expected := Fmt(`{"chain_id":"%s","tx":[32,{"args":"[2,{"address":"6164647265737331","permission":1,"value":true}]","input":{"address":"696E70757431","amount":12345,"sequence":250}}]}`,
  163. config.GetString("chain_id"))
  164. if signStr != expected {
  165. t.Errorf("Got unexpected sign string for CallTx. Expected:\n%v\nGot:\n%v", expected, signStr)
  166. }
  167. }