location: extract backend specific part of StripPassword

The tests for the rest backend now reside there.
This commit is contained in:
Michael Eischer 2023-06-08 15:17:00 +02:00
parent 555be49a79
commit 3325a7c862
2 changed files with 92 additions and 91 deletions

View file

@ -1,96 +1,29 @@
package location package location_test
import "testing" import (
"testing"
var passwordTests = []struct { "github.com/restic/restic/internal/backend/location"
input string "github.com/restic/restic/internal/restic"
expected string "github.com/restic/restic/internal/test"
}{ )
{
"local:/srv/repo",
"local:/srv/repo",
},
{
"/dir1/dir2",
"/dir1/dir2",
},
{
`c:\dir1\foobar\dir2`,
`c:\dir1\foobar\dir2`,
},
{
"sftp:user@host:/srv/repo",
"sftp:user@host:/srv/repo",
},
{
"s3://eu-central-1/bucketname",
"s3://eu-central-1/bucketname",
},
{
"swift:container17:/prefix97",
"swift:container17:/prefix97",
},
{
"b2:bucketname:/prefix",
"b2:bucketname:/prefix",
},
{
"rest:",
"rest:/",
},
{
"rest:localhost/",
"rest:localhost/",
},
{
"rest::123/",
"rest::123/",
},
{
"rest:http://",
"rest:http://",
},
{
"rest:http://hostname.foo:1234/",
"rest:http://hostname.foo:1234/",
},
{
"rest:http://user@hostname.foo:1234/",
"rest:http://user@hostname.foo:1234/",
},
{
"rest:http://user:@hostname.foo:1234/",
"rest:http://user:***@hostname.foo:1234/",
},
{
"rest:http://user:p@hostname.foo:1234/",
"rest:http://user:***@hostname.foo:1234/",
},
{
"rest:http://user:pppppaaafhhfuuwiiehhthhghhdkjaoowpprooghjjjdhhwuuhgjsjhhfdjhruuhsjsdhhfhshhsppwufhhsjjsjs@hostname.foo:1234/",
"rest:http://user:***@hostname.foo:1234/",
},
{
"rest:http://user:password@hostname",
"rest:http://user:***@hostname/",
},
{
"rest:http://user:password@:123",
"rest:http://user:***@:123/",
},
{
"rest:http://user:password@",
"rest:http://user:***@/",
},
}
func TestStripPassword(t *testing.T) { func TestStripPassword(t *testing.T) {
for i, test := range passwordTests { registry := location.NewRegistry()
t.Run(test.input, func(t *testing.T) { registry.Register("test",
result := StripPassword(test.input) location.NewHTTPBackendFactory[any, restic.Backend](nil,
if result != test.expected { func(s string) string {
t.Errorf("test %d: expected '%s' but got '%s'", i, test.expected, result) return "cleaned"
} }, nil, nil,
}) ),
} )
t.Run("valid", func(t *testing.T) {
clean := location.StripPassword(registry, "test:secret")
test.Equals(t, "cleaned", clean)
})
t.Run("unknown", func(t *testing.T) {
clean := location.StripPassword(registry, "invalid:secret")
test.Equals(t, "invalid:secret", clean)
})
} }

View file

@ -36,3 +36,71 @@ var configTests = []test.ConfigTestData[Config]{
func TestParseConfig(t *testing.T) { func TestParseConfig(t *testing.T) {
test.ParseConfigTester(t, ParseConfig, configTests) test.ParseConfigTester(t, ParseConfig, configTests)
} }
var passwordTests = []struct {
input string
expected string
}{
{
"rest:",
"rest:/",
},
{
"rest:localhost/",
"rest:localhost/",
},
{
"rest::123/",
"rest::123/",
},
{
"rest:http://",
"rest:http://",
},
{
"rest:http://hostname.foo:1234/",
"rest:http://hostname.foo:1234/",
},
{
"rest:http://user@hostname.foo:1234/",
"rest:http://user@hostname.foo:1234/",
},
{
"rest:http://user:@hostname.foo:1234/",
"rest:http://user:***@hostname.foo:1234/",
},
{
"rest:http://user:p@hostname.foo:1234/",
"rest:http://user:***@hostname.foo:1234/",
},
{
"rest:http://user:pppppaaafhhfuuwiiehhthhghhdkjaoowpprooghjjjdhhwuuhgjsjhhfdjhruuhsjsdhhfhshhsppwufhhsjjsjs@hostname.foo:1234/",
"rest:http://user:***@hostname.foo:1234/",
},
{
"rest:http://user:password@hostname",
"rest:http://user:***@hostname/",
},
{
"rest:http://user:password@:123",
"rest:http://user:***@:123/",
},
{
"rest:http://user:password@",
"rest:http://user:***@/",
},
}
func TestStripPassword(t *testing.T) {
// Make sure that the factory uses the correct method
StripPassword := NewFactory().StripPassword
for i, test := range passwordTests {
t.Run(test.input, func(t *testing.T) {
result := StripPassword(test.input)
if result != test.expected {
t.Errorf("test %d: expected '%s' but got '%s'", i, test.expected, result)
}
})
}
}