|
| 1 | +// |
| 2 | +// Adapted from gogo/protobuf to use multiformats/go-varint for |
| 3 | +// efficient, interoperable length-prefixing. |
| 4 | +// |
| 5 | +// Protocol Buffers for Go with Gadgets |
| 6 | +// |
| 7 | +// Copyright (c) 2013, The GoGo Authors. All rights reserved. |
| 8 | +// http://github.com/gogo/protobuf |
| 9 | +// |
| 10 | +// Redistribution and use in source and binary forms, with or without |
| 11 | +// modification, are permitted provided that the following conditions are |
| 12 | +// met: |
| 13 | +// |
| 14 | +// * Redistributions of source code must retain the above copyright |
| 15 | +// notice, this list of conditions and the following disclaimer. |
| 16 | +// * Redistributions in binary form must reproduce the above |
| 17 | +// copyright notice, this list of conditions and the following disclaimer |
| 18 | +// in the documentation and/or other materials provided with the |
| 19 | +// distribution. |
| 20 | +// |
| 21 | +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 22 | +// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 23 | +// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 24 | +// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 25 | +// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 26 | +// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 27 | +// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 28 | +// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 29 | +// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 30 | +// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 31 | +// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 32 | +// |
| 33 | +package protoio_test |
| 34 | + |
| 35 | +import ( |
| 36 | + "bytes" |
| 37 | + "io" |
| 38 | + "math/rand" |
| 39 | + "testing" |
| 40 | + "time" |
| 41 | + |
| 42 | + "github.com/gogo/protobuf/test" |
| 43 | + |
| 44 | + "github.com/libp2p/go-msgio/protoio" |
| 45 | + "github.com/multiformats/go-varint" |
| 46 | +) |
| 47 | + |
| 48 | +func TestVarintNormal(t *testing.T) { |
| 49 | + buf := newBuffer() |
| 50 | + writer := protoio.NewDelimitedWriter(buf) |
| 51 | + reader := protoio.NewDelimitedReader(buf, 1024*1024) |
| 52 | + if err := iotest(writer, reader); err != nil { |
| 53 | + t.Error(err) |
| 54 | + } |
| 55 | + if !buf.closed { |
| 56 | + t.Fatalf("did not close buffer") |
| 57 | + } |
| 58 | +} |
| 59 | + |
| 60 | +func TestVarintNoClose(t *testing.T) { |
| 61 | + buf := bytes.NewBuffer(nil) |
| 62 | + writer := protoio.NewDelimitedWriter(buf) |
| 63 | + reader := protoio.NewDelimitedReader(buf, 1024*1024) |
| 64 | + if err := iotest(writer, reader); err != nil { |
| 65 | + t.Error(err) |
| 66 | + } |
| 67 | +} |
| 68 | + |
| 69 | +// https://github.com/gogo/protobuf/issues/32 |
| 70 | +func TestVarintMaxSize(t *testing.T) { |
| 71 | + buf := newBuffer() |
| 72 | + writer := protoio.NewDelimitedWriter(buf) |
| 73 | + reader := protoio.NewDelimitedReader(buf, 20) |
| 74 | + if err := iotest(writer, reader); err != io.ErrShortBuffer { |
| 75 | + t.Error(err) |
| 76 | + } else { |
| 77 | + t.Logf("%s", err) |
| 78 | + } |
| 79 | +} |
| 80 | + |
| 81 | +func TestVarintError(t *testing.T) { |
| 82 | + buf := newBuffer() |
| 83 | + // beyond uvarint63 capacity. |
| 84 | + buf.Write([]byte{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff}) |
| 85 | + reader := protoio.NewDelimitedReader(buf, 1024*1024) |
| 86 | + msg := &test.NinOptNative{} |
| 87 | + err := reader.ReadMsg(msg) |
| 88 | + if err != varint.ErrOverflow { |
| 89 | + t.Fatalf("expected varint.ErrOverflow error") |
| 90 | + } |
| 91 | +} |
| 92 | + |
| 93 | +type buffer struct { |
| 94 | + *bytes.Buffer |
| 95 | + closed bool |
| 96 | +} |
| 97 | + |
| 98 | +func (b *buffer) Close() error { |
| 99 | + b.closed = true |
| 100 | + return nil |
| 101 | +} |
| 102 | + |
| 103 | +func newBuffer() *buffer { |
| 104 | + return &buffer{bytes.NewBuffer(nil), false} |
| 105 | +} |
| 106 | + |
| 107 | +func iotest(writer protoio.WriteCloser, reader protoio.ReadCloser) error { |
| 108 | + size := 1000 |
| 109 | + msgs := make([]*test.NinOptNative, size) |
| 110 | + r := rand.New(rand.NewSource(time.Now().UnixNano())) |
| 111 | + for i := range msgs { |
| 112 | + msgs[i] = test.NewPopulatedNinOptNative(r, true) |
| 113 | + // https://github.com/gogo/protobuf/issues/31 |
| 114 | + if i == 5 { |
| 115 | + msgs[i] = &test.NinOptNative{} |
| 116 | + } |
| 117 | + // https://github.com/gogo/protobuf/issues/31 |
| 118 | + if i == 999 { |
| 119 | + msgs[i] = &test.NinOptNative{} |
| 120 | + } |
| 121 | + err := writer.WriteMsg(msgs[i]) |
| 122 | + if err != nil { |
| 123 | + return err |
| 124 | + } |
| 125 | + } |
| 126 | + if err := writer.Close(); err != nil { |
| 127 | + return err |
| 128 | + } |
| 129 | + i := 0 |
| 130 | + for { |
| 131 | + msg := &test.NinOptNative{} |
| 132 | + if err := reader.ReadMsg(msg); err != nil { |
| 133 | + if err == io.EOF { |
| 134 | + break |
| 135 | + } |
| 136 | + return err |
| 137 | + } |
| 138 | + if err := msg.VerboseEqual(msgs[i]); err != nil { |
| 139 | + return err |
| 140 | + } |
| 141 | + i++ |
| 142 | + } |
| 143 | + if i != size { |
| 144 | + panic("not enough messages read") |
| 145 | + } |
| 146 | + if err := reader.Close(); err != nil { |
| 147 | + return err |
| 148 | + } |
| 149 | + return nil |
| 150 | +} |
0 commit comments