Skip to content

Commit

Permalink
Fix commented-out unit tests.
Browse files Browse the repository at this point in the history
I commented out this test while writing #45. Split the Codes function so we can
plumb in a time step for testing, and update the test. This does not change the
observed behaviour for the user.
  • Loading branch information
creachadair authored and Pierre Carrier committed Feb 1, 2022
1 parent 656898b commit bce7065
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 19 deletions.
32 changes: 19 additions & 13 deletions gauth/gauth.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,34 +17,40 @@ import (
"github.com/creachadair/otp/otpauth"
)

// IndexNow returns the current 30-second time slice index, and the number of
// seconds remaining until it ends.
func IndexNow() (int64, int) {
// IndexNow returns the current 30-second time step, and the number of seconds
// remaining until it ends.
func IndexNow() (uint64, int) {
time := time.Now().Unix()
return time / 30, int(time % 30)
return uint64(time / 30), int(time % 30)
}

// Codes returns the previous, current, and next codes from u.
func Codes(u *otpauth.URL) (prev, curr, next string, _ error) {
var ts uint64
if u.Period > 0 {
ts = otp.TimeWindow(u.Period)()
} else {
ts, _ = IndexNow()
}
return CodesAtTimeStep(u, ts)
}

// CodesAtTimeStep returns the previous, current, and next codes from u at the
// given time step value.
func CodesAtTimeStep(u *otpauth.URL, timeStep uint64) (prev, curr, next string, _ error) {
if u.Type != "totp" {
return "", "", "", fmt.Errorf("unsupported type: %q", u.Type)
} else if u.Algorithm != "" && u.Algorithm != "SHA1" {
return "", "", "", fmt.Errorf("unsupported algorithm: %q", u.Algorithm)
}

cfg := otp.Config{Digits: u.Digits}
var ts uint64
if u.Period > 0 {
ts = otp.TimeWindow(u.Period)()
} else {
ts = otp.TimeWindow(30)()
}
if err := cfg.ParseKey(u.RawSecret); err != nil {
return "", "", "", fmt.Errorf("invalid secret: %v", err)
}
prev = cfg.HOTP(ts - 1)
curr = cfg.HOTP(ts)
next = cfg.HOTP(ts + 1)
prev = cfg.HOTP(timeStep - 1)
curr = cfg.HOTP(timeStep)
next = cfg.HOTP(timeStep + 1)
return
}

Expand Down
13 changes: 7 additions & 6 deletions gauth/gauth_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@ import (
"bytes"
"testing"

"github.com/creachadair/otp/otpauth"
"github.com/pcarrier/gauth/gauth"
)

/* TODO: Update this test.
func TestCodes(t *testing.T) {
tests := []struct {
secret string
index int64
index uint64
want string
fail bool
}{
Expand All @@ -22,14 +22,15 @@ func TestCodes(t *testing.T) {
{"blargh!", 123, "", true},
}
for _, test := range tests {
_, got, _, err := gauth.Codes(test.secret, test.index)
u := &otpauth.URL{Type: "totp", RawSecret: test.secret}
_, got, _, err := gauth.CodesAtTimeStep(u, test.index)
if err != nil && !test.fail {
t.Errorf("Code(%q, %d): unexpected error: %v", test.secret, test.index, err)
t.Errorf("Codes(%q) at %d: unexpected error: %v", u, test.index, err)
} else if got != test.want {
t.Errorf("Code(%q, %d): got %q, want %q", test.secret, test.index, got, test.want)
t.Errorf("Codes(%q) at %d: got %q, want %q", u, test.index, got, test.want)
}
}
} */
}

//go:generate openssl enc -aes-128-cbc -md sha256 -pass pass:x -in testdata/plaintext.csv -out testdata/encrypted.csv

Expand Down

0 comments on commit bce7065

Please sign in to comment.