mirror of
https://github.com/shazow/ssh-chat.git
synced 2025-06-15 14:52:08 +03:00
cleaned up the test
This commit is contained in:
parent
f8f8d1f3cf
commit
41a636b103
2
host.go
2
host.go
@ -151,13 +151,11 @@ func (h *Host) Connect(term *sshd.Terminal) {
|
|||||||
switch e.Key {
|
switch e.Key {
|
||||||
case "SSHCHAT_TIMESTAMP":
|
case "SSHCHAT_TIMESTAMP":
|
||||||
if e.Value != "" && e.Value != "0" {
|
if e.Value != "" && e.Value != "0" {
|
||||||
fmt.Println("got timestamp", e.Value)
|
|
||||||
cmd := "/timestamp"
|
cmd := "/timestamp"
|
||||||
if e.Value != "1" {
|
if e.Value != "1" {
|
||||||
cmd += " " + e.Value
|
cmd += " " + e.Value
|
||||||
}
|
}
|
||||||
if msg, ok := message.NewPublicMsg(cmd, user).ParseCommand(); ok {
|
if msg, ok := message.NewPublicMsg(cmd, user).ParseCommand(); ok {
|
||||||
fmt.Println("sending command to server:", cmd)
|
|
||||||
h.Room.HandleMsg(msg)
|
h.Room.HandleMsg(msg)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
70
host_test.go
70
host_test.go
@ -5,6 +5,7 @@ import (
|
|||||||
"crypto/rand"
|
"crypto/rand"
|
||||||
"crypto/rsa"
|
"crypto/rsa"
|
||||||
"errors"
|
"errors"
|
||||||
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
mathRand "math/rand"
|
mathRand "math/rand"
|
||||||
"strings"
|
"strings"
|
||||||
@ -285,61 +286,78 @@ func TestHostKick(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestClientEnvConfig(t *testing.T) {
|
func TestTimestampEnvConfig(t *testing.T) {
|
||||||
key, err := sshd.NewRandomSigner(512)
|
cases := []struct {
|
||||||
|
input string
|
||||||
|
timeformat *string
|
||||||
|
}{
|
||||||
|
{"", strptr("15:04")},
|
||||||
|
{"1", strptr("15:04")},
|
||||||
|
{"0", nil},
|
||||||
|
{"time +8h", strptr("15:04")},
|
||||||
|
{"datetime +8h", strptr("2006-01-02 15:04:05")},
|
||||||
|
}
|
||||||
|
for _, tc := range cases {
|
||||||
|
u, err := connectUserWithConfig("dingus", map[string]string{
|
||||||
|
"SSHCHAT_TIMESTAMP": tc.input,
|
||||||
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
|
userConfig := u.Config()
|
||||||
|
if userConfig.Timeformat != nil && tc.timeformat != nil {
|
||||||
|
if *userConfig.Timeformat != *tc.timeformat {
|
||||||
|
t.Fatal("unexpected timeformat:", *userConfig.Timeformat, "expected:", *tc.timeformat)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func strptr(s string) *string {
|
||||||
|
return &s
|
||||||
|
}
|
||||||
|
|
||||||
|
func connectUserWithConfig(name string, envConfig map[string]string) (*message.User, error) {
|
||||||
|
key, err := sshd.NewRandomSigner(512)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("unable to create signer: %w", err)
|
||||||
|
}
|
||||||
config := sshd.MakeNoAuth()
|
config := sshd.MakeNoAuth()
|
||||||
config.AddHostKey(key)
|
config.AddHostKey(key)
|
||||||
|
|
||||||
s, err := sshd.ListenSSH("localhost:0", config)
|
s, err := sshd.ListenSSH("localhost:0", config)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
return nil, fmt.Errorf("unable to create a test server: %w", err)
|
||||||
}
|
}
|
||||||
defer s.Close()
|
defer s.Close()
|
||||||
host := NewHost(s, nil)
|
host := NewHost(s, nil)
|
||||||
go host.Serve()
|
go host.Serve()
|
||||||
|
|
||||||
clientConfig := sshd.NewClientConfig("dingus")
|
clientConfig := sshd.NewClientConfig(name)
|
||||||
conn, err := ssh.Dial("tcp", s.Addr().String(), clientConfig)
|
conn, err := ssh.Dial("tcp", s.Addr().String(), clientConfig)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Error(err)
|
return nil, fmt.Errorf("unable to connect to test ssh-chat server: %w", err)
|
||||||
}
|
}
|
||||||
defer conn.Close()
|
defer conn.Close()
|
||||||
|
|
||||||
session, err := conn.NewSession()
|
session, err := conn.NewSession()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Error(err)
|
return nil, fmt.Errorf("unable to open session: %w", err)
|
||||||
}
|
}
|
||||||
defer session.Close()
|
defer session.Close()
|
||||||
|
|
||||||
session.Setenv("SSHCHAT_TIMESTAMP", "datetime +8h")
|
for key := range envConfig {
|
||||||
|
session.Setenv(key, envConfig[key])
|
||||||
stdout, err := session.StdoutPipe()
|
|
||||||
if err != nil {
|
|
||||||
t.Error(err)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
err = session.Shell()
|
err = session.Shell()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Error(err)
|
return nil, fmt.Errorf("unable to open shell: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
// consume
|
u, ok := host.GetUser(name)
|
||||||
bufio.NewScanner(stdout).Scan()
|
|
||||||
|
|
||||||
// this fails occasionally because the user has not been registered by the server yet
|
|
||||||
// add some kind of wait/synchornization mechanism
|
|
||||||
u, ok := host.GetUser("dingus")
|
|
||||||
if !ok {
|
if !ok {
|
||||||
t.Fatal("test user not found in host: ", host.Members)
|
return nil, fmt.Errorf("user %s not found in host", name)
|
||||||
}
|
|
||||||
userConfig := u.Config()
|
|
||||||
// add cases
|
|
||||||
// /timestamp datetime => 2006-01-02 15:04:05
|
|
||||||
// /timestamp time => 15:04
|
|
||||||
if userConfig.Timeformat != nil && *userConfig.Timeformat != "2006-01-02 15:04:05" {
|
|
||||||
t.Fatal("unexpected timeformat:", *userConfig.Timeformat)
|
|
||||||
}
|
}
|
||||||
|
return u, nil
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user