mirror of
https://github.com/openfaas/faas.git
synced 2025-06-19 04:26:35 +00:00
Vendoring with Glide and delete function handler
This commit is contained in:
1
gateway/vendor/github.com/matttproud/golang_protobuf_extensions/pbutil/.gitignore
generated
vendored
Normal file
1
gateway/vendor/github.com/matttproud/golang_protobuf_extensions/pbutil/.gitignore
generated
vendored
Normal file
@ -0,0 +1 @@
|
||||
cover.dat
|
7
gateway/vendor/github.com/matttproud/golang_protobuf_extensions/pbutil/Makefile
generated
vendored
Normal file
7
gateway/vendor/github.com/matttproud/golang_protobuf_extensions/pbutil/Makefile
generated
vendored
Normal file
@ -0,0 +1,7 @@
|
||||
all:
|
||||
|
||||
cover:
|
||||
go test -cover -v -coverprofile=cover.dat ./...
|
||||
go tool cover -func cover.dat
|
||||
|
||||
.PHONY: cover
|
178
gateway/vendor/github.com/matttproud/golang_protobuf_extensions/pbutil/all_test.go
generated
vendored
Normal file
178
gateway/vendor/github.com/matttproud/golang_protobuf_extensions/pbutil/all_test.go
generated
vendored
Normal file
@ -0,0 +1,178 @@
|
||||
// Copyright 2013 Matt T. Proud
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
package pbutil
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"testing"
|
||||
|
||||
"github.com/golang/protobuf/proto"
|
||||
|
||||
. "github.com/matttproud/golang_protobuf_extensions/testdata"
|
||||
)
|
||||
|
||||
func TestWriteDelimited(t *testing.T) {
|
||||
t.Parallel()
|
||||
for _, test := range []struct {
|
||||
msg proto.Message
|
||||
buf []byte
|
||||
n int
|
||||
err error
|
||||
}{
|
||||
{
|
||||
msg: &Empty{},
|
||||
n: 1,
|
||||
buf: []byte{0},
|
||||
},
|
||||
{
|
||||
msg: &GoEnum{Foo: FOO_FOO1.Enum()},
|
||||
n: 3,
|
||||
buf: []byte{2, 8, 1},
|
||||
},
|
||||
{
|
||||
msg: &Strings{
|
||||
StringField: proto.String(`This is my gigantic, unhappy string. It exceeds
|
||||
the encoding size of a single byte varint. We are using it to fuzz test the
|
||||
correctness of the header decoding mechanisms, which may prove problematic.
|
||||
I expect it may. Let's hope you enjoy testing as much as we do.`),
|
||||
},
|
||||
n: 271,
|
||||
buf: []byte{141, 2, 10, 138, 2, 84, 104, 105, 115, 32, 105, 115, 32, 109,
|
||||
121, 32, 103, 105, 103, 97, 110, 116, 105, 99, 44, 32, 117, 110, 104,
|
||||
97, 112, 112, 121, 32, 115, 116, 114, 105, 110, 103, 46, 32, 32, 73,
|
||||
116, 32, 101, 120, 99, 101, 101, 100, 115, 10, 116, 104, 101, 32, 101,
|
||||
110, 99, 111, 100, 105, 110, 103, 32, 115, 105, 122, 101, 32, 111, 102,
|
||||
32, 97, 32, 115, 105, 110, 103, 108, 101, 32, 98, 121, 116, 101, 32,
|
||||
118, 97, 114, 105, 110, 116, 46, 32, 32, 87, 101, 32, 97, 114, 101, 32,
|
||||
117, 115, 105, 110, 103, 32, 105, 116, 32, 116, 111, 32, 102, 117, 122,
|
||||
122, 32, 116, 101, 115, 116, 32, 116, 104, 101, 10, 99, 111, 114, 114,
|
||||
101, 99, 116, 110, 101, 115, 115, 32, 111, 102, 32, 116, 104, 101, 32,
|
||||
104, 101, 97, 100, 101, 114, 32, 100, 101, 99, 111, 100, 105, 110, 103,
|
||||
32, 109, 101, 99, 104, 97, 110, 105, 115, 109, 115, 44, 32, 119, 104,
|
||||
105, 99, 104, 32, 109, 97, 121, 32, 112, 114, 111, 118, 101, 32, 112,
|
||||
114, 111, 98, 108, 101, 109, 97, 116, 105, 99, 46, 10, 73, 32, 101, 120,
|
||||
112, 101, 99, 116, 32, 105, 116, 32, 109, 97, 121, 46, 32, 32, 76, 101,
|
||||
116, 39, 115, 32, 104, 111, 112, 101, 32, 121, 111, 117, 32, 101, 110,
|
||||
106, 111, 121, 32, 116, 101, 115, 116, 105, 110, 103, 32, 97, 115, 32,
|
||||
109, 117, 99, 104, 32, 97, 115, 32, 119, 101, 32, 100, 111, 46},
|
||||
},
|
||||
} {
|
||||
var buf bytes.Buffer
|
||||
if n, err := WriteDelimited(&buf, test.msg); n != test.n || err != test.err {
|
||||
t.Fatalf("WriteDelimited(buf, %#v) = %v, %v; want %v, %v", test.msg, n, err, test.n, test.err)
|
||||
}
|
||||
if out := buf.Bytes(); !bytes.Equal(out, test.buf) {
|
||||
t.Fatalf("WriteDelimited(buf, %#v); buf = %v; want %v", test.msg, out, test.buf)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestReadDelimited(t *testing.T) {
|
||||
t.Parallel()
|
||||
for _, test := range []struct {
|
||||
buf []byte
|
||||
msg proto.Message
|
||||
n int
|
||||
err error
|
||||
}{
|
||||
{
|
||||
buf: []byte{0},
|
||||
msg: &Empty{},
|
||||
n: 1,
|
||||
},
|
||||
{
|
||||
n: 3,
|
||||
buf: []byte{2, 8, 1},
|
||||
msg: &GoEnum{Foo: FOO_FOO1.Enum()},
|
||||
},
|
||||
{
|
||||
buf: []byte{141, 2, 10, 138, 2, 84, 104, 105, 115, 32, 105, 115, 32, 109,
|
||||
121, 32, 103, 105, 103, 97, 110, 116, 105, 99, 44, 32, 117, 110, 104,
|
||||
97, 112, 112, 121, 32, 115, 116, 114, 105, 110, 103, 46, 32, 32, 73,
|
||||
116, 32, 101, 120, 99, 101, 101, 100, 115, 10, 116, 104, 101, 32, 101,
|
||||
110, 99, 111, 100, 105, 110, 103, 32, 115, 105, 122, 101, 32, 111, 102,
|
||||
32, 97, 32, 115, 105, 110, 103, 108, 101, 32, 98, 121, 116, 101, 32,
|
||||
118, 97, 114, 105, 110, 116, 46, 32, 32, 87, 101, 32, 97, 114, 101, 32,
|
||||
117, 115, 105, 110, 103, 32, 105, 116, 32, 116, 111, 32, 102, 117, 122,
|
||||
122, 32, 116, 101, 115, 116, 32, 116, 104, 101, 10, 99, 111, 114, 114,
|
||||
101, 99, 116, 110, 101, 115, 115, 32, 111, 102, 32, 116, 104, 101, 32,
|
||||
104, 101, 97, 100, 101, 114, 32, 100, 101, 99, 111, 100, 105, 110, 103,
|
||||
32, 109, 101, 99, 104, 97, 110, 105, 115, 109, 115, 44, 32, 119, 104,
|
||||
105, 99, 104, 32, 109, 97, 121, 32, 112, 114, 111, 118, 101, 32, 112,
|
||||
114, 111, 98, 108, 101, 109, 97, 116, 105, 99, 46, 10, 73, 32, 101, 120,
|
||||
112, 101, 99, 116, 32, 105, 116, 32, 109, 97, 121, 46, 32, 32, 76, 101,
|
||||
116, 39, 115, 32, 104, 111, 112, 101, 32, 121, 111, 117, 32, 101, 110,
|
||||
106, 111, 121, 32, 116, 101, 115, 116, 105, 110, 103, 32, 97, 115, 32,
|
||||
109, 117, 99, 104, 32, 97, 115, 32, 119, 101, 32, 100, 111, 46},
|
||||
msg: &Strings{
|
||||
StringField: proto.String(`This is my gigantic, unhappy string. It exceeds
|
||||
the encoding size of a single byte varint. We are using it to fuzz test the
|
||||
correctness of the header decoding mechanisms, which may prove problematic.
|
||||
I expect it may. Let's hope you enjoy testing as much as we do.`),
|
||||
},
|
||||
n: 271,
|
||||
},
|
||||
} {
|
||||
msg := proto.Clone(test.msg)
|
||||
msg.Reset()
|
||||
if n, err := ReadDelimited(bytes.NewBuffer(test.buf), msg); n != test.n || err != test.err {
|
||||
t.Fatalf("ReadDelimited(%v, msg) = %v, %v; want %v, %v", test.buf, n, err, test.n, test.err)
|
||||
}
|
||||
if !proto.Equal(msg, test.msg) {
|
||||
t.Fatalf("ReadDelimited(%v, msg); msg = %v; want %v", test.buf, msg, test.msg)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestEndToEndValid(t *testing.T) {
|
||||
t.Parallel()
|
||||
for _, test := range [][]proto.Message{
|
||||
{&Empty{}},
|
||||
{&GoEnum{Foo: FOO_FOO1.Enum()}, &Empty{}, &GoEnum{Foo: FOO_FOO1.Enum()}},
|
||||
{&GoEnum{Foo: FOO_FOO1.Enum()}},
|
||||
{&Strings{
|
||||
StringField: proto.String(`This is my gigantic, unhappy string. It exceeds
|
||||
the encoding size of a single byte varint. We are using it to fuzz test the
|
||||
correctness of the header decoding mechanisms, which may prove problematic.
|
||||
I expect it may. Let's hope you enjoy testing as much as we do.`),
|
||||
}},
|
||||
} {
|
||||
var buf bytes.Buffer
|
||||
var written int
|
||||
for i, msg := range test {
|
||||
n, err := WriteDelimited(&buf, msg)
|
||||
if err != nil {
|
||||
// Assumption: TestReadDelimited and TestWriteDelimited are sufficient
|
||||
// and inputs for this test are explicitly exercised there.
|
||||
t.Fatalf("WriteDelimited(buf, %v[%d]) = ?, %v; wanted ?, nil", test, i, err)
|
||||
}
|
||||
written += n
|
||||
}
|
||||
var read int
|
||||
for i, msg := range test {
|
||||
out := proto.Clone(msg)
|
||||
out.Reset()
|
||||
n, _ := ReadDelimited(&buf, out)
|
||||
// Decide to do EOF checking?
|
||||
read += n
|
||||
if !proto.Equal(out, msg) {
|
||||
t.Fatalf("out = %v; want %v[%d] = %#v", out, test, i, msg)
|
||||
}
|
||||
}
|
||||
if read != written {
|
||||
t.Fatalf("%v read = %d; want %d", test, read, written)
|
||||
}
|
||||
}
|
||||
}
|
75
gateway/vendor/github.com/matttproud/golang_protobuf_extensions/pbutil/decode.go
generated
vendored
Normal file
75
gateway/vendor/github.com/matttproud/golang_protobuf_extensions/pbutil/decode.go
generated
vendored
Normal file
@ -0,0 +1,75 @@
|
||||
// Copyright 2013 Matt T. Proud
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
package pbutil
|
||||
|
||||
import (
|
||||
"encoding/binary"
|
||||
"errors"
|
||||
"io"
|
||||
|
||||
"github.com/golang/protobuf/proto"
|
||||
)
|
||||
|
||||
var errInvalidVarint = errors.New("invalid varint32 encountered")
|
||||
|
||||
// ReadDelimited decodes a message from the provided length-delimited stream,
|
||||
// where the length is encoded as 32-bit varint prefix to the message body.
|
||||
// It returns the total number of bytes read and any applicable error. This is
|
||||
// roughly equivalent to the companion Java API's
|
||||
// MessageLite#parseDelimitedFrom. As per the reader contract, this function
|
||||
// calls r.Read repeatedly as required until exactly one message including its
|
||||
// prefix is read and decoded (or an error has occurred). The function never
|
||||
// reads more bytes from the stream than required. The function never returns
|
||||
// an error if a message has been read and decoded correctly, even if the end
|
||||
// of the stream has been reached in doing so. In that case, any subsequent
|
||||
// calls return (0, io.EOF).
|
||||
func ReadDelimited(r io.Reader, m proto.Message) (n int, err error) {
|
||||
// Per AbstractParser#parsePartialDelimitedFrom with
|
||||
// CodedInputStream#readRawVarint32.
|
||||
var headerBuf [binary.MaxVarintLen32]byte
|
||||
var bytesRead, varIntBytes int
|
||||
var messageLength uint64
|
||||
for varIntBytes == 0 { // i.e. no varint has been decoded yet.
|
||||
if bytesRead >= len(headerBuf) {
|
||||
return bytesRead, errInvalidVarint
|
||||
}
|
||||
// We have to read byte by byte here to avoid reading more bytes
|
||||
// than required. Each read byte is appended to what we have
|
||||
// read before.
|
||||
newBytesRead, err := r.Read(headerBuf[bytesRead : bytesRead+1])
|
||||
if newBytesRead == 0 {
|
||||
if err != nil {
|
||||
return bytesRead, err
|
||||
}
|
||||
// A Reader should not return (0, nil), but if it does,
|
||||
// it should be treated as no-op (according to the
|
||||
// Reader contract). So let's go on...
|
||||
continue
|
||||
}
|
||||
bytesRead += newBytesRead
|
||||
// Now present everything read so far to the varint decoder and
|
||||
// see if a varint can be decoded already.
|
||||
messageLength, varIntBytes = proto.DecodeVarint(headerBuf[:bytesRead])
|
||||
}
|
||||
|
||||
messageBuf := make([]byte, messageLength)
|
||||
newBytesRead, err := io.ReadFull(r, messageBuf)
|
||||
bytesRead += newBytesRead
|
||||
if err != nil {
|
||||
return bytesRead, err
|
||||
}
|
||||
|
||||
return bytesRead, proto.Unmarshal(messageBuf, m)
|
||||
}
|
99
gateway/vendor/github.com/matttproud/golang_protobuf_extensions/pbutil/decode_test.go
generated
vendored
Normal file
99
gateway/vendor/github.com/matttproud/golang_protobuf_extensions/pbutil/decode_test.go
generated
vendored
Normal file
@ -0,0 +1,99 @@
|
||||
// Copyright 2016 Matt T. Proud
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
package pbutil
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"io"
|
||||
"testing"
|
||||
"testing/iotest"
|
||||
)
|
||||
|
||||
func TestReadDelimitedIllegalVarint(t *testing.T) {
|
||||
t.Parallel()
|
||||
var tests = []struct {
|
||||
in []byte
|
||||
n int
|
||||
err error
|
||||
}{
|
||||
{
|
||||
in: []byte{255, 255, 255, 255, 255},
|
||||
n: 5,
|
||||
err: errInvalidVarint,
|
||||
},
|
||||
{
|
||||
in: []byte{255, 255, 255, 255, 255, 255},
|
||||
n: 5,
|
||||
err: errInvalidVarint,
|
||||
},
|
||||
}
|
||||
for _, test := range tests {
|
||||
n, err := ReadDelimited(bytes.NewReader(test.in), nil)
|
||||
if got, want := n, test.n; got != want {
|
||||
t.Errorf("ReadDelimited(%#v, nil) = %#v, ?; want = %v#, ?", test.in, got, want)
|
||||
}
|
||||
if got, want := err, test.err; got != want {
|
||||
t.Errorf("ReadDelimited(%#v, nil) = ?, %#v; want = ?, %#v", test.in, got, want)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestReadDelimitedPrematureHeader(t *testing.T) {
|
||||
t.Parallel()
|
||||
var data = []byte{128, 5} // 256 + 256 + 128
|
||||
n, err := ReadDelimited(bytes.NewReader(data[0:1]), nil)
|
||||
if got, want := n, 1; got != want {
|
||||
t.Errorf("ReadDelimited(%#v, nil) = %#v, ?; want = %v#, ?", data[0:1], got, want)
|
||||
}
|
||||
if got, want := err, io.EOF; got != want {
|
||||
t.Errorf("ReadDelimited(%#v, nil) = ?, %#v; want = ?, %#v", data[0:1], got, want)
|
||||
}
|
||||
}
|
||||
|
||||
func TestReadDelimitedPrematureBody(t *testing.T) {
|
||||
t.Parallel()
|
||||
var data = []byte{128, 5, 0, 0, 0} // 256 + 256 + 128
|
||||
n, err := ReadDelimited(bytes.NewReader(data[:]), nil)
|
||||
if got, want := n, 5; got != want {
|
||||
t.Errorf("ReadDelimited(%#v, nil) = %#v, ?; want = %v#, ?", data, got, want)
|
||||
}
|
||||
if got, want := err, io.ErrUnexpectedEOF; got != want {
|
||||
t.Errorf("ReadDelimited(%#v, nil) = ?, %#v; want = ?, %#v", data, got, want)
|
||||
}
|
||||
}
|
||||
|
||||
func TestReadDelimitedPrematureHeaderIncremental(t *testing.T) {
|
||||
t.Parallel()
|
||||
var data = []byte{128, 5} // 256 + 256 + 128
|
||||
n, err := ReadDelimited(iotest.OneByteReader(bytes.NewReader(data[0:1])), nil)
|
||||
if got, want := n, 1; got != want {
|
||||
t.Errorf("ReadDelimited(%#v, nil) = %#v, ?; want = %v#, ?", data[0:1], got, want)
|
||||
}
|
||||
if got, want := err, io.EOF; got != want {
|
||||
t.Errorf("ReadDelimited(%#v, nil) = ?, %#v; want = ?, %#v", data[0:1], got, want)
|
||||
}
|
||||
}
|
||||
|
||||
func TestReadDelimitedPrematureBodyIncremental(t *testing.T) {
|
||||
t.Parallel()
|
||||
var data = []byte{128, 5, 0, 0, 0} // 256 + 256 + 128
|
||||
n, err := ReadDelimited(iotest.OneByteReader(bytes.NewReader(data[:])), nil)
|
||||
if got, want := n, 5; got != want {
|
||||
t.Errorf("ReadDelimited(%#v, nil) = %#v, ?; want = %v#, ?", data, got, want)
|
||||
}
|
||||
if got, want := err, io.ErrUnexpectedEOF; got != want {
|
||||
t.Errorf("ReadDelimited(%#v, nil) = ?, %#v; want = ?, %#v", data, got, want)
|
||||
}
|
||||
}
|
16
gateway/vendor/github.com/matttproud/golang_protobuf_extensions/pbutil/doc.go
generated
vendored
Normal file
16
gateway/vendor/github.com/matttproud/golang_protobuf_extensions/pbutil/doc.go
generated
vendored
Normal file
@ -0,0 +1,16 @@
|
||||
// Copyright 2013 Matt T. Proud
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
// Package pbutil provides record length-delimited Protocol Buffer streaming.
|
||||
package pbutil
|
46
gateway/vendor/github.com/matttproud/golang_protobuf_extensions/pbutil/encode.go
generated
vendored
Normal file
46
gateway/vendor/github.com/matttproud/golang_protobuf_extensions/pbutil/encode.go
generated
vendored
Normal file
@ -0,0 +1,46 @@
|
||||
// Copyright 2013 Matt T. Proud
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
package pbutil
|
||||
|
||||
import (
|
||||
"encoding/binary"
|
||||
"io"
|
||||
|
||||
"github.com/golang/protobuf/proto"
|
||||
)
|
||||
|
||||
// WriteDelimited encodes and dumps a message to the provided writer prefixed
|
||||
// with a 32-bit varint indicating the length of the encoded message, producing
|
||||
// a length-delimited record stream, which can be used to chain together
|
||||
// encoded messages of the same type together in a file. It returns the total
|
||||
// number of bytes written and any applicable error. This is roughly
|
||||
// equivalent to the companion Java API's MessageLite#writeDelimitedTo.
|
||||
func WriteDelimited(w io.Writer, m proto.Message) (n int, err error) {
|
||||
buffer, err := proto.Marshal(m)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
|
||||
var buf [binary.MaxVarintLen32]byte
|
||||
encodedLength := binary.PutUvarint(buf[:], uint64(len(buffer)))
|
||||
|
||||
sync, err := w.Write(buf[:encodedLength])
|
||||
if err != nil {
|
||||
return sync, err
|
||||
}
|
||||
|
||||
n, err = w.Write(buffer)
|
||||
return n + sync, err
|
||||
}
|
67
gateway/vendor/github.com/matttproud/golang_protobuf_extensions/pbutil/encode_test.go
generated
vendored
Normal file
67
gateway/vendor/github.com/matttproud/golang_protobuf_extensions/pbutil/encode_test.go
generated
vendored
Normal file
@ -0,0 +1,67 @@
|
||||
// Copyright 2016 Matt T. Proud
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
package pbutil
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"errors"
|
||||
"testing"
|
||||
|
||||
"github.com/golang/protobuf/proto"
|
||||
)
|
||||
|
||||
var errMarshal = errors.New("pbutil: can't marshal")
|
||||
|
||||
type cantMarshal struct{ proto.Message }
|
||||
|
||||
func (cantMarshal) Marshal() ([]byte, error) { return nil, errMarshal }
|
||||
|
||||
var _ proto.Message = cantMarshal{}
|
||||
|
||||
func TestWriteDelimitedMarshalErr(t *testing.T) {
|
||||
t.Parallel()
|
||||
var data cantMarshal
|
||||
var buf bytes.Buffer
|
||||
n, err := WriteDelimited(&buf, data)
|
||||
if got, want := n, 0; got != want {
|
||||
t.Errorf("WriteDelimited(buf, %#v) = %#v, ?; want = %v#, ?", data, got, want)
|
||||
}
|
||||
if got, want := err, errMarshal; got != want {
|
||||
t.Errorf("WriteDelimited(buf, %#v) = ?, %#v; want = ?, %#v", data, got, want)
|
||||
}
|
||||
}
|
||||
|
||||
type canMarshal struct{ proto.Message }
|
||||
|
||||
func (canMarshal) Marshal() ([]byte, error) { return []byte{0, 1, 2, 3, 4, 5}, nil }
|
||||
|
||||
var errWrite = errors.New("pbutil: can't write")
|
||||
|
||||
type cantWrite struct{}
|
||||
|
||||
func (cantWrite) Write([]byte) (int, error) { return 0, errWrite }
|
||||
|
||||
func TestWriteDelimitedWriteErr(t *testing.T) {
|
||||
t.Parallel()
|
||||
var data canMarshal
|
||||
var buf cantWrite
|
||||
n, err := WriteDelimited(buf, data)
|
||||
if got, want := n, 0; got != want {
|
||||
t.Errorf("WriteDelimited(buf, %#v) = %#v, ?; want = %v#, ?", data, got, want)
|
||||
}
|
||||
if got, want := err, errWrite; got != want {
|
||||
t.Errorf("WriteDelimited(buf, %#v) = ?, %#v; want = ?, %#v", data, got, want)
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user