2014-11-16 20:13:20 +00:00
|
|
|
// Copyright (C) 2014 The Syncthing Authors.
|
2014-09-29 19:43:32 +00:00
|
|
|
//
|
|
|
|
// This program is free software: you can redistribute it and/or modify it
|
|
|
|
// under the terms of the GNU General Public License as published by the Free
|
|
|
|
// Software Foundation, either version 3 of the License, or (at your option)
|
|
|
|
// any later version.
|
|
|
|
//
|
|
|
|
// This program is distributed in the hope that it will be useful, but WITHOUT
|
|
|
|
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
|
|
// FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
|
|
|
|
// more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU General Public License along
|
|
|
|
// with this program. If not, see <http://www.gnu.org/licenses/>.
|
2014-09-04 06:31:38 +00:00
|
|
|
|
2014-06-29 23:42:03 +00:00
|
|
|
package luhn_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
2014-09-22 19:42:11 +00:00
|
|
|
"github.com/syncthing/syncthing/internal/luhn"
|
2014-06-29 23:42:03 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestGenerate(t *testing.T) {
|
2014-07-04 13:56:33 +00:00
|
|
|
// Base 6 Luhn
|
2014-06-29 23:42:03 +00:00
|
|
|
a := luhn.Alphabet("abcdef")
|
2014-07-04 14:16:50 +00:00
|
|
|
c, err := a.Generate("abcdef")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2014-06-29 23:42:03 +00:00
|
|
|
if c != 'e' {
|
|
|
|
t.Errorf("Incorrect check digit %c != e", c)
|
|
|
|
}
|
2014-07-04 13:56:33 +00:00
|
|
|
|
|
|
|
// Base 10 Luhn
|
|
|
|
a = luhn.Alphabet("0123456789")
|
2014-07-04 14:16:50 +00:00
|
|
|
c, err = a.Generate("7992739871")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2014-07-04 13:56:33 +00:00
|
|
|
if c != '3' {
|
|
|
|
t.Errorf("Incorrect check digit %c != 3", c)
|
|
|
|
}
|
2014-06-29 23:42:03 +00:00
|
|
|
}
|
|
|
|
|
2014-07-04 14:16:50 +00:00
|
|
|
func TestInvalidString(t *testing.T) {
|
|
|
|
a := luhn.Alphabet("ABC")
|
|
|
|
_, err := a.Generate("7992739871")
|
|
|
|
t.Log(err)
|
|
|
|
if err == nil {
|
|
|
|
t.Error("Unexpected nil error")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestBadAlphabet(t *testing.T) {
|
|
|
|
a := luhn.Alphabet("01234566789")
|
|
|
|
_, err := a.Generate("7992739871")
|
|
|
|
t.Log(err)
|
|
|
|
if err == nil {
|
|
|
|
t.Error("Unexpected nil error")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-06-29 23:42:03 +00:00
|
|
|
func TestValidate(t *testing.T) {
|
|
|
|
a := luhn.Alphabet("abcdef")
|
|
|
|
if !a.Validate("abcdefe") {
|
|
|
|
t.Errorf("Incorrect validation response for abcdefe")
|
|
|
|
}
|
|
|
|
if a.Validate("abcdefd") {
|
|
|
|
t.Errorf("Incorrect validation response for abcdefd")
|
|
|
|
}
|
|
|
|
}
|