Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Shant/debugger tests #1466

Merged
merged 10 commits into from
Sep 4, 2020
1 change: 1 addition & 0 deletions data/transactions/logic/debugger.go
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,7 @@ func stackValueToTealValue(sv *stackValue) basics.TealValue {
}
}

// valueDeltaToValueDelta converts delta's bytes to base64 in a new struct
func valueDeltaToValueDelta(vd *basics.ValueDelta) basics.ValueDelta {
return basics.ValueDelta{
Action: vd.Action,
Expand Down
50 changes: 50 additions & 0 deletions data/transactions/logic/debugger_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,10 @@ package logic

import (
"os"
"encoding/base64"
"testing"

"github.com/algorand/go-algorand/data/basics"
"github.com/stretchr/testify/require"
)

Expand Down Expand Up @@ -125,3 +127,51 @@ func TestDebuggerHook(t *testing.T) {
require.Greater(t, testDbg.update, 1)
require.Equal(t, 1, len(testDbg.state.Stack))
}

func TestLineToPC(t *testing.T) {
dState := DebugState{
Disassembly: "abc\ndef\nghi",
PCOffset: []PCOffset{{PC: 1, Offset: 4}, {PC: 2, Offset: 8}, {PC: 3, Offset: 12}},
}
pc := dState.LineToPC(0)
require.Equal(t, 0, pc)

pc = dState.LineToPC(1)
require.Equal(t, 1, pc)

pc = dState.LineToPC(2)
require.Equal(t, 2, pc)

pc = dState.LineToPC(3)
require.Equal(t, 3, pc)

pc = dState.LineToPC(4)
require.Equal(t, 0, pc)

pc = dState.LineToPC(-1)
require.Equal(t, 0, pc)

pc = dState.LineToPC(0x7fffffff)
require.Equal(t, 0, pc)

dState.PCOffset = []PCOffset{}
pc = dState.LineToPC(1)
require.Equal(t, 0, pc)

dState.PCOffset = []PCOffset{{PC: 1, Offset: 0}}
pc = dState.LineToPC(1)
require.Equal(t, 0, pc)
}

func TestValueDeltaToValueDelta(t *testing.T) {
vDelta := basics.ValueDelta{
Action: basics.SetUintAction,
Bytes: "some string",
Uint: uint64(0xffffffff),
}
ans := valueDeltaToValueDelta(&vDelta)
require.Equal(t, vDelta.Action, ans.Action)
require.NotEqual(t, vDelta.Bytes, ans.Bytes)
require.Equal(t, base64.StdEncoding.EncodeToString([]byte(vDelta.Bytes)), ans.Bytes)
require.Equal(t, vDelta.Uint, ans.Uint)
}
36 changes: 36 additions & 0 deletions data/transactions/logic/eval_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,42 @@ func TestEmptyProgram(t *testing.T) {
require.False(t, pass)
}

// TestMinTealVersionParamEval tests eval/check reading the MinTealVersion from the param
func TestMinTealVersionParamEvalCheck(t *testing.T) {
t.Parallel()
params := defaultEvalParams(nil, nil)
version2 := uint64(rekeyingEnabledVersion)
params.MinTealVersion = &version2
program := make([]byte, binary.MaxVarintLen64)
// set the teal program version to 1
binary.PutUvarint(program, 1)

_, err := Check(program, params)
require.Contains(t, err.Error(), fmt.Sprintf("program version must be >= %d", appsEnabledVersion))

// If the param is read correctly, the eval should fail
pass, err := Eval(program, params)
require.Error(t, err)
require.Contains(t, err.Error(), fmt.Sprintf("program version must be >= %d", appsEnabledVersion))
require.False(t, pass)
}

func TestTxnFieldToTealValue(t *testing.T) {

txn := transactions.Transaction{}
groupIndex := 0
field := FirstValid
values := [6]uint64{0, 1, 2, 0xffffffff, 0xffffffffffffffff}

for _, value := range values {
txn.FirstValid = basics.Round(value)
tealValue, err := TxnFieldToTealValue(&txn, groupIndex, field)
require.NoError(t, err)
require.Equal(t, basics.TealUintType, tealValue.Type)
require.Equal(t, value, tealValue.Uint)
}
}

func TestWrongProtoVersion(t *testing.T) {
t.Parallel()
for v := uint64(1); v <= AssemblerMaxVersion; v++ {
Expand Down
2 changes: 0 additions & 2 deletions data/transactions/logic/opcodes.go
Original file line number Diff line number Diff line change
Expand Up @@ -174,8 +174,6 @@ func (a sortByOpcode) Less(i, j int) bool { return a[i].Opcode < a[j].Opcode }

// OpcodesByVersion returns list of opcodes available in a specific version of TEAL
// by copying v1 opcodes to v2 to create a full list.
// This function must be used for documentation only because it modifies opcode versions
// to the first introduced for the opcodes updated in later versions.
func OpcodesByVersion(version uint64) []OpSpec {
// for updated opcodes use the lowest version opcode was introduced in
maxOpcode := 0
Expand Down
55 changes: 54 additions & 1 deletion data/transactions/logic/opcodes_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,57 @@ func TestOpSpecs(t *testing.T) {
}
}

func (os *OpSpec) equals(oso *OpSpec) bool {
if os.Opcode != oso.Opcode {
return false
}
if os.Name != oso.Name {
return false
}
if !reflect.DeepEqual(os.Args, oso.Args) {
return false
}
if !reflect.DeepEqual(os.Returns, oso.Returns) {
return false
}
if os.Version != oso.Version {
return false
}
if os.Modes != oso.Modes {
return false
}

return true
}

func TestOpcodesByVersionReordered(t *testing.T) {

// Make a copy to restore to the original
OpSpecsOrig := make([]OpSpec, len(OpSpecs))
for idx, opspec := range OpSpecs {
cp := opspec
OpSpecsOrig[idx] = cp
}
defer func() {
OpSpecs = OpSpecsOrig
}()

// To test the case where a newer version opcode is before an older version
// Change the order of opcode 0x01 so that version 2 comes before version 1
tmp := OpSpecs[1]
OpSpecs[1] = OpSpecs[4]
OpSpecs[4] = tmp

t.Run("TestOpcodesByVersion", TestOpcodesByVersion)
}

func TestOpcodesByVersion(t *testing.T) {
t.Parallel()
// Make a copy of the OpSpecs to check if OpcodesByVersion will change it
OpSpecs2 := make([]OpSpec, len(OpSpecs))
for idx, opspec := range OpSpecs {
cp := opspec
OpSpecs2[idx] = cp
}

opSpecs := make([][]OpSpec, 2)
for v := uint64(1); v <= LogicVersion; v++ {
Expand All @@ -59,6 +108,10 @@ func TestOpcodesByVersion(t *testing.T) {
})
}
require.Greater(t, len(opSpecs[1]), len(opSpecs[0]))

for idx, opspec := range OpSpecs {
require.True(t, opspec.equals(&OpSpecs2[idx]))
}
}

func TestOpcodesVersioningV2(t *testing.T) {
Expand Down