Skip to content

Add tests for the /motd command #189

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

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
95 changes: 92 additions & 3 deletions host_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,22 @@ import (
"io/ioutil"
"strings"
"testing"
"time"

"github.com/shazow/ssh-chat/chat/message"
"github.com/shazow/ssh-chat/sshd"
"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 {
Expand Down Expand Up @@ -107,9 +117,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] ") {
Expand All @@ -124,6 +132,87 @@ 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)
}

// 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")
}

// 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 {
Copy link
Owner

Choose a reason for hiding this comment

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

I'd get rid of this check and the time.Sleeps, then it should work by reading the response of /motd as the client.

t.Error("failed to allow OPs to modify the MOTD")
}

// Get around rate limitation
time.Sleep(time.Second * 3)
Copy link
Author

Choose a reason for hiding this comment

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

So it seems that it takes a little time for the host ds to "commit" the changes made through /motd(at least on my local machine)

Copy link
Owner

@shazow shazow Aug 6, 2016

Choose a reason for hiding this comment

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

Is checking the response of /motd unreliable?

Copy link
Author

Choose a reason for hiding this comment

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

I don't think so but wouldn't we face the exact same problem (using time.Sleep) by reading the response from /motd instead of h.motd? If h.motdhasn't had "enough time" to commit the changes made from the user prompt then /motd will also return an obsolete MOTD. Wdyt?

Copy link
Owner

Choose a reason for hiding this comment

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

Doesn't reading block until the line is available? So if we know how many lines to expect, we can read until they're there?


// 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
})
}

func TestHostWhitelist(t *testing.T) {
key, err := sshd.NewRandomSigner(512)
if err != nil {
Expand Down