| 1 | package config |
| 2 | |
| 3 | import ( |
| 4 | "os" |
| 5 | "path/filepath" |
| 6 | "testing" |
| 7 | ) |
| 8 | |
| 9 | func TestReadConfig(t *testing.T) { |
| 10 | const defaultMailFrom = "root@localhost" |
| 11 | const defaultMaxEvents = 10000 |
| 12 | const defaultMaxRecipients = 200 |
| 13 | |
| 14 | t.Run("NonExistentConfigFile", func(t *testing.T) { |
| 15 | config := readConfig("/nonexistent", "file.conf") |
| 16 | if config.MailFrom != defaultMailFrom { |
| 17 | t.Errorf("Expected default MailFrom value to be '%s', got '%s'", defaultMailFrom, config.MailFrom) |
| 18 | } |
| 19 | }) |
| 20 | |
| 21 | t.Run("ValidConfigFile", func(t *testing.T) { |
| 22 | tmpDir := t.TempDir() |
| 23 | configFile := filepath.Join(tmpDir, "test.conf") |
| 24 | |
| 25 | configContent := `# Test configuration file |
| 26 | send_reminders=false |
| 27 | smtp_server=mail.example.com # default |
| 28 | smtp_ssl=true |
| 29 | smtp_username=testuser # change? |
| 30 | smtp_password=testpass |
| 31 | [email protected] |
| 32 | max_events_per_user=5000 |
| 33 | max_recipients_per_event=150 |
| 34 | ` |
| 35 | err := os.WriteFile(configFile, []byte(configContent), 0644) |
| 36 | if err != nil { |
| 37 | t.Fatalf("Failed to create test config file: %v", err) |
| 38 | } |
| 39 | |
| 40 | config := readConfig(tmpDir, "test.conf") |
| 41 | |
| 42 | if config.SendReminders != false { |
| 43 | t.Errorf("Expected SendReminders to be false, got %v", config.SendReminders) |
| 44 | } |
| 45 | if config.SmtpServer != "mail.example.com" { |
| 46 | t.Errorf("Expected SmtpServer to be 'mail.example.com', got '%s'", config.SmtpServer) |
| 47 | } |
| 48 | if config.SmtpSsl != true { |
| 49 | t.Errorf("Expected SmtpSsl to be true, got %v", config.SmtpSsl) |
| 50 | } |
| 51 | if config.SmtpUsername != "testuser" { |
| 52 | t.Errorf("Expected SmtpUsername to be 'testuser', got '%s'", config.SmtpUsername) |
| 53 | } |
| 54 | if config.SmtpPassword != "testpass" { |
| 55 | t.Errorf("Expected SmtpPassword to be 'testpass', got '%s'", config.SmtpPassword) |
| 56 | } |
| 57 | if config.MailFrom != "[email protected]" { |
| 58 | t.Errorf("Expected MailFrom to be '[email protected]', got '%s'", config.MailFrom) |
| 59 | } |
| 60 | if config.MaxEventsPerUser != 5000 { |
| 61 | t.Errorf("Expected MaxEventsPerUser to be 5000, got %d", config.MaxEventsPerUser) |
| 62 | } |
| 63 | if config.MaxRecipientsPerEvent != 150 { |
| 64 | t.Errorf("Expected MaxRecipientsPerEvent to be 150, got %d", config.MaxRecipientsPerEvent) |
| 65 | } |
| 66 | }) |
| 67 | |
| 68 | t.Run("ValidConfigFileBooleans", func(t *testing.T) { |
| 69 | tmpDir := t.TempDir() |
| 70 | configFile := filepath.Join(tmpDir, "test.conf") |
| 71 | |
| 72 | configContent := `# Test configuration file |
| 73 | send_reminders=no |
| 74 | smtp_ssl=yes |
| 75 | ` |
| 76 | err := os.WriteFile(configFile, []byte(configContent), 0644) |
| 77 | if err != nil { |
| 78 | t.Fatalf("Failed to create test config file: %v", err) |
| 79 | } |
| 80 | |
| 81 | config := readConfig(tmpDir, "test.conf") |
| 82 | |
| 83 | if config.SendReminders != false { |
| 84 | t.Errorf("Expected SendReminders to be false, got %v", config.SendReminders) |
| 85 | } |
| 86 | if config.SmtpSsl != true { |
| 87 | t.Errorf("Expected SmtpSsl to be true, got %v", config.SmtpSsl) |
| 88 | } |
| 89 | }) |
| 90 | |
| 91 | t.Run("ConfigWithCommentsAndWhitespace", func(t *testing.T) { |
| 92 | tmpDir := t.TempDir() |
| 93 | configFile := filepath.Join(tmpDir, "test.conf") |
| 94 | |
| 95 | configContent := `# This is a comment |
| 96 | # Another comment |
| 97 | send_reminders = 1 |
| 98 | smtp_server = localhost |
| 99 | # More comments with # marks in it too |
| 100 | smtp_ssl=0 |
| 101 | ` |
| 102 | err := os.WriteFile(configFile, []byte(configContent), 0644) |
| 103 | if err != nil { |
| 104 | t.Fatalf("Failed to create test config file: %v", err) |
| 105 | } |
| 106 | |
| 107 | config := readConfig(tmpDir, "test.conf") |
| 108 | |
| 109 | if config.SendReminders != true { |
| 110 | t.Errorf("Expected SendReminders to be true, got %v", config.SendReminders) |
| 111 | } |
| 112 | if config.SmtpServer != "localhost" { |
| 113 | t.Errorf("Expected SmtpServer to be 'localhost', got '%s'", config.SmtpServer) |
| 114 | } |
| 115 | if config.SmtpSsl != false { |
| 116 | t.Errorf("Expected SmtpSsl to be false, got %v", config.SmtpSsl) |
| 117 | } |
| 118 | if config.MailFrom != defaultMailFrom { |
| 119 | t.Errorf("Expected default MailFrom value to be '%s', got '%s'", defaultMailFrom, config.MailFrom) |
| 120 | } |
| 121 | }) |
| 122 | |
| 123 | t.Run("InvalidBooleanValues", func(t *testing.T) { |
| 124 | tmpDir := t.TempDir() |
| 125 | configFile := filepath.Join(tmpDir, "test.conf") |
| 126 | |
| 127 | configContent := `send_reminders=invalid_bool |
| 128 | |
| 129 | smtp_ssl=not_a_boolean |
| 130 | |
| 131 | smtp_server=valid.server.com |
| 132 | max_xml_size_mb=1000 |
| 133 | ` |
| 134 | err := os.WriteFile(configFile, []byte(configContent), 0644) |
| 135 | if err != nil { |
| 136 | t.Fatalf("Failed to create test config file: %v", err) |
| 137 | } |
| 138 | |
| 139 | config := readConfig(tmpDir, "test.conf") |
| 140 | |
| 141 | // Boolean values should remain default due to invalid input |
| 142 | if config.SendReminders != true { |
| 143 | t.Errorf("Expected SendReminders to be true (default due to invalid input), got %v", config.SendReminders) |
| 144 | } |
| 145 | if config.SmtpSsl != false { |
| 146 | t.Errorf("Expected SmtpSsl to be false (default due to invalid input), got %v", config.SmtpSsl) |
| 147 | } |
| 148 | |
| 149 | // Valid string value should be set |
| 150 | if config.SmtpServer != "valid.server.com" { |
| 151 | t.Errorf("Expected SmtpServer to be 'valid.server.com', got '%s'", config.SmtpServer) |
| 152 | } |
| 153 | |
| 154 | // Valid integer value should be set |
| 155 | const max_xml_bytes = 1_000 * 1024 * 1024 |
| 156 | if config.MaxXMLSizeBytes != max_xml_bytes { |
| 157 | t.Errorf("Expected MaxXMLSizeBytes to be %d bytes, got %d bytes", max_xml_bytes, config.MaxXMLSizeBytes) |
| 158 | } |
| 159 | |
| 160 | }) |
| 161 | |
| 162 | t.Run("InvalidLineFormat", func(t *testing.T) { |
| 163 | tmpDir := t.TempDir() |
| 164 | configFile := filepath.Join(tmpDir, "test.conf") |
| 165 | |
| 166 | configContent := `smtp_server=valid.server.com |
| 167 | invalid_line_without_equal_sign |
| 168 | smtp_ssl==1 |
| 169 | [email protected] |
| 170 | ` |
| 171 | err := os.WriteFile(configFile, []byte(configContent), 0644) |
| 172 | if err != nil { |
| 173 | t.Fatalf("Failed to create test config file: %v", err) |
| 174 | } |
| 175 | |
| 176 | config := readConfig(tmpDir, "test.conf") |
| 177 | |
| 178 | // Should process the first valid line but stop at invalid line |
| 179 | if config.SmtpServer != "valid.server.com" { |
| 180 | t.Errorf("Expected SmtpServer to be 'valid.server.com', got '%s'", config.SmtpServer) |
| 181 | } |
| 182 | |
| 183 | if config.SmtpSsl != false { |
| 184 | t.Errorf("Expected SmtpSsl to be false (default due to invalid input), got %v", config.SmtpSsl) |
| 185 | } |
| 186 | |
| 187 | // The line after the invalid one should still be processed |
| 188 | if config.MailFrom != "[email protected]" { |
| 189 | t.Errorf("Expected MailFrom to be '[email protected]', got '%s'", config.MailFrom) |
| 190 | } |
| 191 | }) |
| 192 | |
| 193 | t.Run("InvalidSmtpHostname", func(t *testing.T) { |
| 194 | tmpDir := t.TempDir() |
| 195 | configFile := filepath.Join(tmpDir, "test.conf") |
| 196 | |
| 197 | // Get the default hostname for comparison |
| 198 | hostname, err := os.Hostname() |
| 199 | if err != nil { |
| 200 | hostname = "localhost" |
| 201 | } |
| 202 | |
| 203 | configContent := `smtp_server=.invalid.hostname |
| 204 | smtp_ssl=true |
| 205 | mail_from=test@localhost |
| 206 | ` |
| 207 | err = os.WriteFile(configFile, []byte(configContent), 0644) |
| 208 | if err != nil { |
| 209 | t.Fatalf("Failed to create test config file: %v", err) |
| 210 | } |
| 211 | |
| 212 | config := readConfig(tmpDir, "test.conf") |
| 213 | |
| 214 | // Invalid SMTP hostname should be rejected and default value kept |
| 215 | if config.SmtpServer != hostname { |
| 216 | t.Errorf("Expected SmtpServer to be '%s' (default due to invalid hostname), got '%s'", hostname, config.SmtpServer) |
| 217 | } |
| 218 | |
| 219 | // Other valid values should still be set |
| 220 | if config.SmtpSsl != true { |
| 221 | t.Errorf("Expected SmtpSsl to be true, got %v", config.SmtpSsl) |
| 222 | } |
| 223 | if config.MailFrom != "test@localhost" { |
| 224 | t.Errorf("Expected MailFrom to be 'test@localhost', got '%s'", config.MailFrom) |
| 225 | } |
| 226 | }) |
| 227 | |
| 228 | t.Run("InvalidMailFrom", func(t *testing.T) { |
| 229 | tmpDir := t.TempDir() |
| 230 | configFile := filepath.Join(tmpDir, "test.conf") |
| 231 | |
| 232 | configContent := `mail_from=user@@example.com |
| 233 | max_events_per_user=-10 |
| 234 | max_recipients_per_event=100000 |
| 235 | max_xml_size_mb=0 |
| 236 | smtp_server=mail.example.com |
| 237 | ` |
| 238 | err := os.WriteFile(configFile, []byte(configContent), 0644) |
| 239 | if err != nil { |
| 240 | t.Fatalf("Failed to create test config file: %v", err) |
| 241 | } |
| 242 | |
| 243 | config := readConfig(tmpDir, "test.conf") |
| 244 | |
| 245 | // Invalid mail_from should be rejected, default should be used |
| 246 | if config.MailFrom != defaultMailFrom { |
| 247 | t.Errorf("Expected MailFrom to be default '%s', got '%s'", defaultMailFrom, config.MailFrom) |
| 248 | } |
| 249 | |
| 250 | // Invalid max_events_per_user should be rejected, default should be used |
| 251 | if config.MaxEventsPerUser != defaultMaxEvents { |
| 252 | t.Errorf("Expected MaxEventsPerUser to be default %d, got '%d'", defaultMaxEvents, config.MaxEventsPerUser) |
| 253 | } |
| 254 | |
| 255 | // Invalid max_recipients_per_event should be rejected, default should be used |
| 256 | if config.MaxRecipientsPerEvent != defaultMaxRecipients { |
| 257 | t.Errorf("Expected MaxRecipientsPerEvent to be default %d, got %d", defaultMaxRecipients, config.MaxRecipientsPerEvent) |
| 258 | } |
| 259 | |
| 260 | // Invalid max_xml_size_mb should be rejected, default should be used |
| 261 | const defaultMaxXML = 50 * 1024 * 1024 |
| 262 | if config.MaxXMLSizeBytes != defaultMaxXML { |
| 263 | t.Errorf("Expected MaxXMLSizeBytes to be default %d, got %d", defaultMaxXML, config.MaxXMLSizeBytes) |
| 264 | } |
| 265 | |
| 266 | // Other valid values should still be set |
| 267 | if config.SmtpServer != "mail.example.com" { |
| 268 | t.Errorf("Expected SmtpServer to be 'mail.example.com', got '%s'", config.SmtpServer) |
| 269 | } |
| 270 | }) |
| 271 | |
| 272 | t.Run("MailFromWithDisplayName", func(t *testing.T) { |
| 273 | tmpDir := t.TempDir() |
| 274 | configFile := filepath.Join(tmpDir, "test.conf") |
| 275 | |
| 276 | // Try to use email with display name format |
| 277 | configContent := `mail_from=John Doe <[email protected]> |
| 278 | ` |
| 279 | err := os.WriteFile(configFile, []byte(configContent), 0644) |
| 280 | if err != nil { |
| 281 | t.Fatalf("Failed to create test config file: %v", err) |
| 282 | } |
| 283 | |
| 284 | config := readConfig(tmpDir, "test.conf") |
| 285 | |
| 286 | // Display name format should be rejected, default should be used |
| 287 | if config.MailFrom != defaultMailFrom { |
| 288 | t.Errorf("Expected MailFrom to be default '%s' due to display name format, got '%s'", defaultMailFrom, config.MailFrom) |
| 289 | } |
| 290 | }) |
| 291 | |
| 292 | t.Run("EmptyConfigFile", func(t *testing.T) { |
| 293 | tmpDir := t.TempDir() |
| 294 | configFile := filepath.Join(tmpDir, "test.conf") |
| 295 | |
| 296 | err := os.WriteFile(configFile, []byte(""), 0644) |
| 297 | if err != nil { |
| 298 | t.Fatalf("Failed to create test config file: %v", err) |
| 299 | } |
| 300 | |
| 301 | config := readConfig(tmpDir, "test.conf") |
| 302 | |
| 303 | // All values should be defaults |
| 304 | if config.SendReminders != true { |
| 305 | t.Errorf("Expected SendReminders to be true, got %v", config.SendReminders) |
| 306 | } |
| 307 | if config.MailFrom != defaultMailFrom { |
| 308 | t.Errorf("Expected MailFrom to be '%s', got '%s'", defaultMailFrom, config.MailFrom) |
| 309 | } |
| 310 | }) |
| 311 | |
| 312 | t.Run("OnlyCommentsConfigFile", func(t *testing.T) { |
| 313 | tmpDir := t.TempDir() |
| 314 | configFile := filepath.Join(tmpDir, "test.conf") |
| 315 | |
| 316 | configContent := `# This is a comment |
| 317 | # Another comment |
| 318 | # Yet another comment |
| 319 | ` |
| 320 | err := os.WriteFile(configFile, []byte(configContent), 0644) |
| 321 | if err != nil { |
| 322 | t.Fatalf("Failed to create test config file: %v", err) |
| 323 | } |
| 324 | |
| 325 | config := readConfig(tmpDir, "test.conf") |
| 326 | |
| 327 | // All values should be defaults |
| 328 | if config.SendReminders != true { |
| 329 | t.Errorf("Expected SendReminders to be true, got %v", config.SendReminders) |
| 330 | } |
| 331 | if config.MailFrom != defaultMailFrom { |
| 332 | t.Errorf("Expected MailFrom to be '%s', got '%s'", defaultMailFrom, config.MailFrom) |
| 333 | } |
| 334 | }) |
| 335 | |
| 336 | t.Run("SmtpUsernameTooLong", func(t *testing.T) { |
| 337 | tmpDir := t.TempDir() |
| 338 | configFile := filepath.Join(tmpDir, "test.conf") |
| 339 | |
| 340 | longUsername := string(make([]byte, 129)) // 129 characters |
| 341 | for i := range longUsername { |
| 342 | longUsername = string([]byte{byte('a' + (i % 26))}) + longUsername[1:] |
| 343 | } |
| 344 | |
| 345 | configContent := `smtp_username=` + longUsername + ` |
| 346 | smtp_password=validpass |
| 347 | ` |
| 348 | err := os.WriteFile(configFile, []byte(configContent), 0644) |
| 349 | if err != nil { |
| 350 | t.Fatalf("Failed to create test config file: %v", err) |
| 351 | } |
| 352 | |
| 353 | config := readConfig(tmpDir, "test.conf") |
| 354 | |
| 355 | // Username that's too long should be rejected, default (empty string) should be used |
| 356 | if config.SmtpUsername != "" { |
| 357 | t.Errorf("Expected SmtpUsername to be empty (default) due to excessive length, got '%s'", config.SmtpUsername) |
| 358 | } |
| 359 | |
| 360 | // Other valid values should still be set |
| 361 | if config.SmtpPassword != "validpass" { |
| 362 | t.Errorf("Expected SmtpPassword to be 'validpass', got '%s'", config.SmtpPassword) |
| 363 | } |
| 364 | }) |
| 365 | |
| 366 | t.Run("SmtpPasswordTooLong", func(t *testing.T) { |
| 367 | tmpDir := t.TempDir() |
| 368 | configFile := filepath.Join(tmpDir, "test.conf") |
| 369 | |
| 370 | longPassword := string(make([]byte, 129)) // 129 characters long |
| 371 | for i := range longPassword { |
| 372 | longPassword = string([]byte{byte('A' + (i % 26))}) + longPassword[1:] |
| 373 | } |
| 374 | |
| 375 | configContent := `smtp_username=validuser |
| 376 | smtp_password=` + longPassword + ` |
| 377 | ` |
| 378 | err := os.WriteFile(configFile, []byte(configContent), 0644) |
| 379 | if err != nil { |
| 380 | t.Fatalf("Failed to create test config file: %v", err) |
| 381 | } |
| 382 | |
| 383 | config := readConfig(tmpDir, "test.conf") |
| 384 | |
| 385 | // Password that's too long should be rejected, default (empty string) should be used |
| 386 | if config.SmtpPassword != "" { |
| 387 | t.Errorf("Expected SmtpPassword to be empty (default) due to excessive length, got '%s'", config.SmtpPassword) |
| 388 | } |
| 389 | |
| 390 | // Other valid values should still be set |
| 391 | if config.SmtpUsername != "validuser" { |
| 392 | t.Errorf("Expected SmtpUsername to be 'validuser', got '%s'", config.SmtpUsername) |
| 393 | } |
| 394 | }) |
| 395 | } |
| 396 | |
| 397 | func TestGetUserConfigFilePath(t *testing.T) { |
| 398 | t.Run("NoConfigFiles", func(t *testing.T) { |
| 399 | tmpDir := t.TempDir() |
| 400 | path, _ := GetUserConfigFilename(tmpDir) |
| 401 | |
| 402 | if path != "" { |
| 403 | t.Errorf("Expected path to be empty, got '%s'", path) |
| 404 | } |
| 405 | }) |
| 406 | |
| 407 | t.Run("OnlyLegacyFile", func(t *testing.T) { |
| 408 | tmpDir := t.TempDir() |
| 409 | legacyPath := filepath.Join(tmpDir, LegacyUserConfigFile) |
| 410 | |
| 411 | if err := os.WriteFile(legacyPath, []byte("legacy content"), 0644); err != nil { |
| 412 | t.Fatalf("Failed to create legacy config file: %v", err) |
| 413 | } |
| 414 | |
| 415 | filename, _ := GetUserConfigFilename(tmpDir) |
| 416 | if filename != LegacyUserConfigFile { |
| 417 | t.Errorf("Expected path to be '%s', got '%s'", LegacyUserConfigFile, filename) |
| 418 | } |
| 419 | }) |
| 420 | |
| 421 | t.Run("OnlyPreferredFile", func(t *testing.T) { |
| 422 | tmpDir := t.TempDir() |
| 423 | if err := os.MkdirAll(filepath.Join(tmpDir, UserConfigDir), 0755); err != nil { |
| 424 | t.Fatalf("Failed to create preferred config directory: %v", err) |
| 425 | } |
| 426 | |
| 427 | preferredFilename := filepath.Join(UserConfigDir, UserConfigFile) |
| 428 | if err := os.WriteFile(filepath.Join(tmpDir, preferredFilename), []byte("preferred content"), 0644); err != nil { |
| 429 | t.Fatalf("Failed to create preferred config file: %v", err) |
| 430 | } |
| 431 | |
| 432 | filename, _ := GetUserConfigFilename(tmpDir) |
| 433 | |
| 434 | if filename != preferredFilename { |
| 435 | t.Errorf("Expected path to be '%s', got '%s'", preferredFilename, filename) |
| 436 | } |
| 437 | }) |
| 438 | |
| 439 | t.Run("BothFilesExist", func(t *testing.T) { |
| 440 | tmpDir := t.TempDir() |
| 441 | |
| 442 | // Create legacy file |
| 443 | legacyPath := filepath.Join(tmpDir, LegacyUserConfigFile) |
| 444 | if err := os.WriteFile(legacyPath, []byte("legacy content"), 0644); err != nil { |
| 445 | t.Fatalf("Failed to create legacy config file: %v", err) |
| 446 | } |
| 447 | |
| 448 | // Create preferred file |
| 449 | if err := os.MkdirAll(filepath.Join(tmpDir, UserConfigDir), 0755); err != nil { |
| 450 | t.Fatalf("Failed to create preferred config directory: %v", err) |
| 451 | } |
| 452 | |
| 453 | preferredFilename := filepath.Join(UserConfigDir, UserConfigFile) |
| 454 | if err := os.WriteFile(filepath.Join(tmpDir, preferredFilename), []byte("preferred content"), 0644); err != nil { |
| 455 | t.Fatalf("Failed to create preferred config file: %v", err) |
| 456 | } |
| 457 | |
| 458 | filename, _ := GetUserConfigFilename(tmpDir) |
| 459 | |
| 460 | // Should prefer the new config file over the legacy one |
| 461 | if filename != preferredFilename { |
| 462 | t.Errorf("Expected path to be '%s' (preferred), got '%s'", preferredFilename, filename) |
| 463 | } |
| 464 | }) |
| 465 | } |