@@ -12,9 +12,11 @@ import (
1212 "image"
1313 "image/jpeg"
1414 "image/png"
15+ "net"
1516 "os"
1617 "path/filepath"
1718 "reflect"
19+ "runtime"
1820 "strings"
1921 "testing"
2022 "time"
@@ -3016,6 +3018,98 @@ func TestUnixAddrValidation(t *testing.T) {
30163018 }
30173019}
30183020
3021+ func TestUnixDomainSocketExistsValidation (t * testing.T ) {
3022+ if runtime .GOOS == "windows" {
3023+ t .Skip ("Unix domain sockets are not supported on Windows" )
3024+ }
3025+
3026+ validate := New ()
3027+
3028+ t .Run ("empty" , func (t * testing.T ) {
3029+ errs := validate .Var ("" , "uds_exists" )
3030+ NotEqual (t , errs , nil )
3031+ AssertError (t , errs , "" , "" , "" , "" , "uds_exists" )
3032+ })
3033+
3034+ t .Run ("non_existent" , func (t * testing.T ) {
3035+ errs := validate .Var ("/tmp/nonexistent.sock" , "uds_exists" )
3036+ NotEqual (t , errs , nil )
3037+ AssertError (t , errs , "" , "" , "" , "" , "uds_exists" )
3038+ })
3039+
3040+ t .Run ("sock_file" , func (t * testing.T ) {
3041+ sockPath := "/tmp/test_validator.sock"
3042+ var lc net.ListenConfig
3043+ listener , err := lc .Listen (t .Context (), "unix" , sockPath )
3044+ if err != nil {
3045+ t .Fatalf ("Failed to create test socket: %v" , err )
3046+ }
3047+ defer func () {
3048+ _ = os .Remove (sockPath )
3049+ _ = listener .Close ()
3050+ }()
3051+
3052+ errs := validate .Var (sockPath , "uds_exists" )
3053+ Equal (t , errs , nil )
3054+ })
3055+
3056+ t .Run ("regular_file" , func (t * testing.T ) {
3057+ regularFile := "/tmp/test_validator_regular.txt"
3058+ if err := os .WriteFile (regularFile , []byte ("test" ), 0644 ); err != nil {
3059+ t .Fatalf ("Failed to create regular file: %v" , err )
3060+ }
3061+ defer func () {
3062+ _ = os .Remove (regularFile )
3063+ }()
3064+
3065+ errs := validate .Var (regularFile , "uds_exists" )
3066+ NotEqual (t , errs , nil )
3067+ AssertError (t , errs , "" , "" , "" , "" , "uds_exists" )
3068+ })
3069+
3070+ t .Run ("directory" , func (t * testing.T ) {
3071+ dirPath := "/tmp/test_validator_dir"
3072+ if err := os .Mkdir (dirPath , 0755 ); err != nil && ! os .IsExist (err ) {
3073+ t .Fatalf ("Failed to create directory: %v" , err )
3074+ }
3075+ defer func () {
3076+ _ = os .RemoveAll (dirPath )
3077+ }()
3078+
3079+ errs := validate .Var (dirPath , "uds_exists" )
3080+ NotEqual (t , errs , nil )
3081+ AssertError (t , errs , "" , "" , "" , "" , "uds_exists" )
3082+ })
3083+
3084+ // only supported on linux
3085+ t .Run ("abstract_sockets" , func (t * testing.T ) {
3086+ if runtime .GOOS != "linux" {
3087+ return
3088+ }
3089+
3090+ t .Run ("non_existent" , func (t * testing.T ) {
3091+ errs := validate .Var ("@nonexistent_abstract_socket" , "uds_exists" )
3092+ NotEqual (t , errs , nil )
3093+ AssertError (t , errs , "" , "" , "" , "" , "uds_exists" )
3094+ })
3095+
3096+ t .Run ("existing" , func (t * testing.T ) {
3097+ abstractSockName := "@test_abstract_socket_" + fmt .Sprintf ("%d" , time .Now ().UnixNano ())
3098+ var lc net.ListenConfig
3099+ abstractListener , err := lc .Listen (t .Context (), "unix" , "\x00 " + abstractSockName [1 :])
3100+ if err != nil {
3101+ t .Fatalf ("Failed to create abstract socket: %v" , err )
3102+ }
3103+ defer func () {
3104+ _ = abstractListener .Close ()
3105+ }()
3106+
3107+ errs := validate .Var (abstractSockName , "uds_exists" )
3108+ Equal (t , errs , nil )
3109+ })
3110+ })
3111+ }
3112+
30193113func TestSliceMapArrayChanFuncPtrInterfaceRequiredValidation (t * testing.T ) {
30203114 validate := New ()
30213115
0 commit comments