Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: don't write from the read function #50

Merged
merged 2 commits into from
Feb 4, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 0 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ env:
global:
- GOTFLAGS="-race"
matrix:
- BUILD_DEPTYPE=gx
- BUILD_DEPTYPE=gomod


Expand All @@ -24,7 +23,6 @@ script:

cache:
directories:
- $GOPATH/src/gx
- $GOPATH/pkg/mod
- /home/travis/.cache/go-build

Expand Down
16 changes: 0 additions & 16 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,22 +35,6 @@ Note that `go-multistream` is packaged with Gx, so it is recommended to use Gx t

## Usage

### Using Gx and Gx-go

This module is packaged with [Gx](https://github.com/whyrusleeping/gx). In order to use it in your own project do:

```sh
go get -u github.com/whyrusleeping/gx
go get -u github.com/whyrusleeping/gx-go
cd <your-project-repository>
gx init
gx import github.com/multiformats/go-multistream
gx install --global
gx-go --rewrite
```

Please check [Gx](https://github.com/whyrusleeping/gx) and [Gx-go](https://github.com/whyrusleeping/gx-go) documentation for more information.

### Example


Expand Down
6 changes: 3 additions & 3 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ func handshake(rw io.ReadWriter) error {
return <-errCh
}

func readMultistreamHeader(r io.ReadWriter) error {
func readMultistreamHeader(r io.Reader) error {
tok, err := ReadNextToken(r)
if err != nil {
return err
Expand All @@ -106,8 +106,8 @@ func trySelect(proto string, rwc io.ReadWriteCloser) error {
return readProto(proto, rwc)
}

func readProto(proto string, rw io.ReadWriter) error {
tok, err := ReadNextToken(rw)
func readProto(proto string, r io.Reader) error {
tok, err := ReadNextToken(r)
if err != nil {
return err
}
Expand Down
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
module github.com/multiformats/go-multistream

go 1.12
16 changes: 6 additions & 10 deletions multistream.go
Original file line number Diff line number Diff line change
Expand Up @@ -381,29 +381,25 @@ func (msm *MultistreamMuxer) Handle(rwc io.ReadWriteCloser) error {
return h(p, rwc)
}

// ReadNextToken extracts a token from a ReadWriter. It is used during
// ReadNextToken extracts a token from a Reader. It is used during
// protocol negotiation and returns a string.
func ReadNextToken(rw io.ReadWriter) (string, error) {
tok, err := ReadNextTokenBytes(rw)
func ReadNextToken(r io.Reader) (string, error) {
tok, err := ReadNextTokenBytes(r)
if err != nil {
return "", err
}

return string(tok), nil
}

// ReadNextTokenBytes extracts a token from a ReadWriter. It is used
// ReadNextTokenBytes extracts a token from a Reader. It is used
// during protocol negotiation and returns a byte slice.
func ReadNextTokenBytes(rw io.ReadWriter) ([]byte, error) {
data, err := lpReadBuf(rw)
func ReadNextTokenBytes(r io.Reader) ([]byte, error) {
data, err := lpReadBuf(r)
switch err {
case nil:
return data, nil
case ErrTooLarge:
err := delimWriteBuffered(rw, []byte("messages over 64k are not allowed"))
if err != nil {
return nil, err
}
Comment on lines -403 to -406
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

are we reporting this error anywhere now?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Back to the user. We don't bother sending it to the other side. If someone establishes a connection and sends us garbage, we'll reset the connection and walk away.

return nil, ErrTooLarge
default:
return nil, err
Expand Down
53 changes: 52 additions & 1 deletion multistream_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,42 @@ import (
"time"
)

type rwcStrict struct {
writing, reading bool
t *testing.T
rwc io.ReadWriteCloser
}

func newRwcStrict(t *testing.T, rwc io.ReadWriteCloser) io.ReadWriteCloser {
return &rwcStrict{t: t, rwc: rwc}
}

func (s *rwcStrict) Read(b []byte) (int, error) {
if s.reading {
s.t.Error("concurrent read")
return 0, fmt.Errorf("concurrent read")
}
s.reading = true
n, err := s.rwc.Read(b)
s.reading = false
return n, err
}

func (s *rwcStrict) Write(b []byte) (int, error) {
if s.writing {
s.t.Error("concurrent write")
return 0, fmt.Errorf("concurrent write")
}
s.writing = true
n, err := s.rwc.Write(b)
s.writing = false
return n, err
}

func (s *rwcStrict) Close() error {
return s.rwc.Close()
}

func newPipe(t *testing.T) (io.ReadWriteCloser, io.ReadWriteCloser) {
ln, err := net.Listen("tcp", "localhost:0")
if err != nil {
Expand All @@ -28,7 +64,7 @@ func newPipe(t *testing.T) (io.ReadWriteCloser, io.ReadWriteCloser) {
if err != nil {
t.Error(err)
}
return <-cchan, c
return newRwcStrict(t, <-cchan), newRwcStrict(t, c)
}

func TestProtocolNegotiation(t *testing.T) {
Expand Down Expand Up @@ -573,6 +609,21 @@ func TestTooLargeMessage(t *testing.T) {
}
}

// this exercises https://github.com/libp2p/go-libp2p-pnet/issues/31
func TestLargeMessageNegotiate(t *testing.T) {
mes := make([]byte, 100*1024)

a, b := newPipe(t)
err := delimWrite(a, mes)
if err != nil {
t.Fatal(err)
}
err = SelectProtoOrFail("/foo/bar", b)
if err == nil {
t.Error("should have failed to read large message")
}
}

func TestLs(t *testing.T) {
t.Run("none-eager", subtestLs(nil, false))
t.Run("one-eager", subtestLs([]string{"a"}, false))
Expand Down
16 changes: 0 additions & 16 deletions package.json

This file was deleted.