github.com/jeffallen/go-ethereum@v1.1.4-0.20150910155051-571d3236c49c/rpc/api/api_test.go (about) 1 // Copyright 2015 The go-ethereum Authors 2 // This file is part of the go-ethereum library. 3 // 4 // The go-ethereum library is free software: you can redistribute it and/or modify 5 // it under the terms of the GNU Lesser 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 // The go-ethereum library 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 Lesser General Public License for more details. 13 // 14 // You should have received a copy of the GNU Lesser General Public License 15 // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>. 16 17 package api 18 19 import ( 20 "testing" 21 22 "encoding/json" 23 "strconv" 24 25 "github.com/ethereum/go-ethereum/common/compiler" 26 "github.com/ethereum/go-ethereum/eth" 27 "github.com/ethereum/go-ethereum/rpc/codec" 28 "github.com/ethereum/go-ethereum/rpc/shared" 29 "github.com/ethereum/go-ethereum/xeth" 30 ) 31 32 func TestParseApiString(t *testing.T) { 33 apis, err := ParseApiString("", codec.JSON, nil, nil) 34 if err == nil { 35 t.Errorf("Expected an err from parsing empty API string but got nil") 36 } 37 38 if len(apis) != 0 { 39 t.Errorf("Expected 0 apis from empty API string") 40 } 41 42 apis, err = ParseApiString("eth", codec.JSON, nil, nil) 43 if err != nil { 44 t.Errorf("Expected nil err from parsing empty API string but got %v", err) 45 } 46 47 if len(apis) != 1 { 48 t.Errorf("Expected 1 apis but got %d - %v", apis, apis) 49 } 50 51 apis, err = ParseApiString("eth,eth", codec.JSON, nil, nil) 52 if err != nil { 53 t.Errorf("Expected nil err from parsing empty API string but got \"%v\"", err) 54 } 55 56 if len(apis) != 2 { 57 t.Errorf("Expected 2 apis but got %d - %v", apis, apis) 58 } 59 60 apis, err = ParseApiString("eth,invalid", codec.JSON, nil, nil) 61 if err == nil { 62 t.Errorf("Expected an err but got no err") 63 } 64 65 } 66 67 const solcVersion = "0.9.23" 68 69 func TestCompileSolidity(t *testing.T) { 70 71 solc, err := compiler.New("") 72 if solc == nil { 73 t.Skip("no solc found: skip") 74 } else if solc.Version() != solcVersion { 75 t.Skip("WARNING: skipping test because of solc different version (%v, test written for %v, may need to update)", solc.Version(), solcVersion) 76 } 77 source := `contract test {\n` + 78 " /// @notice Will multiply `a` by 7." + `\n` + 79 ` function multiply(uint a) returns(uint d) {\n` + 80 ` return a * 7;\n` + 81 ` }\n` + 82 `}\n` 83 84 jsonstr := `{"jsonrpc":"2.0","method":"eth_compileSolidity","params":["` + source + `"],"id":64}` 85 86 expCode := "0x605880600c6000396000f3006000357c010000000000000000000000000000000000000000000000000000000090048063c6888fa114602e57005b603d6004803590602001506047565b8060005260206000f35b60006007820290506053565b91905056" 87 expAbiDefinition := `[{"constant":false,"inputs":[{"name":"a","type":"uint256"}],"name":"multiply","outputs":[{"name":"d","type":"uint256"}],"type":"function"}]` 88 expUserDoc := `{"methods":{"multiply(uint256)":{"notice":"Will multiply ` + "`a`" + ` by 7."}}}` 89 expDeveloperDoc := `{"methods":{}}` 90 expCompilerVersion := solc.Version() 91 expLanguage := "Solidity" 92 expLanguageVersion := "0" 93 expSource := source 94 95 eth := ð.Ethereum{} 96 xeth := xeth.NewTest(eth, nil) 97 api := NewEthApi(xeth, eth, codec.JSON) 98 99 var rpcRequest shared.Request 100 json.Unmarshal([]byte(jsonstr), &rpcRequest) 101 102 response, err := api.CompileSolidity(&rpcRequest) 103 if err != nil { 104 t.Errorf("Execution failed, %v", err) 105 } 106 107 respjson, err := json.Marshal(response) 108 if err != nil { 109 t.Errorf("expected no error, got %v", err) 110 } 111 112 var contracts = make(map[string]*compiler.Contract) 113 err = json.Unmarshal(respjson, &contracts) 114 if err != nil { 115 t.Errorf("expected no error, got %v", err) 116 } 117 118 if len(contracts) != 1 { 119 t.Errorf("expected one contract, got %v", len(contracts)) 120 } 121 122 contract := contracts["test"] 123 124 if contract.Code != expCode { 125 t.Errorf("Expected \n%s got \n%s", expCode, contract.Code) 126 } 127 128 if strconv.Quote(contract.Info.Source) != `"`+expSource+`"` { 129 t.Errorf("Expected \n'%s' got \n'%s'", expSource, strconv.Quote(contract.Info.Source)) 130 } 131 132 if contract.Info.Language != expLanguage { 133 t.Errorf("Expected %s got %s", expLanguage, contract.Info.Language) 134 } 135 136 if contract.Info.LanguageVersion != expLanguageVersion { 137 t.Errorf("Expected %s got %s", expLanguageVersion, contract.Info.LanguageVersion) 138 } 139 140 if contract.Info.CompilerVersion != expCompilerVersion { 141 t.Errorf("Expected %s got %s", expCompilerVersion, contract.Info.CompilerVersion) 142 } 143 144 userdoc, err := json.Marshal(contract.Info.UserDoc) 145 if err != nil { 146 t.Errorf("expected no error, got %v", err) 147 } 148 149 devdoc, err := json.Marshal(contract.Info.DeveloperDoc) 150 if err != nil { 151 t.Errorf("expected no error, got %v", err) 152 } 153 154 abidef, err := json.Marshal(contract.Info.AbiDefinition) 155 if err != nil { 156 t.Errorf("expected no error, got %v", err) 157 } 158 159 if string(abidef) != expAbiDefinition { 160 t.Errorf("Expected \n'%s' got \n'%s'", expAbiDefinition, string(abidef)) 161 } 162 163 if string(userdoc) != expUserDoc { 164 t.Errorf("Expected \n'%s' got \n'%s'", expUserDoc, string(userdoc)) 165 } 166 167 if string(devdoc) != expDeveloperDoc { 168 t.Errorf("Expected %s got %s", expDeveloperDoc, string(devdoc)) 169 } 170 }