smithy

ref: c01a995021a3fd87fae141b3f7e9d615f67ce998

pkg/go-git-http/rpc_reader_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
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
package githttp_test

import (
	"io"
	"io/ioutil"
	"net/http"
	"os"
	"path/filepath"
	"reflect"
	"testing"

	"github.com/AaronO/go-git-http"
)

func TestRpcReader(t *testing.T) {
	tests := []struct {
		rpc  string
		file string

		want []githttp.Event
	}{
		{
			rpc:  "receive-pack",
			file: "receive-pack.0",

			want: []githttp.Event{
				(githttp.Event)(githttp.Event{
					Type:    (githttp.EventType)(githttp.PUSH),
					Commit:  (string)("92eef6dcb9cc198bc3ac6010c108fa482773f116"),
					Dir:     (string)(""),
					Tag:     (string)(""),
					Last:    (string)("0000000000000000000000000000000000000000"),
					Branch:  (string)("master"),
					Error:   (error)(nil),
					Request: (*http.Request)(nil),
				}),
			},
		},

		// A tag using letters only.
		{
			rpc:  "receive-pack",
			file: "receive-pack.1",

			want: []githttp.Event{
				(githttp.Event)(githttp.Event{
					Type:    (githttp.EventType)(githttp.TAG),
					Commit:  (string)("3da295397738f395c2ca5fd5570f01a9fcea3be3"),
					Dir:     (string)(""),
					Tag:     (string)("sometextualtag"),
					Last:    (string)("0000000000000000000000000000000000000000"),
					Branch:  (string)(""),
					Error:   (error)(nil),
					Request: (*http.Request)(nil),
				}),
			},
		},

		// A tag containing the string "00".
		{
			rpc:  "receive-pack",
			file: "receive-pack.2",

			want: []githttp.Event{
				(githttp.Event)(githttp.Event{
					Type:    (githttp.EventType)(githttp.TAG),
					Commit:  (string)("3da295397738f395c2ca5fd5570f01a9fcea3be3"),
					Dir:     (string)(""),
					Tag:     (string)("1.000.1"),
					Last:    (string)("0000000000000000000000000000000000000000"),
					Branch:  (string)(""),
					Error:   (error)(nil),
					Request: (*http.Request)(nil),
				}),
			},
		},

		// Multiple tags containing string "00" in one git push operation.
		{
			rpc:  "receive-pack",
			file: "receive-pack.3",

			want: []githttp.Event{
				(githttp.Event)(githttp.Event{
					Type:    (githttp.EventType)(githttp.TAG),
					Commit:  (string)("3da295397738f395c2ca5fd5570f01a9fcea3be3"),
					Dir:     (string)(""),
					Tag:     (string)("1.000.2"),
					Last:    (string)("0000000000000000000000000000000000000000"),
					Branch:  (string)(""),
					Error:   (error)(nil),
					Request: (*http.Request)(nil),
				}),
				(githttp.Event)(githttp.Event{
					Type:    (githttp.EventType)(githttp.TAG),
					Commit:  (string)("3da295397738f395c2ca5fd5570f01a9fcea3be3"),
					Dir:     (string)(""),
					Tag:     (string)("1.000.3"),
					Last:    (string)("0000000000000000000000000000000000000000"),
					Branch:  (string)(""),
					Error:   (error)(nil),
					Request: (*http.Request)(nil),
				}),
				(githttp.Event)(githttp.Event{
					Type:    (githttp.EventType)(githttp.TAG),
					Commit:  (string)("3da295397738f395c2ca5fd5570f01a9fcea3be3"),
					Dir:     (string)(""),
					Tag:     (string)("1.000.4"),
					Last:    (string)("0000000000000000000000000000000000000000"),
					Branch:  (string)(""),
					Error:   (error)(nil),
					Request: (*http.Request)(nil),
				}),
			},
		},

		{
			rpc:  "upload-pack",
			file: "upload-pack.0",

			want: []githttp.Event{
				(githttp.Event)(githttp.Event{
					Type:    (githttp.EventType)(githttp.FETCH),
					Commit:  (string)("a647ec2ea40ee9ca35d32232dc28de22b1537e00"),
					Dir:     (string)(""),
					Tag:     (string)(""),
					Last:    (string)(""),
					Branch:  (string)(""),
					Error:   (error)(nil),
					Request: (*http.Request)(nil),
				}),
			},
		},

		{
			rpc:  "upload-pack",
			file: "upload-pack.1",

			want: []githttp.Event{
				(githttp.Event)(githttp.Event{
					Type:    (githttp.EventType)(githttp.FETCH),
					Commit:  (string)("92eef6dcb9cc198bc3ac6010c108fa482773f116"),
					Dir:     (string)(""),
					Tag:     (string)(""),
					Last:    (string)(""),
					Branch:  (string)(""),
					Error:   (error)(nil),
					Request: (*http.Request)(nil),
				}),
			},
		},
	}

	for _, tt := range tests {
		f, err := os.Open(filepath.Join("testdata", tt.file))
		if err != nil {
			t.Fatal(err)
		}

		r := fragmentedReader{f}

		rr := &githttp.RpcReader{
			Reader: r,
			Rpc:    tt.rpc,
		}

		_, err = io.Copy(ioutil.Discard, rr)
		if err != nil {
			t.Errorf("io.Copy: %v", err)
		}

		f.Close()

		if got := rr.Events; !reflect.DeepEqual(got, tt.want) {
			t.Errorf("test %q/%q:\n got: %#v\nwant: %#v\n", tt.rpc, tt.file, got, tt.want)
		}
	}
}

// fragmentedReader reads from R, with each Read call returning at most fragmentLen bytes even
// if len(p) is greater than fragmentLen.
// It purposefully adds a layer of inefficiency around R, and exists for testing purposes only.
type fragmentedReader struct {
	R io.Reader // Underlying reader.
}

func (r fragmentedReader) Read(p []byte) (n int, err error) {
	const fragmentLen = 1
	if len(p) <= fragmentLen {
		return r.R.Read(p)
	}
	return r.R.Read(p[:fragmentLen])
}