eclipse/lib/gitcache/giturls_test.go

73 lines
1.9 KiB
Go
Raw Permalink Normal View History

// Copyright (C) 2024 Umorpha Systems
// SPDX-License-Identifier: AGPL-3.0-or-later
package gitcache
import (
"net/url"
"strings"
"testing"
"github.com/stretchr/testify/assert"
)
var normalizeURLTestcases = map[string]string{
"httpS://bogus@GitHub.com/LukeShu/repo.git/#foo": "https://github.com/LukeShu/repo.git",
"gopher://bogus@GitHub.com/LukeShu/repo.git/": "",
"git@lukeshu.com:repo.git": "ssh://git@lukeshu.com/~/repo.git",
"git@lukeshu.com:~/repo.git": "ssh://git@lukeshu.com/~/repo.git",
"git@lukeshu.com:~other/repo.git": "ssh://git@lukeshu.com/~other/repo.git",
"ssh://": "",
"/": "file:///",
"http://example.com/daemon.cgi/?svc=git&q=/": "http://example.com/daemon.cgi?svc=git&q=",
}
func TestNormalizeURL(t *testing.T) {
t.Parallel()
for in, expOut := range normalizeURLTestcases {
in := in
expOut := expOut
t.Run(in, func(t *testing.T) {
t.Parallel()
t.Logf("in = %q", in)
actOut, ok := NormalizeURL(in)
assert.Equal(t, expOut != "", ok)
assert.Equal(t, expOut, actOut)
})
}
}
func urlScheme(url string) (string, bool) {
if !reIsURL.MatchString(url) {
return "", false
}
scheme, _, _ := strings.Cut(url, ":")
return strings.ToLower(scheme), true
}
func FuzzNormalizeURL(f *testing.F) {
for in, out := range normalizeURLTestcases {
f.Add(in)
f.Add(out)
}
f.Fuzz(func(t *testing.T, in string) {
t.Logf("in = %q", in)
out, ok := NormalizeURL(in)
if !ok {
assert.Equal(t, "", out)
return
}
u, err := url.Parse(out)
assert.NoError(t, err)
if origScheme, ok := urlScheme(in); ok {
assert.Equal(t, origScheme, u.Scheme)
}
if out != in {
out2, ok := NormalizeURL(out)
assert.True(t, ok, "output is not valid")
assert.Equal(t, out, out2, "output is not stable")
}
})
}