aboutsummaryrefslogtreecommitdiffstats
path: root/plumbing/protocol/packp/gitproto_test.go
blob: 9cf1049fdbeafac9e41c6e357e351efafd7c4faf (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
package packp

import (
	"bytes"
	"testing"
)

func TestEncodeEmptyGitProtoRequest(t *testing.T) {
	var buf bytes.Buffer
	var p GitProtoRequest
	err := p.Encode(&buf)
	if err == nil {
		t.Fatal("expected error")
	}
}

func TestEncodeGitProtoRequest(t *testing.T) {
	var buf bytes.Buffer
	p := GitProtoRequest{
		RequestCommand: "command",
		Pathname:       "pathname",
		Host:           "host",
		ExtraParams:    []string{"param1", "param2"},
	}
	err := p.Encode(&buf)
	if err != nil {
		t.Fatal(err)
	}
	expected := "002ecommand pathname\x00host=host\x00\x00param1\x00param2\x00"
	if buf.String() != expected {
		t.Fatalf("expected %q, got %q", expected, buf.String())
	}
}

func TestEncodeInvalidGitProtoRequest(t *testing.T) {
	var buf bytes.Buffer
	p := GitProtoRequest{
		RequestCommand: "command",
	}
	err := p.Encode(&buf)
	if err == nil {
		t.Fatal("expected error")
	}
}

func TestDecodeEmptyGitProtoRequest(t *testing.T) {
	var buf bytes.Buffer
	var p GitProtoRequest
	err := p.Decode(&buf)
	if err == nil {
		t.Fatal("expected error")
	}
}

func TestDecodeGitProtoRequest(t *testing.T) {
	var buf bytes.Buffer
	buf.WriteString("002ecommand pathname\x00host=host\x00\x00param1\x00param2\x00")
	var p GitProtoRequest
	err := p.Decode(&buf)
	if err != nil {
		t.Fatal(err)
	}
	expected := GitProtoRequest{
		RequestCommand: "command",
		Pathname:       "pathname",
		Host:           "host",
		ExtraParams:    []string{"param1", "param2"},
	}
	if p.RequestCommand != expected.RequestCommand {
		t.Fatalf("expected %q, got %q", expected.RequestCommand, p.RequestCommand)
	}
	if p.Pathname != expected.Pathname {
		t.Fatalf("expected %q, got %q", expected.Pathname, p.Pathname)
	}
	if p.Host != expected.Host {
		t.Fatalf("expected %q, got %q", expected.Host, p.Host)
	}
	if len(p.ExtraParams) != len(expected.ExtraParams) {
		t.Fatalf("expected %d, got %d", len(expected.ExtraParams), len(p.ExtraParams))
	}
}

func TestDecodeInvalidGitProtoRequest(t *testing.T) {
	var buf bytes.Buffer
	buf.WriteString("0026command \x00host=host\x00\x00param1\x00param2")
	var p GitProtoRequest
	err := p.Decode(&buf)
	if err == nil {
		t.Fatal("expected error")
	}
}

func TestValidateEmptyGitProtoRequest(t *testing.T) {
	var p GitProtoRequest
	err := p.validate()
	if err == nil {
		t.Fatal("expected error")
	}
}