summaryrefslogtreecommitdiffhomepage
path: root/pkg/sentry/device/device_test.go
blob: dfec45046cbde084df4071d5341e771074f6da47 (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
// Copyright 2018 Google Inc.
//
// 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 device

import (
	"testing"
)

func TestMultiDevice(t *testing.T) {
	device := &MultiDevice{}

	// Check that Load fails to install virtual inodes that are
	// uninitialized.
	if device.Load(MultiDeviceKey{}, 0) {
		t.Fatalf("got load of invalid virtual inode 0, want unsuccessful")
	}

	inode := device.Map(MultiDeviceKey{})

	// Assert that the same raw device and inode map to
	// a consistent virtual inode.
	if i := device.Map(MultiDeviceKey{}); i != inode {
		t.Fatalf("got inode %d, want %d in %s", i, inode, device)
	}

	// Assert that a new inode or new device does not conflict.
	if i := device.Map(MultiDeviceKey{Device: 0, Inode: 1}); i == inode {
		t.Fatalf("got reused inode %d, want new distinct inode in %s", i, device)
	}
	last := device.Map(MultiDeviceKey{Device: 1, Inode: 0})
	if last == inode {
		t.Fatalf("got reused inode %d, want new distinct inode in %s", last, device)
	}

	// Virtual is the virtual inode we want to load.
	virtual := last + 1

	// Assert that we can load a virtual inode at a new place.
	if !device.Load(MultiDeviceKey{Device: 0, Inode: 2}, virtual) {
		t.Fatalf("got load of virtual inode %d failed, want success in %s", virtual, device)
	}

	// Assert that the next inode skips over the loaded one.
	if i := device.Map(MultiDeviceKey{Device: 0, Inode: 3}); i != virtual+1 {
		t.Fatalf("got inode %d, want %d in %s", i, virtual+1, device)
	}
}