Skip to content

Commit

Permalink
Merge pull request #23046 from mountkin/fix-23045
Browse files Browse the repository at this point in the history
fix flaky test TestImportFileWithMessage
  • Loading branch information
LK4D4 committed May 31, 2016
2 parents d1e9e86 + 9d6989b commit 0b5e84c
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 14 deletions.
11 changes: 8 additions & 3 deletions cmd/dockerd/hack/malformed_host_override.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
31 changes: 20 additions & 11 deletions cmd/dockerd/hack/malformed_host_override_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
Expand All @@ -34,26 +42,27 @@ 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]))
}
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) {
Expand Down

0 comments on commit 0b5e84c

Please sign in to comment.