github.com/DFWallet/tendermint-cosmos@v0.0.2/test/fuzz/p2p/secret_connection/read_write.go (about)

     1  package secretconnection
     2  
     3  import (
     4  	"bytes"
     5  	"fmt"
     6  	"io"
     7  	"log"
     8  
     9  	"github.com/DFWallet/tendermint-cosmos/crypto/ed25519"
    10  	"github.com/DFWallet/tendermint-cosmos/libs/async"
    11  	sc "github.com/DFWallet/tendermint-cosmos/p2p/conn"
    12  )
    13  
    14  func Fuzz(data []byte) int {
    15  	if len(data) == 0 {
    16  		return -1
    17  	}
    18  
    19  	fooConn, barConn := makeSecretConnPair()
    20  	n, err := fooConn.Write(data)
    21  	if err != nil {
    22  		panic(err)
    23  	}
    24  	dataRead := make([]byte, n)
    25  	m, err := barConn.Read(dataRead)
    26  	if err != nil {
    27  		panic(err)
    28  	}
    29  	if !bytes.Equal(data[:n], dataRead[:m]) {
    30  		panic(fmt.Sprintf("bytes written %X != read %X", data[:n], dataRead[:m]))
    31  	}
    32  	return 1
    33  }
    34  
    35  type kvstoreConn struct {
    36  	*io.PipeReader
    37  	*io.PipeWriter
    38  }
    39  
    40  func (drw kvstoreConn) Close() (err error) {
    41  	err2 := drw.PipeWriter.CloseWithError(io.EOF)
    42  	err1 := drw.PipeReader.Close()
    43  	if err2 != nil {
    44  		return err
    45  	}
    46  	return err1
    47  }
    48  
    49  // Each returned ReadWriteCloser is akin to a net.Connection
    50  func makeKVStoreConnPair() (fooConn, barConn kvstoreConn) {
    51  	barReader, fooWriter := io.Pipe()
    52  	fooReader, barWriter := io.Pipe()
    53  	return kvstoreConn{fooReader, fooWriter}, kvstoreConn{barReader, barWriter}
    54  }
    55  
    56  func makeSecretConnPair() (fooSecConn, barSecConn *sc.SecretConnection) {
    57  	var (
    58  		fooConn, barConn = makeKVStoreConnPair()
    59  		fooPrvKey        = ed25519.GenPrivKey()
    60  		fooPubKey        = fooPrvKey.PubKey()
    61  		barPrvKey        = ed25519.GenPrivKey()
    62  		barPubKey        = barPrvKey.PubKey()
    63  	)
    64  
    65  	// Make connections from both sides in parallel.
    66  	var trs, ok = async.Parallel(
    67  		func(_ int) (val interface{}, abort bool, err error) {
    68  			fooSecConn, err = sc.MakeSecretConnection(fooConn, fooPrvKey)
    69  			if err != nil {
    70  				log.Printf("failed to establish SecretConnection for foo: %v", err)
    71  				return nil, true, err
    72  			}
    73  			remotePubBytes := fooSecConn.RemotePubKey()
    74  			if !remotePubBytes.Equals(barPubKey) {
    75  				err = fmt.Errorf("unexpected fooSecConn.RemotePubKey.  Expected %v, got %v",
    76  					barPubKey, fooSecConn.RemotePubKey())
    77  				log.Print(err)
    78  				return nil, true, err
    79  			}
    80  			return nil, false, nil
    81  		},
    82  		func(_ int) (val interface{}, abort bool, err error) {
    83  			barSecConn, err = sc.MakeSecretConnection(barConn, barPrvKey)
    84  			if barSecConn == nil {
    85  				log.Printf("failed to establish SecretConnection for bar: %v", err)
    86  				return nil, true, err
    87  			}
    88  			remotePubBytes := barSecConn.RemotePubKey()
    89  			if !remotePubBytes.Equals(fooPubKey) {
    90  				err = fmt.Errorf("unexpected barSecConn.RemotePubKey.  Expected %v, got %v",
    91  					fooPubKey, barSecConn.RemotePubKey())
    92  				log.Print(err)
    93  				return nil, true, err
    94  			}
    95  			return nil, false, nil
    96  		},
    97  	)
    98  
    99  	if trs.FirstError() != nil {
   100  		log.Fatalf("unexpected error: %v", trs.FirstError())
   101  	}
   102  	if !ok {
   103  		log.Fatal("Unexpected task abortion")
   104  	}
   105  
   106  	return fooSecConn, barSecConn
   107  }