GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing github.com — locks/config_test.go at master · asciimoth/locks Expand file tree/ Copy pathconfig_test.goMore file actions 188 lines (181 loc) · 7.3 KB / Copy pathconfig_test.goFile metadata and controls188 lines (181 loc) · 7.3 KB 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 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 package locks import ( "reflect" "runtime" "strings" "testing" ) func pathSeeds() []string { return []string{"", ";;", "HTTP://example.com/a", "check=Https://example.com/a", "/a;/b;ro=/c;check=https://example.com/team", "/a=/b", "check,ro=\"https://example.com/?label=a;b\"", "\"/a;b\"", "\"/a\"\"b\"", "ro,check=/a", "/a;/a;ro=/a", "ro,ro=/a", "check,check=/a", "bad=/a", ",ro=/a", "ro,=/a", "ro=", "relative", "\"\"", "\"/a", "\"/a\"x", "/a\"b", " ;/a", "ro=/a;;", `C:\a;ro=\\server\share\locks`, `C:relative`, `C:`, `\\?\C:\a`, `\\.\a`, `\\host\share`, `\\host`, `\\host\`, `\rooted`, "https://example.com/?x=a=b", "ro=https://example.com/?key=x"} } func endpointSeeds() []string { return []string{"https://example.com/foo/bar/?tenant=west#note", "https://example.com/a%2Fb/%2F/?x=%2f", "http://[::1]:123/a", "http://example.com", "https://example.com/?key=x", "https://example.com/?%6bey=x", "https://example.com/?other=%zz", "https://example.com/?%zz=x", "https://a:[email protected]/a", "ftp://example.com/a", "/relative", "http:", "http:///x", "https://%zz/", "http://x/%zz", "https://x/a?x=+", "http://x/?key=%zz"} } func TestPath(t *testing.T) { for _, s := range pathSeeds() { for _, os := range []string{"linux", "windows"} { _, _ = ParsePathForOS(s, os) } } p, e := ParsePathForOS("/a;/a;ro=/a;check,ro=\"https://example.com/a;b\";;", "linux") if e != nil || len(p) != 3 || !p[2].CheckOnly || !p[2].ReadOnly { t.Fatal(p, e) } p, e = ParsePathForOS(`C:\work\locks;ro=\\fileserver\team\locks`, "windows") if e != nil || len(p) != 2 { t.Fatal(p, e) } for _, s := range []string{`C:relative`, `C:`, `\\?\C:\a`, `\\.\a`, `\\host`, `\\host\`, `\rooted`, `/posix`, `\\.\share`, `\\host\..`} { if _, e = ParsePathForOS(s, "windows"); e == nil { t.Fatal(s) } } for _, s := range []string{"bad=/a", "ro,ro=/a", "check,check=/a", "ro=", `"/a"x`, `""`, `"/a`, "relative", "/a\"b", "ro=/a;check=", "/a\x00"} { if _, e = ParsePathForOS(s, "linux"); e == nil { t.Fatal(s) } } } func TestDiscoverNilLookup(t *testing.T) { _, err := Discover("", nil) requireErr(t, err, ErrFormat) } func TestEndpoint(t *testing.T) { for _, s := range endpointSeeds() { _, _ = Endpoint(s) } for _, tt := range []struct{ base, want string }{{"https://example.com/foo/bar/?tenant=west#note", "https://example.com/foo/bar/lock?tenant=west"}, {"https://example.com/a%2Fb/%2F/?x=%2f", "https://example.com/a%2Fb/%2F/lock?x=%2f"}} { got, e := Endpoint(tt.base) if e != nil || got != tt.want { t.Fatal(got, e) } } keys, e := queryKeys("x=%zz&key=raw.blake2.YWJj&key=a+b&%6bey=a%2520b") if e != nil || strings.Join(keys, "|") != "raw.blake2.YWJj|a+b|a%20b" { t.Fatal(keys, e) } } func TestDiscover(t *testing.T) { if runtime.GOOS != "linux" { t.Skip("Linux defaults") } get := func(vars map[string]string) func(string) (string, bool) { return func(k string) (string, bool) { s, ok := vars[k]; return s, ok } } for _, tt := range []struct { env map[string]string project string want string }{{map[string]string{"LOCK_PATH": ""}, "/p", ""}, {map[string]string{"LOCK_PATH": "/explicit"}, "/p", "/explicit"}, {map[string]string{"HOME": "/home/a"}, "/p", "/p/.lock/locks;/home/a/.cache/locks;/var/lib/locks/locks"}, {map[string]string{"HOME": "/home/a", "XDG_CACHE_HOME": "relative"}, "", "/home/a/.cache/locks;/var/lib/locks/locks"}, {map[string]string{"XDG_CACHE_HOME": "/cache"}, "", "/cache/locks;/var/lib/locks/locks"}, {map[string]string{}, "", "/var/lib/locks/locks"}} { p, e := Discover(tt.project, get(tt.env)) var ss []string for _, x := range p { ss = append(ss, x.Address) } if e != nil || strings.Join(ss, ";") != tt.want { t.Fatal(p, e) } } _, e := Discover("relative", get(nil)) requireErr(t, e, ErrFormat) _, e = Discover("/tmp/\x00/../project", get(nil)) requireErr(t, e, ErrFormat) } func TestOtherPlatformDefaults(t *testing.T) { env := func(k string) (string, bool) { if k == "HOME" { return "/home/a", true } return "", false } for _, os := range []string{"darwin", "freebsd", "openbsd", "netbsd", "dragonfly", "windows"} { t.Run(os, func(t *testing.T) { if runtime.GOOS == "windows" && os != "windows" { t.Skip("discovery uses native path joins; POSIX defaults need a POSIX host") } for _, folders := range []func() (string, string){func() (string, string) { return `C:\Users\a\AppData\Local`, `C:\ProgramData` }, func() (string, string) { return "", "" }, knownFolders} { p, e := discover(os, "", env, folders) if e != nil { t.Fatal(e) } for _, loc := range p { if !validLocation(loc.Address, os) { t.Fatal(loc) } } } }) } for _, s := range []string{`//?/C:/a`, `\\?/C:/a`, `//./C:/a`} { if validLocation(s, "windows") { t.Fatal("device alias", s) } } if _, e := Endpoint("http://:80"); e == nil { t.Fatal("empty host") } } func TestDiscoverRejectsInvalidDefaultBases(t *testing.T) { if runtime.GOOS == "windows" { t.Skip("POSIX environment paths") } for _, tt := range []struct { name, goos, home, cache, want string }{ {"home-nul", "linux", "/tmp/\x00/../home", "", "/var/lib/locks/locks"}, {"cache-nul", "linux", "/home/a", "/tmp/\x00/../cache", "/home/a/.cache/locks;/var/lib/locks/locks"}, {"mac-home-nul", "darwin", "/tmp/\x00/../home", "", "/Library/Application Support/Locks/locks"}, } { t.Run(tt.name, func(t *testing.T) { getenv := func(key string) (string, bool) { value, ok := map[string]string{"HOME": tt.home, "XDG_CACHE_HOME": tt.cache}[key] return value, ok } locations, err := discover(tt.goos, "", getenv, knownFolders) var addresses []string for _, loc := range locations { addresses = append(addresses, loc.Address) } if err != nil || strings.Join(addresses, ";") != tt.want { t.Fatal(locations, err) } }) } } func TestWindowsDefaultFolders(t *testing.T) { for _, tt := range []struct { name, user, host string want []string }{ {"both", `C:\Users\a\AppData\Local`, `C:\ProgramData`, []string{"C:/Users/a/AppData/Local/Locks/locks", "C:/ProgramData/Locks/locks"}}, {"user-only", `C:\Users\a\AppData\Local`, "", []string{"C:/Users/a/AppData/Local/Locks/locks"}}, {"host-only", "", `C:\ProgramData`, []string{"C:/ProgramData/Locks/locks"}}, {"unc", `\\server\share\user`, `\\server\share\host`, []string{"//server/share/user/Locks/locks", "//server/share/host/Locks/locks"}}, {"absent", "", "", nil}, {"relative", `C:user`, `host`, nil}, {"nul", "C:/tmp/\x00/../user", "C:/tmp/\x00/../host", nil}, } { t.Run(tt.name, func(t *testing.T) { locations, err := discover("windows", "", func(string) (string, bool) { return "", false }, func() (string, string) { return tt.user, tt.host }) var addresses []string for _, loc := range locations { if !validLocation(loc.Address, "windows") || loc.CheckOnly || loc.ReadOnly { t.Fatalf("invalid default location: %+v", loc) } addresses = append(addresses, strings.ReplaceAll(loc.Address, `\`, "/")) } if err != nil || !reflect.DeepEqual(addresses, tt.want) { t.Fatalf("locations = %v, error = %v; want %v", addresses, err, tt.want) } }) } } func TestHTTPSchemeCase(t *testing.T) { for _, os := range []string{"linux", "windows"} { locations, e := ParsePathForOS("HTTP://example.com/a;check=Https://example.com/b", os) if e != nil || len(locations) != 2 { t.Fatal(locations, e) } } } |
| You are visitor 01402792 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |