good morning!!!!

Skip to content
Snippets Groups Projects
Unverified Commit 9e32354f authored by Anmol Sethi's avatar Anmol Sethi
Browse files

Fix randString method in tests

parent b6b56b74
No related branches found
No related tags found
No related merge requests found
......@@ -45,6 +45,7 @@ func assertJSONRead(t *testing.T, ctx context.Context, c *websocket.Conn, exp in
func randString(t *testing.T, n int) string {
s := strings.ToValidUTF8(string(randBytes(t, n)), "_")
s = strings.ReplaceAll(s, "\x00", "_")
if len(s) > n {
return s[:n]
}
......
FROM golang:1
RUN apt-get update
RUN apt-get install -y chromium
RUN apt-get install -y chromium npm
ENV GOFLAGS="-mod=readonly"
ENV PAGER=cat
......
......@@ -18,6 +18,38 @@ import (
"nhooyr.io/websocket"
)
func TestFuzz(t *testing.T) {
t.Parallel()
s, closeFn := testServer(t, func(w http.ResponseWriter, r *http.Request) {
c, err := websocket.Accept(w, r, &websocket.AcceptOptions{
CompressionOptions: websocket.CompressionOptions{
Mode: websocket.CompressionContextTakeover,
},
})
assert.Success(t, "accept", err)
defer c.Close(websocket.StatusInternalError, "")
err = echoLoop(r.Context(), c)
assertCloseStatus(t, websocket.StatusNormalClosure, err)
}, false)
defer closeFn()
ctx, cancel := context.WithTimeout(context.Background(), time.Minute)
defer cancel()
opts := &websocket.DialOptions{
CompressionOptions: websocket.CompressionOptions{
Mode: websocket.CompressionContextTakeover,
},
}
opts.HTTPClient = s.Client()
c, _, err := websocket.Dial(ctx, wsURL(s), opts)
assert.Success(t, "dial", err)
assertJSONEcho(t, ctx, c, 8393)
}
func TestConn(t *testing.T) {
t.Parallel()
......@@ -25,23 +57,18 @@ func TestConn(t *testing.T) {
s, closeFn := testServer(t, func(w http.ResponseWriter, r *http.Request) {
c, err := websocket.Accept(w, r, &websocket.AcceptOptions{
Subprotocols: []string{"echo"},
InsecureSkipVerify: true,
CompressionOptions: websocket.CompressionOptions{
Mode: websocket.CompressionContextTakeover,
Threshold: 1,
},
})
assert.Success(t, "accept", err)
defer c.Close(websocket.StatusInternalError, "")
err = echoLoop(r.Context(), c)
t.Logf("server: %v", err)
assertCloseStatus(t, websocket.StatusNormalClosure, err)
}, false)
defer closeFn()
wsURL := strings.Replace(s.URL, "http", "ws", 1)
ctx, cancel := context.WithTimeout(context.Background(), time.Minute)
defer cancel()
......@@ -49,14 +76,13 @@ func TestConn(t *testing.T) {
Subprotocols: []string{"echo"},
CompressionOptions: websocket.CompressionOptions{
Mode: websocket.CompressionContextTakeover,
Threshold: 1,
},
}
opts.HTTPClient = s.Client()
c, _, err := websocket.Dial(ctx, wsURL, opts)
c, _, err := websocket.Dial(ctx, wsURL(s), opts)
assert.Success(t, "dial", err)
assertJSONEcho(t, ctx, c, 2)
assertJSONEcho(t, ctx, c, 8393)
})
}
......@@ -149,3 +175,7 @@ func echoLoop(ctx context.Context, c *websocket.Conn) error {
}
}
}
func wsURL(s *httptest.Server) string {
return strings.Replace(s.URL, "http", "ws", 1)
}
package errd
import "golang.org/x/xerrors"
import (
"fmt"
"golang.org/x/xerrors"
)
type wrapError struct {
msg string
err error
frame xerrors.Frame
}
func (e *wrapError) Error() string {
return fmt.Sprint(e)
}
func (e *wrapError) Format(s fmt.State, v rune) { xerrors.FormatError(e, s, v) }
func (e *wrapError) FormatError(p xerrors.Printer) (next error) {
p.Print(e.msg)
e.frame.Format(p)
return e.err
}
func (e *wrapError) Unwrap() error {
return e.err
}
// Wrap wraps err with xerrors.Errorf if err is non nil.
// Intended for use with defer and a named error return.
// Inspired by https://github.com/golang/go/issues/32676.
func Wrap(err *error, f string, v ...interface{}) {
if *err != nil {
*err = xerrors.Errorf(f+": %w", append(v, *err)...)
*err = &wrapError{
msg: fmt.Sprintf(f, v...),
err: *err,
frame: xerrors.Caller(1),
}
}
}
......@@ -145,7 +145,7 @@ func (mw *msgWriter) Write(p []byte) (_ int, err error) {
return 0, xerrors.New("cannot use closed writer")
}
// TODO can make threshold detection robust across writes by writing to buffer
// TODO can make threshold detection robust across writes by writing to bufio writer
if mw.flate ||
mw.c.flate() && len(p) >= mw.c.flateThreshold {
mw.ensureFlate()
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment