Skip to content

Commit

Permalink
Fix invalid lengths #32
Browse files Browse the repository at this point in the history
  • Loading branch information
frozzare committed Jun 12, 2023
1 parent 11f7b7f commit 663c02b
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 6 deletions.
10 changes: 4 additions & 6 deletions personnummer.go
Original file line number Diff line number Diff line change
Expand Up @@ -211,12 +211,6 @@ func (p *Personnummer) parse(pin string, options *Options) error {

dateBytes := getCleanNumber(pin)

if len(dateBytes) == 0 || len(dateBytes) < 8 {
return errInvalidSecurityNumber
}

plus := strings.Contains(pin, "+")

switch len(dateBytes) {
case lengthWithCentury:
century = string(dateBytes[0:2])
Expand All @@ -231,6 +225,8 @@ func (p *Personnummer) parse(pin string, options *Options) error {
check = string(dateBytes[9:])
dateBytes = dateBytes[0:6]
break
default:
return errInvalidSecurityNumber
}

if num == "000" {
Expand All @@ -247,6 +243,8 @@ func (p *Personnummer) parse(pin string, options *Options) error {
}
}

plus := strings.Contains(pin, "+")

p.Century = century
p.Year = year
p.FullYear = toString(century + year)
Expand Down
7 changes: 7 additions & 0 deletions personnummer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,13 @@ func TestLuhn(t *testing.T) {
assert.False(t, luhn([]byte("12120111X3")))
}

func TestInvalidLengths(t *testing.T) {
numbers := []string{"", "1", "12", "123", "1234", "12345", "123456", "12345678", "123456789", "1234567891", "12345678911", "123456789111", "1234567891111"}
for _, n := range numbers {
assert.False(t, Valid(n))
}
}

func BenchmarkValid(b *testing.B) {
for i := 0; i < b.N; i++ {
Valid(testList[0].LongFormat)
Expand Down

0 comments on commit 663c02b

Please sign in to comment.