Add 'twofactor/' from commit 'c999bf35b0e47de4f63d59abbe0d7efc76c13ced'
git-subtree-dir: twofactor git-subtree-mainline:4dc135cfe0git-subtree-split:c999bf35b0
This commit is contained in:
53
twofactor/util_test.go
Normal file
53
twofactor/util_test.go
Normal file
@@ -0,0 +1,53 @@
|
||||
package twofactor
|
||||
|
||||
import (
|
||||
"encoding/base32"
|
||||
"fmt"
|
||||
"math/rand"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
const letters = "1234567890!@#$%^&*()abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
|
||||
|
||||
func randString() string {
|
||||
b := make([]byte, rand.Intn(len(letters)))
|
||||
for i := range b {
|
||||
b[i] = letters[rand.Intn(len(letters))]
|
||||
}
|
||||
return base32.StdEncoding.EncodeToString(b)
|
||||
}
|
||||
|
||||
func TestPadding(t *testing.T) {
|
||||
for i := 0; i < 300; i++ {
|
||||
b := randString()
|
||||
origEncoding := string(b)
|
||||
modEncoding := strings.ReplaceAll(string(b), "=", "")
|
||||
str, err := base32.StdEncoding.DecodeString(origEncoding)
|
||||
if err != nil {
|
||||
fmt.Println("Can't decode: ", string(b))
|
||||
t.FailNow()
|
||||
}
|
||||
|
||||
paddedEncoding := Pad(modEncoding)
|
||||
if origEncoding != paddedEncoding {
|
||||
fmt.Println("Padding failed:")
|
||||
fmt.Printf("Expected: '%s'", origEncoding)
|
||||
fmt.Printf("Got: '%s'", paddedEncoding)
|
||||
t.FailNow()
|
||||
} else {
|
||||
mstr, err := base32.StdEncoding.DecodeString(paddedEncoding)
|
||||
if err != nil {
|
||||
fmt.Println("Can't decode: ", paddedEncoding)
|
||||
t.FailNow()
|
||||
}
|
||||
|
||||
if string(mstr) != string(str) {
|
||||
fmt.Println("Re-padding failed:")
|
||||
fmt.Printf("Expected: '%s'", str)
|
||||
fmt.Printf("Got: '%s'", mstr)
|
||||
t.FailNow()
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user