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
|
// 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 syserror_test
import (
"errors"
"testing"
"golang.org/x/sys/unix"
"gvisor.dev/gvisor/pkg/syserror"
)
var globalError error
func BenchmarkAssignErrno(b *testing.B) {
for i := b.N; i > 0; i-- {
globalError = unix.EINVAL
}
}
func BenchmarkAssignError(b *testing.B) {
for i := b.N; i > 0; i-- {
globalError = syserror.EINVAL
}
}
func BenchmarkCompareErrno(b *testing.B) {
globalError = unix.EAGAIN
j := 0
for i := b.N; i > 0; i-- {
if globalError == unix.EINVAL {
j++
}
}
}
func BenchmarkCompareError(b *testing.B) {
globalError = syserror.EAGAIN
j := 0
for i := b.N; i > 0; i-- {
if globalError == syserror.EINVAL {
j++
}
}
}
func BenchmarkSwitchErrno(b *testing.B) {
globalError = unix.EPERM
j := 0
for i := b.N; i > 0; i-- {
switch globalError {
case unix.EINVAL:
j += 1
case unix.EINTR:
j += 2
case unix.EAGAIN:
j += 3
}
}
}
func BenchmarkSwitchError(b *testing.B) {
globalError = syserror.EPERM
j := 0
for i := b.N; i > 0; i-- {
switch globalError {
case syserror.EINVAL:
j += 1
case syserror.EINTR:
j += 2
case syserror.EAGAIN:
j += 3
}
}
}
type translationTestTable struct {
fn string
errIn error
syscallErrorIn unix.Errno
expectedBool bool
expectedTranslation unix.Errno
}
func TestErrorTranslation(t *testing.T) {
myError := errors.New("My test error")
myError2 := errors.New("Another test error")
testTable := []translationTestTable{
{"TranslateError", myError, 0, false, 0},
{"TranslateError", myError2, 0, false, 0},
{"AddErrorTranslation", myError, unix.EAGAIN, true, 0},
{"AddErrorTranslation", myError, unix.EAGAIN, false, 0},
{"AddErrorTranslation", myError, unix.EPERM, false, 0},
{"TranslateError", myError, 0, true, unix.EAGAIN},
{"TranslateError", myError2, 0, false, 0},
{"AddErrorTranslation", myError2, unix.EPERM, true, 0},
{"AddErrorTranslation", myError2, unix.EPERM, false, 0},
{"AddErrorTranslation", myError2, unix.EAGAIN, false, 0},
{"TranslateError", myError, 0, true, unix.EAGAIN},
{"TranslateError", myError2, 0, true, unix.EPERM},
}
for _, tt := range testTable {
switch tt.fn {
case "TranslateError":
err, ok := syserror.TranslateError(tt.errIn)
if ok != tt.expectedBool {
t.Fatalf("%v(%v) => %v expected %v", tt.fn, tt.errIn, ok, tt.expectedBool)
} else if err != tt.expectedTranslation {
t.Fatalf("%v(%v) (error) => %v expected %v", tt.fn, tt.errIn, err, tt.expectedTranslation)
}
case "AddErrorTranslation":
ok := syserror.AddErrorTranslation(tt.errIn, tt.syscallErrorIn)
if ok != tt.expectedBool {
t.Fatalf("%v(%v) => %v expected %v", tt.fn, tt.errIn, ok, tt.expectedBool)
}
default:
t.Fatalf("Unknown function %v", tt.fn)
}
}
}
|