Skip to content

Commit 5c4088e

Browse files
committed
✅ [channel/test] Add options to ignore the App + Assets.
Signed-off-by: Oliver Tale-Yazdi <oliver@perun.network>
1 parent 3b79c96 commit 5c4088e

File tree

1 file changed

+49
-25
lines changed

1 file changed

+49
-25
lines changed

channel/test/backend.go

Lines changed: 49 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -28,26 +28,49 @@ import (
2828
type addressCreator = func() wallet.Address
2929

3030
// Setup provides all objects needed for the generic channel tests.
31-
type Setup struct {
32-
// Params are the random parameters of `State`
33-
Params *channel.Params
34-
// Params2 are the parameters of `State2` and must differ in all fields from `Params`
35-
Params2 *channel.Params
36-
37-
// State is a random state with parameters `Params`
38-
State *channel.State
39-
// State2 is a random state with parameters `Params2` and should differ in all fields from `State`
40-
State2 *channel.State
41-
42-
// Account is a random account
43-
Account wallet.Account
44-
45-
// RandomAddress returns a new random address
46-
RandomAddress addressCreator
31+
type (
32+
Setup struct {
33+
// Params are the random parameters of `State`
34+
Params *channel.Params
35+
// Params2 are the parameters of `State2` and must differ in all fields from `Params`
36+
Params2 *channel.Params
37+
38+
// State is a random state with parameters `Params`
39+
State *channel.State
40+
// State2 is a random state with parameters `Params2` and should differ in all fields from `State`
41+
State2 *channel.State
42+
43+
// Account is a random account
44+
Account wallet.Account
45+
46+
// RandomAddress returns a new random address
47+
RandomAddress addressCreator
48+
}
49+
50+
// GenericTestOption can be used to control the behaviour of generic tests.
51+
GenericTestOption int
52+
// GenericTestOptions is a collection of GenericTestOption.
53+
GenericTestOptions map[GenericTestOption]bool
54+
)
55+
56+
const (
57+
// IgnoreAssets ignores the Assets in tests that support it.
58+
IgnoreAssets GenericTestOption = iota
59+
// IgnoreApp ignores the App in tests that support it.
60+
IgnoreApp
61+
)
62+
63+
// mergeTestOpts merges all passed options into one and returns it.
64+
func mergeTestOpts(opts ...GenericTestOption) GenericTestOptions {
65+
ret := make(GenericTestOptions)
66+
for _, o := range opts {
67+
ret[o] = true
68+
}
69+
return ret
4770
}
4871

4972
// GenericBackendTest tests the interface functions of the global channel.Backend with the passed test data.
50-
func GenericBackendTest(t *testing.T, s *Setup) {
73+
func GenericBackendTest(t *testing.T, s *Setup, opts ...GenericTestOption) {
5174
require := require.New(t)
5275
ID := channel.CalcID(s.Params)
5376
require.Equal(ID, s.State.ID, "ChannelID(params) should match the States ID")
@@ -64,7 +87,7 @@ func GenericBackendTest(t *testing.T, s *Setup) {
6487
})
6588

6689
t.Run("Verify", func(t *testing.T) {
67-
genericVerifyTest(t, s)
90+
genericVerifyTest(t, s, opts...)
6891
})
6992
}
7093

@@ -85,7 +108,7 @@ func genericSignTest(t *testing.T, s *Setup) {
85108
assert.NoError(t, err, "Sign should not return an error")
86109
}
87110

88-
func genericVerifyTest(t *testing.T, s *Setup) {
111+
func genericVerifyTest(t *testing.T, s *Setup, opts ...GenericTestOption) {
89112
addr := s.Account.Address()
90113
require.Equal(t, channel.CalcID(s.Params), s.Params.ID(), "Invalid test params")
91114
sig, err := channel.Sign(s.Account, s.State)
@@ -95,7 +118,7 @@ func genericVerifyTest(t *testing.T, s *Setup) {
95118
assert.NoError(t, err, "Verify should not return an error")
96119
assert.True(t, ok, "Verify should return true")
97120

98-
for i, _modState := range buildModifiedStates(s.State, s.State2, false) {
121+
for i, _modState := range buildModifiedStates(s.State, s.State2, append(opts, IgnoreApp)...) {
99122
modState := _modState
100123
ok, err = channel.Verify(addr, &modState, sig)
101124
assert.Falsef(t, ok, "Verify should return false: index %d", i)
@@ -170,7 +193,8 @@ func appendModParams(a []channel.Params, modParams channel.Params) []channel.Par
170193
// every member from `s1`.
171194
// `modifyApp` indicates whether the app should also be changed or not. In some cases (signature) it is desirable
172195
// not to modify it.
173-
func buildModifiedStates(s1, s2 *channel.State, modifyApp bool) (ret []channel.State) {
196+
func buildModifiedStates(s1, s2 *channel.State, _opts ...GenericTestOption) (ret []channel.State) {
197+
opts := mergeTestOpts(_opts...)
174198
// Modify state
175199
{
176200
// Modify complete state
@@ -191,7 +215,7 @@ func buildModifiedStates(s1, s2 *channel.State, modifyApp bool) (ret []channel.S
191215
ret = append(ret, *modState)
192216
}
193217
// Modify App
194-
if modifyApp {
218+
if !opts[IgnoreApp] {
195219
modState := s1.Clone()
196220
modState.App = s2.App
197221
ret = append(ret, *modState)
@@ -205,7 +229,7 @@ func buildModifiedStates(s1, s2 *channel.State, modifyApp bool) (ret []channel.S
205229
ret = append(ret, *modState)
206230
}
207231
// Modify Assets
208-
{
232+
if !opts[IgnoreAssets] {
209233
// Modify complete Assets
210234
{
211235
modState := s1.Clone()
@@ -331,11 +355,11 @@ func ensureBalanceVectorLength(bals []channel.Bal, l int) []channel.Bal {
331355
}
332356

333357
// GenericStateEqualTest tests the State.Equal function.
334-
func GenericStateEqualTest(t *testing.T, s1, s2 *channel.State) {
358+
func GenericStateEqualTest(t *testing.T, s1, s2 *channel.State, opts ...GenericTestOption) {
335359
assert.NoError(t, s1.Equal(s1))
336360
assert.NoError(t, s2.Equal(s2))
337361

338-
for _, differentState := range buildModifiedStates(s1, s2, true) {
362+
for _, differentState := range buildModifiedStates(s1, s2, opts...) {
339363
assert.Error(t, differentState.Equal(s1))
340364
}
341365
}

0 commit comments

Comments
 (0)