@@ -181,29 +181,35 @@ func TestUDPv5_handshakeRepeatChallenge(t *testing.T) {
181181 nonce1 := v5wire.Nonce {1 }
182182 nonce2 := v5wire.Nonce {2 }
183183 nonce3 := v5wire.Nonce {3 }
184- check := func (p * v5wire.Whoareyou , wantNonce v5wire.Nonce ) {
184+ var firstAuthTag * v5wire.Nonce
185+ check := func (p * v5wire.Whoareyou , authTag , wantNonce v5wire.Nonce ) {
185186 t .Helper ()
186187 if p .Nonce != wantNonce {
187- t .Error ("wrong nonce in WHOAREYOU:" , p .Nonce , wantNonce )
188+ t .Error ("wrong nonce in WHOAREYOU:" , p .Nonce , "want:" , wantNonce )
189+ }
190+ if firstAuthTag == nil {
191+ firstAuthTag = & authTag
192+ } else if authTag != * firstAuthTag {
193+ t .Error ("wrong auth tag in WHOAREYOU header:" , authTag , "want:" , * firstAuthTag )
188194 }
189195 }
190196
191197 // Unknown packet from unknown node.
192198 test .packetIn (& v5wire.Unknown {Nonce : nonce1 })
193- test .waitPacketOut (func (p * v5wire.Whoareyou , addr netip.AddrPort , _ v5wire.Nonce ) {
194- check (p , nonce1 )
199+ test .waitPacketOut (func (p * v5wire.Whoareyou , addr netip.AddrPort , authTag v5wire.Nonce ) {
200+ check (p , authTag , nonce1 )
195201 })
196202
197203 // Second unknown packet. Here we expect the response to reference the
198204 // first unknown packet.
199205 test .packetIn (& v5wire.Unknown {Nonce : nonce2 })
200- test .waitPacketOut (func (p * v5wire.Whoareyou , addr netip.AddrPort , _ v5wire.Nonce ) {
201- check (p , nonce1 )
206+ test .waitPacketOut (func (p * v5wire.Whoareyou , addr netip.AddrPort , authTag v5wire.Nonce ) {
207+ check (p , authTag , nonce1 )
202208 })
203209 // Third unknown packet. This should still return the first nonce.
204210 test .packetIn (& v5wire.Unknown {Nonce : nonce3 })
205- test .waitPacketOut (func (p * v5wire.Whoareyou , addr netip.AddrPort , _ v5wire.Nonce ) {
206- check (p , nonce1 )
211+ test .waitPacketOut (func (p * v5wire.Whoareyou , addr netip.AddrPort , authTag v5wire.Nonce ) {
212+ check (p , authTag , nonce1 )
207213 })
208214}
209215
@@ -766,20 +772,30 @@ type testCodecFrame struct {
766772}
767773
768774func (c * testCodec ) Encode (toID enode.ID , addr string , p v5wire.Packet , _ * v5wire.Whoareyou ) ([]byte , v5wire.Nonce , error ) {
775+ // To match the behavior of v5wire.Codec, we return the cached encoding of
776+ // WHOAREYOU challenges.
777+ if wp , ok := p .(* v5wire.Whoareyou ); ok && len (wp .Encoded ) > 0 {
778+ return wp .Encoded , wp .Nonce , nil
779+ }
780+
769781 c .ctr ++
770782 var authTag v5wire.Nonce
771783 binary .BigEndian .PutUint64 (authTag [:], c .ctr )
784+ penc , _ := rlp .EncodeToBytes (p )
785+ frame , err := rlp .EncodeToBytes (testCodecFrame {c .id , authTag , p .Kind (), penc })
786+ if err != nil {
787+ return frame , authTag , err
788+ }
772789
790+ // Store recently sent challenges.
773791 if w , ok := p .(* v5wire.Whoareyou ); ok {
774- // Store recently sent Whoareyou challenges.
792+ w .Nonce = authTag
793+ w .Encoded = frame
775794 if c .sentChallenges == nil {
776795 c .sentChallenges = make (map [enode.ID ]* v5wire.Whoareyou )
777796 }
778797 c .sentChallenges [toID ] = w
779798 }
780-
781- penc , _ := rlp .EncodeToBytes (p )
782- frame , err := rlp .EncodeToBytes (testCodecFrame {c .id , authTag , p .Kind (), penc })
783799 return frame , authTag , err
784800}
785801
0 commit comments