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
162
163
|
package cli
import (
"os"
"path/filepath"
"testing"
)
func TestProfileList(t *testing.T) {
tmpDir := t.TempDir()
// Create some dummy profile files
profiles := []string{"home.conf", "work.conf", "not-a-conf.txt"}
for _, p := range profiles {
err := os.WriteFile(filepath.Join(tmpDir, p), []byte("test content"), 0644)
if err != nil {
t.Fatalf("failed to create test profile %s: %v", p, err)
}
}
app := NewApp([]string{"wg-wrap", "profile", "list"})
app.ConfigDir = tmpDir
err := app.Route()
if err != nil {
t.Errorf("expected no error, got %v", err)
}
}
func TestProfileImport(t *testing.T) {
tmpDir := t.TempDir()
profilesDir := filepath.Join(tmpDir, "profiles")
err := os.MkdirAll(profilesDir, 0755)
if err != nil {
t.Fatalf("failed to create profiles dir: %v", err)
}
srcFile := filepath.Join(tmpDir, "source.conf")
err = os.WriteFile(srcFile, []byte("[Interface]\nPrivateKey = test\n"), 0644)
if err != nil {
t.Fatalf("failed to create source conf: %v", err)
}
// 1. Test importing with derived name (no explicit name argument)
app := NewApp([]string{"wg-wrap", "profile", "import", srcFile})
app.ConfigDir = profilesDir
err = app.Route()
if err != nil {
t.Errorf("expected no error, got %v", err)
}
// Verify the file was actually copied to derived name and has correct permissions
destFile := filepath.Join(profilesDir, "source.conf")
if _, err := os.Stat(destFile); os.IsNotExist(err) {
t.Errorf("expected profile to be imported to %s", destFile)
}
var info os.FileInfo
info, err = os.Stat(destFile)
if err != nil {
t.Fatalf("failed to stat imported profile: %v", err)
}
if info.Mode().Perm() != 0600 {
t.Errorf("expected imported profile to have permissions 0600, got %o", info.Mode().Perm())
}
// 2. Test importing with explicit name argument
customName := "custom-vpn"
appCustom := NewApp([]string{"wg-wrap", "profile", "import", srcFile, customName})
appCustom.ConfigDir = profilesDir
err = appCustom.Route()
if err != nil {
t.Errorf("expected no error for custom name import, got %v", err)
}
destCustomFile := filepath.Join(profilesDir, customName+".conf")
if _, err := os.Stat(destCustomFile); os.IsNotExist(err) {
t.Errorf("expected profile to be imported to %s", destCustomFile)
}
info, err = os.Stat(destCustomFile)
if err != nil {
t.Fatalf("failed to stat imported profile: %v", err)
}
if info.Mode().Perm() != 0600 {
t.Errorf("expected imported profile to have permissions 0600, got %o", info.Mode().Perm())
}
// 3. Test duplicate import (should fail)
appDup := NewApp([]string{"wg-wrap", "profile", "import", srcFile, customName})
appDup.ConfigDir = profilesDir
err = appDup.Route()
if err == nil {
t.Errorf("expected error when importing duplicate profile, got nil")
}
}
func TestProfileDelete(t *testing.T) {
tmpDir := t.TempDir()
profilesDir := filepath.Join(tmpDir, "profiles")
err := os.MkdirAll(profilesDir, 0755)
if err != nil {
t.Fatalf("failed to create profiles dir: %v", err)
}
profileName := "test-profile"
profileFile := filepath.Join(profilesDir, profileName+".conf")
err = os.WriteFile(profileFile, []byte("[Interface]\nPrivateKey = test\n"), 0644)
if err != nil {
t.Fatalf("failed to create profile file: %v", err)
}
app := NewApp([]string{"wg-wrap", "profile", "delete", profileName})
app.ConfigDir = profilesDir
err = app.Route()
if err != nil {
t.Errorf("expected no error, got %v", err)
}
if _, err := os.Stat(profileFile); !os.IsNotExist(err) {
t.Errorf("expected profile file %s to be deleted", profileFile)
}
}
func TestProfileDeleteNotFound(t *testing.T) {
tmpDir := t.TempDir()
app := NewApp([]string{"wg-wrap", "profile", "delete", "non-existent"})
app.ConfigDir = tmpDir
err := app.Route()
if err == nil {
t.Errorf("expected error when deleting non-existent profile, got nil")
}
}
func TestProfileConfigure(t *testing.T) {
tmpDir := t.TempDir()
profilesDir := filepath.Join(tmpDir, "profiles")
err := os.MkdirAll(profilesDir, 0755)
if err != nil {
t.Fatalf("failed to create profiles dir: %v", err)
}
profileName := "test-profile"
profileFile := filepath.Join(profilesDir, profileName+".conf")
err = os.WriteFile(profileFile, []byte("[Interface]\nPrivateKey = test\n"), 0644)
if err != nil {
t.Fatalf("failed to create profile file: %v", err)
}
app := NewApp([]string{"wg-wrap", "profile", "configure", profileName})
app.ConfigDir = profilesDir
// Use "true" as the mock editor to ensure it exits successfully immediately
t.Setenv("EDITOR", "true")
err = app.Route()
if err != nil {
t.Errorf("expected successful configuration, got error: %v", err)
}
}
|