github.com/Serizao/go-winio@v0.0.0-20230906082528-f02f7f4ad6e8/hvsock_go118_test.go (about)

     1  //go:build windows && go1.18
     2  
     3  package winio
     4  
     5  import (
     6  	"errors"
     7  	"fmt"
     8  	"testing"
     9  	"time"
    10  )
    11  
    12  func FuzzHvSockRxTx(f *testing.F) {
    13  	for _, b := range [][]byte{
    14  		[]byte("hello?"),
    15  		[]byte("This is a really long string that should be a good example of the really long " +
    16  			"payloads that may be sent over hvsockets when really long inputs are being used, tautologically. " +
    17  			"That means that we will have to test with really long input sequences, which means that " +
    18  			"we need to include really long byte sequences or strings in our testing so that we know that " +
    19  			"the sockets can deal with really long inputs. Look at this key mashing: " +
    20  			"sdflhsdfgkjdhskljjsad;kljfasd;lfkjsadl ;fasdjfopiwej09q34iur092\"i4o[piwajfliasdkf-012ior]-" +
    21  			"01oi3;'lSD<Fplkasdjgoisaefjoiasdlj\"hgfoaisdkf';laksdjdf[poaiseefk-0923i4roi3qwjrf9" +
    22  			"08sEJKEFOLIsaejf[09saEJFLKSADjf;lkasdjf;kljaslddhgaskghk"),
    23  		{0x5c, 0xbd, 0xb5, 0xe7, 0x6b, 0xcb, 0xe7, 0x23, 0xff, 0x7a, 0x19, 0x77, 0x2c, 0xca, 0xab, 0x3b},
    24  	} {
    25  		f.Add(b)
    26  	}
    27  
    28  	f.Fuzz(func(t *testing.T, a []byte) {
    29  		if string(a) == "" {
    30  			t.Skip("skipping empty string")
    31  		}
    32  		t.Logf("testing %q (%d)", a, len(a))
    33  		u := newUtil(t)
    34  		cl, sv, _ := clientServer(u)
    35  
    36  		svCh := u.Go(func() error {
    37  			n, err := cl.Write(a)
    38  			if err != nil {
    39  				return fmt.Errorf("client write: %w", err)
    40  			}
    41  			if n != len(a) {
    42  				return errors.New("client did not send full message")
    43  			}
    44  
    45  			b := make([]byte, len(a)+5) // a little extra to make sure nothing else is sent
    46  			n, err = cl.Read(b)
    47  			if err != nil {
    48  				return fmt.Errorf("client read: %w", err)
    49  			}
    50  			if n != len(a) {
    51  				return errors.New("client did not read full message")
    52  			}
    53  			bn := b[:n]
    54  			if string(a) != string(bn) {
    55  				return fmt.Errorf("client payload mismatch %q != %q", a, bn)
    56  			}
    57  			t.Log("client received")
    58  			return nil
    59  		})
    60  
    61  		clCh := u.Go(func() error {
    62  			b := make([]byte, len(a)+5) // a little extra to make sure nothing else is sent
    63  			n, err := sv.Read(b)
    64  			if err != nil {
    65  				return fmt.Errorf("server read: %w", err)
    66  			}
    67  			if n != len(a) {
    68  				return errors.New("server did not read full message")
    69  			}
    70  			bn := b[:n]
    71  			if string(a) != string(bn) {
    72  				return fmt.Errorf("server payload mismatch %q != %q", a, bn)
    73  			}
    74  
    75  			n, err = sv.Write(bn)
    76  			if err != nil {
    77  				return fmt.Errorf("server write: %w", err)
    78  			}
    79  			if n != len(a) {
    80  				return errors.New("server did not send full message")
    81  			}
    82  			t.Log("server sent")
    83  			return nil
    84  		})
    85  		u.WaitErr(svCh, 250*time.Millisecond)
    86  		u.WaitErr(clCh, 250*time.Millisecond)
    87  	})
    88  }