api_test.go 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387
  1. // Copyright 2018 The go-ethereum Authors
  2. // This file is part of go-ethereum.
  3. //
  4. // go-ethereum is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // go-ethereum is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU General Public License
  15. // along with go-ethereum. If not, see <http://www.gnu.org/licenses/>.
  16. //
  17. package core
  18. import (
  19. "bytes"
  20. "context"
  21. "fmt"
  22. "io/ioutil"
  23. "math/big"
  24. "os"
  25. "path/filepath"
  26. "testing"
  27. "time"
  28. "github.com/ethereum/go-ethereum/accounts/keystore"
  29. "github.com/ethereum/go-ethereum/cmd/utils"
  30. "github.com/ethereum/go-ethereum/common"
  31. "github.com/ethereum/go-ethereum/common/hexutil"
  32. "github.com/ethereum/go-ethereum/core/types"
  33. "github.com/ethereum/go-ethereum/internal/ethapi"
  34. "github.com/ethereum/go-ethereum/rlp"
  35. )
  36. //Used for testing
  37. type HeadlessUI struct {
  38. controller chan string
  39. }
  40. func (ui *HeadlessUI) OnSignerStartup(info StartupInfo) {
  41. }
  42. func (ui *HeadlessUI) OnApprovedTx(tx ethapi.SignTransactionResult) {
  43. fmt.Printf("OnApproved called")
  44. }
  45. func (ui *HeadlessUI) ApproveTx(request *SignTxRequest) (SignTxResponse, error) {
  46. switch <-ui.controller {
  47. case "Y":
  48. return SignTxResponse{request.Transaction, true, <-ui.controller}, nil
  49. case "M": //Modify
  50. old := big.Int(request.Transaction.Value)
  51. newVal := big.NewInt(0).Add(&old, big.NewInt(1))
  52. request.Transaction.Value = hexutil.Big(*newVal)
  53. return SignTxResponse{request.Transaction, true, <-ui.controller}, nil
  54. default:
  55. return SignTxResponse{request.Transaction, false, ""}, nil
  56. }
  57. }
  58. func (ui *HeadlessUI) ApproveSignData(request *SignDataRequest) (SignDataResponse, error) {
  59. if "Y" == <-ui.controller {
  60. return SignDataResponse{true, <-ui.controller}, nil
  61. }
  62. return SignDataResponse{false, ""}, nil
  63. }
  64. func (ui *HeadlessUI) ApproveExport(request *ExportRequest) (ExportResponse, error) {
  65. return ExportResponse{<-ui.controller == "Y"}, nil
  66. }
  67. func (ui *HeadlessUI) ApproveImport(request *ImportRequest) (ImportResponse, error) {
  68. if "Y" == <-ui.controller {
  69. return ImportResponse{true, <-ui.controller, <-ui.controller}, nil
  70. }
  71. return ImportResponse{false, "", ""}, nil
  72. }
  73. func (ui *HeadlessUI) ApproveListing(request *ListRequest) (ListResponse, error) {
  74. switch <-ui.controller {
  75. case "A":
  76. return ListResponse{request.Accounts}, nil
  77. case "1":
  78. l := make([]Account, 1)
  79. l[0] = request.Accounts[1]
  80. return ListResponse{l}, nil
  81. default:
  82. return ListResponse{nil}, nil
  83. }
  84. }
  85. func (ui *HeadlessUI) ApproveNewAccount(request *NewAccountRequest) (NewAccountResponse, error) {
  86. if "Y" == <-ui.controller {
  87. return NewAccountResponse{true, <-ui.controller}, nil
  88. }
  89. return NewAccountResponse{false, ""}, nil
  90. }
  91. func (ui *HeadlessUI) ShowError(message string) {
  92. //stdout is used by communication
  93. fmt.Fprint(os.Stderr, message)
  94. }
  95. func (ui *HeadlessUI) ShowInfo(message string) {
  96. //stdout is used by communication
  97. fmt.Fprint(os.Stderr, message)
  98. }
  99. func tmpDirName(t *testing.T) string {
  100. d, err := ioutil.TempDir("", "eth-keystore-test")
  101. if err != nil {
  102. t.Fatal(err)
  103. }
  104. d, err = filepath.EvalSymlinks(d)
  105. if err != nil {
  106. t.Fatal(err)
  107. }
  108. return d
  109. }
  110. func setup(t *testing.T) (*SignerAPI, chan string) {
  111. controller := make(chan string, 10)
  112. db, err := NewAbiDBFromFile("../../cmd/clef/4byte.json")
  113. if err != nil {
  114. utils.Fatalf(err.Error())
  115. }
  116. var (
  117. ui = &HeadlessUI{controller}
  118. api = NewSignerAPI(
  119. 1,
  120. tmpDirName(t),
  121. true,
  122. ui,
  123. db,
  124. true)
  125. )
  126. return api, controller
  127. }
  128. func createAccount(control chan string, api *SignerAPI, t *testing.T) {
  129. control <- "Y"
  130. control <- "apassword"
  131. _, err := api.New(context.Background())
  132. if err != nil {
  133. t.Fatal(err)
  134. }
  135. // Some time to allow changes to propagate
  136. time.Sleep(250 * time.Millisecond)
  137. }
  138. func failCreateAccount(control chan string, api *SignerAPI, t *testing.T) {
  139. control <- "N"
  140. acc, err := api.New(context.Background())
  141. if err != ErrRequestDenied {
  142. t.Fatal(err)
  143. }
  144. if acc.Address != (common.Address{}) {
  145. t.Fatal("Empty address should be returned")
  146. }
  147. }
  148. func list(control chan string, api *SignerAPI, t *testing.T) []Account {
  149. control <- "A"
  150. list, err := api.List(context.Background())
  151. if err != nil {
  152. t.Fatal(err)
  153. }
  154. return list
  155. }
  156. func TestNewAcc(t *testing.T) {
  157. api, control := setup(t)
  158. verifyNum := func(num int) {
  159. if list := list(control, api, t); len(list) != num {
  160. t.Errorf("Expected %d accounts, got %d", num, len(list))
  161. }
  162. }
  163. // Testing create and create-deny
  164. createAccount(control, api, t)
  165. createAccount(control, api, t)
  166. failCreateAccount(control, api, t)
  167. failCreateAccount(control, api, t)
  168. createAccount(control, api, t)
  169. failCreateAccount(control, api, t)
  170. createAccount(control, api, t)
  171. failCreateAccount(control, api, t)
  172. verifyNum(4)
  173. // Testing listing:
  174. // Listing one Account
  175. control <- "1"
  176. list, err := api.List(context.Background())
  177. if err != nil {
  178. t.Fatal(err)
  179. }
  180. if len(list) != 1 {
  181. t.Fatalf("List should only show one Account")
  182. }
  183. // Listing denied
  184. control <- "Nope"
  185. list, err = api.List(context.Background())
  186. if len(list) != 0 {
  187. t.Fatalf("List should be empty")
  188. }
  189. if err != ErrRequestDenied {
  190. t.Fatal("Expected deny")
  191. }
  192. }
  193. func TestSignData(t *testing.T) {
  194. api, control := setup(t)
  195. //Create two accounts
  196. createAccount(control, api, t)
  197. createAccount(control, api, t)
  198. control <- "1"
  199. list, err := api.List(context.Background())
  200. if err != nil {
  201. t.Fatal(err)
  202. }
  203. a := common.NewMixedcaseAddress(list[0].Address)
  204. control <- "Y"
  205. control <- "wrongpassword"
  206. h, err := api.Sign(context.Background(), a, []byte("EHLO world"))
  207. if h != nil {
  208. t.Errorf("Expected nil-data, got %x", h)
  209. }
  210. if err != keystore.ErrDecrypt {
  211. t.Errorf("Expected ErrLocked! %v", err)
  212. }
  213. control <- "No way"
  214. h, err = api.Sign(context.Background(), a, []byte("EHLO world"))
  215. if h != nil {
  216. t.Errorf("Expected nil-data, got %x", h)
  217. }
  218. if err != ErrRequestDenied {
  219. t.Errorf("Expected ErrRequestDenied! %v", err)
  220. }
  221. control <- "Y"
  222. control <- "apassword"
  223. h, err = api.Sign(context.Background(), a, []byte("EHLO world"))
  224. if err != nil {
  225. t.Fatal(err)
  226. }
  227. if h == nil || len(h) != 65 {
  228. t.Errorf("Expected 65 byte signature (got %d bytes)", len(h))
  229. }
  230. }
  231. func mkTestTx(from common.MixedcaseAddress) SendTxArgs {
  232. to := common.NewMixedcaseAddress(common.HexToAddress("0x1337"))
  233. gas := hexutil.Uint64(21000)
  234. gasPrice := (hexutil.Big)(*big.NewInt(2000000000))
  235. value := (hexutil.Big)(*big.NewInt(1e18))
  236. nonce := (hexutil.Uint64)(0)
  237. data := hexutil.Bytes(common.Hex2Bytes("01020304050607080a"))
  238. tx := SendTxArgs{
  239. From: from,
  240. To: &to,
  241. Gas: gas,
  242. GasPrice: gasPrice,
  243. Value: value,
  244. Data: &data,
  245. Nonce: nonce}
  246. return tx
  247. }
  248. func TestSignTx(t *testing.T) {
  249. var (
  250. list Accounts
  251. res, res2 *ethapi.SignTransactionResult
  252. err error
  253. )
  254. api, control := setup(t)
  255. createAccount(control, api, t)
  256. control <- "A"
  257. list, err = api.List(context.Background())
  258. if err != nil {
  259. t.Fatal(err)
  260. }
  261. a := common.NewMixedcaseAddress(list[0].Address)
  262. methodSig := "test(uint)"
  263. tx := mkTestTx(a)
  264. control <- "Y"
  265. control <- "wrongpassword"
  266. res, err = api.SignTransaction(context.Background(), tx, &methodSig)
  267. if res != nil {
  268. t.Errorf("Expected nil-response, got %v", res)
  269. }
  270. if err != keystore.ErrDecrypt {
  271. t.Errorf("Expected ErrLocked! %v", err)
  272. }
  273. control <- "No way"
  274. res, err = api.SignTransaction(context.Background(), tx, &methodSig)
  275. if res != nil {
  276. t.Errorf("Expected nil-response, got %v", res)
  277. }
  278. if err != ErrRequestDenied {
  279. t.Errorf("Expected ErrRequestDenied! %v", err)
  280. }
  281. control <- "Y"
  282. control <- "apassword"
  283. res, err = api.SignTransaction(context.Background(), tx, &methodSig)
  284. if err != nil {
  285. t.Fatal(err)
  286. }
  287. parsedTx := &types.Transaction{}
  288. rlp.Decode(bytes.NewReader(res.Raw), parsedTx)
  289. //The tx should NOT be modified by the UI
  290. if parsedTx.Value().Cmp(tx.Value.ToInt()) != 0 {
  291. t.Errorf("Expected value to be unchanged, expected %v got %v", tx.Value, parsedTx.Value())
  292. }
  293. control <- "Y"
  294. control <- "apassword"
  295. res2, err = api.SignTransaction(context.Background(), tx, &methodSig)
  296. if err != nil {
  297. t.Fatal(err)
  298. }
  299. if !bytes.Equal(res.Raw, res2.Raw) {
  300. t.Error("Expected tx to be unmodified by UI")
  301. }
  302. //The tx is modified by the UI
  303. control <- "M"
  304. control <- "apassword"
  305. res2, err = api.SignTransaction(context.Background(), tx, &methodSig)
  306. if err != nil {
  307. t.Fatal(err)
  308. }
  309. parsedTx2 := &types.Transaction{}
  310. rlp.Decode(bytes.NewReader(res.Raw), parsedTx2)
  311. //The tx should be modified by the UI
  312. if parsedTx2.Value().Cmp(tx.Value.ToInt()) != 0 {
  313. t.Errorf("Expected value to be unchanged, got %v", parsedTx.Value())
  314. }
  315. if bytes.Equal(res.Raw, res2.Raw) {
  316. t.Error("Expected tx to be modified by UI")
  317. }
  318. }
  319. /*
  320. func TestAsyncronousResponses(t *testing.T){
  321. //Set up one account
  322. api, control := setup(t)
  323. createAccount(control, api, t)
  324. // Two transactions, the second one with larger value than the first
  325. tx1 := mkTestTx()
  326. newVal := big.NewInt(0).Add((*big.Int) (tx1.Value), big.NewInt(1))
  327. tx2 := mkTestTx()
  328. tx2.Value = (*hexutil.Big)(newVal)
  329. control <- "W" //wait
  330. control <- "Y" //
  331. control <- "apassword"
  332. control <- "Y" //
  333. control <- "apassword"
  334. var err error
  335. h1, err := api.SignTransaction(context.Background(), common.HexToAddress("1111"), tx1, nil)
  336. h2, err := api.SignTransaction(context.Background(), common.HexToAddress("2222"), tx2, nil)
  337. }
  338. */