github.com/klaytn/klaytn@v1.12.1/networks/p2p/rlpx/buffer_test.go (about)

     1  // Copyright 2021 The go-ethereum Authors
     2  // This file is part of the go-ethereum library.
     3  //
     4  // The go-ethereum library is free software: you can redistribute it and/or modify
     5  // it under the terms of the GNU Lesser General Public License as published by
     6  // the Free Software Foundation, either version 3 of the License, or
     7  // (at your option) any later version.
     8  //
     9  // The go-ethereum library is distributed in the hope that it will be useful,
    10  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    11  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    12  // GNU Lesser General Public License for more details.
    13  //
    14  // You should have received a copy of the GNU Lesser General Public License
    15  // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
    16  //
    17  // This file is derived from p2p/rlpx/buffer_test.go (2023/08/31).
    18  // Modified and improved for the klaytn development.
    19  package rlpx
    20  
    21  import (
    22  	"bytes"
    23  	"testing"
    24  
    25  	"github.com/klaytn/klaytn/common/hexutil"
    26  	"github.com/stretchr/testify/assert"
    27  )
    28  
    29  func TestReadBufferReset(t *testing.T) {
    30  	reader := bytes.NewReader(hexutil.MustDecode("0x010202030303040505"))
    31  	var b readBuffer
    32  
    33  	s1, _ := b.read(reader, 1)
    34  	s2, _ := b.read(reader, 2)
    35  	s3, _ := b.read(reader, 3)
    36  
    37  	assert.Equal(t, []byte{1}, s1)
    38  	assert.Equal(t, []byte{2, 2}, s2)
    39  	assert.Equal(t, []byte{3, 3, 3}, s3)
    40  
    41  	b.reset()
    42  
    43  	s4, _ := b.read(reader, 1)
    44  	s5, _ := b.read(reader, 2)
    45  
    46  	assert.Equal(t, []byte{4}, s4)
    47  	assert.Equal(t, []byte{5, 5}, s5)
    48  
    49  	s6, err := b.read(reader, 2)
    50  
    51  	assert.EqualError(t, err, "EOF")
    52  	assert.Nil(t, s6)
    53  }