From dadfd14babccc30757ddb3f3eb8fbb7cd3bf4b5a Mon Sep 17 00:00:00 2001 From: Alex Brainman Date: Thu, 10 Nov 2016 13:00:35 +1100 Subject: [PATCH] os: add more tests in TestReadStdin TestReadStdin always fill up buffer provided by ReadFile caller full. But we do not know if real ReadFile does the same. Add tests where buffer is only filled with limited data. Change-Id: I0fc776325c2b1fe60511126c439f4b0560e9d653 Reviewed-on: https://go-review.googlesource.com/33030 Reviewed-by: Ian Lance Taylor Run-TryBot: Ian Lance Taylor TryBot-Result: Gobot Gobot --- src/os/os_windows_test.go | 55 +++++++++++++++++++++------------------ 1 file changed, 30 insertions(+), 25 deletions(-) diff --git a/src/os/os_windows_test.go b/src/os/os_windows_test.go index f03d91517d..91849e50c1 100644 --- a/src/os/os_windows_test.go +++ b/src/os/os_windows_test.go @@ -691,33 +691,38 @@ func TestReadStdin(t *testing.T) { }, } ) - for _, bufsize := range []int{1, 2, 3, 4, 5, 8, 10, 16, 20, 50, 100} { - nextTest: - for ti, test := range tests { - input := bytes.NewBuffer(test.input) - *os.ReadFileP = func(h syscall.Handle, buf []byte, done *uint32, o *syscall.Overlapped) error { - n, err := input.Read(buf) - *done = uint32(n) - return err - } - *os.GetCPP = func() uint32 { - return test.cp - } - var bigbuf []byte - for len(bigbuf) < len([]byte(test.output)) { - buf := make([]byte, bufsize) - n, err := testConsole.Read(buf) - if err != nil { - t.Errorf("test=%d bufsize=%d: read failed: %v", ti, bufsize, err) + for _, consoleReadBufSize := range []int{1, 2, 3, 4, 5, 8, 10, 16, 20, 50, 100} { + for _, readFileBufSize := range []int{1, 2, 3, 10, 16, 100, 1000} { + nextTest: + for ti, test := range tests { + input := bytes.NewBuffer(test.input) + *os.ReadFileP = func(h syscall.Handle, buf []byte, done *uint32, o *syscall.Overlapped) error { + if len(buf) > readFileBufSize { + buf = buf[:readFileBufSize] + } + n, err := input.Read(buf) + *done = uint32(n) + return err + } + *os.GetCPP = func() uint32 { + return test.cp + } + var bigbuf []byte + for len(bigbuf) < len([]byte(test.output)) { + buf := make([]byte, consoleReadBufSize) + n, err := testConsole.Read(buf) + if err != nil { + t.Errorf("test=%d bufsizes=%d,%d: read failed: %v", ti, consoleReadBufSize, readFileBufSize, err) + continue nextTest + } + bigbuf = append(bigbuf, buf[:n]...) + } + have := hex.Dump(bigbuf) + expected := hex.Dump([]byte(test.output)) + if have != expected { + t.Errorf("test=%d bufsizes=%d,%d: %q expected, but %q received", ti, consoleReadBufSize, readFileBufSize, expected, have) continue nextTest } - bigbuf = append(bigbuf, buf[:n]...) - } - have := hex.Dump(bigbuf) - expected := hex.Dump([]byte(test.output)) - if have != expected { - t.Errorf("test=%d bufsize=%d: %q expected, but %q received", ti, bufsize, expected, have) - continue nextTest } } }