package crypto

import (
	"bytes"
	"crypto/aes"
	"testing"

	"github.com/stretchr/testify/require"

	"github.com/gotd/ige"

	"go.mau.fi/mautrix-telegram/pkg/gotd/bin"
)

func TestOldKeys(t *testing.T) {
	a := require.New(t)
	var (
		authKey Key
		msgKey  bin.Int128
	)
	for i := range authKey {
		authKey[i] = byte(i)
	}
	for i := range msgKey {
		msgKey[i] = byte(i)
	}

	data := bytes.Repeat([]byte("aboba"), 16)
	encrypted := make([]byte, len(data))
	{
		key, iv := OldKeys(authKey, msgKey, Server)
		a.Equal(key, bin.Int256{
			0xbb, 0x17, 0xb0, 0x7e, 0xb9, 0x11, 0x10, 0x64, 0x70, 0x98, 0xb0, 0x69, 0xbd, 0x1a, 0x9b, 0x6f,
			0xe5, 0xc4, 0xbc, 0xc3, 0xc3, 0x1f, 0x8e, 0x67, 0xe8, 0x31, 0xd0, 0x7a, 0x61, 0x08, 0x5f, 0x68,
		})
		a.Equal(iv, bin.Int256{
			0x51, 0x97, 0xfc, 0x1e, 0x25, 0xb4, 0x1f, 0xe3, 0x6f, 0x18, 0xb5, 0xa3, 0xa8, 0xb2, 0xb3, 0x6c,
			0xb2, 0xcb, 0x06, 0x1f, 0x1f, 0x15, 0x7b, 0x35, 0x14, 0xfe, 0x42, 0xe7, 0x4f, 0xb5, 0x83, 0x59,
		})

		block, err := aes.NewCipher(key[:])
		a.NoError(err)
		ige.EncryptBlocks(block, iv[:], encrypted, data)
	}
}
