Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Set skew to 1 by default and create a test #42

Closed
KEINOS opened this issue Apr 30, 2024 · 0 comments
Closed

Set skew to 1 by default and create a test #42

KEINOS opened this issue Apr 30, 2024 · 0 comments

Comments

@KEINOS
Copy link
Owner

KEINOS commented Apr 30, 2024

In our usual case, most of the time the code validation works well.

But, due to the default skew value being as 0, code validation often fails if the period are short.

Here's an example to reproduce. This test will pass if the skew is set to 1.

func TestKey_skew_as_one(t *testing.T) {
	// Backup and defer restore
	oldTimeNow := timeNow
	defer func() {
		timeNow = oldTimeNow
	}()

	key, err := GenerateKey("dummy issuer", "dummy account")
	require.NoError(t, err, "failed to generate TOTP key during test setup")

	key.Options.Period = 3 // 3 seconds
	//key.Options.Skew = 1   // ± 1 period of tolerance

	getCode := func(t *testing.T, key *Key) string {
		t.Helper()

		// Monkey patch timeNow
		timeNow = func() time.Time {
			return time.Now()
		}

		passCode, err := key.PassCode()
		require.NoError(t, err, "failed to generate passcode")

		return passCode
	}

	validateCode := func(t *testing.T, key *Key, passCode string) bool {
		t.Helper()

		// Monkey patch timeNow
		timeNow = func() time.Time {
			return time.Now().Add(time.Second * 200)
		}

		// sleep for 2 sec. This delay causes error/invalid 60% of the
		// time.
		time.Sleep(time.Second * 2)

		return key.Validate(passCode)
	}

	numValid := 0
	numIterations := 10

	for i := 0; i < numIterations; i++ {
		passCode := getCode(t, key)
		ok := validateCode(t, key, passCode)

		if ok {
			numValid++
		}
	}

	expect := numIterations
	actual := numValid

	require.Equal(t, expect, actual,
		"not all generated passcodes are valid")
}
$ go test -timeout 30s -run ^TestKey_skew_as_one$ github.com/KEINOS/go-totp/totp

--- FAIL: TestKey_skew_as_one (20.01s)
    /Users/admin/Library/Mobile Documents/com~apple~CloudDocs/Data_Git/GitHub/PublicRepos/go-totp/totp/issue_44_test.go:64: 
        	Error Trace:	/Users/admin/Library/Mobile Documents/com~apple~CloudDocs/Data_Git/GitHub/PublicRepos/go-totp/totp/issue_44_test.go:64
        	Error:      	Not equal: 
        	            	expected: 10
        	            	actual  : 4
        	Test:       	TestKey_skew_as_one
        	Messages:   	not all generated passcodes are valid
FAIL
FAIL	github.com/KEINOS/go-totp/totp	20.250s
FAIL
KEINOS added a commit that referenced this issue May 1, 2024
KEINOS added a commit that referenced this issue May 1, 2024
KEINOS added a commit that referenced this issue May 1, 2024
KEINOS added a commit that referenced this issue May 1, 2024
@KEINOS KEINOS closed this as completed in e663df8 May 1, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant