From bf811361487a38156288e05794b90d615171e5d6 Mon Sep 17 00:00:00 2001 From: "M. J. Fromberger" Date: Wed, 27 Mar 2019 11:53:52 -0700 Subject: [PATCH] Add basic unit tests for the gauth package. --- gauth/gauth_test.go | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 gauth/gauth_test.go diff --git a/gauth/gauth_test.go b/gauth/gauth_test.go new file mode 100644 index 0000000..4bed176 --- /dev/null +++ b/gauth/gauth_test.go @@ -0,0 +1,30 @@ +package gauth_test + +import ( + "testing" + + "github.com/pcarrier/gauth/gauth" +) + +func TestCode(t *testing.T) { + tests := []struct { + secret string + index int64 + want string + fail bool + }{ + // Manually verified with the Google authenticator app. + {"ABCDEFGH", 51790421, "305441", false}, + + // Invalid Base32 input for the secret. + {"blargh!", 123, "", true}, + } + for _, test := range tests { + got, err := gauth.Code(test.secret, test.index) + if err != nil && !test.fail { + t.Errorf("Code(%q, %d): unexpected error: %v", test.secret, test.index, err) + } else if got != test.want { + t.Errorf("Code(%q, %d): got %q, want %q", test.secret, test.index, got, test.want) + } + } +}