main: Update host_test.go to pass vet, use errgroup

This commit is contained in:
Andrey Petrov 2021-03-26 12:49:08 -04:00
parent 3f81d84cf1
commit 3848014d41
3 changed files with 47 additions and 55 deletions

4
go.mod
View File

@ -2,12 +2,12 @@ module github.com/shazow/ssh-chat
require ( require (
github.com/alexcesaro/log v0.0.0-20150915221235-61e686294e58 github.com/alexcesaro/log v0.0.0-20150915221235-61e686294e58
github.com/howeyc/gopass v0.0.0-20190910152052-7cb4b85ec19c
github.com/jessevdk/go-flags v1.5.0 github.com/jessevdk/go-flags v1.5.0
github.com/shazow/rateio v0.0.0-20200113175441-4461efc8bdc4 github.com/shazow/rateio v0.0.0-20200113175441-4461efc8bdc4
golang.org/x/crypto v0.0.0-20210322153248-0c34fe9e7dc2 golang.org/x/crypto v0.0.0-20210322153248-0c34fe9e7dc2
golang.org/x/sync v0.0.0-20210220032951-036812b2e83c
golang.org/x/sys v0.0.0-20210324051608-47abb6519492 golang.org/x/sys v0.0.0-20210324051608-47abb6519492
golang.org/x/term v0.0.0-20210317153231-de623e64d2a6 // indirect golang.org/x/term v0.0.0-20210317153231-de623e64d2a6
golang.org/x/text v0.3.5 golang.org/x/text v0.3.5
) )

4
go.sum
View File

@ -1,7 +1,5 @@
github.com/alexcesaro/log v0.0.0-20150915221235-61e686294e58 h1:MkpmYfld/S8kXqTYI68DfL8/hHXjHogL120Dy00TIxc= github.com/alexcesaro/log v0.0.0-20150915221235-61e686294e58 h1:MkpmYfld/S8kXqTYI68DfL8/hHXjHogL120Dy00TIxc=
github.com/alexcesaro/log v0.0.0-20150915221235-61e686294e58/go.mod h1:YNfsMyWSs+h+PaYkxGeMVmVCX75Zj/pqdjbu12ciCYE= github.com/alexcesaro/log v0.0.0-20150915221235-61e686294e58/go.mod h1:YNfsMyWSs+h+PaYkxGeMVmVCX75Zj/pqdjbu12ciCYE=
github.com/howeyc/gopass v0.0.0-20190910152052-7cb4b85ec19c h1:aY2hhxLhjEAbfXOx2nRJxCXezC6CO2V/yN+OCr1srtk=
github.com/howeyc/gopass v0.0.0-20190910152052-7cb4b85ec19c/go.mod h1:lADxMC39cJJqL93Duh1xhAs4I2Zs8mKS89XWXFGp9cs=
github.com/jessevdk/go-flags v1.5.0 h1:1jKYvbxEjfUl0fmqTCOfonvskHHXMjBySTLW4y9LFvc= github.com/jessevdk/go-flags v1.5.0 h1:1jKYvbxEjfUl0fmqTCOfonvskHHXMjBySTLW4y9LFvc=
github.com/jessevdk/go-flags v1.5.0/go.mod h1:Fw0T6WPc1dYxT4mKEZRfG5kJhaTDP9pj1c2EWnYs/m4= github.com/jessevdk/go-flags v1.5.0/go.mod h1:Fw0T6WPc1dYxT4mKEZRfG5kJhaTDP9pj1c2EWnYs/m4=
github.com/shazow/rateio v0.0.0-20200113175441-4461efc8bdc4 h1:zwQ1HBo5FYwn1ksMd19qBCKO8JAWE9wmHivEpkw/DvE= github.com/shazow/rateio v0.0.0-20200113175441-4461efc8bdc4 h1:zwQ1HBo5FYwn1ksMd19qBCKO8JAWE9wmHivEpkw/DvE=
@ -9,6 +7,8 @@ github.com/shazow/rateio v0.0.0-20200113175441-4461efc8bdc4/go.mod h1:vt2jWY/3Qw
golang.org/x/crypto v0.0.0-20210322153248-0c34fe9e7dc2 h1:It14KIkyBFYkHkwZ7k45minvA9aorojkyjGk9KJ5B/w= golang.org/x/crypto v0.0.0-20210322153248-0c34fe9e7dc2 h1:It14KIkyBFYkHkwZ7k45minvA9aorojkyjGk9KJ5B/w=
golang.org/x/crypto v0.0.0-20210322153248-0c34fe9e7dc2/go.mod h1:T9bdIzuCu7OtxOm1hfPfRQxPLYneinmdGuTeoZ9dtd4= golang.org/x/crypto v0.0.0-20210322153248-0c34fe9e7dc2/go.mod h1:T9bdIzuCu7OtxOm1hfPfRQxPLYneinmdGuTeoZ9dtd4=
golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg= golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=
golang.org/x/sync v0.0.0-20210220032951-036812b2e83c h1:5KslGYwFpkhGh+Q16bwMP3cOontH8FOep7tGV86Y7SQ=
golang.org/x/sync v0.0.0-20210220032951-036812b2e83c/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210320140829-1e4c9ba3b0c4/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20210320140829-1e4c9ba3b0c4/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210324051608-47abb6519492 h1:Paq34FxTluEPvVyayQqMPgHm+vTOrIifmcYxFBx9TLg= golang.org/x/sys v0.0.0-20210324051608-47abb6519492 h1:Paq34FxTluEPvVyayQqMPgHm+vTOrIifmcYxFBx9TLg=

View File

@ -13,6 +13,7 @@ import (
"github.com/shazow/ssh-chat/chat/message" "github.com/shazow/ssh-chat/chat/message"
"github.com/shazow/ssh-chat/sshd" "github.com/shazow/ssh-chat/sshd"
"golang.org/x/crypto/ssh" "golang.org/x/crypto/ssh"
"golang.org/x/sync/errgroup"
) )
func stripPrompt(s string) string { func stripPrompt(s string) string {
@ -91,11 +92,12 @@ func TestHostNameCollision(t *testing.T) {
host := NewHost(s, nil) host := NewHost(s, nil)
go host.Serve() go host.Serve()
done := make(chan struct{}, 1) ready := make(chan struct{})
g := errgroup.Group{}
// First client // First client
go func() { g.Go(func() error {
err := sshd.ConnectShell(s.Addr().String(), "foo", func(r io.Reader, w io.WriteCloser) error { return sshd.ConnectShell(s.Addr().String(), "foo", func(r io.Reader, w io.WriteCloser) error {
scanner := bufio.NewScanner(r) scanner := bufio.NewScanner(r)
// Consume the initial buffer // Consume the initial buffer
@ -107,7 +109,7 @@ func TestHostNameCollision(t *testing.T) {
} }
// Ready for second client // Ready for second client
done <- struct{}{} ready <- struct{}{}
scanner.Scan() scanner.Scan()
actual = scanner.Text() actual = scanner.Text()
@ -123,38 +125,35 @@ func TestHostNameCollision(t *testing.T) {
} }
// Wrap it up. // Wrap it up.
close(done) close(ready)
return nil return nil
}) })
if err != nil { })
done <- struct{}{}
t.Fatal(err)
}
}()
// Wait for first client // Wait for first client
<-done <-ready
// Second client // Second client
err = sshd.ConnectShell(s.Addr().String(), "foo", func(r io.Reader, w io.WriteCloser) error { g.Go(func() error {
scanner := bufio.NewScanner(r) return sshd.ConnectShell(s.Addr().String(), "foo", func(r io.Reader, w io.WriteCloser) error {
scanner := bufio.NewScanner(r)
// Consume the initial buffer // Consume the initial buffer
scanner.Scan() scanner.Scan()
scanner.Scan() scanner.Scan()
scanner.Scan() scanner.Scan()
actual := scanner.Text() actual := scanner.Text()
if !strings.HasPrefix(actual, "[Guest1] ") { if !strings.HasPrefix(actual, "[Guest1] ") {
t.Errorf("Second client did not get Guest1 name: %q", actual) t.Errorf("Second client did not get Guest1 name: %q", actual)
} }
return nil return nil
})
}) })
if err != nil {
t.Fatal(err)
}
<-done if err := g.Wait(); err != nil {
t.Error(err)
}
} }
func TestHostWhitelist(t *testing.T) { func TestHostWhitelist(t *testing.T) {
@ -215,13 +214,13 @@ func TestHostKick(t *testing.T) {
host := NewHost(s, nil) host := NewHost(s, nil)
go host.Serve() go host.Serve()
g := errgroup.Group{}
connected := make(chan struct{}) connected := make(chan struct{})
kicked := make(chan struct{}) kicked := make(chan struct{})
done := make(chan struct{})
go func() { g.Go(func() error {
// First client // First client
err := sshd.ConnectShell(addr, "foo", func(r io.Reader, w io.WriteCloser) error { return sshd.ConnectShell(addr, "foo", func(r io.Reader, w io.WriteCloser) error {
scanner := bufio.NewScanner(r) scanner := bufio.NewScanner(r)
// Consume the initial buffer // Consume the initial buffer
@ -252,41 +251,34 @@ func TestHostKick(t *testing.T) {
} }
kicked <- struct{}{} kicked <- struct{}{}
return nil return nil
}) })
if err != nil { })
connected <- struct{}{}
close(connected)
t.Fatal(err)
close(done)
}
}()
go func() { g.Go(func() error {
// Second client // Second client
err := sshd.ConnectShell(addr, "bar", func(r io.Reader, w io.WriteCloser) error { return sshd.ConnectShell(addr, "bar", func(r io.Reader, w io.WriteCloser) error {
scanner := bufio.NewScanner(r) scanner := bufio.NewScanner(r)
<-connected <-connected
scanner.Scan() scanner.Scan()
<-kicked <-kicked
if _, err := w.Write([]byte("am I still here?\r\n")); err != nil { if _, err := w.Write([]byte("am I still here?\r\n")); err != io.EOF {
return err return errors.New("expected to be kicked")
} }
scanner.Scan() scanner.Scan()
return scanner.Err() if err := scanner.Err(); err == io.EOF {
// All good, we got kicked.
return nil
} else {
return err
}
}) })
if err == io.EOF { })
// All good, we got kicked.
} else if err != nil {
close(done)
t.Fatal(err)
}
close(done)
}()
<-done if err := g.Wait(); err != nil {
t.Error(err)
}
} }