-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathsolc_test.go
218 lines (203 loc) · 5.9 KB
/
solc_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
package solc
import (
"fmt"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
type args struct {
sources map[string]SourceIn
}
type res struct {
errorsLen int
bytecode map[string]map[string]Bytecode
methodIdentiers map[string]map[string]map[string]string
abisLen map[string]map[string]int
}
type testCase struct {
name string
commit string
args args
expectErr bool
expectRes res
}
func TestSolc(t *testing.T) {
tests := []testCase{
// Solc 0.6.2 with pragma ^0.6.1
{
"Solc 0.6.2 with pragma ^0.6.1",
"0.6.2+commit.bacdbe57",
args{
sources: map[string]SourceIn{
"One.sol": SourceIn{Content: "pragma solidity ^0.6.1; contract One { function one() public pure returns (uint) { return 1; } }"},
},
},
false,
res{
bytecode: map[string]map[string]Bytecode{
"One.sol": map[string]Bytecode{
"One": Bytecode{Object: "6080604052348015600f57600080fd5b50609c8061001e6000396000f3fe6080604052348015600f57600080fd5b50600436106044577c01000000000000000000000000000000000000000000000000000000006000350463901717d181146049575b600080fd5b604f6061565b60408051918252519081900360200190f35b60019056fea26469706673582212208c7c407543955dc2f62329d58792b557b7b6776ac58353f0d17e7ec75f2d3bfd64736f6c63430006020033"},
},
},
abisLen: map[string]map[string]int{
"One.sol": map[string]int{"One": 1},
},
methodIdentiers: map[string]map[string]map[string]string{
"One.sol": map[string]map[string]string{
"One": map[string]string{"one()": "901717d1"},
},
},
},
},
// Solc 0.6.2 with pragma ^0.4.3
{
"Solc 0.6.2 with pragma ^0.4.3",
"0.6.2+commit.bacdbe57",
args{
sources: map[string]SourceIn{
"One.sol": SourceIn{Content: "pragma solidity ^0.4.3; contract One { function one() public pure returns (uint) { return 1; } }"},
},
},
false,
res{
errorsLen: 1,
},
},
// Solc 0.5.9 with pragma ^0.6.2 (Invalid)
{
"Solc 0.5.9 with pragma ^0.6.2",
"0.5.9+commit.e560f70d",
args{
sources: map[string]SourceIn{
"One.sol": SourceIn{Content: "pragma solidity ^0.6.2; contract One { function one() public pure returns (uint) { return 1; } }"},
},
},
false,
res{
errorsLen: 1,
},
},
// Solc 0.5.9 with pragma ^0.5.2
{
"Solc 0.5.9 with pragma ^0.5.2",
"0.5.9+commit.e560f70d",
args{
sources: map[string]SourceIn{
"One.sol": SourceIn{Content: "pragma solidity ^0.5.2; contract One { function one() public pure returns (uint) { return 1; } }"},
},
},
false,
res{
bytecode: map[string]map[string]Bytecode{
"One.sol": map[string]Bytecode{
"One": Bytecode{Object: "6080604052348015600f57600080fd5b50609b8061001e6000396000f3fe6080604052348015600f57600080fd5b50600436106044577c01000000000000000000000000000000000000000000000000000000006000350463901717d181146049575b600080fd5b604f6061565b60408051918252519081900360200190f35b60019056fea265627a7a72305820690bfd951ab80f52d55fa4f9af420c83a8870e28e4913ed147d0aa31bd85c5db64736f6c63430005090032"},
},
},
abisLen: map[string]map[string]int{
"One.sol": map[string]int{"One": 1},
},
methodIdentiers: map[string]map[string]map[string]string{
"One.sol": map[string]map[string]string{
"One": map[string]string{"one()": "901717d1"},
},
},
},
},
}
for _, test := range tests {
t.Run(
test.name,
func(t *testing.T) {
testSolc(t, test)
},
)
}
}
func testSolc(t *testing.T, test testCase) {
// Read Solsjon file
solc, err := NewFromFile(fmt.Sprintf("./solc-bin/soljson-v%v.js", test.commit))
require.NoError(t, err, "Creating Solc from valid solc emscripten binary should not error")
// Test License and Version methods
assert.Greater(t, len(solc.License()), 10, "License should be valid")
assert.Equal(t, fmt.Sprintf("%v.Emscripten.clang", test.commit), solc.Version(), "Version should be correct")
// Prepare Compilation input
in := &Input{
Language: "Solidity",
Sources: test.args.sources,
Settings: Settings{
Optimizer: Optimizer{
Enabled: true,
Runs: 200,
},
EVMVersion: "byzantium",
OutputSelection: map[string]map[string][]string{
"*": map[string][]string{
"*": []string{
"abi",
"devdoc",
"userdoc",
"metadata",
"ir",
"irOptimized",
"storageLayout",
"evm.bytecode.object",
"evm.bytecode.sourceMap",
"evm.bytecode.linkReferences",
"evm.deployedBytecode.object",
"evm.deployedBytecode.sourceMap",
"evm.deployedBytecode.linkReferences",
"evm.methodIdentifiers",
"evm.gasEstimates",
},
"": []string{
"ast",
"legacyAST",
},
},
},
},
}
// Run compilation
out, err := solc.Compile(in)
if !test.expectErr {
require.NoErrorf(t, err, "Compile should not error")
} else {
require.Errorf(t, err, "Compile should error")
}
// Test Errors
require.Len(t, out.Errors, test.expectRes.errorsLen, "Invalid count of compilation error")
// Test Bytecode
for source, bytecodes := range test.expectRes.bytecode {
for contract, bytecode := range bytecodes {
assert.Equal(
t,
bytecode.Object,
out.Contracts[source][contract].EVM.Bytecode.Object,
"%v@%v: Bytecode does not match", contract, source,
)
}
}
// Test ABIs
for source, abiLens := range test.expectRes.abisLen {
for contract, abiLen := range abiLens {
assert.Len(
t,
out.Contracts[source][contract].ABI,
abiLen,
"%v@%v: Incorrect ABI lenght", contract, source,
)
}
}
// Test method identifiers
for source, contracts := range test.expectRes.methodIdentiers {
for contract, methodIdentiers := range contracts {
for method, methodIdentier := range methodIdentiers {
assert.Equal(
t,
methodIdentier,
out.Contracts[source][contract].EVM.MethodIdentifiers[method],
"%v.%v@%v: Method identifier does not match", contract, method, source)
}
}
}
}