aboutsummaryrefslogtreecommitdiff
path: root/internal/encoding/ssh/filexfer/openssh/statvfs_test.go
blob: 014aa6374a4bf5562e5e2d0d63d34876410fa243 (plain) (blame)
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
package openssh

import (
	"bytes"
	"testing"

	sshfx "github.com/pkg/sftp/internal/encoding/ssh/filexfer"
)

var _ sshfx.PacketMarshaller = &StatVFSExtendedPacket{}

func init() {
	RegisterExtensionStatVFS()
}

func TestStatVFSExtendedPacket(t *testing.T) {
	const (
		id   = 42
		path = "/foo"
	)

	ep := &StatVFSExtendedPacket{
		Path: path,
	}

	data, err := sshfx.ComposePacket(ep.MarshalPacket(id, nil))
	if err != nil {
		t.Fatal("unexpected error:", err)
	}

	want := []byte{
		0x00, 0x00, 0x00, 36,
		200,
		0x00, 0x00, 0x00, 42,
		0x00, 0x00, 0x00, 19, 's', 't', 'a', 't', 'v', 'f', 's', '@', 'o', 'p', 'e', 'n', 's', 's', 'h', '.', 'c', 'o', 'm',
		0x00, 0x00, 0x00, 4, '/', 'f', 'o', 'o',
	}

	if !bytes.Equal(data, want) {
		t.Fatalf("MarshalPacket() = %X, but wanted %X", data, want)
	}

	var p sshfx.ExtendedPacket

	// UnmarshalPacketBody assumes the (length, type, request-id) have already been consumed.
	if err := p.UnmarshalPacketBody(sshfx.NewBuffer(data[9:])); err != nil {
		t.Fatal("unexpected error:", err)
	}

	if p.ExtendedRequest != extensionStatVFS {
		t.Errorf("UnmarshalPacketBody(): ExtendedRequest was %q, but expected %q", p.ExtendedRequest, extensionStatVFS)
	}

	ep, ok := p.Data.(*StatVFSExtendedPacket)
	if !ok {
		t.Fatalf("UnmarshaledPacketBody(): Data was type %T, but expected *StatVFSExtendedPacket", p.Data)
	}

	if ep.Path != path {
		t.Errorf("UnmarshalPacketBody(): Path was %q, but expected %q", ep.Path, path)
	}
}

var _ sshfx.PacketMarshaller = &FStatVFSExtendedPacket{}

func init() {
	RegisterExtensionFStatVFS()
}

func TestFStatVFSExtendedPacket(t *testing.T) {
	const (
		id   = 42
		path = "/foo"
	)

	ep := &FStatVFSExtendedPacket{
		Path: path,
	}

	data, err := sshfx.ComposePacket(ep.MarshalPacket(id, nil))
	if err != nil {
		t.Fatal("unexpected error:", err)
	}

	want := []byte{
		0x00, 0x00, 0x00, 37,
		200,
		0x00, 0x00, 0x00, 42,
		0x00, 0x00, 0x00, 20, 'f', 's', 't', 'a', 't', 'v', 'f', 's', '@', 'o', 'p', 'e', 'n', 's', 's', 'h', '.', 'c', 'o', 'm',
		0x00, 0x00, 0x00, 4, '/', 'f', 'o', 'o',
	}

	if !bytes.Equal(data, want) {
		t.Fatalf("MarshalPacket() = %X, but wanted %X", data, want)
	}

	var p sshfx.ExtendedPacket

	// UnmarshalPacketBody assumes the (length, type, request-id) have already been consumed.
	if err := p.UnmarshalPacketBody(sshfx.NewBuffer(data[9:])); err != nil {
		t.Fatal("unexpected error:", err)
	}

	if p.ExtendedRequest != extensionFStatVFS {
		t.Errorf("UnmarshalPacketBody(): ExtendedRequest was %q, but expected %q", p.ExtendedRequest, extensionFStatVFS)
	}

	ep, ok := p.Data.(*FStatVFSExtendedPacket)
	if !ok {
		t.Fatalf("UnmarshaledPacketBody(): Data was type %T, but expected *FStatVFSExtendedPacket", p.Data)
	}

	if ep.Path != path {
		t.Errorf("UnmarshalPacketBody(): Path was %q, but expected %q", ep.Path, path)
	}
}

var _ sshfx.Packet = &StatVFSExtendedReplyPacket{}

func TestStatVFSExtendedReplyPacket(t *testing.T) {
	const (
		id   = 42
		path = "/foo"
	)

	const (
		BlockSize = uint64(iota + 13)
		FragmentSize
		Blocks
		BlocksFree
		BlocksAvail
		Files
		FilesFree
		FilesAvail
		FilesystemID
		MountFlags
		MaxNameLength
	)

	ep := &StatVFSExtendedReplyPacket{
		BlockSize:     BlockSize,
		FragmentSize:  FragmentSize,
		Blocks:        Blocks,
		BlocksFree:    BlocksFree,
		BlocksAvail:   BlocksAvail,
		Files:         Files,
		FilesFree:     FilesFree,
		FilesAvail:    FilesAvail,
		FilesystemID:  FilesystemID,
		MountFlags:    MountFlags,
		MaxNameLength: MaxNameLength,
	}

	data, err := sshfx.ComposePacket(ep.MarshalPacket(id, nil))
	if err != nil {
		t.Fatal("unexpected error:", err)
	}

	want := []byte{
		0x00, 0x00, 0x00, 93,
		201,
		0x00, 0x00, 0x00, 42,
		0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 13,
		0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 14,
		0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 15,
		0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 16,
		0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 17,
		0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 18,
		0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 19,
		0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 20,
		0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 21,
		0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 22,
		0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 23,
	}

	if !bytes.Equal(data, want) {
		t.Fatalf("MarshalPacket() = %X, but wanted %X", data, want)
	}

	*ep = StatVFSExtendedReplyPacket{}

	p := sshfx.ExtendedReplyPacket{
		Data: ep,
	}

	// UnmarshalPacketBody assumes the (length, type, request-id) have already been consumed.
	if err := p.UnmarshalPacketBody(sshfx.NewBuffer(data[9:])); err != nil {
		t.Fatal("unexpected error:", err)
	}

	ep, ok := p.Data.(*StatVFSExtendedReplyPacket)
	if !ok {
		t.Fatalf("UnmarshaledPacketBody(): Data was type %T, but expected *StatVFSExtendedReplyPacket", p.Data)
	}

	if ep.BlockSize != BlockSize {
		t.Errorf("UnmarshalPacketBody(): BlockSize was %d, but expected %d", ep.BlockSize, BlockSize)
	}

	if ep.FragmentSize != FragmentSize {
		t.Errorf("UnmarshalPacketBody(): FragmentSize was %d, but expected %d", ep.FragmentSize, FragmentSize)
	}

	if ep.Blocks != Blocks {
		t.Errorf("UnmarshalPacketBody(): Blocks was %d, but expected %d", ep.Blocks, Blocks)
	}

	if ep.BlocksFree != BlocksFree {
		t.Errorf("UnmarshalPacketBody(): BlocksFree was %d, but expected %d", ep.BlocksFree, BlocksFree)
	}

	if ep.BlocksAvail != BlocksAvail {
		t.Errorf("UnmarshalPacketBody(): BlocksAvail was %d, but expected %d", ep.BlocksAvail, BlocksAvail)
	}

	if ep.Files != Files {
		t.Errorf("UnmarshalPacketBody(): Files was %d, but expected %d", ep.Files, Files)
	}

	if ep.FilesFree != FilesFree {
		t.Errorf("UnmarshalPacketBody(): FilesFree was %d, but expected %d", ep.FilesFree, FilesFree)
	}

	if ep.FilesAvail != FilesAvail {
		t.Errorf("UnmarshalPacketBody(): FilesAvail was %d, but expected %d", ep.FilesAvail, FilesAvail)
	}

	if ep.FilesystemID != FilesystemID {
		t.Errorf("UnmarshalPacketBody(): FilesystemID was %d, but expected %d", ep.FilesystemID, FilesystemID)
	}

	if ep.MountFlags != MountFlags {
		t.Errorf("UnmarshalPacketBody(): MountFlags was %d, but expected %d", ep.MountFlags, MountFlags)
	}

	if ep.MaxNameLength != MaxNameLength {
		t.Errorf("UnmarshalPacketBody(): MaxNameLength was %d, but expected %d", ep.MaxNameLength, MaxNameLength)
	}
}