1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
|
package utils
import (
"crypto/ed25519"
"crypto/rand"
"os"
"path/filepath"
"strings"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"gitlab.com/whiteboardguy/eno/internal/crypt"
"golang.org/x/crypto/ssh"
)
const alpha = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
const alphanum = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
func TestCheckFileAuthMode_Pass(t *testing.T) {
dir := t.TempDir()
path := filepath.Join(dir, "test.eno")
plaintext := []byte("auth mode test")
password := []byte("testpassword")
enc, err := crypt.EncryptBytesPass(plaintext, password)
require.NoError(t, err)
require.NoError(t, os.WriteFile(path, enc, 0600))
mode, err := CheckFileAuthMode(path)
require.NoError(t, err)
assert.Equal(t, crypt.Pass, mode)
}
func TestCheckFileAuthMode_SSH(t *testing.T) {
dir := t.TempDir()
path := filepath.Join(dir, "test.eno")
pubKey, _, err := ed25519.GenerateKey(rand.Reader)
require.NoError(t, err)
sshPub, err := ssh.NewPublicKey(pubKey)
require.NoError(t, err)
plaintext := []byte("auth mode test")
enc, _, err := crypt.EncryptBytesSSH(plaintext, [][]byte{ssh.MarshalAuthorizedKey(sshPub)})
require.NoError(t, err)
require.NoError(t, os.WriteFile(path, enc, 0600))
mode, err := CheckFileAuthMode(path)
require.NoError(t, err)
assert.Equal(t, crypt.SSH, mode)
}
func TestCheckFileAuthMode_NotFound(t *testing.T) {
_, err := CheckFileAuthMode("/nonexistent/file.eno")
assert.Error(t, err)
}
func TestCheckFileAuthMode_Corrupt(t *testing.T) {
dir := t.TempDir()
path := filepath.Join(dir, "test.eno")
require.NoError(t, os.WriteFile(path, []byte("not-an-eno-file"), 0600))
_, err := CheckFileAuthMode(path)
assert.Error(t, err)
}
func TestCheckFileAuthMode_EmptyFile(t *testing.T) {
dir := t.TempDir()
path := filepath.Join(dir, "test.eno")
require.NoError(t, os.WriteFile(path, []byte{}, 0600))
_, err := CheckFileAuthMode(path)
assert.Error(t, err)
}
func TestSecureTempFile(t *testing.T) {
f, err := SecureTempFile()
require.NoError(t, err)
defer os.Remove(f.Name())
assert.NotNil(t, f)
_, err = os.Stat(f.Name())
require.NoError(t, err, "temp file should exist on disk")
content := []byte("test data")
n, err := f.Write(content)
require.NoError(t, err)
assert.Equal(t, len(content), n)
readBack, err := os.ReadFile(f.Name())
require.NoError(t, err)
assert.Equal(t, content, readBack)
}
func TestSecureTempFile_Content(t *testing.T) {
f, err := SecureTempFile()
require.NoError(t, err)
defer os.Remove(f.Name())
content := []byte("secure temp file test content")
_, err = f.Write(content)
require.NoError(t, err)
require.NoError(t, f.Close())
got, err := os.ReadFile(f.Name())
require.NoError(t, err)
assert.Equal(t, content, got)
}
func TestClearTerm(t *testing.T) {
assert.NotPanics(t, func() { ClearTerm() })
}
func TestRandomLetters_Length(t *testing.T) {
result := RandomLetters(37)
require.Len(t, result, 37)
}
func TestRandomLetters_OnlyAlpha(t *testing.T) {
result := RandomLetters(100)
for _, ch := range result {
assert.True(t, strings.ContainsRune(alpha, rune(ch)), "char %q not in alphabet", ch)
}
}
func TestRandomLetters_Unique(t *testing.T) {
a := RandomLetters(50)
b := RandomLetters(50)
assert.NotEqual(t, string(a), string(b), "two calls should produce different results")
}
func TestRandomLetters_Zero(t *testing.T) {
result := RandomLetters(0)
assert.Empty(t, result)
}
func TestRandomLettersNumbers_Length(t *testing.T) {
result := RandomLettersNumbers(37)
require.Len(t, result, 37)
}
func TestRandomLettersNumbers_OnlyAlphaNum(t *testing.T) {
result := RandomLettersNumbers(100)
for _, ch := range result {
assert.True(t, strings.ContainsRune(alphanum, rune(ch)), "char %q not in alphanum", ch)
}
}
func TestRandomLettersNumbers_Unique(t *testing.T) {
a := RandomLettersNumbers(50)
b := RandomLettersNumbers(50)
assert.NotEqual(t, string(a), string(b), "two calls should produce different results")
}
func TestRandomLettersNumbers_Zero(t *testing.T) {
result := RandomLettersNumbers(0)
assert.Empty(t, result)
}
|