github.com/mckael/restic@v0.8.3/internal/backend/sftp/config_test.go (about) 1 package sftp 2 3 import "testing" 4 5 var configTests = []struct { 6 in string 7 cfg Config 8 }{ 9 // first form, user specified sftp://user@host/dir 10 { 11 "sftp://user@host/dir/subdir", 12 Config{User: "user", Host: "host", Path: "dir/subdir"}, 13 }, 14 { 15 "sftp://host/dir/subdir", 16 Config{Host: "host", Path: "dir/subdir"}, 17 }, 18 { 19 "sftp://host//dir/subdir", 20 Config{Host: "host", Path: "/dir/subdir"}, 21 }, 22 { 23 "sftp://host:10022//dir/subdir", 24 Config{Host: "host:10022", Path: "/dir/subdir"}, 25 }, 26 { 27 "sftp://user@host:10022//dir/subdir", 28 Config{User: "user", Host: "host:10022", Path: "/dir/subdir"}, 29 }, 30 { 31 "sftp://user@host/dir/subdir/../other", 32 Config{User: "user", Host: "host", Path: "dir/other"}, 33 }, 34 { 35 "sftp://user@host/dir///subdir", 36 Config{User: "user", Host: "host", Path: "dir/subdir"}, 37 }, 38 39 // second form, user specified sftp:user@host:/dir 40 { 41 "sftp:user@host:/dir/subdir", 42 Config{User: "user", Host: "host", Path: "/dir/subdir"}, 43 }, 44 { 45 "sftp:host:../dir/subdir", 46 Config{Host: "host", Path: "../dir/subdir"}, 47 }, 48 { 49 "sftp:user@host:dir/subdir:suffix", 50 Config{User: "user", Host: "host", Path: "dir/subdir:suffix"}, 51 }, 52 { 53 "sftp:user@host:dir/subdir/../other", 54 Config{User: "user", Host: "host", Path: "dir/other"}, 55 }, 56 { 57 "sftp:user@host:dir///subdir", 58 Config{User: "user", Host: "host", Path: "dir/subdir"}, 59 }, 60 } 61 62 func TestParseConfig(t *testing.T) { 63 for i, test := range configTests { 64 cfg, err := ParseConfig(test.in) 65 if err != nil { 66 t.Errorf("test %d:%s failed: %v", i, test.in, err) 67 continue 68 } 69 70 if cfg != test.cfg { 71 t.Errorf("test %d:\ninput:\n %s\n wrong config, want:\n %v\ngot:\n %v", 72 i, test.in, test.cfg, cfg) 73 continue 74 } 75 } 76 } 77 78 var configTestsInvalid = []string{ 79 "sftp://host:dir", 80 } 81 82 func TestParseConfigInvalid(t *testing.T) { 83 for i, test := range configTestsInvalid { 84 _, err := ParseConfig(test) 85 if err == nil { 86 t.Errorf("test %d: invalid config %s did not return an error", i, test) 87 continue 88 } 89 } 90 }