|
4 | 4 | "fmt" |
5 | 5 | "os" |
6 | 6 | "path/filepath" |
| 7 | + "strings" |
7 | 8 | "testing" |
8 | 9 | "time" |
9 | 10 |
|
@@ -1276,7 +1277,10 @@ func TestStaleKeychainCleanupOnFallback(t *testing.T) { |
1276 | 1277 | } |
1277 | 1278 |
|
1278 | 1279 | // Cleanup |
1279 | | - _ = DeleteAuthField(PRIVATE_KEY) |
| 1280 | + err = DeleteAuthField(PRIVATE_KEY) |
| 1281 | + if err != nil { |
| 1282 | + t.Fatalf("DeleteAuthField failed: %v", err) |
| 1283 | + } |
1280 | 1284 | } |
1281 | 1285 |
|
1282 | 1286 | func TestFileNotCreatedOnReadOrDelete(t *testing.T) { |
@@ -1317,7 +1321,6 @@ func TestFileNotCreatedOnReadOrDelete(t *testing.T) { |
1317 | 1321 | } |
1318 | 1322 |
|
1319 | 1323 | func TestFileRemovedWhenEmpty(t *testing.T) { |
1320 | | - |
1321 | 1324 | profile, err := config.GetProfile() |
1322 | 1325 | if err != nil { |
1323 | 1326 | t.Fatalf("get profile: %v", err) |
@@ -1472,41 +1475,117 @@ func TestLoginUserCleansUpServiceAccountCredentials(t *testing.T) { |
1472 | 1475 | } |
1473 | 1476 |
|
1474 | 1477 | // Cleanup |
1475 | | - _ = LogoutUser() |
| 1478 | + err := LogoutUser() |
| 1479 | + if err != nil { |
| 1480 | + t.Fatalf("LogoutUser() failed: %v", err) |
| 1481 | + } |
1476 | 1482 | } |
1477 | 1483 |
|
1478 | | -func TestKeyringWriteCleansUpTextFile(t *testing.T) { |
1479 | | - keyring.MockInit() |
1480 | | - |
1481 | | - profile, err := config.GetProfile() |
1482 | | - if err != nil { |
1483 | | - t.Fatalf("get profile: %v", err) |
| 1484 | +func TestSetAuthFieldWithProfile(t *testing.T) { |
| 1485 | + tests := []struct { |
| 1486 | + description string |
| 1487 | + keyringMockErr error |
| 1488 | + setupCorruptFile bool |
| 1489 | + setupStaleFileVal string |
| 1490 | + key authFieldKey |
| 1491 | + value string |
| 1492 | + wantErr bool |
| 1493 | + errContains string |
| 1494 | + verifyInKeyring bool |
| 1495 | + verifyInFile bool |
| 1496 | + }{ |
| 1497 | + { |
| 1498 | + description: "keyring write succeeds and cleans up stale text file entry", |
| 1499 | + setupStaleFileVal: "old-file-val", |
| 1500 | + key: "test-sync-key", |
| 1501 | + value: "new-keyring-val", |
| 1502 | + wantErr: false, |
| 1503 | + verifyInKeyring: true, |
| 1504 | + }, |
| 1505 | + { |
| 1506 | + description: "keyring write succeeds but fallback file cleanup fails", |
| 1507 | + setupCorruptFile: true, |
| 1508 | + key: "test-corrupt-file-key", |
| 1509 | + value: "value", |
| 1510 | + wantErr: true, |
| 1511 | + errContains: "saved to keyring, but failed to clean up legacy fallback file", |
| 1512 | + }, |
| 1513 | + { |
| 1514 | + description: "keyring write fails and keyring cleanup fails with unexpected error", |
| 1515 | + keyringMockErr: fmt.Errorf("access denied to secure enclave"), |
| 1516 | + key: "test-keyring-delete-fail", |
| 1517 | + value: "value", |
| 1518 | + wantErr: true, |
| 1519 | + errContains: "cleanup stale key from keyring", |
| 1520 | + }, |
| 1521 | + { |
| 1522 | + description: "keyring write fails and keyring cleanup is ignored when keyring is unavailable", |
| 1523 | + keyringMockErr: fmt.Errorf("dbus daemon unavailable"), |
| 1524 | + key: "test-keyring-unavailable", |
| 1525 | + value: "value", |
| 1526 | + wantErr: false, |
| 1527 | + verifyInFile: true, |
| 1528 | + }, |
1484 | 1529 | } |
1485 | 1530 |
|
1486 | | - key := authFieldKey("test-sync-key") |
| 1531 | + for _, tt := range tests { |
| 1532 | + t.Run(tt.description, func(t *testing.T) { |
| 1533 | + profile, err := config.GetProfile() |
| 1534 | + if err != nil { |
| 1535 | + t.Fatalf("get profile: %v", err) |
| 1536 | + } |
| 1537 | + |
| 1538 | + if tt.keyringMockErr != nil { |
| 1539 | + keyring.MockInitWithError(tt.keyringMockErr) |
| 1540 | + } else { |
| 1541 | + keyring.MockInit() |
| 1542 | + } |
1487 | 1543 |
|
1488 | | - // Simulate old value in text file |
1489 | | - if err := setAuthFieldInEncodedTextFile(profile, key, "old-file-value"); err != nil { |
1490 | | - t.Fatalf("set in text file: %v", err) |
1491 | | - } |
| 1544 | + textFilePath := filepath.Join(config.GetProfileFolderPath(profile), textFileName) |
| 1545 | + if tt.setupCorruptFile { |
| 1546 | + if err := os.WriteFile(textFilePath, []byte("invalid-base64!!!"), 0o600); err != nil { |
| 1547 | + t.Fatalf("failed to write corrupt file: %v", err) |
| 1548 | + } |
| 1549 | + defer func() { |
| 1550 | + _ = os.Remove(textFilePath) |
| 1551 | + }() |
| 1552 | + } else if tt.setupStaleFileVal != "" { |
| 1553 | + if err := setAuthFieldInEncodedTextFile(profile, tt.key, tt.setupStaleFileVal); err != nil { |
| 1554 | + t.Fatalf("failed to set stale file value: %v", err) |
| 1555 | + } |
| 1556 | + } |
1492 | 1557 |
|
1493 | | - // Now set with setAuthFieldWithProfile (which succeeds in keyring) |
1494 | | - if err := setAuthFieldWithProfile(profile, key, "new-keyring-value"); err != nil { |
1495 | | - t.Fatalf("setAuthFieldWithProfile failed: %v", err) |
1496 | | - } |
| 1558 | + defer func() { |
| 1559 | + _ = deleteAuthFieldInEncodedTextFile(profile, tt.key) |
| 1560 | + _ = deleteAuthFieldInKeyring(profile, tt.key) |
| 1561 | + }() |
1497 | 1562 |
|
1498 | | - // Keyring has new value |
1499 | | - val, err := getAuthFieldFromKeyring(profile, key) |
1500 | | - if err != nil || val != "new-keyring-value" { |
1501 | | - t.Errorf("expected new-keyring-value in keyring, got %q (err: %v)", val, err) |
1502 | | - } |
| 1563 | + err = setAuthFieldWithProfile(profile, tt.key, tt.value) |
| 1564 | + if (err != nil) != tt.wantErr { |
| 1565 | + t.Fatalf("setAuthFieldWithProfile() error = %v, wantErr %v", err, tt.wantErr) |
| 1566 | + } |
1503 | 1567 |
|
1504 | | - // Text file should no longer have the old key |
1505 | | - _, err = getAuthFieldFromEncodedTextFile(profile, key) |
1506 | | - if err == nil { |
1507 | | - t.Errorf("expected key to be removed from text file when set in keyring") |
1508 | | - } |
| 1568 | + if tt.errContains != "" && !strings.Contains(err.Error(), tt.errContains) { |
| 1569 | + t.Errorf("expected error containing %q, got: %v", tt.errContains, err) |
| 1570 | + } |
1509 | 1571 |
|
1510 | | - // Cleanup |
1511 | | - _ = deleteAuthFieldWithProfile(profile, key) |
| 1572 | + if tt.verifyInKeyring { |
| 1573 | + got, err := getAuthFieldFromKeyring(profile, tt.key) |
| 1574 | + if err != nil || got != tt.value { |
| 1575 | + t.Errorf("expected %q in keyring, got %q (err: %v)", tt.value, got, err) |
| 1576 | + } |
| 1577 | + |
| 1578 | + if _, err := getAuthFieldFromEncodedTextFile(profile, tt.key); err == nil { |
| 1579 | + t.Errorf("expected key to be removed from text file") |
| 1580 | + } |
| 1581 | + } |
| 1582 | + |
| 1583 | + if tt.verifyInFile { |
| 1584 | + got, err := getAuthFieldFromEncodedTextFile(profile, tt.key) |
| 1585 | + if err != nil || got != tt.value { |
| 1586 | + t.Errorf("expected %q in text file, got %q (err: %v)", tt.value, got, err) |
| 1587 | + } |
| 1588 | + } |
| 1589 | + }) |
| 1590 | + } |
1512 | 1591 | } |
0 commit comments