package agentcomm

import (
	"crypto/x509"
	"testing"

	"github.com/stretchr/testify/require"
	"google.golang.org/grpc/security/advancedtls"
	storktest "isc.org/stork/server/test/dbmodel"
)

//nolint:gochecknoglobals
var CACertPEM = []byte(`-----BEGIN CERTIFICATE-----
MIIFFjCCAv6gAwIBAgIBATANBgkqhkiG9w0BAQsFADAzMQswCQYDVQQGEwJVUzES
MBAGA1UEChMJSVNDIFN0b3JrMRAwDgYDVQQDEwdSb290IENBMB4XDTIwMTIwODA4
MDc1M1oXDTMwMTIwODA4MDgwM1owMzELMAkGA1UEBhMCVVMxEjAQBgNVBAoTCUlT
QyBTdG9yazEQMA4GA1UEAxMHUm9vdCBDQTCCAiIwDQYJKoZIhvcNAQEBBQADggIP
ADCCAgoCggIBALgcYkndHQGFmLk8yi8+yetaCBI1cLG/nm+hwjh5C2rh3lqqDziG
qRmcITxkEbCFujbxJzlaXop1MeXwg2YJMky3WM1GWomVKv3jOVR+GkQG70pp0qpt
JmU2CuXoNhwMFA0H22CG8pPRiilUGPI7RLXaLWpA8D+AslfPHR9TG00HbJ86Bi3g
m4/uPiGdcHS6Q+wmKQRsKs6wAKSmlCrvmaKfmVOkxpuKyuKgjmIKoCwY3gYL1T8L
idvVePvbP/Z2SRQOVbSV8eMaYuk+uFwGKq8thLHs8bIEKhrIGlzDss6ZlPotTi2V
I6e6lb06oFuCSfhBaiHPw2sldwYvE/I8MkWUAuWtBgNvVE/e64FgJb1lGIzJpYMj
5jUp9Z13INsXy9zA8nKyZAK4fI6vlQGRg3bERn+S4Q6HXQor9Ma8QWxsqbdiC9dt
pxpzyx11tWg0jEgzCEBfk9IZjlGqyCdX5Z9pshHkQZ9VeK+DG0s6tYEm7BO1ssQD
+qbJS2PJq4Cwe82a6gO+lDz8A+xiXk8dJeTb8hf/c1NY192rqSLewI8oaHOLKEQg
XNSPEEkQqtIqn92Y5oKhLYKmYkwfOgldpj0XQQ3YwUnsOCfy2wRVNRg6VYnbjca8
rSy58t2MfovKWz9UcKhpnXefSdMgR7VhGv0ekDddGIfONn153uyjN/LpAgMBAAGj
NTAzMBIGA1UdEwEB/wQIMAYBAf8CAQEwHQYDVR0OBBYEFILkrDPZAlboeF+nav7C
Rf7nN1W+MA0GCSqGSIb3DQEBCwUAA4ICAQCDfvIgo70Y0Mi+Rs0mF6114z2gGQ7a
7/VnxV9w9uIjuaARq42E2DemFs5e72tPIfT9UWncgs5ZfyO5w2tjRpUOaVCSS5VY
93qzXBfTsqgkrkVRwec4qqZxpNqpsL9u2ZIfsSJ3BJWFV3Zq/3cOrDulfR5bk0G4
hYo/GDyLHjNalBFpetJSIk7l0VOkr2CBUvxKBOP0U1IQGXd+NL/8zW6UB6OitqNL
/tO+JztOpjo6ZYKJGZvxyL/3FUsiHmd8UwqAjnFjQRd3w0gseyqWDgILXQaDXQ5D
vs2oK+HheJv4h6CdrcIdWlWRKoZP3odZyWB0l31kpMbgYC/tMPYebG6mjPx+/S4m
7L+K27zmm2wItUaWI12ky2FPgeW78ALoKDYWmQ+CnpBNE1iFUf4qRzmypu77DmmM
bLgLFj8Bb50j0/zciPO7+D1h6hCPxwXdfQk0tnWBqjImmK3enbkEsw77kF8MkNjr
Hka0EeTt0hyEFKGgJ7jVdbjLFnRzre63q1GuQbLkOibyjf9WS/1ljv1Ps82aWeE+
rh78iXtpm8c/2IqrI37sLbAIs08iPj8ULV57RbcZI7iTYFIjKwPlWL8O2U1mopYP
RXkm1+W4cMzZS14MLfmacBHnI7Z4mRKvc+zEdco/l4omlszafmUXxnCOmqZlhqbm
/p0vFt1oteWWSQ==
-----END CERTIFICATE-----`)

// not used for now
// var CAKey []byte = []byte(`-----BEGIN PRIVATE KEY-----
// MIIJRQIBADANBgkqhkiG9w0BAQEFAASCCS8wggkrAgEAAoICAQC4HGJJ3R0BhZi5
// PMovPsnrWggSNXCxv55vocI4eQtq4d5aqg84hqkZnCE8ZBGwhbo28Sc5Wl6KdTHl
// 8INmCTJMt1jNRlqJlSr94zlUfhpEBu9KadKqbSZlNg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3q9pS
// irNeofoYpZQUGu3kB6WWariupJ6wl0Hb11pcsqGly2GvuBkdVDYdkaHsdccd/9G5
// G5/98mpG3dfcXEYjMhXPHaK2JVCNUCzI3tLnMQp1Hzkq/FT4wUOXNtInMEBGq4cU
// BTqRmBcT2tEabo+l8W+kgPzPomyF3FEXNXTgljougwHl+vb4UVQl1VatUyKhoj4a
// OH7L/Lrx/yTJkb00+DtE6QKCAQEA6WaHzr/+gX7BmasbFLpZG1PuGWrck7e61rA2
// fFrp/2oIz5NV8w3RvKRvoC9JvNTWomtjxWmob7/qW8KQO9cwSj5haBRA8IULtHKc
// vlrl5qgt2jePeFUBw/Ysb8WQRftPPOg/fMAQO2VoXyc8ln/EzURqfdymdMsjRIzp
// UanRAm7BUx2aHFIWiGhRyGD+AhJujgeoS9/xCTwzT0tD7W3ja8mZVuLb+uALmGEF
// Soy6yH0FFV1M4P7nitaJEhaIWdGmEn+eNoOfEx2X9W90O06vhzxL4n8BeGvcECno
// T39W7VixyOgtauSuczMMiqocKWqGEWMRnroJdis2MJABafDrawKCAQEAyfATO+P0
// LkncGoNrwwOMS8+pnf4oC3P2AdI9EmO9Ef5mVsf45xGloVE1jdLk3mLE34vFYYmU
// JnWnghIe3CGiA9j3KIglmePI0lSBqVNcB6jrNAOr5zpfNyo22eS3IGIamYunANUF
// UkGWfJg7LtyPA5bsdOAVRldVprfTDJp+gsNRAHpczsi+d5HXiqWgNXkhng6OGrVW
// YeMDpIBXw72QiBa+MXwtHIuGShv7HCnlptbYhiH7lgS3tYS+MoHrP71iehIW6J0w
// mwqOTbH6LxabyPpKmAR0wKT6RHPWj755qQDcchMTn/wZDVVeAd6XZ4yEPCFKV2C6
// 6tJSJChXHVSj+wKCAQEAuPLCUxbYugyTpQ1tXDN+QKV1WzbOq09AAreHtWrlWbvQ
// V4xM41jkBjPXMHvIVpXhrrqiSOC81DCjqSq32HNglLRxw2OpQe5rpAXeJwkofGpw
// SycTzSqvR3VfKQLInGvUElOHW3dmZWqqFhylaiy86lt4d1vNm2cM/1kZ2dhTAzZ4
// g0BFyoNRz626gKo7HbE1Yd63gchm3AaUMhVWMmKNf/A0TiFeZ6H6DjlzU+vIZu6K
// hK92KCRVnjL6Uxb/3QzAKJBv8orCFwewTpCO7iAf/23MRCKYK7BspXJP4A7r9w/9
// cVJqCvZDNo4RNiAuSLNcoZJ7d+e7b6Q2QK0cMbLzGQKCAQEAlZJKzMt1K2vtqX26
// LcHNAsIXojyEMKd9ia/NKHfTrSY/avvHMm9xvODHx/pzJv0FUYbXdf1PTIVMwxzk
// 2qvWsv7bYpwlLaz4LEIHoAFsYwgM3oPxqd0kdVnBMvcjZMsFZMF5uzYNMPX/nt2q
// yHT5QPFixtbLK0S43jm+CtHL7tL4hmAbqdYqranKeple5vyHl+iz/Lhf+TUKl6ew
// Wf1otkQSL9kIGOKHXxEGl7+4qDDuQ7ub+2wXHk3qCsMMGjQOYthPO5BiVhQlfwra
// glI+pUguRBbY22/XJyidve6ZckJ/Bcd3e8JgU+3tKXlTr5D2yQrnY3z0NcX+rWVr
// 0P/GTQKCAQEAmqcrJk8dPEdCwMholgRWxG1qD8BMreyiKU8XdSueUAscEukRW6+g
// TPqh/BIBRBgUMLApETJCuUI8NjhnB3BiBYxLLSb4+pg21pka+REp279eOvLrWKWK
// XOs0tma6YaYjhDx98Fo23hP5p6T4BLKW+mKXssXQ4QDf0Sf/hJKkIrxCrlFI1Kvh
// QGLuNJkbZJtejcIzgzYht2AR7YA2RUtLouq4j+xT4HTJiSfnxQ85qWs7gpLGaBZn
// J2Ai7gtDZY1XT/2fsVeeWqmMNBGrdGuM5WZMhBNe6PkVR3+A9GbIpsYABOswsrdK
// qi9Usgn7km03+rY9jAWHze0lWQye2274Ew==
// -----END PRIVATE KEY-----`)

//nolint:gochecknoglobals
var ServerCertPEM = []byte(`-----BEGIN CERTIFICATE-----
MIIGLTCCBBWgAwIBAgIBAjANBgkqhkiG9w0BAQsFADAzMQswCQYDVQQGEwJVUzES
MBAGA1UECh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that it is possible to connect to a new agent and that the
// statistics can be gathered for this agent.
func TestConnectingToAgent(t *testing.T) {
	settings := AgentsSettings{}
	fec := &storktest.FakeEventCenter{}
	agents := NewConnectedAgents(&settings, fec, CACertPEM, ServerCertPEM, ServerKeyPEM)
	defer agents.Shutdown()

	// connect one agent and check if it is in agents map
	agent, err := agents.GetConnectedAgent("127.0.0.1:8080")
	require.NoError(t, err)
	_, ok := agents.AgentsMap["127.0.0.1:8080"]
	require.True(t, ok)

	// Initially, there should be no stats.
	require.NotNil(t, agent)
	require.Empty(t, agent.Stats.agentCommErrors)
	require.Empty(t, agent.Stats.keaCommErrors)
	require.Empty(t, agent.Stats.bind9CommErrors)

	// Let's modify some stats.
	agent.Stats.agentCommErrors["foo"] = 1

	// We should be able to get pointer to stats via the convenience
	// function.
	stats := agents.GetConnectedAgentStatsWrapper("127.0.0.1", 8080)
	require.NotNil(t, stats)
	defer stats.Close()
	require.Len(t, stats.GetStats().agentCommErrors, 1)
	require.Contains(t, stats.GetStats().agentCommErrors, "foo")
}

// Check if credentials for TLS can be prepared using prepareTLSCreds.
func TestPrepareTLSCreds(t *testing.T) {
	creds, err := prepareTLSCreds(CACertPEM, ServerCertPEM, ServerKeyPEM)
	require.NoError(t, err)
	require.NotNil(t, creds)
}

// The verification function must deny access if the extended key usage is
// missing.
func TestVerifyPeerMissingExtendedKeyUsage(t *testing.T) {
	// Arrange
	cert := &x509.Certificate{Raw: []byte("foo")}

	// Act
	rsp, err := verifyPeer(&advancedtls.HandshakeVerificationInfo{
		Leaf: cert,
	})

	// Assert
	require.Nil(t, rsp)
	require.ErrorContains(t, err, "peer certificate does not have the extended key usage set")
}

// Test that the verification function allows access if the certificate meets
// the requirements.
func TestVerifyPeerCorrectCertificate(t *testing.T) {
	// Arrange
	cert := &x509.Certificate{
		Raw:         []byte("foo"),
		ExtKeyUsage: []x509.ExtKeyUsage{x509.ExtKeyUsageClientAuth},
	}

	// Act
	rsp, err := verifyPeer(&advancedtls.HandshakeVerificationInfo{
		Leaf: cert,
	})

	// Assert
	require.NotNil(t, rsp)
	require.NoError(t, err)
}
