summaryrefslogtreecommitdiffhomepage
path: root/pkg/state/tests/register_test.go
blob: 75bdbfc6ea5818e5bb5ecf022614e075e4171098 (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
// Copyright 2018 The gVisor Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

// +build race

package tests

import (
	"testing"

	"gvisor.dev/gvisor/pkg/state"
)

// faker calls itself whatever is in the name field.
type faker struct {
	Name   string
	Fields []string
}

func (f *faker) StateTypeName() string {
	return f.Name
}

func (f *faker) StateFields() []string {
	return f.Fields
}

// fakerWithSaverLoader has all it needs.
type fakerWithSaverLoader struct {
	faker
}

func (f *fakerWithSaverLoader) StateSave(m state.Sink) {}

func (f *fakerWithSaverLoader) StateLoad(m state.Source) {}

// fakerOther calls itself .. uh, itself?
type fakerOther string

func (f *fakerOther) StateTypeName() string {
	return string(*f)
}

func (f *fakerOther) StateFields() []string {
	return nil
}

func newFakerOther(name string) *fakerOther {
	f := fakerOther(name)
	return &f
}

// fakerOtherBadFields returns non-nil fields.
type fakerOtherBadFields string

func (f *fakerOtherBadFields) StateTypeName() string {
	return string(*f)
}

func (f *fakerOtherBadFields) StateFields() []string {
	return []string{string(*f)}
}

func newFakerOtherBadFields(name string) *fakerOtherBadFields {
	f := fakerOtherBadFields(name)
	return &f
}

// fakerOtherSaverLoader implements SaverLoader methods.
type fakerOtherSaverLoader string

func (f *fakerOtherSaverLoader) StateTypeName() string {
	return string(*f)
}

func (f *fakerOtherSaverLoader) StateFields() []string {
	return nil
}

func (f *fakerOtherSaverLoader) StateSave(m state.Sink) {}

func (f *fakerOtherSaverLoader) StateLoad(m state.Source) {}

func newFakerOtherSaverLoader(name string) *fakerOtherSaverLoader {
	f := fakerOtherSaverLoader(name)
	return &f
}

func TestRegisterPrimitives(t *testing.T) {
	for _, typeName := range []string{
		"int",
		"int8",
		"int16",
		"int32",
		"int64",
		"uint",
		"uintptr",
		"uint8",
		"uint16",
		"uint32",
		"uint64",
		"float32",
		"float64",
		"complex64",
		"complex128",
		"string",
	} {
		t.Run("struct/"+typeName, func(t *testing.T) {
			defer func() {
				if r := recover(); r == nil {
					t.Errorf("Registering type %q did not panic", typeName)
				}
			}()
			state.Register(&faker{
				Name: typeName,
			})
		})
		t.Run("other/"+typeName, func(t *testing.T) {
			defer func() {
				if r := recover(); r == nil {
					t.Errorf("Registering type %q did not panic", typeName)
				}
			}()
			state.Register(newFakerOther(typeName))
		})
	}
}

func TestRegisterBad(t *testing.T) {
	const (
		goodName    = "foo"
		firstField  = "a"
		secondField = "b"
	)
	for name, object := range map[string]state.Type{
		"non-struct-with-fields":                  newFakerOtherBadFields(goodName),
		"non-struct-with-saverloader":             newFakerOtherSaverLoader(goodName),
		"struct-without-saverloader":              &faker{Name: goodName},
		"non-struct-duplicate-with-struct":        newFakerOther((new(alreadyRegisteredStruct)).StateTypeName()),
		"non-struct-duplicate-with-non-struct":    newFakerOther((new(alreadyRegisteredOther)).StateTypeName()),
		"struct-duplicate-with-struct":            &fakerWithSaverLoader{faker{Name: (new(alreadyRegisteredStruct)).StateTypeName()}},
		"struct-duplicate-with-non-struct":        &fakerWithSaverLoader{faker{Name: (new(alreadyRegisteredOther)).StateTypeName()}},
		"struct-with-empty-field":                 &fakerWithSaverLoader{faker{Name: goodName, Fields: []string{""}}},
		"struct-with-empty-field-and-non-empty":   &fakerWithSaverLoader{faker{Name: goodName, Fields: []string{firstField, ""}}},
		"struct-with-duplicate-field":             &fakerWithSaverLoader{faker{Name: goodName, Fields: []string{firstField, firstField}}},
		"struct-with-duplicate-field-and-non-dup": &fakerWithSaverLoader{faker{Name: goodName, Fields: []string{firstField, secondField, firstField}}},
	} {
		t.Run(name, func(t *testing.T) {
			defer func() {
				if r := recover(); r == nil {
					t.Errorf("Registering object %#v did not panic", object)
				}
			}()
			state.Register(object)
		})

	}
}

func TestRegisterTypeOnlyStruct(t *testing.T) {
	defer func() {
		if r := recover(); r == nil {
			t.Errorf("Register did not panic")
		}
	}()
	state.Register((*typeOnlyEmptyStruct)(nil))
}