mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-08-09 20:17:41 +00:00
improve remotecommand testing fuzzing the data stream
Change-Id: I1303bc79a8c43fc4fb04bf76afcf90653ceb9e14
This commit is contained in:
parent
d6f81c01f4
commit
60d25c3ed7
@ -17,10 +17,13 @@ limitations under the License.
|
|||||||
package remotecommand
|
package remotecommand
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"bytes"
|
||||||
"context"
|
"context"
|
||||||
|
"crypto/rand"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"errors"
|
"errors"
|
||||||
"io"
|
"io"
|
||||||
|
"io/ioutil"
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/http/httptest"
|
"net/http/httptest"
|
||||||
"net/url"
|
"net/url"
|
||||||
@ -365,3 +368,61 @@ func TestStreamExitsAfterConnectionIsClosed(t *testing.T) {
|
|||||||
return
|
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))
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user