Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 32 additions & 7 deletions tests/zz_handshake_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -487,7 +487,7 @@ func TestHandshakeTrustPersistence(t *testing.T) {

// Verify trust store file exists. The store is written just after the
// peer is marked trusted in memory, so give the write a moment.
waitForFile(trustPathA, 2*time.Second)
waitForFile(trustPathA, 5*time.Second)
if _, err := os.Stat(trustPathA); err != nil {
t.Fatalf("trust store not created: %v", err)
}
Expand Down Expand Up @@ -616,12 +616,11 @@ func TestHandshakeTrustLoadVerify(t *testing.T) {
}
}

// Verify trust file was created and contains correct data
waitForFile(trustPath, 2*time.Second)
data, err := os.ReadFile(trustPath)
if err != nil {
t.Fatalf("read trust file: %v", err)
}
// Verify trust file was created and contains correct data. The
// handshake manager writes it from a goroutine after the in-memory state
// changes; the first write can hold only the pending request, so wait
// for one that lists the peer as trusted.
data := waitForTrustedInFile(t, trustPath, 5*time.Second)

var snap struct {
Trusted []struct {
Expand Down Expand Up @@ -756,6 +755,32 @@ func TestHandshakeTrustLoadFromDisk(t *testing.T) {
var _ = driver.Connect // keep driver import
var _ = os.Remove // keep os import

// waitForTrustedInFile returns the trust store's contents once it lists at
// least one trusted peer, failing the test if that has not happened within
// timeout.
func waitForTrustedInFile(t *testing.T, path string, timeout time.Duration) []byte {
t.Helper()
var last []byte
var lastErr error
for deadline := time.Now().Add(timeout); time.Now().Before(deadline); time.Sleep(10 * time.Millisecond) {
last, lastErr = os.ReadFile(path)
if lastErr != nil {
continue
}
var snap struct {
Trusted []json.RawMessage `json:"trusted"`
}
if json.Unmarshal(last, &snap) == nil && len(snap.Trusted) > 0 {
return last
}
}
if lastErr != nil {
t.Fatalf("read trust file: %v", lastErr)
}
t.Fatalf("trust file lists no trusted peer after %v: %s", timeout, last)
return nil
}

// waitForFile returns once path exists or the timeout has passed; the
// caller's own check reports a missing file.
func waitForFile(path string, timeout time.Duration) {
Expand Down
Loading