Merge pull request #2243 from treymo/ls-length-2104

backend find: Check prefix length < snapshot IDs when searching
This commit is contained in:
Alexander Neumann 2019-04-23 09:41:19 +02:00
commit 18ec27a0da
2 changed files with 52 additions and 1 deletions

View file

@ -24,7 +24,7 @@ func Find(be Lister, t FileType, prefix string) (string, error) {
defer cancel()
err := be.List(ctx, t, func(fi FileInfo) error {
if prefix == fi.Name[:len(prefix)] {
if len(fi.Name) >= len(prefix) && prefix == fi.Name[:len(prefix)] {
if match == "" {
match = fi.Name
} else {

View file

@ -24,6 +24,57 @@ var samples = IDs{
TestParseID("fa31d65b87affcd167b119e9d3d2a27b8236ca4836cb077ed3e96fcbe209b792"),
}
func TestFind(t *testing.T) {
list := samples
m := mockBackend{}
m.list = func(ctx context.Context, t FileType, fn func(FileInfo) error) error {
for _, id := range list {
err := fn(FileInfo{Name: id.String()})
if err != nil {
return err
}
}
return nil
}
f, err := Find(m, SnapshotFile, "20bdc1402a6fc9b633aa")
if err != nil {
t.Error(err)
}
expected_match := "20bdc1402a6fc9b633aaffffffffffffffffffffffffffffffffffffffffffff"
if f != expected_match {
t.Errorf("Wrong match returned want %s, got %s", expected_match, f)
}
f, err = Find(m, SnapshotFile, "NotAPrefix")
if err != ErrNoIDPrefixFound {
t.Error("Expected no snapshots to be found.")
}
if f != "" {
t.Errorf("Find should not return a match on error.")
}
// Try to match with a prefix longer than any ID.
extra_length_id := samples[0].String() + "f"
f, err = Find(m, SnapshotFile, extra_length_id)
if err != ErrNoIDPrefixFound {
t.Error("Expected no snapshots to be matched.")
}
if f != "" {
t.Errorf("Find should not return a match on error.")
}
// Use a prefix that will match the prefix of multiple Ids in `samples`.
f, err = Find(m, SnapshotFile, "20bdc140")
if err != ErrMultipleIDMatches {
t.Error("Expected multiple snapshots to be matched.")
}
if f != "" {
t.Errorf("Find should not return a match on error.")
}
}
func TestPrefixLength(t *testing.T) {
list := samples