...
1
2
3
4 package winio
5
6 import (
7 "os"
8 "testing"
9
10 "golang.org/x/sys/windows"
11 )
12
13
14
15
16 func checkFileStandardInfo(t *testing.T, current, expected *FileStandardInfo) {
17 if current.AllocationSize < expected.AllocationSize {
18 t.Fatalf("FileStandardInfo unexpectedly had AllocationSize %d, expecting >=%d", current.AllocationSize, expected.AllocationSize)
19 }
20
21 if current.EndOfFile != expected.EndOfFile {
22 t.Fatalf("FileStandardInfo unexpectedly had EndOfFile %d, expecting %d", current.EndOfFile, expected.EndOfFile)
23 }
24
25 if current.NumberOfLinks != expected.NumberOfLinks {
26 t.Fatalf("FileStandardInfo unexpectedly had NumberOfLinks %d, expecting %d", current.NumberOfLinks, expected.NumberOfLinks)
27 }
28
29 if current.DeletePending != expected.DeletePending {
30 if current.DeletePending {
31 t.Fatalf("FileStandardInfo unexpectedly DeletePending")
32 } else {
33 t.Fatalf("FileStandardInfo unexpectedly not DeletePending")
34 }
35 }
36
37 if current.Directory != expected.Directory {
38 if current.Directory {
39 t.Fatalf("FileStandardInfo unexpectedly Directory")
40 } else {
41 t.Fatalf("FileStandardInfo unexpectedly not Directory")
42 }
43 }
44 }
45
46 func TestGetFileStandardInfo_File(t *testing.T) {
47 f, err := os.CreateTemp("", "tst")
48 if err != nil {
49 t.Fatal(err)
50 }
51 defer f.Close()
52 defer os.Remove(f.Name())
53
54 expectedFileInfo := &FileStandardInfo{
55 AllocationSize: 0,
56 EndOfFile: 0,
57 NumberOfLinks: 1,
58 DeletePending: false,
59 Directory: false,
60 }
61
62 info, err := GetFileStandardInfo(f)
63 if err != nil {
64 t.Fatal(err)
65 }
66 checkFileStandardInfo(t, info, expectedFileInfo)
67
68 bytesWritten, err := f.Write([]byte("0123456789"))
69 if err != nil {
70 t.Fatal(err)
71 }
72
73 expectedFileInfo.EndOfFile = int64(bytesWritten)
74 expectedFileInfo.AllocationSize = int64(bytesWritten)
75
76 info, err = GetFileStandardInfo(f)
77 if err != nil {
78 t.Fatal(err)
79 }
80 checkFileStandardInfo(t, info, expectedFileInfo)
81
82 linkName := f.Name() + ".link"
83
84 if err = os.Link(f.Name(), linkName); err != nil {
85 t.Fatal(err)
86 }
87 defer os.Remove(linkName)
88
89 expectedFileInfo.NumberOfLinks = 2
90
91 info, err = GetFileStandardInfo(f)
92 if err != nil {
93 t.Fatal(err)
94 }
95 checkFileStandardInfo(t, info, expectedFileInfo)
96
97 os.Remove(linkName)
98
99 expectedFileInfo.NumberOfLinks = 1
100
101 info, err = GetFileStandardInfo(f)
102 if err != nil {
103 t.Fatal(err)
104 }
105 checkFileStandardInfo(t, info, expectedFileInfo)
106 }
107
108 func TestGetFileStandardInfo_Directory(t *testing.T) {
109 tempDir := t.TempDir()
110
111
112 f, err := OpenForBackup(tempDir, windows.GENERIC_READ, 0, windows.OPEN_EXISTING)
113 if err != nil {
114 t.Fatal(err)
115 }
116 defer f.Close()
117
118 expectedFileInfo := &FileStandardInfo{
119 AllocationSize: 0,
120 EndOfFile: 0,
121 NumberOfLinks: 1,
122 DeletePending: false,
123 Directory: true,
124 }
125
126 info, err := GetFileStandardInfo(f)
127 if err != nil {
128 t.Fatal(err)
129 }
130 checkFileStandardInfo(t, info, expectedFileInfo)
131 }
132
View as plain text