You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

157 lines
4.0 KiB

  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. package protoio_test
  29. import (
  30. "bytes"
  31. "encoding/binary"
  32. "fmt"
  33. "io"
  34. "math/rand"
  35. "testing"
  36. "time"
  37. "github.com/gogo/protobuf/proto"
  38. "github.com/gogo/protobuf/test"
  39. "github.com/tendermint/tendermint/libs/protoio"
  40. )
  41. func iotest(writer protoio.WriteCloser, reader protoio.ReadCloser) error {
  42. varint := make([]byte, binary.MaxVarintLen64)
  43. size := 1000
  44. msgs := make([]*test.NinOptNative, size)
  45. r := rand.New(rand.NewSource(time.Now().UnixNano()))
  46. for i := range msgs {
  47. msgs[i] = test.NewPopulatedNinOptNative(r, true)
  48. // issue 31
  49. if i == 5 {
  50. msgs[i] = &test.NinOptNative{}
  51. }
  52. // issue 31
  53. if i == 999 {
  54. msgs[i] = &test.NinOptNative{}
  55. }
  56. // FIXME Check size
  57. bz, err := proto.Marshal(msgs[i])
  58. if err != nil {
  59. return err
  60. }
  61. visize := binary.PutUvarint(varint, uint64(len(bz)))
  62. n, err := writer.WriteMsg(msgs[i])
  63. if err != nil {
  64. return err
  65. }
  66. if n != len(bz)+visize {
  67. return fmt.Errorf("WriteMsg() wrote %v bytes, expected %v", n, len(bz)+visize) // nolint
  68. }
  69. }
  70. if err := writer.Close(); err != nil {
  71. return err
  72. }
  73. i := 0
  74. for {
  75. msg := &test.NinOptNative{}
  76. if err := reader.ReadMsg(msg); err != nil {
  77. if err == io.EOF {
  78. break
  79. }
  80. return err
  81. }
  82. if err := msg.VerboseEqual(msgs[i]); err != nil {
  83. return err
  84. }
  85. i++
  86. }
  87. if i != size {
  88. panic("not enough messages read")
  89. }
  90. if err := reader.Close(); err != nil {
  91. return err
  92. }
  93. return nil
  94. }
  95. type buffer struct {
  96. *bytes.Buffer
  97. closed bool
  98. }
  99. func (b *buffer) Close() error {
  100. b.closed = true
  101. return nil
  102. }
  103. func newBuffer() *buffer {
  104. return &buffer{bytes.NewBuffer(nil), false}
  105. }
  106. func TestVarintNormal(t *testing.T) {
  107. buf := newBuffer()
  108. writer := protoio.NewDelimitedWriter(buf)
  109. reader := protoio.NewDelimitedReader(buf, 1024*1024)
  110. if err := iotest(writer, reader); err != nil {
  111. t.Error(err)
  112. }
  113. if !buf.closed {
  114. t.Fatalf("did not close buffer")
  115. }
  116. }
  117. func TestVarintNoClose(t *testing.T) {
  118. buf := bytes.NewBuffer(nil)
  119. writer := protoio.NewDelimitedWriter(buf)
  120. reader := protoio.NewDelimitedReader(buf, 1024*1024)
  121. if err := iotest(writer, reader); err != nil {
  122. t.Error(err)
  123. }
  124. }
  125. // issue 32
  126. func TestVarintMaxSize(t *testing.T) {
  127. buf := newBuffer()
  128. writer := protoio.NewDelimitedWriter(buf)
  129. reader := protoio.NewDelimitedReader(buf, 20)
  130. if err := iotest(writer, reader); err == nil {
  131. t.Error(err)
  132. } else {
  133. t.Logf("%s", err)
  134. }
  135. }
  136. func TestVarintError(t *testing.T) {
  137. buf := newBuffer()
  138. buf.Write([]byte{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x7f})
  139. reader := protoio.NewDelimitedReader(buf, 1024*1024)
  140. msg := &test.NinOptNative{}
  141. err := reader.ReadMsg(msg)
  142. if err == nil {
  143. t.Fatalf("Expected error")
  144. }
  145. }