Skip to content

Commit

Permalink
PR: spelling, constants, fuzzing
Browse files Browse the repository at this point in the history
Moved HVSocket fuzzing tests to separate file with go 1.18 build
constraint.

Signed-off-by: Hamza El-Saawy <hamzaelsaawy@microsoft.com>
  • Loading branch information
helsaawy committed Aug 22, 2022
1 parent 9b6856b commit 3109bf7
Show file tree
Hide file tree
Showing 5 changed files with 93 additions and 86 deletions.
2 changes: 1 addition & 1 deletion README.md
Expand Up @@ -60,7 +60,7 @@ Alternatively, `golangci-lint` can be [installed locally][lint-install] and run

### Go Generate

The pipeline checks that auto-generated code, via `go generate`, be up to date.
The pipeline checks that auto-generated code, via `go generate`, are up to date.

This can be done for the entire repo:

Expand Down
2 changes: 1 addition & 1 deletion backup.go
Expand Up @@ -39,7 +39,7 @@ const (
//nolint:revive // var-naming: ALL_CAPS
const (
WRITE_DAC = windows.WRITE_DAC
WRITE_OWNER = windows.WRITE_DAC
WRITE_OWNER = windows.WRITE_OWNER
ACCESS_SYSTEM_SECURITY = windows.ACCESS_SYSTEM_SECURITY
)

Expand Down
9 changes: 3 additions & 6 deletions backup_test.go
Expand Up @@ -8,6 +8,8 @@ import (
"os"
"syscall"
"testing"

"golang.org/x/sys/windows"
)

var testFileName string
Expand Down Expand Up @@ -200,12 +202,7 @@ func makeSparseFile() error {
}
defer f.Close()

const (
fsctlSetSparse = 0x000900c4
fsctlSetZeroData = 0x000980c8
)

err = syscall.DeviceIoControl(syscall.Handle(f.Fd()), fsctlSetSparse, nil, 0, nil, 0, nil, nil)
err = syscall.DeviceIoControl(syscall.Handle(f.Fd()), windows.FSCTL_SET_SPARSE, nil, 0, nil, 0, nil, nil)
if err != nil {
return err
}
Expand Down
88 changes: 88 additions & 0 deletions hvsock_go118_test.go
@@ -0,0 +1,88 @@
//go:build windows && go1.18

package winio

import (
"errors"
"fmt"
"testing"
"time"
)

func FuzzHvSockRxTx(f *testing.F) {
for _, b := range [][]byte{
[]byte("hello?"),
[]byte("This is a really long string that should be a good example of the really long " +
"payloads that may be sent over hvsockets when really long inputs are being used, tautologically. " +
"That means that we will have to test with really long input sequences, which means that " +
"we need to include really long byte sequences or strings in our testing so that we know that " +
"the sockets can deal with really long inputs. Look at this key mashing: " +
"sdflhsdfgkjdhskljjsad;kljfasd;lfkjsadl ;fasdjfopiwej09q34iur092\"i4o[piwajfliasdkf-012ior]-" +
"01oi3;'lSD<Fplkasdjgoisaefjoiasdlj\"hgfoaisdkf';laksdjdf[poaiseefk-0923i4roi3qwjrf9" +
"08sEJKEFOLIsaejf[09saEJFLKSADjf;lkasdjf;kljaslddhgaskghk"),
{0x5c, 0xbd, 0xb5, 0xe7, 0x6b, 0xcb, 0xe7, 0x23, 0xff, 0x7a, 0x19, 0x77, 0x2c, 0xca, 0xab, 0x3b},
} {
f.Add(b)
}

f.Fuzz(func(t *testing.T, a []byte) {
if string(a) == "" {
t.Skip("skipping empty string")
}
t.Logf("testing %q (%d)", a, len(a))
u := newUtil(t)
cl, sv, _ := clientServer(u)

svCh := u.Go(func() error {
n, err := cl.Write(a)
if err != nil {
return fmt.Errorf("client write: %w", err)
}
if n != len(a) {
return errors.New("client did not send full message")
}

b := make([]byte, len(a)+5) // a little extra to make sure nothing else is sent
n, err = cl.Read(b)
if err != nil {
return fmt.Errorf("client read: %w", err)
}
if n != len(a) {
return errors.New("client did not read full message")
}
bn := b[:n]
if string(a) != string(bn) {
return fmt.Errorf("client payload mismatch %q != %q", a, bn)
}
t.Log("client received")
return nil
})

clCh := u.Go(func() error {
b := make([]byte, len(a)+5) // a little extra to make sure nothing else is sent
n, err := sv.Read(b)
if err != nil {
return fmt.Errorf("server read: %w", err)
}
if n != len(a) {
return errors.New("server did not read full message")
}
bn := b[:n]
if string(a) != string(bn) {
return fmt.Errorf("server payload mismatch %q != %q", a, bn)
}

n, err = sv.Write(bn)
if err != nil {
return fmt.Errorf("server write: %w", err)
}
if n != len(a) {
return errors.New("server did not send full message")
}
t.Log("server sent")
return nil
})
u.WaitErr(svCh, 250*time.Millisecond)
u.WaitErr(clCh, 250*time.Millisecond)
})
}
78 changes: 0 additions & 78 deletions hvsock_test.go
Expand Up @@ -571,84 +571,6 @@ func TestHvSockAcceptClose(t *testing.T) {
u.Is(err, ErrFileClosed)
}

func FuzzHvSockRxTx(f *testing.F) {
for _, b := range [][]byte{
[]byte("hello?"),
[]byte("This is a really long string that should be a good example of the really long " +
"payloads that may be sent over hvsockets when really long inputs are being used, tautologically. " +
"That means that we will have to test with really long input sequences, which means that " +
"we need to include really long byte sequences or strings in our testing so that we know that " +
"the sockets can deal with really long inputs. Look at this key mashing: " +
"sdflhsdfgkjdhskljjsad;kljfasd;lfkjsadl ;fasdjfopiwej09q34iur092\"i4o[piwajfliasdkf-012ior]-" +
"01oi3;'lSD<Fplkasdjgoisaefjoiasdlj\"hgfoaisdkf';laksdjdf[poaiseefk-0923i4roi3qwjrf9" +
"08sEJKEFOLIsaejf[09saEJFLKSADjf;lkasdjf;kljaslddhgaskghk"),
{0x5c, 0xbd, 0xb5, 0xe7, 0x6b, 0xcb, 0xe7, 0x23, 0xff, 0x7a, 0x19, 0x77, 0x2c, 0xca, 0xab, 0x3b},
} {
f.Add(b)
}

f.Fuzz(func(t *testing.T, a []byte) {
if string(a) == "" {
t.Skip("skipping empty string")
}
t.Logf("testing %q (%d)", a, len(a))
u := newUtil(t)
cl, sv, _ := clientServer(u)

svCh := u.Go(func() error {
n, err := cl.Write(a)
if err != nil {
return fmt.Errorf("client write: %w", err)
}
if n != len(a) {
return errors.New("client did not send full message")
}

b := make([]byte, len(a)+5) // a little extra to make sure nothing else is sent
n, err = cl.Read(b)
if err != nil {
return fmt.Errorf("client read: %w", err)
}
if n != len(a) {
return errors.New("client did not read full message")
}
bn := b[:n]
if string(a) != string(bn) {
return fmt.Errorf("client payload mismatch %q != %q", a, bn)
}
t.Log("client received")
return nil
})

clCh := u.Go(func() error {
b := make([]byte, len(a)+5) // a little extra to make sure nothing else is sent
n, err := sv.Read(b)
if err != nil {
return fmt.Errorf("server read: %w", err)
}
if n != len(a) {
return errors.New("server did not read full message")
}
bn := b[:n]
if string(a) != string(bn) {
return fmt.Errorf("server payload mismatch %q != %q", a, bn)
}

n, err = sv.Write(bn)
if err != nil {
return fmt.Errorf("server write: %w", err)
}
if n != len(a) {
return errors.New("server did not send full message")
}
t.Log("server sent")
return nil
})
u.WaitErr(svCh, 250*time.Millisecond)
u.WaitErr(clCh, 250*time.Millisecond)
})
}

//
// helpers
//
Expand Down

0 comments on commit 3109bf7

Please sign in to comment.