all repos

mugit @ e07d1c9

🐮 git server that your cow will love

mugit/internal/ssh/ssh_test.go (view raw)

Oleksandr Smirnov Oleksandr Smirnov
olexsmir@gmail.com
ssh: print modt on ssh -T, 28 days ago
1
package ssh
2
3
import (
4
	"bytes"
5
	"strings"
6
	"testing"
7
8
	"olexsmir.xyz/mugit/internal/config"
9
	"olexsmir.xyz/x/is"
10
)
11
12
var validKey = "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIOMqqnkVzrm0SdG6UOoqKLsabgH5C9okWi0dh2l9GKJl"
13
14
func TestNewShell(t *testing.T) {
15
	tests := []struct {
16
		name    string
17
		keys    []string
18
		wantErr string
19
	}{
20
		{"valid key", []string{validKey}, ""},
21
		{"invalid key", []string{"invalid-key"}, "ssh: no key found"},
22
		{"multiple keys", []string{validKey, validKey}, ""},
23
		{"no keys", []string{}, ""},
24
	}
25
26
	for _, tt := range tests {
27
		t.Run(tt.name, func(t *testing.T) {
28
			cfg := &config.Config{SSH: config.SSHConfig{Keys: tt.keys}}
29
			shell, err := NewShell(cfg)
30
			if tt.wantErr == "" {
31
				is.Err(t, err, nil)
32
				is.Equal(t, len(shell.keys), len(cfg.SSH.Keys))
33
			} else {
34
				is.Err(t, err, tt.wantErr)
35
			}
36
		})
37
	}
38
}
39
40
func TestShellParseCommand(t *testing.T) {
41
	cfg := &config.Config{
42
		SSH: config.SSHConfig{
43
			Keys: []string{validKey},
44
		},
45
	}
46
47
	shell, err := NewShell(cfg)
48
	is.Err(t, err, nil)
49
50
	tests := []struct {
51
		cmd        string
52
		wantGitCmd string
53
		wantRepo   string
54
		wantErr    string
55
	}{
56
		{"git-upload-pack 'myrepo'", "git-upload-pack", "myrepo", ""},
57
		{"git-upload-pack \"myrepo\"", "git-upload-pack", "myrepo", ""},
58
		{"git-upload-pack myrepo", "git-upload-pack", "myrepo", ""},
59
		{"git-upload-archive 'archive-repo'", "git-upload-archive", "archive-repo", ""},
60
		{"git-upload-pack", "", "", "invalid command"},
61
		{"git-upload-pack ''", "", "", "empty repository name"},
62
		{"git-receive-pack repo.git && echo hi", "", "", "invalid command"},
63
		{"echo hi", "", "", "invalid command"},
64
		{"", "", "", "invalid command"},
65
	}
66
67
	for _, tt := range tests {
68
		t.Run(tt.cmd, func(t *testing.T) {
69
			gitCmd, repo, err := shell.parseCommand(tt.cmd)
70
			if tt.wantErr == "" {
71
				is.Err(t, err, nil)
72
				is.Equal(t, gitCmd, tt.wantGitCmd)
73
				is.Equal(t, repo, tt.wantRepo)
74
			} else {
75
				is.Err(t, err, tt.wantErr)
76
			}
77
		})
78
	}
79
}
80
81
func TestShellAuthorizedKeys(t *testing.T) {
82
	shell, err := NewShell(&config.Config{
83
		SSH: config.SSHConfig{Keys: []string{validKey}},
84
	})
85
	is.Err(t, err, nil)
86
87
	result := shell.AuthorizedKeys("/usr/bin/mugit")
88
	if !strings.Contains(result, `command="/usr/bin/mugit shell",no-port-forwarding,no-X11-forwarding,no-agent-forwarding,no-pty`) {
89
		t.Errorf("AuthorizedKeys() missing expected format\ngot: %s", result)
90
	}
91
	if !strings.Contains(result, validKey) {
92
		t.Errorf("AuthorizedKeys() missing SSH key\ngot: %s", result)
93
	}
94
}
95
96
func TestShellHandleCommand_modt(t *testing.T) {
97
	modt := "test modt"
98
	shell, err := NewShell(&config.Config{Meta: config.MetaConfig{
99
		Modt: modt,
100
	}})
101
	is.Err(t, err, nil)
102
103
	var stdout, stderr bytes.Buffer
104
	err = shell.HandleCommand(t.Context(), "", strings.NewReader(""), &stdout, &stderr)
105
	is.Err(t, err, nil)
106
	is.Equal(t, stdout.String(), "")
107
108
	if !strings.Contains(stderr.String(), modt) {
109
		t.Fatalf("expected MOTD in stderr\ngot: %s", stderr.String())
110
	}
111
}