From 60d25c3ed77cbd8341fc7ae984e165e7046e5c9b Mon Sep 17 00:00:00 2001 From: Antonio Ojea Date: Wed, 1 Mar 2023 19:56:41 +0000 Subject: [PATCH] improve remotecommand testing fuzzing the data stream Change-Id: I1303bc79a8c43fc4fb04bf76afcf90653ceb9e14 --- .../tools/remotecommand/remotecommand_test.go | 61 +++++++++++++++++++ 1 file changed, 61 insertions(+) diff --git a/staging/src/k8s.io/client-go/tools/remotecommand/remotecommand_test.go b/staging/src/k8s.io/client-go/tools/remotecommand/remotecommand_test.go index 9144a14526c..7e01d76e80f 100644 --- a/staging/src/k8s.io/client-go/tools/remotecommand/remotecommand_test.go +++ b/staging/src/k8s.io/client-go/tools/remotecommand/remotecommand_test.go @@ -17,10 +17,13 @@ limitations under the License. package remotecommand import ( + "bytes" "context" + "crypto/rand" "encoding/json" "errors" "io" + "io/ioutil" "net/http" "net/http/httptest" "net/url" @@ -365,3 +368,61 @@ func TestStreamExitsAfterConnectionIsClosed(t *testing.T) { return } } + +func TestStreamRandomData(t *testing.T) { + server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) { + var stdin, stdout bytes.Buffer + ctx, err := createHTTPStreams(w, req, &StreamOptions{ + Stdin: &stdin, + Stdout: &stdout, + }) + if err != nil { + t.Errorf("error on createHTTPStreams: %v", err) + return + } + defer ctx.conn.Close() + + io.Copy(ctx.stdoutStream, ctx.stdinStream) + })) + + defer server.Close() + + uri, _ := url.Parse(server.URL) + exec, err := NewSPDYExecutor(&rest.Config{Host: uri.Host}, "POST", uri) + if err != nil { + t.Fatal(err) + } + + randomData := make([]byte, 1024*1024) + if _, err := rand.Read(randomData); err != nil { + t.Errorf("unexpected error reading random data: %v", err) + } + var stdout bytes.Buffer + options := &StreamOptions{ + Stdin: bytes.NewReader(randomData), + Stdout: &stdout, + } + errorChan := make(chan error) + go func() { + errorChan <- exec.StreamWithContext(context.Background(), *options) + }() + + select { + case <-time.After(wait.ForeverTestTimeout): + t.Fatalf("expect stream to be closed after connection is closed.") + case err := <-errorChan: + if err != nil { + t.Errorf("unexpected error") + } + } + + data, err := ioutil.ReadAll(bytes.NewReader(stdout.Bytes())) + if err != nil { + t.Errorf("error reading the stream: %v", err) + return + } + if !bytes.Equal(randomData, data) { + t.Errorf("unexpected data received: %d sent: %d", len(data), len(randomData)) + } + +}