| 1 | package util |
| 2 | |
| 3 | import ( |
| 4 | "fmt" |
| 5 | "io" |
| 6 | "net/mail" |
| 7 | "os" |
| 8 | "path/filepath" |
| 9 | "strings" |
| 10 | "testing" |
| 11 | "time" |
| 12 | ) |
| 13 | |
| 14 | func validateEmailMessage(t *testing.T, rawMessage string, wantEmptyBody bool) { |
| 15 | t.Helper() |
| 16 | |
| 17 | msg, err := mail.ReadMessage(strings.NewReader(rawMessage)) |
| 18 | if err != nil { |
| 19 | t.Errorf("Failed to parse email message: %v", err) |
| 20 | return |
| 21 | } |
| 22 | |
| 23 | requiredHeaders := []string{"From", "To", "Subject", "MIME-Version", "Content-Type"} |
| 24 | for _, header := range requiredHeaders { |
| 25 | if msg.Header.Get(header) == "" { |
| 26 | t.Errorf("Missing required header: %s", header) |
| 27 | } |
| 28 | } |
| 29 | |
| 30 | body, err := io.ReadAll(msg.Body) |
| 31 | if err != nil { |
| 32 | t.Errorf("Failed to read body from parsed message: %v", err) |
| 33 | } |
| 34 | if !wantEmptyBody && len(body) == 0 { |
| 35 | t.Errorf("Empty body") |
| 36 | } |
| 37 | } |
| 38 | |
| 39 | func TestEmailRecipient_NameAndEmail(t *testing.T) { |
| 40 | tests := []struct { |
| 41 | recipient EmailRecipient |
| 42 | want string |
| 43 | }{ |
| 44 | {recipient: EmailRecipient{Email: ""}, want: ""}, |
| 45 | {recipient: EmailRecipient{Email: "[email protected]", FirstName: "John", LastName: "Doe"}, want: "\"John Doe\" <[email protected]>"}, |
| 46 | {recipient: EmailRecipient{Email: "[email protected]", FirstName: "John"}, want: "\"John\" <[email protected]>"}, |
| 47 | {recipient: EmailRecipient{Email: "[email protected]", LastName: "Doe"}, want: "\"Doe\" <[email protected]>"}, |
| 48 | {recipient: EmailRecipient{Email: "[email protected]"}, want: "<[email protected]>"}, |
| 49 | {recipient: EmailRecipient{Email: "[email protected]", FirstName: "John", LastName: "Doe Smith"}, want: "\"John Doe Smith\" <[email protected]>"}, |
| 50 | {recipient: EmailRecipient{Email: "[email protected]", FirstName: "René"}, want: "=?utf-8?q?Ren=C3=A9?= <[email protected]>"}, |
| 51 | {recipient: EmailRecipient{Email: "[email protected]", FirstName: "John", LastName: "García"}, want: "=?utf-8?q?John_Garc=C3=ADa?= <[email protected]>"}, |
| 52 | {recipient: EmailRecipient{Email: "[email protected]", FirstName: "🎉"}, want: "=?utf-8?q?=F0=9F=8E=89?= <[email protected]>"}, |
| 53 | } |
| 54 | |
| 55 | for _, tt := range tests { |
| 56 | if got := tt.recipient.NameAndEmail(); got != tt.want { |
| 57 | t.Errorf("NameAndEmail() = %q, want %q", got, tt.want) |
| 58 | } |
| 59 | } |
| 60 | } |
| 61 | |
| 62 | func TestIsLastDayOfTheMonth(t *testing.T) { |
| 63 | tests := []struct { |
| 64 | name string |
| 65 | date time.Time |
| 66 | want bool |
| 67 | }{ |
| 68 | { |
| 69 | name: "last day of January (31st)", |
| 70 | date: time.Date(2023, time.January, 31, 10, 30, 0, 0, time.UTC), |
| 71 | want: true, |
| 72 | }, |
| 73 | { |
| 74 | name: "not last day of January (30th)", |
| 75 | date: time.Date(2023, time.January, 30, 10, 30, 0, 0, time.UTC), |
| 76 | want: false, |
| 77 | }, |
| 78 | { |
| 79 | name: "last day of February in non-leap year (28th)", |
| 80 | date: time.Date(2023, time.February, 28, 10, 30, 0, 0, time.UTC), |
| 81 | want: true, |
| 82 | }, |
| 83 | { |
| 84 | name: "not last day of February in non-leap year (27th)", |
| 85 | date: time.Date(2023, time.February, 27, 10, 30, 0, 0, time.UTC), |
| 86 | want: false, |
| 87 | }, |
| 88 | { |
| 89 | name: "last day of February in leap year (29th)", |
| 90 | date: time.Date(2024, time.February, 29, 10, 30, 0, 0, time.UTC), |
| 91 | want: true, |
| 92 | }, |
| 93 | { |
| 94 | name: "not last day of February in leap year (28th)", |
| 95 | date: time.Date(2024, time.February, 28, 10, 30, 0, 0, time.UTC), |
| 96 | want: false, |
| 97 | }, |
| 98 | { |
| 99 | name: "first day of month (1st)", |
| 100 | date: time.Date(2023, time.March, 1, 10, 30, 0, 0, time.UTC), |
| 101 | want: false, |
| 102 | }, |
| 103 | { |
| 104 | name: "middle of month (15th)", |
| 105 | date: time.Date(2023, time.March, 15, 10, 30, 0, 0, time.UTC), |
| 106 | want: false, |
| 107 | }, |
| 108 | } |
| 109 | |
| 110 | for _, tt := range tests { |
| 111 | t.Run(tt.name, func(t *testing.T) { |
| 112 | result := IsLastDayOfTheMonth(tt.date) |
| 113 | if result != tt.want { |
| 114 | t.Errorf("IsLastDayOfTheMonth(%v) = %v, want %v", |
| 115 | tt.date, result, tt.want) |
| 116 | } |
| 117 | }) |
| 118 | } |
| 119 | } |
| 120 | |
| 121 | func TestParseBool(t *testing.T) { |
| 122 | tests := []struct { |
| 123 | value string |
| 124 | want bool |
| 125 | wantError bool |
| 126 | }{ |
| 127 | // Accepted values |
| 128 | { |
| 129 | value: "yes", |
| 130 | want: true, |
| 131 | wantError: false, |
| 132 | }, |
| 133 | { |
| 134 | value: "YES", |
| 135 | want: true, |
| 136 | wantError: false, |
| 137 | }, |
| 138 | { |
| 139 | value: "Yes", |
| 140 | want: true, |
| 141 | wantError: false, |
| 142 | }, |
| 143 | { |
| 144 | value: "no", |
| 145 | want: false, |
| 146 | wantError: false, |
| 147 | }, |
| 148 | { |
| 149 | value: "NO", |
| 150 | want: false, |
| 151 | wantError: false, |
| 152 | }, |
| 153 | { |
| 154 | value: "No", |
| 155 | want: false, |
| 156 | wantError: false, |
| 157 | }, |
| 158 | { |
| 159 | value: "true", |
| 160 | want: true, |
| 161 | wantError: false, |
| 162 | }, |
| 163 | { |
| 164 | value: "false", |
| 165 | want: false, |
| 166 | wantError: false, |
| 167 | }, |
| 168 | { |
| 169 | value: "1", |
| 170 | want: true, |
| 171 | wantError: false, |
| 172 | }, |
| 173 | { |
| 174 | value: "0", |
| 175 | want: false, |
| 176 | wantError: false, |
| 177 | }, |
| 178 | { |
| 179 | value: "t", |
| 180 | want: true, |
| 181 | wantError: false, |
| 182 | }, |
| 183 | { |
| 184 | value: "f", |
| 185 | want: false, |
| 186 | wantError: false, |
| 187 | }, |
| 188 | { |
| 189 | value: "TRUE", |
| 190 | want: true, |
| 191 | wantError: false, |
| 192 | }, |
| 193 | { |
| 194 | value: "FALSE", |
| 195 | want: false, |
| 196 | wantError: false, |
| 197 | }, |
| 198 | |
| 199 | // Errors |
| 200 | { |
| 201 | value: "invalid", |
| 202 | want: false, |
| 203 | wantError: true, |
| 204 | }, |
| 205 | { |
| 206 | value: "", |
| 207 | want: false, |
| 208 | wantError: true, |
| 209 | }, |
| 210 | { |
| 211 | value: "123", |
| 212 | want: false, |
| 213 | wantError: true, |
| 214 | }, |
| 215 | { |
| 216 | value: " ", |
| 217 | want: false, |
| 218 | wantError: true, |
| 219 | }, |
| 220 | } |
| 221 | |
| 222 | for _, tt := range tests { |
| 223 | t.Run(tt.value, func(t *testing.T) { |
| 224 | result, err := ParseBool(tt.value) |
| 225 | |
| 226 | if tt.wantError { |
| 227 | if err == nil { |
| 228 | t.Errorf("ParseBool(%q) expected error, but got none", tt.value) |
| 229 | } |
| 230 | } else { |
| 231 | if err != nil { |
| 232 | t.Errorf("ParseBool(%q) unexpected error: %v", tt.value, err) |
| 233 | } |
| 234 | } |
| 235 | |
| 236 | if result != tt.want { |
| 237 | t.Errorf("ParseBool(%q) = %v, want %v", tt.value, result, tt.want) |
| 238 | } |
| 239 | }) |
| 240 | } |
| 241 | } |
| 242 | |
| 243 | func TestValidateEmail(t *testing.T) { |
| 244 | tests := []struct { |
| 245 | email string |
| 246 | wantError bool |
| 247 | }{ |
| 248 | // Valid email addresses (RFC5322) |
| 249 | { |
| 250 | email: "root@localhost", |
| 251 | wantError: false, |
| 252 | }, |
| 253 | { |
| 254 | email: "[email protected]", |
| 255 | wantError: false, |
| 256 | }, |
| 257 | { |
| 258 | email: "[email protected]", |
| 259 | wantError: false, |
| 260 | }, |
| 261 | { |
| 262 | email: "[email protected]", |
| 263 | wantError: false, |
| 264 | }, |
| 265 | { |
| 266 | email: "[email protected]", |
| 267 | wantError: false, |
| 268 | }, |
| 269 | { |
| 270 | email: "[email protected]", |
| 271 | wantError: false, |
| 272 | }, |
| 273 | |
| 274 | // Valid local-only addresses (for local Postfix delivery) |
| 275 | { |
| 276 | email: "user123", |
| 277 | wantError: false, |
| 278 | }, |
| 279 | { |
| 280 | email: "first.last", |
| 281 | wantError: false, |
| 282 | }, |
| 283 | { |
| 284 | email: "user_name", |
| 285 | wantError: false, |
| 286 | }, |
| 287 | { |
| 288 | email: "test-user", |
| 289 | wantError: false, |
| 290 | }, |
| 291 | { |
| 292 | email: "mail.example.com", // becomes mail.example.com@localhost |
| 293 | wantError: false, |
| 294 | }, |
| 295 | { |
| 296 | email: "domain!user", // becomes user@domain (legacy UUCP mapping) |
| 297 | wantError: false, |
| 298 | }, |
| 299 | |
| 300 | // Some mail servers accept emails without a local part |
| 301 | { |
| 302 | email: "@localhost", |
| 303 | wantError: false, |
| 304 | }, |
| 305 | { |
| 306 | email: "@mail.example.com", |
| 307 | wantError: false, |
| 308 | }, |
| 309 | |
| 310 | // Invalid email addresses |
| 311 | { |
| 312 | email: "", |
| 313 | wantError: true, |
| 314 | }, |
| 315 | { |
| 316 | email: "root@", |
| 317 | wantError: true, |
| 318 | }, |
| 319 | { |
| 320 | email: "@", |
| 321 | wantError: true, |
| 322 | }, |
| 323 | { |
| 324 | email: "user @example.com", |
| 325 | wantError: true, |
| 326 | }, |
| 327 | { |
| 328 | email: "[email protected]\nBcc: [email protected]", |
| 329 | wantError: true, |
| 330 | }, |
| 331 | { |
| 332 | email: "[email protected]\rBcc: [email protected]", |
| 333 | wantError: true, |
| 334 | }, |
| 335 | { |
| 336 | email: "[email protected]\x00", |
| 337 | wantError: true, |
| 338 | }, |
| 339 | { |
| 340 | email: "user@@example.com", |
| 341 | wantError: true, |
| 342 | }, |
| 343 | { |
| 344 | email: "John Doe <[email protected]>", |
| 345 | wantError: true, |
| 346 | }, |
| 347 | |
| 348 | // Length boundary tests |
| 349 | { |
| 350 | // 254 bytes: valid (maximum allowed length) |
| 351 | email: fmt.Sprintf("%s@%s.com", strings.Repeat("a", 64), strings.Repeat("b", 185)), |
| 352 | wantError: false, |
| 353 | }, |
| 354 | { |
| 355 | // 255 bytes: invalid (exceeds maximum allowed length) |
| 356 | email: fmt.Sprintf("%s@%s.com", strings.Repeat("a", 64), strings.Repeat("b", 186)), |
| 357 | wantError: true, |
| 358 | }, |
| 359 | } |
| 360 | |
| 361 | for _, tt := range tests { |
| 362 | t.Run(tt.email, func(t *testing.T) { |
| 363 | err := ValidateEmail(tt.email) |
| 364 | |
| 365 | if tt.wantError { |
| 366 | if err == nil { |
| 367 | t.Errorf("ValidateEmail(%q) expected error, but got none", tt.email) |
| 368 | } |
| 369 | } else { |
| 370 | if err != nil { |
| 371 | t.Errorf("ValidateEmail(%q) unexpected error: %v", tt.email, err) |
| 372 | } |
| 373 | } |
| 374 | }) |
| 375 | } |
| 376 | } |
| 377 | |
| 378 | func TestAssembleRawMessage(t *testing.T) { |
| 379 | const ( |
| 380 | defaultVersion = "0.0.1" |
| 381 | fixedHeadersFormat = "X-Auto-Response-Suppress: All\r\n" + |
| 382 | "Content-Language: en\r\n" + |
| 383 | "User-Agent: email-reminder/%s\r\n" + |
| 384 | "MIME-Version: 1.0\r\n" + |
| 385 | "Content-Type: text/plain; charset=UTF-8\r\n" + |
| 386 | "Content-Disposition: inline\r\n" + |
| 387 | "Content-Transfer-Encoding: quoted-printable\r\n" |
| 388 | ) |
| 389 | tests := []struct { |
| 390 | name string |
| 391 | body string |
| 392 | subject string |
| 393 | recipient string |
| 394 | sender string |
| 395 | appVersion string |
| 396 | want string |
| 397 | }{ |
| 398 | { |
| 399 | name: "simple message", |
| 400 | body: "This is a test message.", |
| 401 | subject: "Test Subject", |
| 402 | recipient: "[email protected]", |
| 403 | sender: "[email protected]", |
| 404 | appVersion: defaultVersion, |
| 405 | want: fmt.Sprintf( |
| 406 | "From: Email-Reminder <[email protected]>\r\n"+ |
| 407 | "To: [email protected]\r\n"+ |
| 408 | "Subject: Test Subject\r\n"+ |
| 409 | fixedHeadersFormat+ |
| 410 | "\r\nThis is a test message.", |
| 411 | defaultVersion, |
| 412 | ), |
| 413 | }, |
| 414 | { |
| 415 | name: "multi-line body", |
| 416 | body: "Line 1\nLine 2\nLine 3", |
| 417 | subject: "Multi-line", |
| 418 | recipient: "[email protected]", |
| 419 | sender: "[email protected]", |
| 420 | appVersion: defaultVersion, |
| 421 | want: fmt.Sprintf( |
| 422 | "From: Email-Reminder <[email protected]>\r\n"+ |
| 423 | "To: [email protected]\r\n"+ |
| 424 | "Subject: Multi-line\r\n"+ |
| 425 | fixedHeadersFormat+ |
| 426 | "\r\nLine 1\r\nLine 2\r\nLine 3", |
| 427 | defaultVersion, |
| 428 | ), |
| 429 | }, |
| 430 | { |
| 431 | name: "unicode subject", |
| 432 | body: "Test with unicode subject", |
| 433 | subject: "Tëst Sübject with émojis 🎉", |
| 434 | recipient: "[email protected]", |
| 435 | sender: "[email protected]", |
| 436 | appVersion: defaultVersion, |
| 437 | want: fmt.Sprintf( |
| 438 | "From: Email-Reminder <[email protected]>\r\n"+ |
| 439 | "To: [email protected]\r\n"+ |
| 440 | "Subject: =?UTF-8?q?T=C3=ABst_S=C3=BCbject_with_=C3=A9mojis_=F0=9F=8E=89?=\r\n"+ |
| 441 | fixedHeadersFormat+ |
| 442 | "\r\nTest with unicode subject", |
| 443 | defaultVersion, |
| 444 | ), |
| 445 | }, |
| 446 | { |
| 447 | name: "special characters in body", |
| 448 | body: "Body with special chars: äöü & < > \"", |
| 449 | subject: "Special Chars", |
| 450 | recipient: "[email protected]", |
| 451 | sender: "[email protected]", |
| 452 | appVersion: defaultVersion, |
| 453 | want: fmt.Sprintf( |
| 454 | "From: Email-Reminder <[email protected]>\r\n"+ |
| 455 | "To: [email protected]\r\n"+ |
| 456 | "Subject: Special Chars\r\n"+ |
| 457 | fixedHeadersFormat+ |
| 458 | "\r\nBody with special chars: =C3=A4=C3=B6=C3=BC & < > \"", |
| 459 | defaultVersion, |
| 460 | ), |
| 461 | }, |
| 462 | { |
| 463 | name: "empty body", |
| 464 | body: "", |
| 465 | subject: "Empty Body", |
| 466 | recipient: "[email protected]", |
| 467 | sender: "[email protected]", |
| 468 | appVersion: defaultVersion, |
| 469 | want: fmt.Sprintf( |
| 470 | "From: Email-Reminder <[email protected]>\r\n"+ |
| 471 | "To: [email protected]\r\n"+ |
| 472 | "Subject: Empty Body\r\n"+ |
| 473 | fixedHeadersFormat+ |
| 474 | "\r\n", |
| 475 | defaultVersion, |
| 476 | ), |
| 477 | }, |
| 478 | { |
| 479 | name: "long line that needs quoted-printable encoding", |
| 480 | body: "This is a very long line that exceeds the typical 76 character limit for email lines and should be properly encoded using quoted-printable encoding to ensure proper email delivery.", |
| 481 | subject: "Long Line Test", |
| 482 | recipient: "[email protected]", |
| 483 | sender: "[email protected]", |
| 484 | appVersion: "1.2.3-test", |
| 485 | want: fmt.Sprintf( |
| 486 | "From: Email-Reminder <[email protected]>\r\n"+ |
| 487 | "To: [email protected]\r\n"+ |
| 488 | "Subject: Long Line Test\r\n"+ |
| 489 | fixedHeadersFormat+ |
| 490 | "\r\nThis is a very long line that exceeds the typical 76 character limit for em=\r\n"+ |
| 491 | "ail lines and should be properly encoded using quoted-printable encoding to=\r\n"+ |
| 492 | " ensure proper email delivery.", |
| 493 | "1.2.3-test", |
| 494 | ), |
| 495 | }, |
| 496 | } |
| 497 | |
| 498 | for _, tt := range tests { |
| 499 | t.Run(tt.name, func(t *testing.T) { |
| 500 | result := AssembleRawMessage(tt.body, tt.subject, tt.recipient, tt.sender, tt.appVersion) |
| 501 | |
| 502 | // Validate that the result is a properly formatted email message |
| 503 | validateEmailMessage(t, result, len(tt.body) == 0) |
| 504 | |
| 505 | // Also check that it matches expected output |
| 506 | if result != tt.want { |
| 507 | t.Errorf("AssembleRawMessage() = %q, want %q", result, tt.want) |
| 508 | } |
| 509 | }) |
| 510 | } |
| 511 | } |
| 512 | |
| 513 | func TestValidateHomeDirectory(t *testing.T) { |
| 514 | tempDir := t.TempDir() |
| 515 | |
| 516 | // Test case 1: Valid directory |
| 517 | err := ValidateHomeDirectory(tempDir, uint32(os.Getuid())) |
| 518 | if err != nil { |
| 519 | t.Errorf("ValidateHomeDirectory() for valid directory failed: %v", err) |
| 520 | } |
| 521 | |
| 522 | // Test case 2: Non-existent directory |
| 523 | err = ValidateHomeDirectory("/non/existent/path", uint32(os.Getuid())) |
| 524 | if err == nil { |
| 525 | t.Error("ValidateHomeDirectory() should fail for non-existent directory") |
| 526 | } |
| 527 | |
| 528 | // Test case 3: Create a symlink to test symlink detection |
| 529 | symlinkPath := filepath.Join(tempDir, "symlink") |
| 530 | targetPath := filepath.Join(tempDir, "target") |
| 531 | os.Mkdir(targetPath, 0755) |
| 532 | err = os.Symlink(targetPath, symlinkPath) |
| 533 | if err != nil { |
| 534 | t.Fatalf("Failed to create test symlink: %v", err) |
| 535 | } |
| 536 | |
| 537 | err = ValidateHomeDirectory(symlinkPath, uint32(os.Getuid())) |
| 538 | if err == nil { |
| 539 | t.Error("ValidateHomeDirectory() should fail for symlink") |
| 540 | } |
| 541 | if !strings.Contains(err.Error(), "symlink") { |
| 542 | t.Errorf("Error message should mention symlink, got: %v", err) |
| 543 | } |
| 544 | |
| 545 | // Test case 4: Wrong owner (we can't easily test this without root permissions, |
| 546 | // but we can test the error handling by passing an invalid UID) |
| 547 | err = ValidateHomeDirectory(tempDir, uint32(0xFFFFFFFF)) // Use max uint32 as invalid UID |
| 548 | if err == nil { |
| 549 | t.Error("ValidateHomeDirectory() should fail for wrong owner") |
| 550 | } |
| 551 | } |
| 552 | |
| 553 | func TestValidateOpenFile(t *testing.T) { |
| 554 | // Test case 1: Correctly owned regular file |
| 555 | tempFile := filepath.Join(t.TempDir(), "testfile") |
| 556 | file, err := os.Create(tempFile) |
| 557 | if err != nil { |
| 558 | t.Fatalf("Failed to create test file: %v", err) |
| 559 | } |
| 560 | defer file.Close() |
| 561 | |
| 562 | err = ValidateOpenFile(file, uint32(os.Getuid())) |
| 563 | if err != nil { |
| 564 | t.Errorf("ValidateOpenFile() for correctly owned file failed: %v", err) |
| 565 | } |
| 566 | |
| 567 | // Test case 2: Wrong owner |
| 568 | err = ValidateOpenFile(file, uint32(0xFFFFFFFF)) // Use max uint32 as invalid UID |
| 569 | if err == nil { |
| 570 | t.Error("ValidateOpenFile() should fail for wrong owner") |
| 571 | } |
| 572 | if !strings.Contains(err.Error(), "not owned by uid") { |
| 573 | t.Errorf("Error message should mention ownership, got: %v", err) |
| 574 | } |
| 575 | |
| 576 | // Test case 3: Not a regular file (directory) |
| 577 | tempDir := t.TempDir() |
| 578 | dirFile, err := os.Open(tempDir) |
| 579 | if err != nil { |
| 580 | t.Fatalf("Failed to open temp directory: %v", err) |
| 581 | } |
| 582 | defer dirFile.Close() |
| 583 | |
| 584 | err = ValidateOpenFile(dirFile, uint32(os.Getuid())) |
| 585 | if err == nil { |
| 586 | t.Error("ValidateOpenFile() should fail for directory") |
| 587 | } |
| 588 | if !strings.Contains(err.Error(), "not a regular file") { |
| 589 | t.Errorf("Error message should mention regular file, got: %v", err) |
| 590 | } |
| 591 | } |
| 592 | |
| 593 | func TestFullBody(t *testing.T) { |
| 594 | tests := []struct { |
| 595 | name string |
| 596 | body string |
| 597 | recipient EmailRecipient |
| 598 | version string |
| 599 | want string |
| 600 | }{ |
| 601 | { |
| 602 | name: "without a name", |
| 603 | body: "Your appointment is scheduled for 2pm.", |
| 604 | recipient: EmailRecipient{Email: "[email protected]"}, |
| 605 | version: "0.8.3", |
| 606 | want: `Hi there, |
| 607 | |
| 608 | Your appointment is scheduled for 2pm. |
| 609 | |
| 610 | Have a good day! |
| 611 | |
| 612 | -- |
| 613 | Sent by Email-Reminder 0.8.3 |
| 614 | https://launchpad.net/email-reminder`, |
| 615 | }, |
| 616 | { |
| 617 | name: "with only a last name", |
| 618 | body: "Test message", |
| 619 | recipient: EmailRecipient{FirstName: "", LastName: "Smith", Email: "[email protected]"}, |
| 620 | version: "1.0.0", |
| 621 | want: `Hi there, |
| 622 | |
| 623 | Test message |
| 624 | |
| 625 | Have a good day! |
| 626 | |
| 627 | -- |
| 628 | Sent by Email-Reminder 1.0.0 |
| 629 | https://launchpad.net/email-reminder`, |
| 630 | }, |
| 631 | { |
| 632 | name: "with name and multiline body", |
| 633 | body: "Line 1\nLine 2\nLine 3", |
| 634 | recipient: EmailRecipient{FirstName: "Alice", Email: "[email protected]"}, |
| 635 | version: "0.8.3", |
| 636 | want: `Hi Alice, |
| 637 | |
| 638 | Line 1 |
| 639 | Line 2 |
| 640 | Line 3 |
| 641 | |
| 642 | Have a good day! |
| 643 | |
| 644 | -- |
| 645 | Sent by Email-Reminder 0.8.3 |
| 646 | https://launchpad.net/email-reminder`, |
| 647 | }, |
| 648 | } |
| 649 | |
| 650 | for _, tt := range tests { |
| 651 | t.Run(tt.name, func(t *testing.T) { |
| 652 | got := FullBody(tt.body, tt.recipient, tt.version) |
| 653 | if got != tt.want { |
| 654 | t.Errorf("FullBody() = %q, want %q", got, tt.want) |
| 655 | } |
| 656 | }) |
| 657 | } |
| 658 | } |
| 659 | |
| 660 | func TestSecureExecCommand(t *testing.T) { |
| 661 | // Test with a legitimate system binary (should exist and be owned by root) |
| 662 | t.Run("valid_system_binary", func(t *testing.T) { |
| 663 | // Try /bin/echo which should be safe and available on most systems |
| 664 | output, err := SecureExecCommand("/bin/echo", "test") |
| 665 | if err != nil { |
| 666 | // This might fail if /bin/echo doesn't exist or isn't owned by root |
| 667 | // which is acceptable for this security-focused test |
| 668 | t.Logf("Expected error for system binary (may not be root-owned in test environment): %v", err) |
| 669 | return |
| 670 | } |
| 671 | if len(output) == 0 { |
| 672 | t.Error("Expected non-empty output from /bin/echo") |
| 673 | } |
| 674 | }) |
| 675 | |
| 676 | t.Run("nonexistent_binary", func(t *testing.T) { |
| 677 | _, err := SecureExecCommand("/nonexistent/binary") |
| 678 | if err == nil { |
| 679 | t.Error("Expected error for nonexistent binary") |
| 680 | } |
| 681 | if !strings.Contains(err.Error(), "cannot stat executable") { |
| 682 | t.Errorf("Expected 'cannot stat executable' error, got: %v", err) |
| 683 | } |
| 684 | }) |
| 685 | |
| 686 | t.Run("non_root_owned_binary", func(t *testing.T) { |
| 687 | // Create a temporary executable owned by the current user |
| 688 | tmpDir := t.TempDir() |
| 689 | tmpScript := filepath.Join(tmpDir, "test_script") |
| 690 | |
| 691 | // Create a simple shell script |
| 692 | content := []byte("#!/bin/sh\necho test\n") |
| 693 | if err := os.WriteFile(tmpScript, content, 0755); err != nil { |
| 694 | t.Fatalf("Failed to create test script: %v", err) |
| 695 | } |
| 696 | |
| 697 | // Try to execute it - should fail because it's not owned by root |
| 698 | _, err := SecureExecCommand(tmpScript) |
| 699 | if err == nil { |
| 700 | t.Error("Expected error for non-root owned binary") |
| 701 | } |
| 702 | if !strings.Contains(err.Error(), "not owned by root") { |
| 703 | t.Errorf("Expected 'not owned by root' error, got: %v", err) |
| 704 | } |
| 705 | }) |
| 706 | } |