From 636ec68c85ee167f059c9f9ca5cad23aad4d0628 Mon Sep 17 00:00:00 2001 From: Aaron Date: Sat, 6 Aug 2016 20:38:46 +0300 Subject: [PATCH 1/5] Create function to advance scanner tokens --- host_test.go | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/host_test.go b/host_test.go index d29a875a..ca8c0bfb 100644 --- a/host_test.go +++ b/host_test.go @@ -15,6 +15,15 @@ import ( "golang.org/x/crypto/ssh" ) +func nextScanToken(scanner *bufio.Scanner, i int) *bufio.Scanner { + count := 0 + for count < i { + scanner.Scan() + count++ + } + return scanner +} + func stripPrompt(s string) string { pos := strings.LastIndex(s, "\033[K") if pos < 0 { @@ -107,9 +116,7 @@ func TestHostNameCollision(t *testing.T) { scanner := bufio.NewScanner(r) // Consume the initial buffer - scanner.Scan() - scanner.Scan() - scanner.Scan() + nextScanToken(scanner, 3) actual := scanner.Text() if !strings.HasPrefix(actual, "[Guest1] ") { From 2182105fe63b4e578bfafe16b62ae7dd4bc087dd Mon Sep 17 00:00:00 2001 From: Aaron Date: Sat, 6 Aug 2016 20:39:25 +0300 Subject: [PATCH 2/5] Import the "time" package --- host_test.go | 1 + 1 file changed, 1 insertion(+) diff --git a/host_test.go b/host_test.go index ca8c0bfb..df013427 100644 --- a/host_test.go +++ b/host_test.go @@ -9,6 +9,7 @@ import ( "io/ioutil" "strings" "testing" + "time" "github.com/shazow/ssh-chat/chat/message" "github.com/shazow/ssh-chat/sshd" From a27433e95704ee633a91a93fa293e2a9bd3cd0b8 Mon Sep 17 00:00:00 2001 From: Aaron Date: Sat, 6 Aug 2016 20:40:52 +0300 Subject: [PATCH 3/5] Create motd test for regular user w/ no parameters --- host_test.go | 48 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) diff --git a/host_test.go b/host_test.go index df013427..8cebd7fc 100644 --- a/host_test.go +++ b/host_test.go @@ -132,6 +132,54 @@ func TestHostNameCollision(t *testing.T) { <-done } +func TestMotdCommand(t *testing.T) { + key, err := sshd.NewRandomSigner(512) + if err != nil { + t.Fatal(err) + } + + auth := NewAuth() + config := sshd.MakeAuth(auth) + config.AddHostKey(key) + + s, err := sshd.ListenSSH("localhost:0", config) + if err != nil { + t.Fatal(err) + } + defer s.Close() + host := NewHost(s, auth) + go host.Serve() + + err = sshd.ConnectShell(s.Addr().String(), "baz", func(r io.Reader, w io.WriteCloser) error { + if err != nil { + t.Error(err) + } + member, _ := host.Room.MemberById("baz") + if member == nil { + return errors.New("failed to load MemberById") + } + + scanner := bufio.NewScanner(r) + testMotd := "foobar" + host.motd = testMotd + + // Test as regular user with no parameters - expected behaviour: should print the MOTD + w.Write([]byte("/motd\r\n")) + + // Consuming buffer + nextScanToken(scanner, 3) + + actual := scanner.Text() + actual = stripPrompt(actual)[3:] + expected := "foobar" + if strings.Compare(actual, expected) != 0 { + t.Error("failed to print MOTD using /motd with no parameters", "actual:", actual, "expected:", expected) + } + + return nil + }) +} + func TestHostWhitelist(t *testing.T) { key, err := sshd.NewRandomSigner(512) if err != nil { From 0297c1a341c3ae7e5e0c54e559d407d2380ec8e0 Mon Sep 17 00:00:00 2001 From: Aaron Date: Sat, 6 Aug 2016 20:41:27 +0300 Subject: [PATCH 4/5] Create motd test for regular user with MESSAGE param --- host_test.go | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/host_test.go b/host_test.go index 8cebd7fc..2e0adf1e 100644 --- a/host_test.go +++ b/host_test.go @@ -176,6 +176,11 @@ func TestMotdCommand(t *testing.T) { t.Error("failed to print MOTD using /motd with no parameters", "actual:", actual, "expected:", expected) } + // Test as regular user - expected behaviour: should return an error + w.Write([]byte("/motd foobarbaz\r\n")) + if strings.Compare(host.motd, "foobar") != 0 { + t.Error("failed to hinder non-OPs to modify the MOTD") + } return nil }) } From 293921480ba610bbc85c20446d35c6b8b8efb8bd Mon Sep 17 00:00:00 2001 From: Aaron Date: Sat, 6 Aug 2016 20:42:19 +0300 Subject: [PATCH 5/5] Create motd tests for OP --- host_test.go | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/host_test.go b/host_test.go index 2e0adf1e..320802e0 100644 --- a/host_test.go +++ b/host_test.go @@ -181,6 +181,34 @@ func TestMotdCommand(t *testing.T) { if strings.Compare(host.motd, "foobar") != 0 { t.Error("failed to hinder non-OPs to modify the MOTD") } + + // Test as OP - expected behaviour: should modify the MOTD + host.Room.Ops.Add(member) + testMotd = "barfoo" + w.Write([]byte("/motd barfoo\r\n")) + + // Fix this during the code-review process + time.Sleep(time.Millisecond * 500) + + if strings.Compare(host.motd, testMotd) != 0 { + t.Error("failed to allow OPs to modify the MOTD") + } + + // Get around rate limitation + time.Sleep(time.Second * 3) + + // Test as OP - expected behaviour: should print the MOTD even if OP + w.Write([]byte("/motd\r\n")) + + nextScanToken(scanner, 8) + + actual = scanner.Text() + actual = stripPrompt(actual)[3:] + expected = "barfoo" + if strings.Compare(actual, expected) != 0 { + t.Error("failed to print MOTD using /motd with no parameters - as OP") + } + return nil }) }