github.com/lazyledger/lazyledger-core@v0.35.0-dev.0.20210613111200-4c651f053571/libs/protoio/io_test.go (about)

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