/*
Copyright (c) Edgeless Systems GmbH

SPDX-License-Identifier: AGPL-3.0-only
*/

package config

import (
	"encoding/json"
	"fmt"
	"testing"

	"github.com/edgelesssys/constellation/v2/internal/attestation/measurements"
	"github.com/edgelesssys/constellation/v2/internal/attestation/variant"
	"github.com/edgelesssys/constellation/v2/internal/cloud/cloudprovider"
	"github.com/stretchr/testify/assert"
	"github.com/stretchr/testify/require"
	"gopkg.in/yaml.v3"
)

// testCertPEM is a certificate in PEM format used for unit tests.
var testCertPEM = `-----BEGIN CERTIFICATE-----\nMIIGYzCCBBKgAwIBAgIDAQAAMEYGCSqGSIb3DQEBCjA5oA8wDQYJYIZIAWUDBAIC\nBQChHDAaBgkqhkiG9w0BAQgwDQYJYIZIAWUDBAICBQCiAwIBMKMDAgEBMHsxFDAS\nBgNVBAsMC0VuZ2luZWVyaW5nMQswCQYDVQQGEwJVUzEUMBIGA1UEBwwLU2FudGEg\nQ2xhcmExCzAJBgNVBAgMAkNBMR8wHQYDVQQKDBZBZHZhbmNlZCBNaWNybyBEZXZp\nY2VzMRIwEAYDVQQDDAlBUkstTWlsYW4wHhcNMjAxMDIyMTcyMzA1WhcNNDUxMDIy\nMTcyMzA1WjB7MRQwEgYDVQQLDAtFbmdpbmVlcmluZzELMAkGA1UEBhMCVVMxFDAS\nBgNVBAcMC1NhbnRhIENsYXJhMQswCQYDVQQIDAJDQTEfMB0GA1UECgwWQWR2YW5j\nZWQgTWljcm8gRGV2aWNlczESMBAGA1UEAwwJQVJLLU1pbGFuMIICIjANBgkqhkiG\n9w0BAQEFAAOCAg8AMIICCgKCAgEA0Ld52RJOdeiJlqK2JdsVmD7FktuotWwX1fNg\nW41XY9Xz1HEhSUmhLz9Cu9DHRlvgJSNxbeYYsnJfvyjx1MfU0V5tkKiU1EesNFta\n1kTA0szNisdYc9isqk7mXT5+KfGRbfc4V/9zRIcE8jlHN61S1ju8X93+6dxDUrG2\nSzxqJ4BhqyYmUDruPXJSX4vUc01P7j98MpqOS95rORdGH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KqPQ3P+N9r1hjEFY1plBg93t53OOo49GNI+V1zvXPLI6xIFVsh+mto2RtgEX/e\npmMKTNN6psW88qg7c1hTWtN6MbRuQ0vm+O+/2tKBF2h8THb94OvvHHoFDpbCELlq\nHnIYhxy0YKXGyaW1NjfULxrrmxVW4wcn5E8GddmvNa6yYm8scJagEi13mhGu4Jqh\n3QU3sf8iUSUr09xQDwHtOQUVIqx4maBZPBtSMf+qUDtjXSSq8lfWcd8bLr9mdsUn\nJZJ0+tuPMKmBnSH860llKk+VpVQsgqbzDIvOLvD6W1Umq25boxCYJ+TuBoa4s+HH\nCViAvgT9kf/rBq1d+ivj6skkHxuzcxbk1xv6ZGxrteJxVH7KlX7YRdZ6eARKwLe4\nAFZEAwoKCQ==\n-----END CERTIFICATE-----\n`

func TestUnmarshalAttestationConfig(t *testing.T) {
	testCases := map[string]struct {
		cfg AttestationCfg
	}{
		"AWSSEVSNP": {
			cfg: &AWSSEVSNP{Measurements: measurements.DefaultsFor(cloudprovider.AWS, variant.AWSSEVSNP{})},
		},
		"AWSNitroTPM": {
			cfg: &AWSNitroTPM{Measurements: measurements.DefaultsFor(cloudprovider.AWS, variant.AWSNitroTPM{})},
		},
		"AzureSEVSNP": {
			cfg: DefaultForAzureSEVSNP(),
		},
		"AzureTrustedLaunch": {
			cfg: &AzureTrustedLaunch{Measurements: measurements.DefaultsFor(cloudprovider.Azure, variant.AzureTrustedLaunch{})},
		},
		"GCPSEVES": {
			cfg: &GCPSEVES{Measurements: measurements.DefaultsFor(cloudprovider.GCP, variant.GCPSEVES{})},
		},
		"QEMUVTPM": {
			cfg: &QEMUVTPM{Measurements: measurements.DefaultsFor(cloudprovider.QEMU, variant.QEMUVTPM{})},
		},
		"QEMUTDX": {
			cfg: &QEMUTDX{Measurements: measurements.DefaultsFor(cloudprovider.QEMU, variant.QEMUTDX{})},
		},
	}

	for name, tc := range testCases {
		t.Run(name, func(t *testing.T) {
			require := require.New(t)
			assert := assert.New(t)

			out, err := json.Marshal(tc.cfg)
			require.NoError(err)

			cfg, err := UnmarshalAttestationConfig(out, tc.cfg.GetVariant())
			require.NoError(err)
			assert.Equal(tc.cfg, cfg)
		})
	}
}

func TestCertificateMarshalJSON(t *testing.T) {
	require := require.New(t)
	assert := assert.New(t)

	jsonCert := fmt.Sprintf("\"%s\"", testCertPEM)
	var cert Certificate
	require.NoError(json.Unmarshal([]byte(jsonCert), &cert))

	out, err := json.Marshal(cert)
	require.NoError(err)
	assert.JSONEq(jsonCert, string(out))
}

func TestEmptyCertificateMarshalJSON(t *testing.T) {
	require := require.New(t)
	assert := assert.New(t)

	jsonCert := "\"\""
	var cert Certificate
	require.NoError(json.Unmarshal([]byte(jsonCert), &cert))

	out, err := json.Marshal(cert)
	require.NoError(err)
	assert.JSONEq(jsonCert, string(out))
}

func TestCertificateMarshalYAML(t *testing.T) {
	require := require.New(t)
	assert := assert.New(t)

	yamlCert := fmt.Sprintf("\"%s\"", testCertPEM)
	var cert Certificate
	require.NoError(yaml.Unmarshal([]byte(yamlCert), &cert))

	out, err := yaml.Marshal(cert)
	require.NoError(err)
	assert.YAMLEq(yamlCert, string(out))
}

func TestEmptyCertificateMarshalYAML(t *testing.T) {
	require := require.New(t)
	assert := assert.New(t)

	yamlCert := "\"\""
	var cert Certificate
	require.NoError(yaml.Unmarshal([]byte(yamlCert), &cert))

	out, err := yaml.Marshal(cert)
	require.NoError(err)
	assert.YAMLEq(yamlCert, string(out))
}
