summaryrefslogtreecommitdiff
path: root/data/basics/teal_test.go
blob: 35aa9a193f4c08c82dc0b8d0e9ef8e101b205ba2 (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
// 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 basics

import (
	"strings"
	"testing"

	"github.com/stretchr/testify/require"

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

func TestStateDeltaValid(t *testing.T) {
	partitiontest.PartitionTest(t)

	a := require.New(t)

	// test pre-applications proto
	protoPreF := config.Consensus[protocol.ConsensusV23]
	a.False(protoPreF.Application)
	sd := StateDelta{"key": ValueDelta{Action: SetBytesAction, Bytes: "val"}}
	err := sd.Valid(&protoPreF)
	a.Error(err)
	a.Contains(err.Error(), "proto.MaxAppKeyLen is 0")

	sd = StateDelta{"": ValueDelta{Action: SetUintAction, Uint: 1}}
	err = sd.Valid(&protoPreF)
	a.Error(err)
	a.Contains(err.Error(), "proto.MaxAppKeyLen is 0")

	sd = StateDelta{"": ValueDelta{Action: SetBytesAction, Bytes: ""}}
	err = sd.Valid(&protoPreF)
	a.Error(err)
	a.Contains(err.Error(), "proto.MaxAppKeyLen is 0")

	// test vFuture proto with applications
	sd = StateDelta{"key": ValueDelta{Action: SetBytesAction, Bytes: "val"}}
	protoF := config.Consensus[protocol.ConsensusFuture]
	err = sd.Valid(&protoF)
	a.NoError(err)

	// vFuture: key too long, short value
	tooLongKey := strings.Repeat("a", protoF.MaxAppKeyLen+1)
	sd = StateDelta{tooLongKey: ValueDelta{Action: SetBytesAction, Bytes: "val"}}
	err = sd.Valid(&protoF)
	a.Error(err)
	a.Contains(err.Error(), "key too long")
	delete(sd, tooLongKey)

	// vFuture: max size key, value too long: total size bigger than MaxAppSumKeyValueLens
	longKey := tooLongKey[1:]
	tooLongValue := strings.Repeat("b", protoF.MaxAppSumKeyValueLens-len(longKey)+1)
	sd = StateDelta{longKey: ValueDelta{Action: SetBytesAction, Bytes: tooLongValue}}
	err = sd.Valid(&protoF)
	a.Error(err)
	a.Contains(err.Error(), "key/value total too long for key")

	sd[longKey] = ValueDelta{Action: SetBytesAction, Bytes: tooLongValue[1:]}
	sd["intval"] = ValueDelta{Action: DeltaAction(10), Uint: 0}
	err = sd.Valid(&protoF)
	a.Error(err)
	a.Contains(err.Error(), "unknown delta action")

	sd["intval"] = ValueDelta{Action: SetUintAction, Uint: 0}
	sd["delval"] = ValueDelta{Action: DeleteAction, Uint: 0, Bytes: tooLongValue}
	err = sd.Valid(&protoF)
	a.NoError(err)
}

func TestStateDeltaValidV24(t *testing.T) {
	partitiontest.PartitionTest(t)

	a := require.New(t)

	// v24: short key, value too long: hits MaxAppBytesValueLen
	protoV24 := config.Consensus[protocol.ConsensusV24]
	shortKey := "k"
	reallyLongValue := strings.Repeat("b", protoV24.MaxAppBytesValueLen+1)
	sd := StateDelta{shortKey: ValueDelta{Action: SetBytesAction, Bytes: reallyLongValue}}
	err := sd.Valid(&protoV24)
	a.Error(err)
	a.Contains(err.Error(), "value too long for key")

	// v24: key too long, short value
	tooLongKey := strings.Repeat("a", protoV24.MaxAppKeyLen+1)
	sd = StateDelta{tooLongKey: ValueDelta{Action: SetBytesAction, Bytes: "val"}}
	err = sd.Valid(&protoV24)
	a.Error(err)
	a.Contains(err.Error(), "key too long")
	delete(sd, tooLongKey)
}

func TestStateDeltaEqual(t *testing.T) {
	partitiontest.PartitionTest(t)

	a := require.New(t)

	var d1 StateDelta = nil
	var d2 StateDelta = nil
	a.True(d1.Equal(d2))

	d2 = StateDelta{}
	a.True(d1.Equal(d2))

	d2 = StateDelta{"test": {Action: SetUintAction, Uint: 0}}
	a.False(d1.Equal(d2))

	d1 = StateDelta{}
	d2 = StateDelta{}
	a.True(d1.Equal(d2))

	d2 = StateDelta{"test": {Action: SetUintAction, Uint: 0}}
	a.False(d1.Equal(d2))

	d1 = StateDelta{"test2": {Action: SetBytesAction, Uint: 0}}
	a.False(d1.Equal(d2))

	d1 = StateDelta{"test": {Action: SetUintAction, Uint: 0}}
	d2 = StateDelta{"test": {Action: SetUintAction, Uint: 0}}
	a.True(d1.Equal(d2))

	d1 = StateDelta{"test": {Action: SetBytesAction, Bytes: "val"}}
	d2 = StateDelta{"test": {Action: SetBytesAction, Bytes: "val"}}
	a.True(d1.Equal(d2))

	d2 = StateDelta{"test": {Action: SetBytesAction, Bytes: "val1"}}
	a.False(d1.Equal(d2))
}