| 1 |
package types_test |
| 2 |
|
| 3 |
import ( |
| 4 |
"encoding/json" |
| 5 |
"os" |
| 6 |
"path/filepath" |
| 7 |
"runtime" |
| 8 |
"testing" |
| 9 |
|
| 10 |
"github.com/jkroepke/openvpn-auth-oauth2/v2/internal/config/types" |
| 11 |
"github.com/jkroepke/openvpn-auth-oauth2/v2/internal/ui/assets" |
| 12 |
"github.com/stretchr/testify/assert" |
| 13 |
"github.com/stretchr/testify/require" |
| 14 |
) |
| 15 |
|
| 16 |
func TestFS(t *testing.T) { |
| 17 |
t.Parallel() |
| 18 |
|
| 19 |
for _, tc := range []struct { |
| 20 |
name string |
| 21 |
input string |
| 22 |
err string |
| 23 |
}{ |
| 24 |
{ |
| 25 |
"empty", |
| 26 |
"", |
| 27 |
"open :", |
| 28 |
}, |
| 29 |
{ |
| 30 |
"dir", |
| 31 |
".", |
| 32 |
"", |
| 33 |
}, |
| 34 |
{ |
| 35 |
"invalid", |
| 36 |
"...", |
| 37 |
"error open \"...\":", |
| 38 |
}, |
| 39 |
{ |
| 40 |
"not found", |
| 41 |
"a", |
| 42 |
"error open \"a\":", |
| 43 |
}, |
| 44 |
{ |
| 45 |
"file", |
| 46 |
"../../../README.md", |
| 47 |
"not a directory", |
| 48 |
}, |
| 49 |
} { |
| 50 |
t.Run(tc.name, func(t *testing.T) { |
| 51 |
t.Parallel() |
| 52 |
|
| 53 |
_, err := types.NewFS(tc.input) |
| 54 |
if tc.err == "" { |
| 55 |
require.NoError(t, err) |
| 56 |
} else { |
| 57 |
require.ErrorContains(t, err, tc.err) |
| 58 |
} |
| 59 |
}) |
| 60 |
} |
| 61 |
} |
| 62 |
|
| 63 |
//nolint:exhaustruct |
| 64 |
func TestIsFSEmpty(t *testing.T) { |
| 65 |
t.Parallel() |
| 66 |
|
| 67 |
for _, tc := range []struct { |
| 68 |
name string |
| 69 |
fs *types.FS |
| 70 |
expect bool |
| 71 |
}{ |
| 72 |
{ |
| 73 |
"nil", |
| 74 |
nil, |
| 75 |
true, |
| 76 |
}, |
| 77 |
{ |
| 78 |
"empty", |
| 79 |
&types.FS{}, |
| 80 |
true, |
| 81 |
}, |
| 82 |
{ |
| 83 |
"non-empty", |
| 84 |
&types.FS{FS: assets.FS}, |
| 85 |
false, |
| 86 |
}, |
| 87 |
} { |
| 88 |
t.Run(tc.name, func(t *testing.T) { |
| 89 |
t.Parallel() |
| 90 |
assert.Equal(t, tc.expect, tc.fs.IsEmpty()) |
| 91 |
}) |
| 92 |
} |
| 93 |
} |
| 94 |
|
| 95 |
func TestFSUnmarshalText(t *testing.T) { |
| 96 |
t.Parallel() |
| 97 |
|
| 98 |
for _, tc := range []struct { |
| 99 |
name string |
| 100 |
input string |
| 101 |
err string |
| 102 |
}{ |
| 103 |
{ |
| 104 |
"empty", |
| 105 |
"", |
| 106 |
"open :", |
| 107 |
}, |
| 108 |
{ |
| 109 |
"dir", |
| 110 |
".", |
| 111 |
"", |
| 112 |
}, |
| 113 |
} { |
| 114 |
t.Run(tc.name, func(t *testing.T) { |
| 115 |
t.Parallel() |
| 116 |
|
| 117 |
dirFS := types.FS{} |
| 118 |
|
| 119 |
err := dirFS.UnmarshalText([]byte(tc.input)) |
| 120 |
if tc.err == "" { |
| 121 |
require.NoError(t, err) |
| 122 |
} else { |
| 123 |
require.ErrorContains(t, err, tc.err) |
| 124 |
} |
| 125 |
}) |
| 126 |
} |
| 127 |
} |
| 128 |
|
| 129 |
func TestFSPreventsPathTraversal(t *testing.T) { |
| 130 |
t.Parallel() |
| 131 |
|
| 132 |
parentDir := t.TempDir() |
| 133 |
rootDir := filepath.Join(parentDir, "root") |
| 134 |
require.NoError(t, os.Mkdir(rootDir, 0o750)) |
| 135 |
require.NoError(t, os.WriteFile(filepath.Join(parentDir, "outside.conf"), []byte("outside"), 0o600)) |
| 136 |
require.NoError(t, os.WriteFile(filepath.Join(rootDir, "profile ä !.conf"), []byte("inside"), 0o600)) |
| 137 |
|
| 138 |
rootedFS, err := types.NewFS(rootDir) |
| 139 |
require.NoError(t, err) |
| 140 |
validFile, err := rootedFS.Open("profile ä !.conf") |
| 141 |
require.NoError(t, err) |
| 142 |
require.NoError(t, validFile.Close()) |
| 143 |
|
| 144 |
_, err = rootedFS.Open("../outside.conf") |
| 145 |
require.Error(t, err) |
| 146 |
|
| 147 |
if runtime.GOOS == "windows" { |
| 148 |
return |
| 149 |
} |
| 150 |
|
| 151 |
require.NoError(t, os.Symlink(filepath.Join(parentDir, "outside.conf"), filepath.Join(rootDir, "link.conf"))) |
| 152 |
|
| 153 |
_, err = rootedFS.Open("link.conf") |
| 154 |
require.Error(t, err) |
| 155 |
} |
| 156 |
|
| 157 |
func TestFSMarshalText(t *testing.T) { |
| 158 |
t.Parallel() |
| 159 |
|
| 160 |
dirFS, err := types.NewFS(".") |
| 161 |
require.NoError(t, err) |
| 162 |
|
| 163 |
path, err := dirFS.MarshalText() |
| 164 |
require.NoError(t, err) |
| 165 |
|
| 166 |
assert.Equal(t, []byte("."), path) |
| 167 |
} |
| 168 |
|
| 169 |
func TestFSMarshalJSON(t *testing.T) { |
| 170 |
t.Parallel() |
| 171 |
|
| 172 |
dirFS, err := types.NewFS(".") |
| 173 |
require.NoError(t, err) |
| 174 |
|
| 175 |
body, err := json.Marshal(&dirFS) |
| 176 |
require.NoError(t, err) |
| 177 |
|
| 178 |
assert.JSONEq(t, `"."`, string(body)) |
| 179 |
} |