summaryrefslogtreecommitdiff
path: root/test/e2e-go/cli/tealdbg/expect/tealdbgTest.exp
blob: 7d7b84ab4eb11f925b3192f96f6bcf9771602bb9 (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
#!/usr/bin/expect -f
set err 0
log_user 1

# workaround for scoping issue in TestTealdbg and setting URL inside expect_background's re scope
set URL ""

proc TestTealdbg { TEAL_PROG_FILE PROTOCOL_VERSION ARGS } {
    variable URL
    set URL ""
    set PASSED 0
    spawn tealdbg debug -v $TEAL_PROG_FILE -p $PROTOCOL_VERSION {*}[lrange $ARGS 0 end]
    expect_background {
        timeout { puts "tealdbg debug timed out"; exit 1 }
        -re {CDT debugger listening on: (ws://[.a-z0-9:/]+)} { set URL $expect_out(1,string); }
        eof {
            catch wait result
            if { [lindex $result 3] != 0 } {
                puts "returned error code is [lindex $result 3]"
                exit 1
            }
        }
    }
    set tealdbg_spawn_id $spawn_id

    # wait until URL is set or timeout
    set it 0
    while { $it < 10 && $URL == "" } {
        set it [expr {$it + 1}]
        sleep 1
    }
    if { $URL == "" } {
        puts "ERROR: URL is not set after timeout"
        exit 1
    }

    spawn cdtmock $URL
    expect {
        timeout { puts "cdt-mock debug timed out"; exit 1 }
        -re {Debugger.paused} { set PASSED 1; }
        eof { catch wait result; if { [lindex $result 3] == 0 } { puts "Expected non-zero exit code"; exit [lindex $result 3] } }
    }

    if { $PASSED == 0 } {
        puts "ERROR: have not found 'Debugger.paused' in cdtmock output"
        exit 1
    }

    puts "Shutting down tealdbg"
    close -i $tealdbg_spawn_id
}

if { [catch {

    set TEST_ALGO_DIR [lindex $argv 0]
    set timeout 30

    set TEST_DIR $TEST_ALGO_DIR
    exec mkdir -p $TEST_DIR

    set TEAL_PROG_FILE "$TEST_DIR/trivial.teal"
    # this is ConsensusV25
    set PROTOCOL_VERSION_2 "https://github.com/algorandfoundation/specs/tree/bea19289bf41217d2c0af30522fa222ef1366466"

    # this is ConsensusV26
    set PROTOCOL_VERSION_3 "https://github.com/algorandfoundation/specs/tree/ac2255d586c4474d4ebcf3809acccb59b7ef34ff"

    # run the test using version 2 on protocol version 2:
    exec printf "#pragma version 2\nint 1\ndup\n+\n" > $TEAL_PROG_FILE
    TestTealdbg $TEAL_PROG_FILE $PROTOCOL_VERSION_2 ""

    # run the test using version 2 on protocol version 3:
    TestTealdbg $TEAL_PROG_FILE $PROTOCOL_VERSION_3 "--remote-debugging-port 9392 --listen 127.0.0.1"

    # run the test using version 3 on protocol version 3:
    exec printf "#pragma version 3\nint 1\ndup\n+\n" > $TEAL_PROG_FILE
    TestTealdbg $TEAL_PROG_FILE $PROTOCOL_VERSION_3 "--remote-debugging-port 9392 --listen 127.0.0.1"

    # run the test using version 3 on protocol version 2 (this should fail)
    set FAILED 0
    spawn tealdbg debug -v $TEAL_PROG_FILE -p $PROTOCOL_VERSION_2 --remote-debugging-port 9392 --listen 127.0.0.1
    expect {
        timeout { puts "tealdbg debug timed out"; exit 1 }
        -re {Debug error: program version \([0-9]+\) is beyond the maximum supported protocol version \([0-9]+\)} { set FAILED 1; close }
    }
    if { $FAILED == 0 } {
        puts "ERROR: the command should have failed"
        exit 1
    }
    puts "The command failed as expected"

    exec rm $TEAL_PROG_FILE

} EXCEPTION ] } {
    puts "ERROR in tealdbgTest: $EXCEPTION"
    exit 1
}