-
Notifications
You must be signed in to change notification settings - Fork 0
/
ignoring_fds.go
87 lines (78 loc) · 3.01 KB
/
ignoring_fds.go
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
// Copyright 2022 Harald Albrecht.
//
// 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.
//go:build linux
package fdooze
import (
"fmt"
"github.com/onsi/gomega/format"
"github.com/onsi/gomega/types"
)
// IgnoringFiledescriptors succeeds if an actual FileDescriptor in contained in
// a slice of expected file descriptors. An actual FileDescriptor is considered
// to be contained, if the slice must contains a FileDescriptor with the same fd
// number and [filedesc.FileDescriptor.Equal] considers both file descriptors to
// be equal.
//
// Please note that fd flags and file offsets are ignored when testing for
// equality, in order to avoid spurious false positives.
func IgnoringFiledescriptors(fds []FileDescriptor) types.GomegaMatcher {
m := &ignoringFds{
ignoreFds: map[int]FileDescriptor{},
}
for _, fd := range fds {
m.ignoreFds[fd.FdNo()] = fd
}
return m
}
type ignoringFds struct {
ignoreFds map[int]FileDescriptor
}
// Match succeeds if actual is a [filedesc.FileDescriptor] that is contained in
// the set of expected file descriptors. Containment uses
// [filedesc.FileDescriptor.Equal] to test for file descriptor equality.
func (matcher *ignoringFds) Match(actual interface{}) (success bool, err error) {
actualFd, ok := actual.(FileDescriptor)
if !ok {
return false, fmt.Errorf(
"IgnoringFiledescriptor matcher expects a filedesc.FileDescriptor. Got:\n%s",
format.Object(actual, 1))
}
fd, ok := matcher.ignoreFds[actualFd.FdNo()]
if !ok {
return false, nil
}
return actualFd.Equal(fd), nil
}
// FailureMessage returns a failure message if the actual file descriptor isn't
// in the set of file descriptors to be ignored.
func (matcher *ignoringFds) FailureMessage(actual interface{}) (message string) {
expected := make([]FileDescriptor, 0, len(matcher.ignoreFds))
for _, fd := range matcher.ignoreFds {
expected = append(expected, fd)
}
return fmt.Sprintf("Expected\n%s\nto be contained in the list of expected file descriptors\n%s",
format.Object(actual, 1),
dumpFds(expected, 1))
}
// NegatedFailureMessage returns a failure message if the actual file descriptor
// actually is in the set of file descriptors to be ignored.
func (matcher *ignoringFds) NegatedFailureMessage(actual interface{}) (message string) {
expected := make([]FileDescriptor, 0, len(matcher.ignoreFds))
for _, fd := range matcher.ignoreFds {
expected = append(expected, fd)
}
return fmt.Sprintf("Expected\n%s\nnot to be contained in the list of expected file descriptors\n%s",
format.Object(actual, 1),
dumpFds(expected, 1))
}