summaryrefslogtreecommitdiff
path: root/cmd/tealdbg/debugger_test.go
blob: d1d815b1a3f1a04c55b670307bbd2e8da8bbd12a (plain)
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
// Copyright (C) 2019-2021 Algorand, Inc.
// This file is part of go-algorand
//
// go-algorand is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as
// published by the Free Software Foundation, either version 3 of the
// License, or (at your option) any later version.
//
// go-algorand is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with go-algorand.  If not, see <https://www.gnu.org/licenses/>.

package main

import (
	"fmt"
	"testing"

	"github.com/stretchr/testify/require"

	"github.com/algorand/go-algorand/config"
	"github.com/algorand/go-algorand/data/transactions"
	"github.com/algorand/go-algorand/data/transactions/logic"
	"github.com/algorand/go-algorand/protocol"
	"github.com/algorand/go-algorand/test/partitiontest"
)

type testDbgAdapter struct {
	debugger      Control
	notifications chan Notification

	started    bool
	ended      bool
	eventCount int

	done chan struct{}

	t *testing.T
}

func makeTestDbgAdapter(t *testing.T) (d *testDbgAdapter) {
	d = new(testDbgAdapter)
	d.done = make(chan struct{})
	d.t = t
	return d
}

func (d *testDbgAdapter) WaitForCompletion() {
	<-d.done
}

func (d *testDbgAdapter) SessionStarted(sid string, debugger Control, ch chan Notification) {
	d.debugger = debugger
	d.notifications = ch

	go d.eventLoop()

	d.started = true
}

func (d *testDbgAdapter) SessionEnded(sid string) {
	d.ended = true
}

func (d *testDbgAdapter) URL() string {
	return ""
}

func (d *testDbgAdapter) eventLoop() {
	for {
		select {
		case n := <-d.notifications:
			d.eventCount++
			if n.Event == "completed" {
				d.done <- struct{}{}
				return
			}
			if n.Event == "registered" {
				require.NotNil(d.t, n.DebugState.Globals)
				require.NotNil(d.t, n.DebugState.Scratch)
				require.NotEmpty(d.t, n.DebugState.Disassembly)
				require.NotEmpty(d.t, n.DebugState.ExecID)
				d.debugger.SetBreakpoint(n.DebugState.Line + 1)
			}
			d.debugger.Resume()
		}
	}
}

func TestDebuggerSimple(t *testing.T) {
	partitiontest.PartitionTest(t)
	proto := config.Consensus[protocol.ConsensusV18]
	require.Greater(t, proto.LogicSigVersion, uint64(0))
	debugger := MakeDebugger()

	da := makeTestDbgAdapter(t)
	debugger.AddAdapter(da)

	ep := logic.EvalParams{
		Proto:    &proto,
		Debugger: debugger,
		Txn:      &transactions.SignedTxn{},
	}

	source := `int 0
int 1
+
`
	ops, err := logic.AssembleStringWithVersion(source, 1)
	require.NoError(t, err)

	_, err = logic.Eval(ops.Program, ep)
	require.NoError(t, err)

	da.WaitForCompletion()

	require.True(t, da.started)
	require.True(t, da.ended)
	require.Equal(t, 3, da.eventCount) // register, update, complete
}

func TestSession(t *testing.T) {
	partitiontest.PartitionTest(t)
	source := fmt.Sprintf("#pragma version %d\nint 1\ndup\n+\n", logic.LogicVersion)
	ops, err := logic.AssembleStringWithVersion(source, logic.LogicVersion)
	require.NoError(t, err)
	disassembly, err := logic.Disassemble(ops.Program)
	require.NoError(t, err)

	// create a sample disassembly line to pc mapping
	// this simple source is similar to disassembly except intcblock at the beginning
	pcOffset := make(map[int]int, len(ops.OffsetToLine))
	for pc, line := range ops.OffsetToLine {
		pcOffset[line+1] = pc
	}

	s := makeSession(disassembly, 0)
	s.source = source
	s.programName = "test"
	s.offsetToLine = ops.OffsetToLine
	s.pcOffset = pcOffset
	err = s.SetBreakpoint(2)
	require.NoError(t, err)

	ackCount := 0
	done := make(chan struct{})
	ackFunc := func() {
		<-s.acknowledged
		ackCount++
		done <- struct{}{}
	}
	go ackFunc()

	s.Resume()
	<-done

	require.Equal(t, breakpointLine(2), s.debugConfig.BreakAtLine)
	require.Equal(t, breakpoint{true, true}, s.breakpoints[2])
	require.Equal(t, 1, ackCount)

	s.SetBreakpointsActive(false)
	require.Equal(t, breakpoint{true, false}, s.breakpoints[2])

	s.SetBreakpointsActive(true)
	require.Equal(t, breakpoint{true, true}, s.breakpoints[2])

	s.RemoveBreakpoint(2)
	require.Equal(t, breakpoint{false, false}, s.breakpoints[2])

	go ackFunc()

	s.Step()
	<-done

	require.Equal(t, stepBreak, s.debugConfig.BreakAtLine)
	require.Equal(t, 2, ackCount)

	data, err := s.GetSourceMap()
	require.NoError(t, err)
	require.Greater(t, len(data), 0)

	name, data := s.GetSource()
	require.NotEmpty(t, name)
	require.Greater(t, len(data), 0)
}