|
| 1 | +package totp |
| 2 | + |
| 3 | +import ( |
| 4 | + "testing" |
| 5 | + "time" |
| 6 | +) |
| 7 | + |
| 8 | +func TestGenerateSecret(t *testing.T) { |
| 9 | + // Test generating a secret |
| 10 | + secret, err := GenerateSecret() |
| 11 | + if err != nil { |
| 12 | + t.Fatalf("Failed to generate secret: %v", err) |
| 13 | + } |
| 14 | + |
| 15 | + // Check secret length (base32 encoded, should be around 16 characters) |
| 16 | + if len(secret) < 10 || len(secret) > 20 { |
| 17 | + t.Errorf("Generated secret length is invalid: %d", len(secret)) |
| 18 | + } |
| 19 | +} |
| 20 | + |
| 21 | +func TestTOTP(t *testing.T) { |
| 22 | + // Test scenarios |
| 23 | + testCases := []struct { |
| 24 | + name string |
| 25 | + duration int |
| 26 | + wantErr bool |
| 27 | + }{ |
| 28 | + {"Standard 30-second interval", 30, false}, |
| 29 | + {"Custom 60-second interval", 60, false}, |
| 30 | + {"Invalid duration", -1, false}, |
| 31 | + } |
| 32 | + |
| 33 | + for _, tc := range testCases { |
| 34 | + t.Run(tc.name, func(t *testing.T) { |
| 35 | + // Generate a secret |
| 36 | + secret, err := GenerateSecret() |
| 37 | + if err != nil { |
| 38 | + t.Fatalf("Failed to generate secret: %v", err) |
| 39 | + } |
| 40 | + |
| 41 | + // Generate TOTP |
| 42 | + code, err := TOTP(secret, tc.duration) |
| 43 | + if tc.wantErr && err == nil { |
| 44 | + t.Error("Expected an error, but got none") |
| 45 | + } |
| 46 | + |
| 47 | + if err != nil && !tc.wantErr { |
| 48 | + t.Errorf("Unexpected error: %v", err) |
| 49 | + } |
| 50 | + |
| 51 | + // Check code length |
| 52 | + if len(code) != 6 { |
| 53 | + t.Errorf("Invalid TOTP code length: %d", len(code)) |
| 54 | + } |
| 55 | + }) |
| 56 | + } |
| 57 | +} |
| 58 | + |
| 59 | +// timeProvider is an interface to allow easier time mocking |
| 60 | +type timeProvider interface { |
| 61 | + Now() time.Time |
| 62 | +} |
| 63 | + |
| 64 | +// realTimeProvider uses the actual system time |
| 65 | +type realTimeProvider struct{} |
| 66 | + |
| 67 | +func (r realTimeProvider) Now() time.Time { |
| 68 | + return time.Now() |
| 69 | +} |
| 70 | + |
| 71 | +// mockTimeProvider allows setting a fixed time for testing |
| 72 | +type mockTimeProvider struct { |
| 73 | + fixedTime time.Time |
| 74 | +} |
| 75 | + |
| 76 | +func (m mockTimeProvider) Now() time.Time { |
| 77 | + return m.fixedTime |
| 78 | +} |
| 79 | + |
| 80 | +// Global variable to hold current time provider |
| 81 | +var currentTimeProvider timeProvider = realTimeProvider{} |
| 82 | + |
| 83 | +// SetTimeProvider allows setting a custom time provider (useful for testing) |
| 84 | +func SetTimeProvider(provider timeProvider) { |
| 85 | + currentTimeProvider = provider |
| 86 | +} |
| 87 | + |
| 88 | +// Modified functions to use the time provider |
| 89 | +func nowFunc() time.Time { |
| 90 | + return currentTimeProvider.Now() |
| 91 | +} |
| 92 | + |
| 93 | + |
| 94 | +// Updated test file |
| 95 | +func TestValidate(t *testing.T) { |
| 96 | + // Reset time provider after the test |
| 97 | + defer SetTimeProvider(realTimeProvider{}) |
| 98 | + |
| 99 | + // Test validation scenarios |
| 100 | + testCases := []struct { |
| 101 | + name string |
| 102 | + duration int |
| 103 | + timeDiff time.Duration |
| 104 | + expected bool |
| 105 | + }{ |
| 106 | + {"Current time", 30, 0, true}, |
| 107 | + {"30 seconds before", 30, -30 * time.Second, true}, |
| 108 | + {"30 seconds after", 30, 30 * time.Second, true}, |
| 109 | + } |
| 110 | + |
| 111 | + for _, tc := range testCases { |
| 112 | + t.Run(tc.name, func(t *testing.T) { |
| 113 | + // Create a fixed time and set it as the current time provider |
| 114 | + fixedTime := time.Now().Add(tc.timeDiff) |
| 115 | + SetTimeProvider(mockTimeProvider{fixedTime}) |
| 116 | + |
| 117 | + // Generate a secret |
| 118 | + secret, err := GenerateSecret() |
| 119 | + if err != nil { |
| 120 | + t.Fatalf("Failed to generate secret: %v", err) |
| 121 | + } |
| 122 | + |
| 123 | + // Generate TOTP code |
| 124 | + code, err := TOTP(secret, tc.duration) |
| 125 | + if err != nil { |
| 126 | + t.Fatalf("Failed to generate TOTP: %v", err) |
| 127 | + } |
| 128 | + |
| 129 | + // Validate the code |
| 130 | + isValid := Validate(secret, tc.duration, code) |
| 131 | + if isValid != tc.expected { |
| 132 | + t.Errorf("Validation result unexpected. Got %v, want %v", isValid, tc.expected) |
| 133 | + } |
| 134 | + }) |
| 135 | + } |
| 136 | +} |
| 137 | + |
| 138 | +func TestInvalidSecret(t *testing.T) { |
| 139 | + // Test with invalid secrets |
| 140 | + invalidSecrets := []string{ |
| 141 | + "INVALID_SECRET", |
| 142 | + "12345", |
| 143 | + } |
| 144 | + |
| 145 | + for _, secret := range invalidSecrets { |
| 146 | + t.Run("Invalid Secret: "+secret, func(t *testing.T) { |
| 147 | + // Try to generate TOTP with invalid secret |
| 148 | + _, err := TOTP(secret, 30) |
| 149 | + if err == nil { |
| 150 | + t.Error("Expected an error with invalid secret, but got none") |
| 151 | + } |
| 152 | + |
| 153 | + // Try to validate with invalid secret |
| 154 | + isValid := Validate(secret, 30, "123456") |
| 155 | + if isValid { |
| 156 | + t.Error("Validation should fail with invalid secret") |
| 157 | + } |
| 158 | + }) |
| 159 | + } |
| 160 | +} |
| 161 | + |
| 162 | +// Example of resetting to real time provider |
| 163 | +func resetTimeProvider() { |
| 164 | + SetTimeProvider(realTimeProvider{}) |
| 165 | +} |
| 166 | + |
| 167 | +func BenchmarkGenerateSecret(b *testing.B) { |
| 168 | + for i := 0; i < b.N; i++ { |
| 169 | + _, _ = GenerateSecret() |
| 170 | + } |
| 171 | +} |
| 172 | + |
| 173 | +func BenchmarkTOTP(b *testing.B) { |
| 174 | + secret, _ := GenerateSecret() |
| 175 | + b.ResetTimer() |
| 176 | + |
| 177 | + for i := 0; i < b.N; i++ { |
| 178 | + _, _ = TOTP(secret, 30) |
| 179 | + } |
| 180 | +} |
| 181 | + |
| 182 | +func BenchmarkValidate(b *testing.B) { |
| 183 | + secret, _ := GenerateSecret() |
| 184 | + code, _ := TOTP(secret, 30) |
| 185 | + b.ResetTimer() |
| 186 | + |
| 187 | + for i := 0; i < b.N; i++ { |
| 188 | + _ = Validate(secret, 30, code) |
| 189 | + } |
| 190 | +} |
| 191 | + |
| 192 | +// Example of how to use the package |
| 193 | +func ExampleTOTP() { |
| 194 | + // Generate a secret |
| 195 | + secret, err := GenerateSecret() |
| 196 | + if err != nil { |
| 197 | + panic(err) |
| 198 | + } |
| 199 | + |
| 200 | + // Generate a TOTP code |
| 201 | + code, err := TOTP(secret, 30) |
| 202 | + if err != nil { |
| 203 | + panic(err) |
| 204 | + } |
| 205 | + |
| 206 | + // Validate the code |
| 207 | + isValid := Validate(secret, 30, code) |
| 208 | + println("Code is valid:", isValid) |
| 209 | +} |
0 commit comments