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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
|
// 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.
// Package tests tests the state packages.
package tests
import (
"bytes"
"context"
"fmt"
"math"
"reflect"
"testing"
"gvisor.dev/gvisor/pkg/state"
"gvisor.dev/gvisor/pkg/state/pretty"
)
// discard is an implementation of wire.Writer.
type discard struct{}
// Write implements wire.Writer.Write.
func (discard) Write(p []byte) (int, error) { return len(p), nil }
// WriteByte implements wire.Writer.WriteByte.
func (discard) WriteByte(byte) error { return nil }
// checkEqual checks if two objects are equal.
//
// N.B. This only handles one level of dereferences for NaN. Otherwise we
// would need to fork the entire implementation of reflect.DeepEqual.
func checkEqual(root, loadedValue interface{}) bool {
if reflect.DeepEqual(root, loadedValue) {
return true
}
// NaN is not equal to itself. We handle the case of raw floating point
// primitives here, but don't handle this case nested.
rf32, ok1 := root.(float32)
lf32, ok2 := loadedValue.(float32)
if ok1 && ok2 && math.IsNaN(float64(rf32)) && math.IsNaN(float64(lf32)) {
return true
}
rf64, ok1 := root.(float64)
lf64, ok2 := loadedValue.(float64)
if ok1 && ok2 && math.IsNaN(rf64) && math.IsNaN(lf64) {
return true
}
// Same real for complex numbers.
rc64, ok1 := root.(complex64)
lc64, ok2 := root.(complex64)
if ok1 && ok2 {
return checkEqual(real(rc64), real(lc64)) && checkEqual(imag(rc64), imag(lc64))
}
rc128, ok1 := root.(complex128)
lc128, ok2 := root.(complex128)
if ok1 && ok2 {
return checkEqual(real(rc128), real(lc128)) && checkEqual(imag(rc128), imag(lc128))
}
return false
}
// runTestCases runs a test for each object in objects.
func runTestCases(t *testing.T, shouldFail bool, prefix string, objects []interface{}) {
t.Helper()
for i, root := range objects {
t.Run(fmt.Sprintf("%s%d", prefix, i), func(t *testing.T) {
t.Logf("Original object:\n%#v", root)
// Save the passed object.
saveBuffer := &bytes.Buffer{}
saveObjectPtr := reflect.New(reflect.TypeOf(root))
saveObjectPtr.Elem().Set(reflect.ValueOf(root))
saveStats, err := state.Save(context.Background(), saveBuffer, saveObjectPtr.Interface())
if err != nil {
if shouldFail {
return
}
t.Fatalf("Save failed unexpectedly: %v", err)
}
// Dump the serialized proto to aid with debugging.
var ppBuf bytes.Buffer
t.Logf("Raw state:\n%v", saveBuffer.Bytes())
if err := pretty.PrintText(&ppBuf, bytes.NewReader(saveBuffer.Bytes())); err != nil {
// We don't count this as a test failure if we
// have shouldFail set, but we will count as a
// failure if we were not expecting to fail.
if !shouldFail {
t.Errorf("PrettyPrint(html=false) failed unexpected: %v", err)
}
}
if err := pretty.PrintHTML(discard{}, bytes.NewReader(saveBuffer.Bytes())); err != nil {
// See above.
if !shouldFail {
t.Errorf("PrettyPrint(html=true) failed unexpected: %v", err)
}
}
t.Logf("Encoded state:\n%s", ppBuf.String())
t.Logf("Save stats:\n%s", saveStats.String())
// Load a new copy of the object.
loadObjectPtr := reflect.New(reflect.TypeOf(root))
loadStats, err := state.Load(context.Background(), bytes.NewReader(saveBuffer.Bytes()), loadObjectPtr.Interface())
if err != nil {
if shouldFail {
return
}
t.Fatalf("Load failed unexpectedly: %v", err)
}
// Compare the values.
loadedValue := loadObjectPtr.Elem().Interface()
if !checkEqual(root, loadedValue) {
if shouldFail {
return
}
t.Fatalf("Objects differ:\n\toriginal: %#v\n\tloaded: %#v\n", root, loadedValue)
}
// Everything went okay. Is that good?
if shouldFail {
t.Fatalf("This test was expected to fail, but didn't.")
}
t.Logf("Load stats:\n%s", loadStats.String())
// Truncate half the bytes in the byte stream,
// and ensure that we can't restore. Then
// truncate only the final byte and ensure that
// we can't restore.
l := saveBuffer.Len()
halfReader := bytes.NewReader(saveBuffer.Bytes()[:l/2])
if _, err := state.Load(context.Background(), halfReader, loadObjectPtr.Interface()); err == nil {
t.Errorf("Load with half bytes succeeded unexpectedly.")
}
missingByteReader := bytes.NewReader(saveBuffer.Bytes()[:l-1])
if _, err := state.Load(context.Background(), missingByteReader, loadObjectPtr.Interface()); err == nil {
t.Errorf("Load with missing byte succeeded unexpectedly.")
}
})
}
}
// convert converts the slice to an []interface{}.
func convert(v interface{}) (r []interface{}) {
s := reflect.ValueOf(v) // Must be slice.
for i := 0; i < s.Len(); i++ {
r = append(r, s.Index(i).Interface())
}
return r
}
// flatten flattens multiple slices.
func flatten(vs ...interface{}) (r []interface{}) {
for _, v := range vs {
r = append(r, convert(v)...)
}
return r
}
// filter maps from one slice to another.
func filter(vs interface{}, fn func(interface{}) (interface{}, bool)) (r []interface{}) {
s := reflect.ValueOf(vs)
for i := 0; i < s.Len(); i++ {
v, ok := fn(s.Index(i).Interface())
if ok {
r = append(r, v)
}
}
return r
}
// combine combines objects in two slices as specified.
func combine(v1, v2 interface{}, fn func(_, _ interface{}) interface{}) (r []interface{}) {
s1 := reflect.ValueOf(v1)
s2 := reflect.ValueOf(v2)
for i := 0; i < s1.Len(); i++ {
for j := 0; j < s2.Len(); j++ {
// Combine using the given function.
r = append(r, fn(s1.Index(i).Interface(), s2.Index(j).Interface()))
}
}
return r
}
// pointersTo is a filter function that returns pointers.
func pointersTo(vs interface{}) []interface{} {
return filter(vs, func(o interface{}) (interface{}, bool) {
v := reflect.New(reflect.TypeOf(o))
v.Elem().Set(reflect.ValueOf(o))
return v.Interface(), true
})
}
// interfacesTo is a filter function that returns interface objects.
func interfacesTo(vs interface{}) []interface{} {
return filter(vs, func(o interface{}) (interface{}, bool) {
var v [1]interface{}
v[0] = o
return v, true
})
}
|