1
0
mirror of https://github.com/getsops/sotp.git synced 2026-02-05 09:45:59 +01:00
Files
sotp/main_test.go
Hidde Beydals f082788430 Update Go to 1.19
This is the lowest stable version today, other versions are technically
at EOL. It includes some minor fixes to deal with changes in underlying
libraries.

Signed-off-by: Hidde Beydals <hidde@hhh.computer>
2023-06-29 23:40:45 +02:00

54 lines
1.4 KiB
Go

package main
import (
"regexp"
"testing"
"time"
"github.com/xlzd/gotp"
)
func TestAccountNameRe(t *testing.T) {
accountNames := []struct {
accountName string
valid bool
}{
{"foobar", true},
{"foo-_bar", true},
{"foo-bar-1337", true},
{"foo=bar", false},
{"foo*bar", false},
}
for _, testData := range accountNames {
isValid := regexp.MustCompile(accountNameRe).MatchString(testData.accountName)
if isValid && !testData.valid {
t.Fatalf("account name %q passes regexp verification %q but was expected to fail", testData.accountName, accountNameRe)
} else if !isValid && testData.valid {
t.Fatalf("account name %q does not pass regexp verification %q but was expected to pass", testData.accountName, accountNameRe)
}
}
}
func TestDecryptConfig(t *testing.T) {
cfg, err := decryptConfig("config.yaml")
if err != nil {
t.Fatal("failed to access configuration at 'config.yaml'", err)
}
if cfg.Accounts[0].Name != "test1" {
t.Fatalf("expected account name `test1` but got %q", cfg.Accounts[0].Name)
}
}
func TestGetTOTP(t *testing.T) {
var testAwsMfaSecret = "YAGQP5IP77OO3HMPS3D2KPMSNLNDIB7EO22EGAN3JEGE3DAR37Z2U5YDGKGN44VA"
otp := gotp.NewDefaultTOTP(testAwsMfaSecret)
ts, err := time.Parse("2006-01-02", "2006-01-02")
if err != nil {
t.Fatal(err)
}
otpValue := otp.At(ts.Unix())
if otpValue != "352864" {
t.Fatalf("expected otp value 352864 but got %q", otpValue)
}
}