-
-
Notifications
You must be signed in to change notification settings - Fork 419
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 { | ||
|
@@ -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] ") { | ||
|
@@ -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 { | ||
t.Error("failed to allow OPs to modify the MOTD") | ||
} | ||
|
||
// Get around rate limitation | ||
time.Sleep(time.Second * 3) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is checking the response of There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 { | ||
|
There was a problem hiding this comment.
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.