-
Notifications
You must be signed in to change notification settings - Fork 60
/
TestLogic.java
380 lines (324 loc) · 12.6 KB
/
TestLogic.java
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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
package com.algorand.algosdk.logic;
import com.google.common.collect.ImmutableList;
import org.junit.jupiter.api.Test;
import java.util.*;
import static com.algorand.algosdk.logic.Logic.*;
import static org.assertj.core.api.Assertions.*;
public class TestLogic {
@Test
public void testParseUvarint1() throws Exception {
byte[] data = {0x01};
VarintResult result = getUVarint(data, 0);
assertThat(result.length).isEqualTo(1);
assertThat(result.value).isEqualTo(1);
}
@Test
public void testParseUvarint2() throws Exception {
byte[] data = {0x02};
VarintResult result = getUVarint(data, 0);
assertThat(result.length).isEqualTo(1);
assertThat(result.value).isEqualTo(2);
}
@Test
public void testParseUvarint3() throws Exception {
byte[] data = {0x7b};
VarintResult result = getUVarint(data, 0);
assertThat(result.length).isEqualTo(1);
assertThat(result.value).isEqualTo(123);
}
@Test
public void testParseUvarint4() throws Exception {
byte[] data = {(byte)0xc8, 0x03};
VarintResult result = getUVarint(data, 0);
assertThat(result.length).isEqualTo(2);
assertThat(result.value).isEqualTo(456);
}
@Test
public void testParseUvarint4AtOffset() throws Exception {
byte[] data = {0x0, 0x0, (byte)0xc8, 0x03};
VarintResult result = getUVarint(data, 2);
assertThat(result.length).isEqualTo(2);
assertThat(result.value).isEqualTo(456);
}
@Test
public void testParseIntcBlock() throws Exception {
byte[] data = {
0x20, 0x05, 0x00, 0x01, (byte)0xc8, 0x03, 0x7b, 0x02
};
IntConstBlock results = readIntConstBlock(data, 0);
assertThat(results.size).isEqualTo(data.length);
assertThat(results.results)
.containsExactlyElementsOf(ImmutableList.of(0, 1, 456, 123, 2));
}
@Test
public void testParseBytecBlock() throws Exception {
byte[] data = {
0x026, 0x02, 0x0d, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x30, 0x31, 0x32, 0x33, 0x02, 0x01, 0x02
};
List<byte[]> values = ImmutableList.of(
new byte[]{ 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x30, 0x31, 0x32, 0x33 },
new byte[]{ 0x1, 0x2 });
Logic.ByteConstBlock results = readByteConstBlock(data, 0);
assertThat(results.size).isEqualTo(data.length);
assertThat(results.results).containsExactlyElementsOf(values);
}
@Test
public void testParsePushIntOp() throws Exception {
byte[] data = {
(byte)0x81, (byte)0x80, (byte)0x80, 0x04
};
IntConstBlock results = readPushIntOp(data, 0);
assertThat(results.size).isEqualTo(data.length);
assertThat(results.results)
.containsExactlyElementsOf(ImmutableList.of(65536));
}
@Test
public void testParsePushBytesOp() throws Exception {
byte[] data = {
(byte)0x80, 0x0b, 0x68, 0x65, 0x6c, 0x6c, 0x6f, 0x20, 0x77, 0x6f, 0x72, 0x6c, 0x64
};
List<byte[]> values = ImmutableList.of(
new byte[]{ 'h', 'e', 'l', 'l', 'o', ' ', 'w', 'o', 'r', 'l', 'd' }
);
Logic.ByteConstBlock results = readPushByteOp(data, 0);
assertThat(results.size).isEqualTo(data.length);
assertThat(results.results).containsExactlyElementsOf(values);
}
@Test
public void testCheckProgramValid() throws Exception {
byte[] program = {
0x01, 0x20, 0x01, 0x01, 0x22 // int 1
};
// Null argument
ProgramData programData = readProgram(program, null);
assertThat(programData.good).isTrue();
assertThat(programData.intBlock)
.containsExactlyElementsOf(ImmutableList.of(1));
assertThat(programData.byteBlock).isEmpty();
// No argument
ArrayList<byte[]> args = new ArrayList<>();
programData = readProgram(program, args);
assertThat(programData.good).isTrue();
assertThat(programData.intBlock)
.containsExactlyElementsOf(ImmutableList.of(1));
assertThat(programData.byteBlock).isEmpty();
// Unused argument
byte[] arg = new byte[10];
Arrays.fill(arg, (byte)0x31);
args.add(arg);
programData = readProgram(program, args);
assertThat(programData.good).isTrue();
assertThat(programData.intBlock)
.containsExactlyElementsOf(ImmutableList.of(1));
assertThat(programData.byteBlock).isEmpty();
// Repeated int constants parsing
byte[] int1 = new byte[10];
Arrays.fill(int1, (byte)0x22);
byte[] program2 = new byte[program.length + int1.length];
System.arraycopy(program, 0, program2, 0, program.length);
System.arraycopy(int1, 0, program2, program.length, int1.length);
programData = readProgram(program2, args);
assertThat(programData.good).isTrue();
assertThat(programData.intBlock)
.containsExactlyElementsOf(ImmutableList.of(1));
assertThat(programData.byteBlock).isEmpty();
}
@Test
public void testCheckProgramLongArgs() throws Exception {
byte[] program = {
0x01, 0x20, 0x01, 0x01, 0x22 // int 1
};
ArrayList<byte[]> args = new ArrayList<byte[]>();
byte[] arg = new byte[1000];
Arrays.fill(arg, (byte)0x31);
args.add(arg);
assertThatThrownBy(() -> readProgram(program, args))
.isInstanceOf(IllegalArgumentException.class)
.hasMessage("program too long");
}
@Test
public void testCheckProgramLong() throws Exception {
byte[] program = {
0x01, 0x20, 0x01, 0x01, 0x22 // int 1
};
byte[] int1 = new byte[1000];
byte[] program2 = new byte[program.length + int1.length];
ArrayList<byte[]> args = new ArrayList<byte[]>();
System.arraycopy(program, 0, program2, 0, program.length);
System.arraycopy(int1, 0, program2, program.length, int1.length);
assertThatThrownBy(() -> checkProgram(program2, args))
.isInstanceOf(IllegalArgumentException.class)
.hasMessage("program too long");
}
@Test
public void testCheckProgramCostly() throws Exception {
byte[] old_versions = {
0x1, 0x2, 0x3
};
byte[] versions = {
0x4
};
byte[] program = {
0x01, 0x26, 0x01, 0x01, 0x01, 0x28, 0x02 // byte 0x01 + keccak256
};
ArrayList<byte[]> args = new ArrayList<byte[]>();
byte[] arg = "aaaaaaaaaa".getBytes();
args.add(arg);
ProgramData programData = readProgram(program, args);
assertThat(programData.good).isTrue();
byte[] keccakx800 = new byte[800];
for (int i = 0; i < keccakx800.length; i++) {
keccakx800[i] = 0x02;
}
byte[] program2 = new byte[program.length + keccakx800.length];
System.arraycopy(program, 0, program2, 0, program.length);
System.arraycopy(keccakx800, 0, program2, program.length, keccakx800.length);
for (byte v : old_versions) {
program2[0] = v;
assertThatThrownBy(() -> readProgram(program2, args))
.isInstanceOf(IllegalArgumentException.class)
.hasMessage("program too costly for Teal version < 4. consider using v4.");
}
for (byte v : versions) {
program2[0] = v;
programData = readProgram(program2, args);
assertThat(programData.good).isTrue();
}
}
@Test
public void testCheckProgramInvalidOpcode() throws Exception {
byte[] program = {
0x01, 0x20, 0x01, 0x01, (byte)0xFF
};
ArrayList<byte[]> args = new ArrayList<>();
assertThatThrownBy(() -> checkProgram(program, args))
.isInstanceOf(IllegalArgumentException.class)
.hasMessage("invalid instruction: 255");
}
@Test
public void testCheckProgramTealV2() throws Exception {
assertThat(getEvalMaxVersion()).isGreaterThanOrEqualTo(2);
assertThat(getLogicSigVersion()).isGreaterThanOrEqualTo(2);
{
// balance
byte[] program = {
0x02, 0x20, 0x01, 0x00, 0x22, 0x60 // int 0; balance
};
boolean valid = checkProgram(program, null);
assertThat(valid).isTrue();
}
// app_opted_in
{
byte[] program = {
0x02, 0x20, 0x01, 0x00, 0x22, 0x22, 0x61 // int 0; int 0; app_opted_in
};
boolean valid = checkProgram(program, null);
assertThat(valid).isTrue();
}
{
// asset_holding_get
byte[] program = {
0x02, 0x20, 0x01, 0x00, 0x22, 0x70, 0x00 // int 0; int 0; asset_holding_get Balance
};
boolean valid = checkProgram(program, null);
assertThat(valid).isTrue();
}
}
@Test
public void testCheckProgramTealV3() throws Exception {
assertThat(getEvalMaxVersion()).isGreaterThanOrEqualTo(3);
assertThat(getLogicSigVersion()).isGreaterThanOrEqualTo(3);
{
// min_balance
byte[] program = {
0x03, 0x20, 0x01, 0x00, 0x22, 0x78 // int 0; min_balance
};
boolean valid = checkProgram(program, null);
assertThat(valid).isTrue();
}
// pushbytes
{
byte[] program = {
0x03, 0x20, 0x01, 0x00, 0x22, (byte)0x80, 0x02, 0x68, 0x69, 0x48 // int 0; pushbytes "hi"; pop
};
boolean valid = checkProgram(program, null);
assertThat(valid).isTrue();
}
{
// pushint
byte[] program = {
0x03, 0x20, 0x01, 0x00, 0x22, (byte)0x81, 0x01, 0x48 // int 0; pushint 1; pop
};
boolean valid = checkProgram(program, null);
assertThat(valid).isTrue();
}
{
// swap
byte[] program = {
0x03, 0x20, 0x02, 0x00, 0x01, 0x22, 0x23, 0x4c, 0x48 // int 0; int 1; swap; pop
};
boolean valid = checkProgram(program, null);
assertThat(valid).isTrue();
}
}
@Test
public void testCheckProgramTealV4() throws Exception {
assertThat(getEvalMaxVersion()).isGreaterThanOrEqualTo(4);
{
// divmodw
byte[] program = {
0x04, 0x20, 0x03, 0x01, 0x00, 0x02, 0x22, (byte) 0x81, (byte) 0xd0, (byte) 0x0f, 0x23, 0x24, 0x1f // int 1; pushint 2000; int 0; int 2; divmodw
};
boolean valid = checkProgram(program, null);
assertThat(valid).isTrue();
}
// gloads i
{
byte[] program = {
0x04, 0x20, 0x01, 0x00, 0x22, 0x3b, 0x00 // int 0; gloads 0
};
boolean valid = checkProgram(program, null);
assertThat(valid).isTrue();
}
{
// callsub
byte[] program = {
0x04, 0x20, 0x02, 0x01, 0x02, 0x22, (byte) 0x88, 0x00, 0x02, 0x23, 0x12, 0x49 // int 1; callsub double; int 2; ==; double: dup;
};
boolean valid = checkProgram(program, null);
assertThat(valid).isTrue();
}
{
// b>=
byte[] program = {
0x04, 0x26, 0x02, 0x01, 0x11, 0x01, 0x10, 0x28, 0x29, (byte) 0xa7 // byte 0x11; byte 0x10; b>=
};
boolean valid = checkProgram(program, null);
assertThat(valid).isTrue();
}
{
// b^
byte[] program = {
0x04, 0x26, 0x02, 0x01, 0x11, 0x01, 0x10, 0x28, 0x29, (byte) 0xa7 // byte 0x11; byte 0x10; b^; byte 0x01; ==
};
boolean valid = checkProgram(program, null);
assertThat(valid).isTrue();
}
{
// callsub, retsub.
byte[] program = {
0x04, 0x20, 0x02, 0x01, 0x02, 0x22, (byte) 0x88, 0x00, 0x03, 0x23, 0x12, 0x43, 0x49, 0x08, (byte) 0x89 // int 1; callsub double; int 2; ==; return; double: dup; +; retsub;
};
boolean valid = checkProgram(program, null);
assertThat(valid).isTrue();
}
{
// loop
byte[] program = {
0x04, 0x20, 0x04, 0x01, 0x02, 0x0a, 0x10, 0x22, 0x23, 0x0b, 0x49, 0x24, 0x0c, 0x40, (byte) 0xff, (byte) 0xf8, 0x25, 0x12 // int 1; loop: int 2; *; dup; int 10; <; bnz loop; int 16; ==
};
boolean valid = checkProgram(program, null);
assertThat(valid).isTrue();
}
}
}