github.com/google/osv-scalibr@v0.4.1/enricher/govulncheck/source/internal/url/url_windows_test.go (about) 1 // Copyright 2025 Google LLC 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 // Derived from https://github.com/golang/go/blob/7c2b69080a0b9e35174cc9c93497b6e7176f8275/src/cmd/go/internal/web/url.go 16 // TODO(golang.org/issue/32456): If accepted, move these functions into the 17 // net/url package. 18 // 19 // Copyright 2023 The Go Authors. All rights reserved. 20 // Use of this source code is governed by a BSD-style 21 // license that can be found in the LICENSE file. 22 //go:build windows 23 24 package url_test 25 26 // Code copied from https://github.com/golang/go/blob/7c2b69080a0b9e35174cc9c93497b6e7176f8275/src/cmd/go/internal/web/url_windows_test.go 27 28 var urlTests = []struct { 29 url string 30 filePath string 31 canonicalURL string // If empty, assume equal to url. 32 wantErr string 33 }{ 34 // Examples from https://blogs.msdn.microsoft.com/ie/2006/12/06/file-uris-in-windows/: 35 36 { 37 url: `file://laptop/My%20Documents/FileSchemeURIs.doc`, 38 filePath: `\\laptop\My Documents\FileSchemeURIs.doc`, 39 }, 40 { 41 url: `file:///C:/Documents%20and%20Settings/davris/FileSchemeURIs.doc`, 42 filePath: `C:\Documents and Settings\davris\FileSchemeURIs.doc`, 43 }, 44 { 45 url: `file:///D:/Program%20Files/Viewer/startup.htm`, 46 filePath: `D:\Program Files\Viewer\startup.htm`, 47 }, 48 { 49 url: `file:///C:/Program%20Files/Music/Web%20Sys/main.html?REQUEST=RADIO`, 50 filePath: `C:\Program Files\Music\Web Sys\main.html`, 51 canonicalURL: `file:///C:/Program%20Files/Music/Web%20Sys/main.html`, 52 }, 53 { 54 url: `file://applib/products/a-b/abc_9/4148.920a/media/start.swf`, 55 filePath: `\\applib\products\a-b\abc_9\4148.920a\media\start.swf`, 56 }, 57 { 58 url: `file:////applib/products/a%2Db/abc%5F9/4148.920a/media/start.swf`, 59 wantErr: "file URL missing drive letter", 60 }, 61 { 62 url: `C:\Program Files\Music\Web Sys\main.html?REQUEST=RADIO`, 63 wantErr: "non-file URL", 64 }, 65 66 // The example "file://D:\Program Files\Viewer\startup.htm" errors out in 67 // url.Parse, so we substitute a slash-based path for testing instead. 68 { 69 url: `file://D:/Program Files/Viewer/startup.htm`, 70 wantErr: "file URL encodes volume in host field: too few slashes?", 71 }, 72 73 // The blog post discourages the use of non-ASCII characters because they 74 // depend on the user's current codepage. However, when we are working with Go 75 // strings we assume UTF-8 encoding, and our url package refuses to encode 76 // URLs to non-ASCII strings. 77 { 78 url: `file:///C:/exampleㄓ.txt`, 79 filePath: `C:\exampleㄓ.txt`, 80 canonicalURL: `file:///C:/example%E3%84%93.txt`, 81 }, 82 { 83 url: `file:///C:/example%E3%84%93.txt`, 84 filePath: `C:\exampleㄓ.txt`, 85 }, 86 87 // Examples from RFC 8089: 88 89 // We allow the drive-letter variation from section E.2, because it is 90 // simpler to support than not to. However, we do not generate the shorter 91 // form in the reverse direction. 92 { 93 url: `file:c:/path/to/file`, 94 filePath: `c:\path\to\file`, 95 canonicalURL: `file:///c:/path/to/file`, 96 }, 97 98 // We encode the UNC share name as the authority following section E.3.1, 99 // because that is what the Microsoft blog post explicitly recommends. 100 { 101 url: `file://host.example.com/Share/path/to/file.txt`, 102 filePath: `\\host.example.com\Share\path\to\file.txt`, 103 }, 104 105 // We decline the four- and five-slash variations from section E.3.2. 106 // The paths in these URLs would change meaning under path.Clean. 107 { 108 url: `file:////host.example.com/path/to/file`, 109 wantErr: "file URL missing drive letter", 110 }, 111 { 112 url: `file://///host.example.com/path/to/file`, 113 wantErr: "file URL missing drive letter", 114 }, 115 }