Repository navigation
Expand file tree
/
Copy pathops_linux_test.go
More file actions
243 lines (200 loc) · 6.66 KB
/
Copy pathops_linux_test.go
File metadata and controls
243 lines (200 loc) · 6.66 KB
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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
// Copyright 2016 VMware, Inc. All Rights Reserved.
//
// 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 linux
package tether
import (
"errors"
"fmt"
"os/user"
"path"
"strconv"
"strings"
"syscall"
"testing"
"github.com/stretchr/testify/assert"
"github.com/vishvananda/netlink"
"github.com/vmware/vic/pkg/trace"
)
type Interface struct {
netlink.LinkAttrs
Up bool
Addrs []netlink.Addr
}
func (t *Interface) Attrs() *netlink.LinkAttrs {
return &t.LinkAttrs
}
func (t *Interface) Type() string {
return "mocked"
}
func (t *Mocker) LinkByName(name string) (netlink.Link, error) {
defer trace.End(trace.Begin(fmt.Sprintf("Getting link by name %s", name)))
return t.Interfaces[name], nil
}
func (t *Mocker) LinkSetName(link netlink.Link, name string) error {
defer trace.End(trace.Begin(fmt.Sprintf("Renaming %s to %s", link.Attrs().Name, name)))
iface := link.(*Interface)
_, ok := t.Interfaces[name]
if ok {
return fmt.Errorf("Interface with name %s already exists", name)
}
oldName := iface.Name
iface.Name = name
// make sure there's no period where the interface isn't "present"
t.Interfaces[name] = iface
delete(t.Interfaces, oldName)
return nil
}
func (t *Mocker) LinkSetDown(link netlink.Link) error {
defer trace.End(trace.Begin(fmt.Sprintf("Bringing %s down", link.Attrs().Name)))
iface := link.(*Interface)
iface.Up = false
// TODO: should this drop addresses?
return nil
}
func (t *Mocker) LinkSetUp(link netlink.Link) error {
defer trace.End(trace.Begin(fmt.Sprintf("Bringing %s up", link.Attrs().Name)))
iface := link.(*Interface)
iface.Up = true
return nil
}
func (t *Mocker) LinkSetAlias(link netlink.Link, alias string) error {
defer trace.End(trace.Begin(fmt.Sprintf("Adding alias %s to %s", alias, link.Attrs().Name)))
iface := link.(*Interface)
iface.Alias = alias
return nil
}
func (t *Mocker) AddrList(link netlink.Link, family int) ([]netlink.Addr, error) {
defer trace.End(trace.Begin(""))
iface := link.(*Interface)
return iface.Addrs, nil
}
func (t *Mocker) AddrAdd(link netlink.Link, addr *netlink.Addr) error {
defer trace.End(trace.Begin(fmt.Sprintf("Adding %s to %s", addr.String(), link.Attrs().Name)))
iface := link.(*Interface)
for _, adr := range iface.Addrs {
if addr.IP.String() == adr.IP.String() {
return syscall.EEXIST
}
}
iface.Addrs = append(iface.Addrs, *addr)
return nil
}
func (t *Mocker) AddrDel(link netlink.Link, addr *netlink.Addr) error {
iface := link.(*Interface)
for i, adr := range iface.Addrs {
if addr.IP.String() == adr.IP.String() {
iface.Addrs = append(iface.Addrs[:i], iface.Addrs[i+1:]...)
return nil
}
}
return syscall.EADDRNOTAVAIL
}
func (t *Mocker) RouteAdd(route *netlink.Route) error {
defer trace.End(trace.Begin("no implemented"))
// currently ignored
return nil
}
func (t *Mocker) RouteDel(route *netlink.Route) error {
defer trace.End(trace.Begin("no implemented"))
// currently ignored
return nil
}
func (t *Mocker) LinkBySlot(slot int32) (netlink.Link, error) {
defer trace.End(trace.Begin(""))
id := int(slot)
for _, intf := range t.Interfaces {
if intf.Attrs().Index == id {
return intf, nil
}
}
return nil, errors.New("no such interface")
}
func TestSlotToPciPath(t *testing.T) {
var tests = []struct {
slot int32
p string
err error
}{
{0, path.Join(pciDevPath, "0000:00:00.0"), nil},
{32, path.Join(pciDevPath, "0000:00:11.0", "0000:*:00.0"), nil},
{33, path.Join(pciDevPath, "0000:00:11.0", "0000:*:01.0"), nil},
{192, path.Join(pciDevPath, "0000:00:16.0", "0000:*:00.0"), nil},
}
for _, te := range tests {
p, err := slotToPCIPath(te.slot)
if te.err != nil {
if err == nil {
t.Fatalf("slotToPCIPath(%d) => (%#v, %#v), want (%s, nil)", te.slot, p, err, te.p)
}
continue
}
if p != te.p {
t.Fatalf("slotToPCIPath(%d) => (%#v, %#v), want (%s, nil)", te.slot, p, err, te.p)
}
}
}
func TestGetUserSysProcAttr(t *testing.T) {
curr, err := user.Current()
if err != nil {
t.Logf("Failed to get current user, skip test")
return
}
currUID, _ := strconv.Atoi(curr.Uid)
currGID, _ := strconv.Atoi(curr.Gid)
moreThanMax := strconv.Itoa(1 << 33)
lessThanMin := "-100"
var tests = []struct {
uid string
gid string
ruid int
rgid int
err error
}{
{"", "", 0, 0, nil},
{"notexist", "notexist", 0, 0, errors.New("Unable to find user notexist")},
{"notexist", curr.Gid, 0, 0, errors.New("Unable to find user notexist")},
{"", "notexist", 0, 0, errors.New("Unable to find group notexist")},
{"0", "notexist", 0, 0, errors.New("Unable to find group notexist")},
{curr.Uid, "notexist", 0, 0, errors.New("Unable to find group notexist")},
{"2000000", "notexist", 0, 0, errors.New("Unable to find group notexist")},
{curr.Username, "notexist", 0, 0, errors.New("Unable to find group notexist")},
{curr.Username, "0", currUID, 0, nil},
{curr.Uid, "1000", currUID, 1000, nil},
{curr.Uid, curr.Gid, currUID, currGID, nil},
{"root", curr.Gid, 0, currGID, nil},
{"root", "root", 0, 0, nil},
{moreThanMax, "root", 0, 0, errors.New("Uids and gids must be in range 0-2147483647")},
{curr.Username, moreThanMax, 0, 0, errors.New("Uids and gids must be in range 0-2147483647")},
{lessThanMin, "root", 0, 0, errors.New("Uids and gids must be in range 0-2147483647")},
{curr.Username, lessThanMin, 0, 0, errors.New("Uids and gids must be in range 0-2147483647")},
}
for _, test := range tests {
t.Logf("uid: %s, gid: %s", test.uid, test.gid)
user, err := getUserSysProcAttr(test.uid, test.gid)
if err != nil {
assert.True(t, test.err != nil, fmt.Sprintf("Should not have error %s", err))
if !strings.Contains(err.Error(), test.err.Error()) {
assert.True(t, false, fmt.Sprintf("error message mismatch, expected %s, actual %s", test.err, err.Error()))
}
continue
}
assert.True(t, test.err == nil, fmt.Sprintf("didn't get expected error: %s", test.err))
if user == nil {
assert.True(t, (test.ruid == 0 && test.rgid == 0), "returned user is nil, but expect not nil result: %d:%d", test.ruid, test.rgid)
continue
}
assert.Equal(t, test.ruid, int(user.Credential.Uid), "returned user id mismatch")
assert.Equal(t, test.rgid, int(user.Credential.Gid), "returned group id mismatch")
}
}