aboutsummaryrefslogtreecommitdiff
path: root/internal/encoding/ssh/filexfer/init_packets_test.go
blob: e7605f983b645f6d4b27cf5a82c5219c7b1d7452 (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
package filexfer

import (
	"bytes"
	"testing"
)

func TestInitPacket(t *testing.T) {
	var version uint8 = 3

	p := &InitPacket{
		Version: uint32(version),
		Extensions: []*ExtensionPair{
			{
				Name: "foo",
				Data: "1",
			},
		},
	}

	buf, err := p.MarshalBinary()
	if err != nil {
		t.Fatal("unexpected error:", err)
	}

	want := []byte{
		0x00, 0x00, 0x00, 17,
		1,
		0x00, 0x00, 0x00, version,
		0x00, 0x00, 0x00, 3, 'f', 'o', 'o',
		0x00, 0x00, 0x00, 1, '1',
	}

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

	*p = InitPacket{}

	// UnmarshalBinary assumes the uint32(length) + uint8(type) have already been consumed.
	if err := p.UnmarshalBinary(buf[5:]); err != nil {
		t.Fatal("unexpected error:", err)
	}

	if p.Version != uint32(version) {
		t.Errorf("UnmarshalBinary(): Version was %d, but expected %d", p.Version, version)
	}

	if len(p.Extensions) != 1 {
		t.Fatalf("UnmarshalBinary(): len(p.Extensions) was %d, but expected %d", len(p.Extensions), 1)
	}

	if got, want := p.Extensions[0].Name, "foo"; got != want {
		t.Errorf("UnmarshalBinary(): p.Extensions[0].Name was %q, but expected %q", got, want)
	}

	if got, want := p.Extensions[0].Data, "1"; got != want {
		t.Errorf("UnmarshalBinary(): p.Extensions[0].Data was %q, but expected %q", got, want)
	}
}

func TestVersionPacket(t *testing.T) {
	var version uint8 = 3

	p := &VersionPacket{
		Version: uint32(version),
		Extensions: []*ExtensionPair{
			{
				Name: "foo",
				Data: "1",
			},
		},
	}

	buf, err := p.MarshalBinary()
	if err != nil {
		t.Fatal("unexpected error:", err)
	}

	want := []byte{
		0x00, 0x00, 0x00, 17,
		2,
		0x00, 0x00, 0x00, version,
		0x00, 0x00, 0x00, 3, 'f', 'o', 'o',
		0x00, 0x00, 0x00, 1, '1',
	}

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

	*p = VersionPacket{}

	// UnmarshalBinary assumes the uint32(length) + uint8(type) have already been consumed.
	if err := p.UnmarshalBinary(buf[5:]); err != nil {
		t.Fatal("unexpected error:", err)
	}

	if p.Version != uint32(version) {
		t.Errorf("UnmarshalBinary(): Version was %d, but expected %d", p.Version, version)
	}

	if len(p.Extensions) != 1 {
		t.Fatalf("UnmarshalBinary(): len(p.Extensions) was %d, but expected %d", len(p.Extensions), 1)
	}

	if got, want := p.Extensions[0].Name, "foo"; got != want {
		t.Errorf("UnmarshalBinary(): p.Extensions[0].Name was %q, but expected %q", got, want)
	}

	if got, want := p.Extensions[0].Data, "1"; got != want {
		t.Errorf("UnmarshalBinary(): p.Extensions[0].Data was %q, but expected %q", got, want)
	}
}