all repos

mugit @ aaf0e5f796a25976779c0da320ef9087bfc51ab0

🐮 git server that your cow will love

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

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