forked from diskfs/go-diskfs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
diskfs_test.go
134 lines (122 loc) · 5.26 KB
/
diskfs_test.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
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
package diskfs_test
import (
"fmt"
"io/ioutil"
"os"
"strings"
"testing"
"github.com/diskfs/go-diskfs"
"github.com/diskfs/go-diskfs/disk"
)
const oneMB = 10 * 1024 * 1024
func tmpDisk(source string) (*os.File, error) {
filename := "disk_test"
f, err := ioutil.TempFile("", filename)
if err != nil {
return nil, fmt.Errorf("Failed to create tempfile %s :%v", filename, err)
}
// either copy the contents of the source file over, or make a file of appropriate size
if source == "" {
// make it a 10MB file
f.Truncate(10 * 1024 * 1024)
} else {
b, err := ioutil.ReadFile(source)
if err != nil {
return nil, fmt.Errorf("Failed to read contents of %s: %v", source, err)
}
written, err := f.Write(b)
if err != nil {
return nil, fmt.Errorf("Failed to write contents of %s to %s: %v", source, filename, err)
}
if written != len(b) {
return nil, fmt.Errorf("Wrote only %d bytes of %s to %s instead of %d", written, source, filename, len(b))
}
}
return f, nil
}
func TestOpen(t *testing.T) {
f, err := tmpDisk("./partition/mbr/testdata/mbr.img")
if err != nil {
t.Fatalf("Error creating new temporary disk: %v", err)
}
defer f.Close()
path := f.Name()
defer os.Remove(path)
fileInfo, err := f.Stat()
if err != nil {
t.Fatalf("Unable to stat temporary file %s: %v", path, err)
}
size := fileInfo.Size()
tests := []struct {
path string
disk *disk.Disk
err error
}{
{"", nil, fmt.Errorf("must pass device name")},
{"/tmp/foo/bar/232323/23/2322/disk.img", nil, fmt.Errorf("")},
{path, &disk.Disk{Type: disk.File, LogicalBlocksize: 512, PhysicalBlocksize: 512, Size: size}, nil},
}
for _, tt := range tests {
d, err := diskfs.Open(tt.path)
msg := fmt.Sprintf("Open(%s)", tt.path)
switch {
case (err == nil && tt.err != nil) || (err != nil && tt.err == nil) || (err != nil && tt.err != nil && !strings.HasPrefix(err.Error(), tt.err.Error())):
t.Errorf("%s: mismatched errors, actual %v expected %v", msg, err, tt.err)
case (d == nil && tt.disk != nil) || (d != nil && tt.disk == nil):
t.Errorf("%s: mismatched disk, actual %v expected %v", msg, d, tt.disk)
case d != nil && (d.LogicalBlocksize != tt.disk.LogicalBlocksize || d.PhysicalBlocksize != tt.disk.PhysicalBlocksize || d.Size != tt.disk.Size || d.Type != tt.disk.Type):
t.Errorf("%s: mismatched disk, actual then expected", msg)
t.Logf("%v", d)
t.Logf("%v", tt.disk)
}
}
for i, tt := range tests {
d, err := diskfs.Open(tt.path, diskfs.WithOpenMode(diskfs.ReadOnly))
msg := fmt.Sprintf("%d: Open(%s)", i, tt.path)
switch {
case (err == nil && tt.err != nil) || (err != nil && tt.err == nil) || (err != nil && tt.err != nil && !strings.HasPrefix(err.Error(), tt.err.Error())):
t.Errorf("%s: mismatched errors, actual %v expected %v", msg, err, tt.err)
case (d == nil && tt.disk != nil) || (d != nil && tt.disk == nil):
t.Errorf("%s: mismatched disk, actual %v expected %v", msg, d, tt.disk)
case d != nil && (d.LogicalBlocksize != tt.disk.LogicalBlocksize || d.PhysicalBlocksize != tt.disk.PhysicalBlocksize || d.Size != tt.disk.Size || d.Type != tt.disk.Type):
t.Errorf("%s: mismatched disk, actual then expected", msg)
t.Logf("%v", d)
t.Logf("%v", tt.disk)
}
}
}
func TestCreate(t *testing.T) {
tests := []struct {
path string
size int64
format diskfs.Format
sectorSize diskfs.SectorSize
disk *disk.Disk
err error
}{
{"", 10 * oneMB, diskfs.Raw, diskfs.SectorSizeDefault, nil, fmt.Errorf("must pass device name")},
{"/tmp/disk.img", 0, diskfs.Raw, diskfs.SectorSizeDefault, nil, fmt.Errorf("must pass valid device size to create")},
{"/tmp/disk.img", -1, diskfs.Raw, diskfs.SectorSizeDefault, nil, fmt.Errorf("must pass valid device size to create")},
{"/tmp/foo/bar/232323/23/2322/disk.img", 10 * oneMB, diskfs.Raw, diskfs.SectorSizeDefault, nil, fmt.Errorf("Could not create device")},
{"/tmp/disk.img", 10 * oneMB, diskfs.Raw, diskfs.SectorSizeDefault, &disk.Disk{LogicalBlocksize: 512, PhysicalBlocksize: 512, Size: 10 * oneMB, Type: disk.File}, nil},
{"/tmp/disk.img", 10 * oneMB, diskfs.Raw, diskfs.SectorSize512, &disk.Disk{LogicalBlocksize: 512, PhysicalBlocksize: 512, Size: 10 * oneMB, Type: disk.File}, nil},
{"/tmp/disk.img", 10 * oneMB, diskfs.Raw, diskfs.SectorSize4k, &disk.Disk{LogicalBlocksize: 4096, PhysicalBlocksize: 4096, Size: 10 * oneMB, Type: disk.File}, nil},
}
for i, tt := range tests {
disk, err := diskfs.Create(tt.path, tt.size, tt.format, tt.sectorSize)
msg := fmt.Sprintf("%d: Create(%s, %d, %v, %d)", i, tt.path, tt.size, tt.format, tt.sectorSize)
switch {
case (err == nil && tt.err != nil) || (err != nil && tt.err == nil) || (err != nil && tt.err != nil && !strings.HasPrefix(err.Error(), tt.err.Error())):
t.Errorf("%s: mismatched errors, actual %v expected %v", msg, err, tt.err)
case (disk == nil && tt.disk != nil) || (disk != nil && tt.disk == nil):
t.Errorf("%s: mismatched disk, actual %v expected %v", msg, disk, tt.disk)
case disk != nil && (disk.LogicalBlocksize != tt.disk.LogicalBlocksize || disk.PhysicalBlocksize != tt.disk.PhysicalBlocksize || disk.Size != tt.disk.Size || disk.Type != tt.disk.Type):
t.Errorf("%s: mismatched disk, actual then expected", msg)
t.Logf("%#v", disk)
t.Logf("%#v", tt.disk)
}
if disk != nil {
os.Remove(tt.path)
}
}
}