|
| 1 | +package passphrase |
| 2 | + |
| 3 | +import ( |
| 4 | + "testing" |
| 5 | +) |
| 6 | + |
| 7 | +// Test basic validation functionality |
| 8 | +func TestValidate(t *testing.T) { |
| 9 | + tests := []struct { |
| 10 | + name string |
| 11 | + passphrase string |
| 12 | + expectValid bool |
| 13 | + }{ |
| 14 | + {"Valid passphrase", "ValidPassword123!", true}, |
| 15 | + {"Too short", "short", false}, |
| 16 | + {"Missing uppercase", "validpassword123!", false}, |
| 17 | + {"Missing lowercase", "VALIDPASSWORD123!", false}, |
| 18 | + {"Missing digit", "ValidPassword!", false}, |
| 19 | + {"Missing special", "ValidPassword123", false}, |
| 20 | + {"Valid minimum", "Password123!", true}, |
| 21 | + } |
| 22 | + |
| 23 | + for _, tt := range tests { |
| 24 | + t.Run(tt.name, func(t *testing.T) { |
| 25 | + result := Validate(tt.passphrase) |
| 26 | + if result.Valid != tt.expectValid { |
| 27 | + t.Errorf("Expected valid=%v, got %v", tt.expectValid, result.Valid) |
| 28 | + } |
| 29 | + }) |
| 30 | + } |
| 31 | +} |
| 32 | + |
| 33 | +// Test hybrid validation functionality |
| 34 | +func TestValidateHybrid(t *testing.T) { |
| 35 | + tests := []struct { |
| 36 | + name string |
| 37 | + passphrase string |
| 38 | + expectValid bool |
| 39 | + }{ |
| 40 | + {"Valid strong passphrase", "MyUniquePassword123!", true}, |
| 41 | + {"Too short", "short", false}, |
| 42 | + {"Missing requirements", "password", false}, |
| 43 | + {"Valid but common", "Password123!", true}, // May have warnings but still valid |
| 44 | + } |
| 45 | + |
| 46 | + for _, tt := range tests { |
| 47 | + t.Run(tt.name, func(t *testing.T) { |
| 48 | + result := ValidateHybrid(tt.passphrase) |
| 49 | + if result.Valid != tt.expectValid { |
| 50 | + t.Errorf("Expected valid=%v, got %v", tt.expectValid, result.Valid) |
| 51 | + } |
| 52 | + // Check that result has expected fields |
| 53 | + if result.Strength == "" { |
| 54 | + t.Error("Expected strength to be set") |
| 55 | + } |
| 56 | + }) |
| 57 | + } |
| 58 | +} |
| 59 | + |
| 60 | +// Test error message formatting |
| 61 | +func TestGetErrorMessage(t *testing.T) { |
| 62 | + result := ValidationResult{ |
| 63 | + Valid: false, |
| 64 | + Errors: []string{"test error"}, |
| 65 | + } |
| 66 | + |
| 67 | + msg := GetErrorMessage(result) |
| 68 | + if msg == "" { |
| 69 | + t.Error("Expected error message, got empty string") |
| 70 | + } |
| 71 | + |
| 72 | + // Test valid result |
| 73 | + validResult := ValidationResult{Valid: true} |
| 74 | + validMsg := GetErrorMessage(validResult) |
| 75 | + if validMsg != "" { |
| 76 | + t.Error("Expected empty message for valid result") |
| 77 | + } |
| 78 | +} |
| 79 | + |
| 80 | +// Test hybrid error message formatting |
| 81 | +func TestGetHybridErrorMessage(t *testing.T) { |
| 82 | + result := HybridValidationResult{ |
| 83 | + Valid: false, |
| 84 | + Errors: []string{"test error"}, |
| 85 | + } |
| 86 | + |
| 87 | + msg := GetHybridErrorMessage(result) |
| 88 | + if msg == "" { |
| 89 | + t.Error("Expected error message, got empty string") |
| 90 | + } |
| 91 | + |
| 92 | + // Test valid result with no warnings |
| 93 | + validResult := HybridValidationResult{Valid: true} |
| 94 | + validMsg := GetHybridErrorMessage(validResult) |
| 95 | + if validMsg != "" { |
| 96 | + t.Error("Expected empty message for valid result with no warnings") |
| 97 | + } |
| 98 | +} |
| 99 | + |
| 100 | +// Test strength assessment |
| 101 | +func TestGetStrength(t *testing.T) { |
| 102 | + tests := []struct { |
| 103 | + name string |
| 104 | + passphrase string |
| 105 | + expected string |
| 106 | + }{ |
| 107 | + {"Very weak", "abc", "Very Weak"}, |
| 108 | + {"Weak invalid", "password", "Weak"}, |
| 109 | + {"Strong valid", "StrongPassword123!", "Strong"}, |
| 110 | + } |
| 111 | + |
| 112 | + for _, tt := range tests { |
| 113 | + t.Run(tt.name, func(t *testing.T) { |
| 114 | + strength := GetStrength(tt.passphrase) |
| 115 | + if strength != tt.expected { |
| 116 | + t.Errorf("Expected %s, got %s", tt.expected, strength) |
| 117 | + } |
| 118 | + }) |
| 119 | + } |
| 120 | +} |
| 121 | + |
| 122 | +// Test special character detection |
| 123 | +func TestIsSpecialChar(t *testing.T) { |
| 124 | + if !isSpecialChar('!') { |
| 125 | + t.Error("Expected '!' to be special character") |
| 126 | + } |
| 127 | + if !isSpecialChar('@') { |
| 128 | + t.Error("Expected '@' to be special character") |
| 129 | + } |
| 130 | + if isSpecialChar('a') { |
| 131 | + t.Error("Expected 'a' to not be special character") |
| 132 | + } |
| 133 | + if isSpecialChar('1') { |
| 134 | + t.Error("Expected '1' to not be special character") |
| 135 | + } |
| 136 | +} |
| 137 | + |
| 138 | +// Test format crack time |
| 139 | +func TestFormatCrackTime(t *testing.T) { |
| 140 | + result := formatCrackTime(nil) |
| 141 | + if result != "unknown" { |
| 142 | + t.Errorf("Expected 'unknown' for nil, got %s", result) |
| 143 | + } |
| 144 | + |
| 145 | + result = formatCrackTime("2 hours") |
| 146 | + if result != "2 hours" { |
| 147 | + t.Errorf("Expected '2 hours', got %s", result) |
| 148 | + } |
| 149 | +} |
| 150 | + |
| 151 | +// Test basic validation (internal function) |
| 152 | +func TestValidateBasicFunction(t *testing.T) { |
| 153 | + result := validateBasic("ValidPassword123!") |
| 154 | + if !result.Valid { |
| 155 | + t.Error("Expected valid result for good password") |
| 156 | + } |
| 157 | + |
| 158 | + result = validateBasic("short") |
| 159 | + if result.Valid { |
| 160 | + t.Error("Expected invalid result for short password") |
| 161 | + } |
| 162 | + if len(result.Errors) == 0 { |
| 163 | + t.Error("Expected errors for invalid password") |
| 164 | + } |
| 165 | +} |
0 commit comments