diff options
| author | Ophestra <cat@gensokyo.uk> | 2025-10-03 16:42:42 +0900 |
|---|---|---|
| committer | Ophestra <cat@gensokyo.uk> | 2025-10-03 16:42:42 +0900 |
| commit | dd0bb0a39104a54c26015df807c1eb113331c242 (patch) | |
| tree | 34af5859fec563e82d36b4b863b42d4deeeb64ba /internal | |
| parent | d16da6da8c4880d90dc35fa9006806f7c0395d7f (diff) | |
internal/app: check username validation
This stuff should be hardcoded in libc, but check it anyway.
Signed-off-by: Ophestra <cat@gensokyo.uk>
Diffstat (limited to 'internal')
| -rw-r--r-- | internal/app/sysconf_test.go | 15 | ||||
| -rw-r--r-- | internal/app/username_test.go | 26 |
2 files changed, 41 insertions, 0 deletions
diff --git a/internal/app/sysconf_test.go b/internal/app/sysconf_test.go new file mode 100644 index 00000000..b73f2d08 --- /dev/null +++ b/internal/app/sysconf_test.go @@ -0,0 +1,15 @@ +package app + +import "testing" + +const ( + _POSIX_LOGIN_NAME_MAX = 9 +) + +func TestSysconf(t *testing.T) { + t.Run("LOGIN_NAME_MAX", func(t *testing.T) { + if got := sysconf(_SC_LOGIN_NAME_MAX); got < _POSIX_LOGIN_NAME_MAX { + t.Errorf("sysconf(_SC_LOGIN_NAME_MAX): %d < _POSIX_LOGIN_NAME_MAX", got) + } + }) +} diff --git a/internal/app/username_test.go b/internal/app/username_test.go new file mode 100644 index 00000000..15f15a4c --- /dev/null +++ b/internal/app/username_test.go @@ -0,0 +1,26 @@ +package app + +import ( + "strings" + "testing" +) + +func TestIsValidUsername(t *testing.T) { + t.Run("long", func(t *testing.T) { + if isValidUsername(strings.Repeat("a", sysconf(_SC_LOGIN_NAME_MAX))) { + t.Errorf("isValidUsername unexpected true") + } + }) + + t.Run("regexp", func(t *testing.T) { + if isValidUsername("0") { + t.Errorf("isValidUsername unexpected true") + } + }) + + t.Run("valid", func(t *testing.T) { + if !isValidUsername("alice") { + t.Errorf("isValidUsername unexpected false") + } + }) +} |
