-
-
Notifications
You must be signed in to change notification settings - Fork 77
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: prevent 1st Read() after record() to return 0 bytes
- Loading branch information
Showing
2 changed files
with
87 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
package layer4 | ||
|
||
import ( | ||
"bytes" | ||
"net" | ||
"testing" | ||
) | ||
|
||
func TestConnection_RecordAndRewind(t *testing.T) { | ||
in, out := net.Pipe() | ||
defer in.Close() | ||
defer out.Close() | ||
|
||
cx := WrapConnection(out, &bytes.Buffer{}) | ||
defer cx.Close() | ||
|
||
matcherData := []byte("foo") | ||
consumeData := []byte("bar") | ||
|
||
buf := make([]byte, len(matcherData)) | ||
|
||
go func() { | ||
in.Write(matcherData) | ||
in.Write(consumeData) | ||
}() | ||
|
||
// 1st matcher | ||
|
||
cx.record() | ||
|
||
n, err := cx.Read(buf) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
if n != len(matcherData) { | ||
t.Fatalf("expected to read %d bytes but got %d", len(matcherData), n) | ||
} | ||
if bytes.Compare(matcherData, buf) != 0 { | ||
t.Fatalf("expected %s but received %s", matcherData, buf) | ||
} | ||
|
||
cx.rewind() | ||
|
||
// 2nd matcher (reads same data) | ||
|
||
cx.record() | ||
|
||
n, err = cx.Read(buf) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
if n != len(matcherData) { | ||
t.Fatalf("expected to read %d bytes but got %d", len(matcherData), n) | ||
} | ||
if bytes.Compare(matcherData, buf) != 0 { | ||
t.Fatalf("expected %s but received %s", matcherData, buf) | ||
} | ||
|
||
cx.rewind() | ||
|
||
// 1st consumer (no record call) | ||
|
||
n, err = cx.Read(buf) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
if n != len(matcherData) { | ||
t.Fatalf("expected to read %d bytes but got %d", len(matcherData), n) | ||
} | ||
if bytes.Compare(matcherData, buf) != 0 { | ||
t.Fatalf("expected %s but received %s", matcherData, buf) | ||
} | ||
|
||
// 2nd consumer (reads other data) | ||
|
||
n, err = cx.Read(buf) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
if n != len(consumeData) { | ||
t.Fatalf("expected to read %d bytes but got %d", len(consumeData), n) | ||
} | ||
if bytes.Compare(consumeData, buf) != 0 { | ||
t.Fatalf("expected %s but received %s", consumeData, buf) | ||
} | ||
} |