// Copyright 2023 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

//go:build go1.21

package quic

import (
	"bytes"
	"context"
	"crypto/rand"
	"crypto/tls"
	"errors"
	"net/netip"
	"testing"
	"time"
)

func TestStatelessResetClientSendsStatelessResetTokenTransportParameter(t *testing.T) {
	// "[The stateless_reset_token] transport parameter MUST NOT be sent by a client [...]"
	// https://www.rfc-editor.org/rfc/rfc9000#section-18.2-4.6.1
	resetToken := testPeerStatelessResetToken(0)
	tc := newTestConn(t, serverSide, func(p *transportParameters) {
		p.statelessResetToken = resetToken[:]
	})
	tc.writeFrames(packetTypeInitial,
		debugFrameCrypto{
			data: tc.cryptoDataIn[tls.QUICEncryptionLevelInitial],
		})
	tc.wantFrame("client provided stateless_reset_token transport parameter",
		packetTypeInitial, debugFrameConnectionCloseTransport{
			code: errTransportParameter,
		})
}

var testStatelessResetKey = func() (key [32]byte) {
	if _, err := rand.Read(key[:]); err != nil {
		panic(err)
	}
	return key
}()

func testStatelessResetToken(cid []byte) statelessResetToken {
	var gen statelessResetTokenGenerator
	gen.init(testStatelessResetKey)
	return gen.tokenForConnID(cid)
}

func testLocalStatelessResetToken(seq int64) statelessResetToken {
	return testStatelessResetToken(testLocalConnID(seq))
}

func newDatagramForReset(cid []byte, size int, addr netip.AddrPort) *datagram {
	dgram := append([]byte{headerFormShort | fixedBit}, cid...)
	for len(dgram) < size {
		dgram = append(dgram, byte(len(dgram))) // semi-random junk
	}
	return &datagram{
		b:    dgram,
		addr: addr,
	}
}

func TestStatelessResetSentSizes(t *testing.T) {
	config := &Config{
		TLSConfig:         newTestTLSConfig(serverSide),
		StatelessResetKey: testStatelessResetKey,
	}
	addr := netip.MustParseAddr("127.0.0.1")
	tl := newTestListener(t, config)
	for i, test := range []struct {
		reqSize  int
		wantSize int
	}{{
		// Datagrams larger than 42 bytes result in a 42-byte stateless reset.
		// This isn't specifically mandated by RFC 9000, but is implied.
		// https://www.rfc-editor.org/rfc/rfc9000#section-10.3-11
		reqSize:  1200,
		wantSize: 42,
	}, {
		// "An endpoint that sends a Stateless Reset in response to a packet
		// that is 43 bytes or shorter SHOULD send a Stateless Reset that is
		// one byte shorter than the packet it responds to."
		// https://www.rfc-editor.org/rfc/rfc9000#section-10.3-11
		reqSize:  43,
		wantSize: 42,
	}, {
		reqSize:  42,
		wantSize: 41,
	}, {
		// We should send a stateless reset in response to the smallest possible
		// valid datagram the peer can send us.
		// The smallest packet is 1-RTT:
		// header byte, conn id, packet num, payload, AEAD.
		reqSize:  1 + connIDLen + 1 + 1 + 16,
		wantSize: 1 + connIDLen + 1 + 1 + 16 - 1,
	}, {
		// The smallest possible stateless reset datagram is 21 bytes.
		// Since our response must be smaller than the incoming datagram,
		// we must not respond to a 21 byte or smaller packet.
		reqSize:  21,
		wantSize: 0,
	}} {
		cid := testLocalConnID(int64(i))
		token := testStatelessResetToken(cid)
		addrport := netip.AddrPortFrom(addr, uint16(8000+i))
		tl.write(newDatagramForReset(cid, test.reqSize, addrport))

		got := tl.read()
		if len(got) != test.wantSize {
			t.Errorf("got %v-byte response to %v-byte req, want %v",
				len(got), test.reqSize, test.wantSize)
		}
		if len(got) == 0 {
			continue
		}
		// "Endpoints MUST send Stateless Resets formatted as
		// a packet with a short header."
		// https://www.rfc-editor.org/rfc/rfc9000#section-10.3-15
		if isLongHeader(got[0]) {
			t.Errorf("response to %v-byte request is not a short-header packet\ngot: %x", test.reqSize, got)
		}
		if !bytes.HasSuffix(got, token[:]) {
			t.Errorf("response to %v-byte request does not end in stateless reset token\ngot: %x\nwant suffix: %x", test.reqSize, got, token)
		}
	}
}

func TestStatelessResetSuccessfulNewConnectionID(t *testing.T) {
	// "[...] Stateless Reset Token field values from [...] NEW_CONNECTION_ID frames [...]"
	// https://www.rfc-editor.org/rfc/rfc9000#section-10.3.1-1
	tc := newTestConn(t, clientSide)
	tc.handshake()
	tc.ignoreFrame(frameTypeAck)

	// Retire connection ID 0.
	tc.writeFrames(packetType1RTT,
		debugFrameNewConnectionID{
			retirePriorTo: 1,
			seq:           2,
			connID:        testPeerConnID(2),
		})
	tc.wantFrame("peer requested we retire conn id 0",
		packetType1RTT, debugFrameRetireConnectionID{
			seq: 0,
		})

	resetToken := testPeerStatelessResetToken(1) // provided during handshake
	dgram := append(make([]byte, 100), resetToken[:]...)
	tc.listener.write(&datagram{
		b: dgram,
	})

	if err := tc.conn.Wait(canceledContext()); !errors.Is(err, errStatelessReset) {
		t.Errorf("conn.Wait() = %v, want errStatelessReset", err)
	}
	tc.wantIdle("closed connection is idle in draining")
	tc.advance(1 * time.Second) // long enough to exit the draining state
	tc.wantIdle("closed connection is idle after draining")
}

func TestStatelessResetSuccessfulTransportParameter(t *testing.T) {
	// "[...] Stateless Reset Token field values from [...]
	// the server's transport parameters [...]"
	// https://www.rfc-editor.org/rfc/rfc9000#section-10.3.1-1
	resetToken := testPeerStatelessResetToken(0)
	tc := newTestConn(t, clientSide, func(p *transportParameters) {
		p.statelessResetToken = resetToken[:]
	})
	tc.handshake()

	dgram := append(make([]byte, 100), resetToken[:]...)
	tc.listener.write(&datagram{
		b: dgram,
	})

	if err := tc.conn.Wait(canceledContext()); !errors.Is(err, errStatelessReset) {
		t.Errorf("conn.Wait() = %v, want errStatelessReset", err)
	}
	tc.wantIdle("closed connection is idle")
}

func TestStatelessResetSuccessfulPrefix(t *testing.T) {
	for _, test := range []struct {
		name   string
		prefix []byte
		size   int
	}{{
		name: "short header and fixed bit",
		prefix: []byte{
			headerFormShort | fixedBit,
		},
		size: 100,
	}, {
		// "[...] endpoints MUST treat [long header packets] ending in a
		// valid stateless reset token as a Stateless Reset [...]"
		// https://www.rfc-editor.org/rfc/rfc9000#section-10.3-15
		name: "long header no fixed bit",
		prefix: []byte{
			headerFormLong,
		},
		size: 100,
	}, {
		// "[...] the comparison MUST be performed when the first packet
		// in an incoming datagram [...] cannot be decrypted."
		// https://www.rfc-editor.org/rfc/rfc9000#section-10.3.1-2
		name: "short header valid DCID",
		prefix: append([]byte{
			headerFormShort | fixedBit,
		}, testLocalConnID(0)...),
		size: 100,
	}, {
		name: "handshake valid DCID",
		prefix: append([]byte{
			headerFormLong | fixedBit | longPacketTypeHandshake,
		}, testLocalConnID(0)...),
		size: 100,
	}, {
		name: "no fixed bit valid DCID",
		prefix: append([]byte{
			0,
		}, testLocalConnID(0)...),
		size: 100,
	}} {
		t.Run(test.name, func(t *testing.T) {
			resetToken := testPeerStatelessResetToken(0)
			tc := newTestConn(t, clientSide, func(p *transportParameters) {
				p.statelessResetToken = resetToken[:]
			})
			tc.handshake()

			dgram := test.prefix
			for len(dgram) < test.size-len(resetToken) {
				dgram = append(dgram, byte(len(dgram))) // semi-random junk
			}
			dgram = append(dgram, resetToken[:]...)
			tc.listener.write(&datagram{
				b: dgram,
			})
			if err := tc.conn.Wait(canceledContext()); !errors.Is(err, errStatelessReset) {
				t.Errorf("conn.Wait() = %v, want errStatelessReset", err)
			}
		})
	}
}

func TestStatelessResetRetiredConnID(t *testing.T) {
	// "An endpoint MUST NOT check for any stateless reset tokens [...]
	// for connection IDs that have been retired."
	// https://www.rfc-editor.org/rfc/rfc9000#section-10.3.1-3
	resetToken := testPeerStatelessResetToken(0)
	tc := newTestConn(t, clientSide, func(p *transportParameters) {
		p.statelessResetToken = resetToken[:]
	})
	tc.handshake()
	tc.ignoreFrame(frameTypeAck)

	// We retire connection ID 0.
	tc.writeFrames(packetType1RTT,
		debugFrameNewConnectionID{
			seq:           2,
			retirePriorTo: 1,
			connID:        testPeerConnID(2),
		})
	tc.wantFrame("peer asked for conn id 0 to be retired",
		packetType1RTT, debugFrameRetireConnectionID{
			seq: 0,
		})

	// Receive a stateless reset for connection ID 0.
	dgram := append(make([]byte, 100), resetToken[:]...)
	tc.listener.write(&datagram{
		b: dgram,
	})

	if err := tc.conn.Wait(canceledContext()); !errors.Is(err, context.Canceled) {
		t.Errorf("conn.Wait() = %v, want connection to be alive", err)
	}
}
