-
Notifications
You must be signed in to change notification settings - Fork 144
Expand file tree
/
Copy pathVMJsonTestBase.cs
More file actions
325 lines (281 loc) · 11.1 KB
/
VMJsonTestBase.cs
File metadata and controls
325 lines (281 loc) · 11.1 KB
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
// Copyright (C) 2015-2026 The Neo Project.
//
// VMJsonTestBase.cs file belongs to the neo project and is free
// software distributed under the MIT software license, see the
// accompanying file LICENSE in the main directory of the
// repository or http://www.opensource.org/licenses/mit-license.php
// for more details.
//
// Redistribution and use in source and binary forms with or without
// modifications are permitted.
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Neo.Test.Extensions;
using Neo.Test.Types;
using Neo.VM;
using Neo.VM.Types;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Text;
using Array = Neo.VM.Types.Array;
using Boolean = Neo.VM.Types.Boolean;
using Buffer = Neo.VM.Types.Buffer;
namespace Neo.Test;
public abstract class VMJsonTestBase
{
/// <summary>
/// Execute this test
/// </summary>
/// <param name="ut">Test</param>
public static void ExecuteTest(VMUT ut)
{
foreach (var test in ut.Tests)
{
Assert.IsFalse(string.IsNullOrEmpty(test.Name), "Name is required");
using TestEngine engine = new();
Debugger debugger = new(engine);
if (test.Script.Length > 0)
{
engine.LoadScript(test.Script);
}
// Execute Steps
if (test.Steps != null)
{
foreach (var step in test.Steps)
{
// Actions
if (step.Actions != null) foreach (var run in step.Actions)
{
switch (run)
{
case VMUTActionType.Execute: debugger.Execute(); break;
case VMUTActionType.StepInto: debugger.StepInto(); break;
case VMUTActionType.StepOut: debugger.StepOut(); break;
case VMUTActionType.StepOver: debugger.StepOver(); break;
}
}
// Review results
var add = string.IsNullOrEmpty(step.Name) ? "" : "-" + step.Name;
AssertResult(step.Result, engine, $"{ut.Category}-{ut.Name}-{test.Name}{add}: ");
}
}
}
}
/// <summary>
/// Assert result
/// </summary>
/// <param name="engine">Engine</param>
/// <param name="result">Result</param>
/// <param name="message">Message</param>
private static void AssertResult(VMUTExecutionEngineState result, TestEngine engine, string message)
{
AssertAreEqual(result.State.ToString().ToLowerInvariant(), engine.State.ToString().ToLowerInvariant(), message + "State is different");
if (engine.State == VMState.FAULT)
{
if (result.ExceptionMessage != null)
{
AssertAreEqual(result.ExceptionMessage, engine.FaultException.Message, message + " [Exception]");
}
return;
}
AssertResult(result.InvocationStack, engine.InvocationStack, message + " [Invocation stack]");
AssertResult(result.ResultStack, engine.ResultStack, message + " [Result stack] ");
}
/// <summary>
/// Assert invocation stack
/// </summary>
/// <param name="stack">Stack</param>
/// <param name="result">Result</param>
/// <param name="message">Message</param>
private static void AssertResult(VMUTExecutionContextState[] result, Stack<ExecutionContext> stack, string message)
{
AssertAreEqual(result == null ? 0 : result.Length, stack.Count, message + "Stack is different");
int x = 0;
foreach (var context in stack)
{
var opcode = context.InstructionPointer >= context.Script.Length ? OpCode.RET : context.Script[context.InstructionPointer];
AssertAreEqual(result[x].NextInstruction, opcode, message + "Next instruction is different");
AssertAreEqual(result[x].InstructionPointer, context.InstructionPointer, message + "Instruction pointer is different");
// Check stack
AssertResult(result[x].EvaluationStack, context.EvaluationStack, message + " [EvaluationStack]");
// Check slots
AssertResult(result[x].Arguments, context.Arguments, message + " [Arguments]");
AssertResult(result[x].LocalVariables, context.LocalVariables, message + " [LocalVariables]");
AssertResult(result[x].StaticFields, context.StaticFields, message + " [StaticFields]");
x++;
}
}
/// <summary>
/// Assert result stack
/// </summary>
/// <param name="stack">Stack</param>
/// <param name="result">Result</param>
/// <param name="message">Message</param>
private static void AssertResult(VMUTStackItem[] result, EvaluationStack stack, string message)
{
AssertAreEqual(stack.Count, result == null ? 0 : result.Length, message + "Stack is different");
for (int x = 0, max = stack.Count; x < max; x++)
{
AssertAreEqual(ItemToJson(stack.Peek(x)).ToString(Formatting.None), PrepareJsonItem(result[x]).ToString(Formatting.None), message + "Stack item is different");
}
}
/// <summary>
/// Assert result slot
/// </summary>
/// <param name="slot">Slot</param>
/// <param name="result">Result</param>
/// <param name="message">Message</param>
private static void AssertResult(VMUTStackItem[] result, Slot slot, string message)
{
AssertAreEqual(slot == null ? 0 : slot.Count, result == null ? 0 : result.Length, message + "Slot is different");
for (int x = 0, max = slot == null ? 0 : slot.Count; x < max; x++)
{
AssertAreEqual(ItemToJson(slot[x]).ToString(Formatting.None), PrepareJsonItem(result[x]).ToString(Formatting.None), message + "Stack item is different");
}
}
private static JObject PrepareJsonItem(VMUTStackItem item)
{
var ret = new JObject
{
["type"] = item.Type.ToString(),
["value"] = item.Value
};
switch (item.Type)
{
case VMUTStackItemType.Null:
{
ret["type"] = VMUTStackItemType.Null.ToString();
ret.Remove("value");
break;
}
case VMUTStackItemType.Pointer:
{
ret["type"] = VMUTStackItemType.Pointer.ToString();
ret["value"] = item.Value.Value<int>();
break;
}
case VMUTStackItemType.String:
{
// Easy access
ret["type"] = VMUTStackItemType.ByteString.ToString();
ret["value"] = Encoding.UTF8.GetBytes(item.Value.Value<string>());
break;
}
case VMUTStackItemType.ByteString:
case VMUTStackItemType.Buffer:
{
var value = ret["value"].Value<string>();
Assert.IsTrue(string.IsNullOrEmpty(value) || value.StartsWith("0x"), $"'0x' prefix required for value: '{value}'");
ret["value"] = value.FromHexString();
break;
}
case VMUTStackItemType.Integer:
{
// Ensure format
ret["value"] = ret["value"].Value<string>();
break;
}
case VMUTStackItemType.Struct:
case VMUTStackItemType.Array:
{
var array = (JArray)ret["value"];
for (int x = 0, m = array.Count; x < m; x++)
{
array[x] = PrepareJsonItem(JsonConvert.DeserializeObject<VMUTStackItem>(array[x].ToString()));
}
ret["value"] = array;
break;
}
case VMUTStackItemType.Map:
{
var obj = (JObject)ret["value"];
foreach (var prop in obj.Properties())
{
obj[prop.Name] = PrepareJsonItem(JsonConvert.DeserializeObject<VMUTStackItem>(prop.Value.ToString()));
}
ret["value"] = obj;
break;
}
}
return ret;
}
private static JObject ItemToJson(StackItem item)
{
if (item == null) return null;
JToken value;
string type = item.GetType().Name;
switch (item)
{
case Null _:
{
return new JObject
{
["type"] = type,
};
}
case Pointer p:
{
return new JObject
{
["type"] = type,
["value"] = p.Position
};
}
case Boolean v: value = new JValue(v.GetBoolean()); break;
case Integer v: value = new JValue(v.GetInteger().ToString()); break;
case ByteString v: value = new JValue(v.GetSpan().ToArray()); break;
case Buffer v: value = new JValue(v.GetSpan().ToArray()); break;
//case VM.Types.Struct v:
case Array v:
{
var jarray = new JArray();
foreach (var entry in v)
{
jarray.Add(ItemToJson(entry));
}
value = jarray;
break;
}
case Map v:
{
var jdic = new JObject();
foreach (var entry in v)
{
jdic.Add(entry.Key.GetSpan().ToArray().ToHexString(), ItemToJson(entry.Value));
}
value = jdic;
break;
}
case InteropInterface v:
{
type = "Interop";
var obj = v.GetInterface<object>();
value = obj.GetType().Name.ToString();
break;
}
default: throw new NotImplementedException();
}
return new JObject
{
["type"] = type,
["value"] = value
};
}
/// <summary>
/// Assert with message
/// </summary>
/// <param name="expected">A</param>
/// <param name="actual">B</param>
/// <param name="message">Message</param>
private static void AssertAreEqual(object expected, object actual, string message)
{
if (expected is byte[] ba) expected = ba.ToHexString().ToUpperInvariant();
if (actual is byte[] bb) actual = bb.ToHexString().ToUpperInvariant();
if (expected.ToJson() != actual.ToJson())
{
throw new Exception(message +
$"{Environment.NewLine}Expected:{Environment.NewLine + expected.ToString() + Environment.NewLine}Actual:{Environment.NewLine + actual.ToString()}");
}
}
}