1
0
Fork 0
mirror of https://github.com/moby/moby.git synced 2022-11-09 12:21:53 -05:00

fix flaky test TestImportFileWithMessage

Signed-off-by: Shijiang Wei <mountkin@gmail.com>
This commit is contained in:
Shijiang Wei 2016-05-27 16:06:15 +08:00
parent 089166ebe2
commit 9d6989bbb6
2 changed files with 28 additions and 14 deletions

View file

@ -48,13 +48,18 @@ func (l *MalformedHostHeaderOverrideConn) Read(b []byte) (n int, err error) {
firstLineFeed = -1
buf []byte
)
for i, bb := range b[:c] {
if bb == '\n' && firstLineFeed == -1 {
for i := 0; i <= c-1-7; i++ {
if b[i] == '\n' && firstLineFeed == -1 {
firstLineFeed = i
}
if bb != '\n' {
if b[i] != '\n' {
continue
}
if b[i+1] == '\r' && b[i+2] == '\n' {
return c, nil
}
if b[i+1] != 'H' {
continue
}

View file

@ -10,8 +10,16 @@ import (
"testing"
)
type bufConn struct {
net.Conn
buf *bytes.Buffer
}
func (bc *bufConn) Read(b []byte) (int, error) {
return bc.buf.Read(b)
}
func TestHeaderOverrideHack(t *testing.T) {
client, srv := net.Pipe()
tests := [][2][]byte{
{
[]byte("GET /foo\nHost: /var/run/docker.sock\nUser-Agent: Docker\r\n\r\n"),
@ -34,13 +42,19 @@ func TestHeaderOverrideHack(t *testing.T) {
[]byte("GET /foo\nFoo: Bar\nHost: /var/run/docker.sock\nUser-Agent: Docker\r\n\r\n"),
},
}
l := MalformedHostHeaderOverrideConn{client, true}
read := make([]byte, 4096)
// Test for https://github.com/docker/docker/issues/23045
h0 := "GET /foo\nUser-Agent: Docker\r\n\r\n"
h0 = h0 + strings.Repeat("a", 4096-len(h0)-1) + "\n"
tests = append(tests, [2][]byte{[]byte(h0), []byte(h0)})
for _, pair := range tests {
go func(x []byte) {
srv.Write(x)
}(pair[0])
read := make([]byte, 4096)
client := &bufConn{
buf: bytes.NewBuffer(pair[0]),
}
l := MalformedHostHeaderOverrideConn{client, true}
n, err := l.Read(read)
if err != nil && err != io.EOF {
t.Fatalf("read: %d - %d, err: %v\n%s", n, len(pair[0]), err, string(read[:n]))
@ -48,12 +62,7 @@ func TestHeaderOverrideHack(t *testing.T) {
if !bytes.Equal(read[:n], pair[1][:n]) {
t.Fatalf("\n%s\n%s\n", read[:n], pair[1][:n])
}
l.first = true
// clean out the slice
read = read[:0]
}
srv.Close()
l.Close()
}
func BenchmarkWithHack(b *testing.B) {