From 104751451579eae998195a658a58381decf464f6 Mon Sep 17 00:00:00 2001 From: Elle Mouton Date: Thu, 30 Mar 2023 11:45:34 +0200 Subject: [PATCH] wtclient: demo addr iterator panic This commit adds a test that shows that it is possible to cause the AddressIterator to panic if the `Next` method is ever called twice when the iterator is at the end of its list without Reset first being called. --- watchtower/wtclient/addr_iterator_test.go | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/watchtower/wtclient/addr_iterator_test.go b/watchtower/wtclient/addr_iterator_test.go index 1e58471dc..436be2a7e 100644 --- a/watchtower/wtclient/addr_iterator_test.go +++ b/watchtower/wtclient/addr_iterator_test.go @@ -216,4 +216,27 @@ func TestAddrIterator(t *testing.T) { iter.ReleaseLock(addr3) require.False(t, iter.HasLocked()) }) + + t.Run("calling Next twice without Reset panics", func(t *testing.T) { + t.Parallel() + + // This defer-function asserts that a panic does occur. + defer func() { + r := recover() + require.NotNilf(t, r, "the code did not panic") + }() + + // Initialise the iterator with addr1. + iter, err := newAddressIterator(addr1) + require.NoError(t, err) + + a1 := iter.Peek() + require.Equal(t, addr1, a1) + + _, err = iter.Next() + require.ErrorIs(t, err, ErrAddressesExhausted) + + _, err = iter.Next() + require.ErrorIs(t, err, ErrAddressesExhausted) + }) }