Skip to content

Commit

Permalink
check for an invalid id with a repeating reserved character
Browse files Browse the repository at this point in the history
  • Loading branch information
4kimov committed Aug 12, 2023
1 parent 1c48d90 commit cfc9842
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 1 deletion.
6 changes: 5 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# CHANGELOG

@todo
**v0.2.0:**
- Bug fix: test for decoding an invalid ID with a repeating reserved character

**v0.1.0:**
- Initial implementation of [the spec](https://github.com/sqids/sqids-spec)
11 changes: 11 additions & 0 deletions encoding_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -213,5 +213,16 @@ func TestEncodingInvalidCharacter(t *testing.T) {
}
}

func TestDecodeInvalidIDWithRepeatingReservedCharacter(t *testing.T) {
s, err := New()
if err != nil {
t.Fatal(err)
}

if !reflect.DeepEqual(s.Decode("fff"), []uint64{}) {
t.Errorf("Could not decode invalid ID with repeating reserved character")
}
}

// TestEncodingOutOfRange - no need since `[]uint64` handles ranges
// func TestEncodingOutOfRange(t *testing.T) {}
9 changes: 9 additions & 0 deletions sqids.go
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,15 @@ func (s *Sqids) Decode(id string) []uint64 {

if len(chunks) > 0 {
alphabetWithoutSeparator := alphabet[:len(alphabet)-1]
charSet := make(map[rune]bool)
for _, c := range alphabetWithoutSeparator {
charSet[c] = true
}
for _, c := range chunks[0] {
if _, exists := charSet[c]; !exists {
return []uint64{}
}
}
ret = append(ret, toNumber(chunks[0], alphabetWithoutSeparator))

if len(chunks) > 1 {
Expand Down

0 comments on commit cfc9842

Please sign in to comment.