package util

import (
	"crypto/x509"
	"encoding/pem"
	"os"
	"testing"

	"github.com/stretchr/testify/assert"
)

// Test certificate created with an OpenSSL command in the following form:
// # Create "root1" certification and "cert1" certification signed by "root1" certification.
// openssl req -x509 -nodes -days 365000 -newkey rsa:4096 -keyout root1.key -out root1.pem -subj "/CN=oauth2-proxy test root1 ca"
// openssl req -nodes -days 365000 -newkey rsa:4096 -keyout cert1.key -out cert1-req.pem -subj "/CN=oauth2-proxy test cert1 ca"
// openssl x509 -req -in cert1-req.pem -days 365000 -CA root1.pem -CAkey root1.key -set_serial 01 -out cert1.pem
// openssl verify -CAfile ./root1.pem cert1.pem
// # Create "root2" certification and "cert2" certification signed by "root2" certification.
// openssl req -x509 -nodes -days 365000 -newkey rsa:4096 -keyout root2.key -out root2.pem -subj "/CN=oauth2-proxy test root2 ca"
// openssl req -nodes -days 365000 -newkey rsa:4096 -keyout cert2.key -out cert2-req.pem -subj "/CN=oauth2-proxy test cert2 ca"
// openssl x509 -req -in cert2-req.pem -days 365000 -CA root2.pem -CAkey root2.key -set_serial 01 -out cert2.pem
// openssl verify -CAfile ./root2.pem cert2.pem
// # Create "root3" certification and "cert3" certification signed by "root3" certification.
// openssl req -x509 -nodes -days 365000 -newkey rsa:4096 -keyout root3.key -out root3.pem -subj "/CN=oauth2-proxy test root3 ca"
// openssl req -nodes -days 365000 -newkey rsa:4096 -keyout cert3.key -out cert3-req.pem -subj "/CN=oauth2-proxy test cert3 ca"
// openssl x509 -req -in cert3-req.pem -days 365000 -CA root3.pem -CAkey root3.key -set_serial 01 -out cert3.pem
// openssl verify -CAfile ./root3.pem cert3.pem

var (
	root1Cert = `-----BEGIN CERTIFICATE-----
MIIFLTCCAxWgAwIBAgIUfVKJjmDCrxB6a4GfPxF7QjP67vwwDQYJKoZIhvcNAQEL
BQAwJTEjMCEGA1UEAwwab2F1dGgyLXByb3h5IHRlc3Qgcm9vdDEgY2EwIBcNMjIw
NzA3MjMyNzUwWhgPMzAyMTExMDcyMzI3N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UvlmBX8Dz+qG66vFfcff+bo2
Ixm/I9aL4lNQiCmE5E044L00IX802hE9iSdYqFbUmefKqk9NWK3CPtDoobcFLi6u
WrW4JMzLaGDtoHxRNNfo8E7fGkQ=
-----END CERTIFICATE-----
`
)

func makeTestCertFile(t *testing.T, pem, dir string) *os.File {
	file, err := os.CreateTemp(dir, "test-certfile")
	assert.NoError(t, err)
	_, err = file.Write([]byte(pem))
	assert.NoError(t, err)
	return file
}

func TestGetCertPool_NoRoots(t *testing.T) {
	_, err := GetCertPool([]string(nil))
	assert.Error(t, err, "invalid empty list of Root CAs file paths")
}

func TestGetCertPool(t *testing.T) {
	tempDir, err := os.MkdirTemp("", "certtest")
	assert.NoError(t, err)
	defer func(path string) {
		rerr := os.RemoveAll(path)
		if rerr != nil {
			panic(rerr)
		}
	}(tempDir)

	certFile1 := makeTestCertFile(t, root1Cert, tempDir)
	certFile2 := makeTestCertFile(t, root2Cert, tempDir)

	certPool, err := GetCertPool([]string{certFile1.Name(), certFile2.Name()})
	certFile1.Close()
	certFile2.Close()
	assert.NoError(t, err)

	cert1Block, _ := pem.Decode([]byte(cert1Cert))
	cert1, _ := x509.ParseCertificate(cert1Block.Bytes)
	assert.Equal(t, cert1.Subject.String(), cert1CertSubj)

	cert2Block, _ := pem.Decode([]byte(cert2Cert))
	cert2, _ := x509.ParseCertificate(cert2Block.Bytes)
	assert.Equal(t, cert2.Subject.String(), cert2CertSubj)

	cert3Block, _ := pem.Decode([]byte(cert3Cert))
	cert3, _ := x509.ParseCertificate(cert3Block.Bytes)
	assert.Equal(t, cert3.Subject.String(), cert3CertSubj)

	opts := x509.VerifyOptions{
		Roots: certPool,
	}

	// "cert1" and "cert2" should be valid because "root1" and "root2" are in the certPool
	// "cert3" should not be valid because "root3" is not in the certPool
	_, err1 := cert1.Verify(opts)
	assert.NoError(t, err1)
	_, err2 := cert2.Verify(opts)
	assert.NoError(t, err2)
	_, err3 := cert3.Verify(opts)
	assert.Error(t, err3)
}
