package cert

import (
	"fmt"
	"io/ioutil"
	"os"
	"path"
	"testing"

	"github.com/stretchr/testify/assert"

	"github.com/argoproj/argo-cd/v2/common"
)

const Test_Cert1CN = "CN=foo.example.com,OU=SpecOps,O=Capone\\, Inc,L=Chicago,ST=IL,C=US"
const Test_Cert2CN = "CN=bar.example.com,OU=Testsuite,O=Testing Corp,L=Hanover,ST=Lower Saxony,C=DE"
const Test_TLSValidSingleCert = `
-----BEGIN CERTIFICATE-----
MIIFvTCCA6WgAwIBAgIUGrTmW3qc39zqnE08e3qNDhUkeWswDQYJKoZIhvcNAQEL
BQAwbjELMAkGA1UEBhMCVVMxCzAJBgNVBAgMAklMMRAwDgYDVQQHDAdDaGljYWdv
MRQwEgYDVQQKDAtDYXBvbmUsIEluYzEQMA4GA1UECwwHU3BlY09wczEYMBYGA1UE
AwwPZm9vLmV4YW1wbGUuY29tMB4XDTE5MDcwODEzNTUwNVoXDTIwMDcwNzEzNTUw
NVowbjELMAkGA1UEBhMCVVMxCzAJBgNVBAgMAklMMRAwDgYDVQQHDAdDaGljYWdv
MRQwEgYDVQQKDAtDYXBvbmUsIEluYzEQMA4GA1UECwwHU3BlY09wczEYMBYGA1UE
AwwPZm9vLmV4YW1wbGUuY29tMIICIjANBgkqhkiG9w0BAQEFAAOCAg8AMIICCgKC
AgEA3csSO13w7qQXKeSLNcpeuAe6wAjXYbRkRl6ariqzTEDcFTKmy2QiXJTKoEGn
bvwxq0T91var7rxY88SGL/qi8Zmo0tVSR0XvKSKcghFIkQOTyDmVgMPZGCvixt4q
gQ7hUVSk4KkFmtcqBVuvnzI1d/DKfZAGKdmGcfRpuAsnVhac3swP0w4Tl1BFrK9U
vuIkz4KwXG77s5oB8rMUnyuLasLsGNpvpvXhkcQRhp6vpcCO2bS7kOTTelAPIucw
P37qkOEdZdiWCLrr57dmhg6tmcVlmBMg6JtmfLxn2HQd9ZrCKlkWxMk5NYs6CAW5
kgbDZUWQTAsnHeoJKbcgtPkIbxDRxNpPukFMtbA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u1xOB/StKDHMoX4/OKyIzuS0q/T1zOATthvasJFoPrAjkohTyaDUz2LN5JoH839hViyEG82yB+MjcFV5MU3N1l1QL3cVUCh93xSaua1N85qivl+siMkPGbO5xR/En4iEY6K2XPASUEMaieWVNTRCtJ4S8H+9
# Azure
ssh.dev.azure.com ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQC7Hr1oTWqNqOlzGJOfGJ4NakVyIzf1rXYd4d7wo6jBlkLvCA4odBlL0mDUyZ0/QUfTTqeu+tm22gOsv+VrVTMk6vwRU75gY/y9ut5Mb3bR5BV58dKXyq9A9UeB5Cakehn5Zgm6x1mKoVyf+FFn26iYqXJRgzIZZcZ5V6hrE0Qg39kZm4az48o0AUbf6Sp4SLdvnuMa2sVNwHBboS7EJkm57XQPVU3/QpyNLHbWDdzwtrlS+ez30S3AdYhLKEOxAG8weOnyrtLJAUen9mTkol8oII1edf7mWWbWVf0nBmly21+nZcmCTISQBtdcyPaEno7fFQMDD26/s0lfKob4Kw8H
vs-ssh.visualstudio.com ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQC7Hr1oTWqNqOlzGJOfGJ4NakVyIzf1rXYd4d7wo6jBlkLvCA4odBlL0mDUyZ0/QUfTTqeu+tm22gOsv+VrVTMk6vwRU75gY/y9ut5Mb3bR5BV58dKXyq9A9UeB5Cakehn5Zgm6x1mKoVyf+FFn26iYqXJRgzIZZcZ5V6hrE0Qg39kZm4az48o0AUbf6Sp4SLdvnuMa2sVNwHBboS7EJkm57XQPVU3/QpyNLHbWDdzwtrlS+ez30S3AdYhLKEOxAG8weOnyrtLJAUen9mTkol8oII1edf7mWWbWVf0nBmly21+nZcmCTISQBtdcyPaEno7fFQMDD26/s0lfKob4Kw8H
`

func Test_TLSCertificate_ValidPEM_ValidCert(t *testing.T) {
	// Valid PEM data, single certificate, expect array of length 1
	certificates, err := ParseTLSCertificatesFromData(Test_TLSValidSingleCert)
	assert.Nil(t, err)
	assert.Equal(t, len(certificates), 1)
	// Expect good decode
	x509Cert, err := DecodePEMCertificateToX509(certificates[0])
	assert.Nil(t, err)
	assert.Equal(t, x509Cert.Subject.String(), Test_Cert1CN)
}

func Test_TLSCertificate_ValidPEM_InvalidCert(t *testing.T) {
	// Valid PEM data, but invalid certificate
	certificates, err := ParseTLSCertificatesFromData(Test_TLSInvalidSingleCert)
	assert.Nil(t, err)
	assert.Equal(t, len(certificates), 1)
	// Expect bad decode
	_, err = DecodePEMCertificateToX509(certificates[0])
	assert.NotNil(t, err)
}

func Test_TLSCertificate_InvalidPEM(t *testing.T) {
	// Invalid PEM data, expect array of length 0
	certificates, err := ParseTLSCertificatesFromData(Test_TLSInvalidPEMData)
	assert.Nil(t, err)
	assert.Equal(t, len(certificates), 0)
}

func Test_TLSCertificate_ValidPEM_ValidCert_Multi(t *testing.T) {
	// Valid PEM data, two certificates, expect array of length 2
	certificates, err := ParseTLSCertificatesFromData(Test_TLSValidMultiCert)
	assert.Nil(t, err)
	assert.Equal(t, len(certificates), 2)
	// Expect good decode
	x509Cert, err := DecodePEMCertificateToX509(certificates[0])
	assert.Nil(t, err)
	assert.Equal(t, x509Cert.Subject.String(), Test_Cert1CN)
	x509Cert, err = DecodePEMCertificateToX509(certificates[1])
	assert.Nil(t, err)
	assert.Equal(t, x509Cert.Subject.String(), Test_Cert2CN)
}

func Test_TLSCertificate_ValidPEM_ValidCert_FromFile(t *testing.T) {
	// Valid PEM data, single certificate from file, expect array of length 1
	certificates, err := ParseTLSCertificatesFromPath("../../test/certificates/cert1.pem")
	assert.Nil(t, err)
	assert.Equal(t, len(certificates), 1)
	// Expect good decode
	x509Cert, err := DecodePEMCertificateToX509(certificates[0])
	assert.Nil(t, err)
	assert.Equal(t, x509Cert.Subject.String(), Test_Cert1CN)
}

func Test_TLSCertPool(t *testing.T) {
	certificates, err := ParseTLSCertificatesFromData(Test_TLSValidMultiCert)
	assert.Nil(t, err)
	assert.Equal(t, len(certificates), 2)
	certPool := GetCertPoolFromPEMData(certificates)
	assert.NotNil(t, certPool)
}

func Test_TLSCertificate_CertFromNonExistingFile(t *testing.T) {
	// Non-existing file, expect err
	_, err := ParseTLSCertificatesFromPath("../../test/certificates/cert_nonexisting.pem")
	assert.NotNil(t, err)
}

func Test_SSHKnownHostsData_ParseData(t *testing.T) {
	// Expect valid data with 7 known host entries
	entries, err := ParseSSHKnownHostsFromData(Test_ValidSSHKnownHostsData)
	assert.Nil(t, err)
	assert.Equal(t, len(entries), 7)
}

func Test_SSHKnownHostsData_ParseFile(t *testing.T) {
	// Expect valid data with 7 known host entries
	entries, err := ParseSSHKnownHostsFromPath("../../test/certificates/ssh_known_hosts")
	assert.Nil(t, err)
	assert.Equal(t, len(entries), 7)
}

func Test_SSHKnownHostsData_ParseNonExistingFile(t *testing.T) {
	// Expect valid data with 7 known host entries
	entries, err := ParseSSHKnownHostsFromPath("../../test/certificates/ssh_known_hosts_invalid")
	assert.NotNil(t, err)
	assert.Nil(t, entries)
}

func Test_SSHKnownHostsData_Tokenize(t *testing.T) {
	// All entries should parse to valid SSH public keys
	// All entries should be tokenizable, and tokens should be feedable to decoder
	entries, err := ParseSSHKnownHostsFromData(Test_ValidSSHKnownHostsData)
	assert.Nil(t, err)
	for _, entry := range entries {
		hosts, _, err := KnownHostsLineToPublicKey(entry)
		assert.Nil(t, err)
		assert.Equal(t, len(hosts), 1)
		hoststring, subtype, certdata, err := TokenizeSSHKnownHostsEntry(entry)
		assert.Nil(t, err)
		hosts, _, err = TokenizedDataToPublicKey(hoststring, subtype, string(certdata))
		assert.Nil(t, err)
		assert.Equal(t, len(hosts), 1)
	}
}

func Test_MatchHostName(t *testing.T) {
	matchHostName := "foo.example.com"
	assert.Equal(t, MatchHostName(matchHostName, "*"), true)
	assert.Equal(t, MatchHostName(matchHostName, "*.example.com"), true)
	assert.Equal(t, MatchHostName(matchHostName, "foo.*"), true)
	assert.Equal(t, MatchHostName(matchHostName, "foo.*.com"), true)
	assert.Equal(t, MatchHostName(matchHostName, "fo?.example.com"), true)
	assert.Equal(t, MatchHostName(matchHostName, "foo?.example.com"), false)
	assert.Equal(t, MatchHostName(matchHostName, "bar.example.com"), false)
	assert.Equal(t, MatchHostName(matchHostName, "*.otherexample.com"), false)
	assert.Equal(t, MatchHostName(matchHostName, "foo.otherexample.*"), false)
}

func Test_SSHFingerprintSHA256(t *testing.T) {
	// actual SHA256 fingerprints for keys defined above
	fingerprints := [...]string{
		"zzXQOXSRBEiUtuE8AikJYKwbHaxvSc0ojez9YXaGp1A",
		"nThbg6kXUpJWGl7E1IGOCspRomTxdCARLviKw6E5SY8",
		"HbW3g8zUjNSksFbqTiUWPWg2Bq1x8xdGUrliXFzSnUw",
		"eUXGGm1YGsMAS7vkcx6JOJdOGHPem5gQp4taiCfCLB8",
		"ROQFvPThGrW4RuWLoL9tq9I9zJ42fK4XywyRtbOz/EQ",
		"ohD8VZEXGWo6Ez8GSEJQ9WpafgLFsOfLOtGGQCQo6Og",
		"ohD8VZEXGWo6Ez8GSEJQ9WpafgLFsOfLOtGGQCQo6Og",
	}
	entries, err := ParseSSHKnownHostsFromData(Test_ValidSSHKnownHostsData)
	assert.Nil(t, err)
	assert.Equal(t, len(entries), 7)
	for idx, entry := range entries {
		_, pubKey, err := KnownHostsLineToPublicKey(entry)
		assert.Nil(t, err)
		fp := SSHFingerprintSHA256(pubKey)
		assert.Equal(t, fp, fingerprints[idx])
	}
}

func Test_SSHFingerPrintSHA256FromString(t *testing.T) {
	// actual SHA256 fingerprints for keys defined above
	fingerprints := [...]string{
		"zzXQOXSRBEiUtuE8AikJYKwbHaxvSc0ojez9YXaGp1A",
		"nThbg6kXUpJWGl7E1IGOCspRomTxdCARLviKw6E5SY8",
		"HbW3g8zUjNSksFbqTiUWPWg2Bq1x8xdGUrliXFzSnUw",
		"eUXGGm1YGsMAS7vkcx6JOJdOGHPem5gQp4taiCfCLB8",
		"ROQFvPThGrW4RuWLoL9tq9I9zJ42fK4XywyRtbOz/EQ",
		"ohD8VZEXGWo6Ez8GSEJQ9WpafgLFsOfLOtGGQCQo6Og",
		"ohD8VZEXGWo6Ez8GSEJQ9WpafgLFsOfLOtGGQCQo6Og",
	}
	entries, err := ParseSSHKnownHostsFromData(Test_ValidSSHKnownHostsData)
	assert.Nil(t, err)
	assert.Equal(t, len(entries), 7)
	for idx, entry := range entries {
		fp := SSHFingerprintSHA256FromString(entry)
		assert.Equal(t, fp, fingerprints[idx])
	}
}

func Test_ServerNameWithoutPort(t *testing.T) {
	hostNames := map[string]string{
		"localhost":            "localhost",
		"localhost:9443":       "localhost",
		"localhost:":           "localhost",
		"localhost:abc":        "localhost",
		"localhost.:22":        "localhost.",
		"foo.example.com:443":  "foo.example.com",
		"foo.example.com.:443": "foo.example.com.",
	}
	for inp, res := range hostNames {
		assert.Equal(t, res, ServerNameWithoutPort(inp))
	}
}

func Test_ValidHostnames(t *testing.T) {
	hostNames := map[string]bool{
		"localhost":                          true,
		"localhost.localdomain":              true,
		"foo.example.com":                    true,
		"argocd-server.svc.kubernetes.local": true,
		"localhost.":                         true,
		"github.com.":                        true,
		"foo_bar.example.com":                true,
		"_svc.example.com":                   true,
		"_svc.example_.com":                  false,
		"_.example.com":                      false,
		"localhost..":                        false,
		"localhost..localdomain":             false,
		".localhost":                         false,
		"local_host":                         true,
		"localhost.local_domain":             true,
	}

	for hostName, valid := range hostNames {
		t.Run(fmt.Sprintf("Test validity for hostname %s", hostName), func(t *testing.T) {
			assert.Equal(t, valid, IsValidHostname(hostName, false))
		})
	}
}

func Test_ValidFQDNs(t *testing.T) {
	hostNames := map[string]bool{
		"localhost":                          false,
		"localhost.localdomain":              false,
		"foo.example.com.":                   true,
		"argocd-server.svc.kubernetes.local": false,
		"localhost.":                         true,
		"github.com.":                        true,
		"localhost..":                        false,
		"localhost..localdomain":             false,
		"localhost..localdomain.":            false,
		".localhost":                         false,
		"local_host":                         false,
		"localhost.local_domain":             false,
		"localhost.local_domain.":            false,
	}

	for hostName, valid := range hostNames {
		assert.Equal(t, valid, IsValidHostname(hostName, true))
	}
}

func Test_EscapeBracketPattern(t *testing.T) {
	// input: expected output
	patternList := map[string]string{
		"foo.bar":         "foo.bar",
		"[foo.bar]":       `\[foo.bar\]`,
		"foo[bar]baz":     `foo\[bar\]baz`,
		`foo\[bar\]baz`:   `foo\\[bar\\]baz`,
		"foo[[[bar]]]baz": `foo\[\[\[bar\]\]\]baz`,
	}

	for original, expected := range patternList {
		assert.Equal(t, nonBracketedPattern(original), expected)
	}
}

func TestGetTLSCertificateDataPath(t *testing.T) {
	t.Run("Get default path", func(t *testing.T) {
		os.Setenv(common.EnvVarTLSDataPath, "")
		path := GetTLSCertificateDataPath()
		assert.Equal(t, common.DefaultPathTLSConfig, path)
	})

	t.Run("Get custom path", func(t *testing.T) {
		os.Setenv(common.EnvVarTLSDataPath, "/some/where")
		path := GetTLSCertificateDataPath()
		assert.Equal(t, "/some/where", path)
		os.Setenv(common.EnvVarTLSDataPath, "")
	})
}

func TestGetSSHKnownHostsDataPath(t *testing.T) {
	t.Run("Get default path", func(t *testing.T) {
		os.Setenv(common.EnvVarSSHDataPath, "")
		p := GetSSHKnownHostsDataPath()
		assert.Equal(t, path.Join(common.DefaultPathSSHConfig, "ssh_known_hosts"), p)
	})

	t.Run("Get custom path", func(t *testing.T) {
		os.Setenv(common.EnvVarSSHDataPath, "/some/where")
		path := GetSSHKnownHostsDataPath()
		assert.Equal(t, "/some/where/ssh_known_hosts", path)
		os.Setenv(common.EnvVarSSHDataPath, "")
	})
}

func TestGetCertificateForConnect(t *testing.T) {
	t.Run("Success", func(t *testing.T) {
		temppath := t.TempDir()
		cert, err := ioutil.ReadFile("../../test/fixture/certs/argocd-test-server.crt")
		if err != nil {
			panic(err)
		}
		err = ioutil.WriteFile(path.Join(temppath, "127.0.0.1"), cert, 0666)
		if err != nil {
			panic(err)
		}
		os.Setenv(common.EnvVarTLSDataPath, temppath)
		certs, err := GetCertificateForConnect("127.0.0.1")
		assert.NoError(t, err)
		assert.Len(t, certs, 1)
	})

	t.Run("No cert found", func(t *testing.T) {
		temppath := t.TempDir()
		os.Setenv(common.EnvVarTLSDataPath, temppath)
		certs, err := GetCertificateForConnect("127.0.0.1")
		assert.NoError(t, err)
		assert.Len(t, certs, 0)
	})

	t.Run("No valid cert in file", func(t *testing.T) {
		temppath := t.TempDir()
		err := ioutil.WriteFile(path.Join(temppath, "127.0.0.1"), []byte("foobar"), 0666)
		if err != nil {
			panic(err)
		}
		os.Setenv(common.EnvVarTLSDataPath, temppath)
		certs, err := GetCertificateForConnect("127.0.0.1")
		assert.Error(t, err)
		assert.Len(t, certs, 0)
		assert.Contains(t, err.Error(), "no certificates found")
	})

}

func TestGetCertBundlePathForRepository(t *testing.T) {
	t.Run("Success", func(t *testing.T) {
		temppath := t.TempDir()
		cert, err := ioutil.ReadFile("../../test/fixture/certs/argocd-test-server.crt")
		if err != nil {
			panic(err)
		}
		err = ioutil.WriteFile(path.Join(temppath, "127.0.0.1"), cert, 0666)
		if err != nil {
			panic(err)
		}
		os.Setenv(common.EnvVarTLSDataPath, temppath)
		certpath, err := GetCertBundlePathForRepository("127.0.0.1")
		assert.NoError(t, err)
		assert.Equal(t, certpath, path.Join(temppath, "127.0.0.1"))
	})

	t.Run("No cert found", func(t *testing.T) {
		temppath := t.TempDir()
		os.Setenv(common.EnvVarTLSDataPath, temppath)
		certpath, err := GetCertBundlePathForRepository("127.0.0.1")
		assert.NoError(t, err)
		assert.Empty(t, certpath)
	})

	t.Run("No valid cert in file", func(t *testing.T) {
		temppath := t.TempDir()
		err := ioutil.WriteFile(path.Join(temppath, "127.0.0.1"), []byte("foobar"), 0666)
		if err != nil {
			panic(err)
		}
		os.Setenv(common.EnvVarTLSDataPath, temppath)
		certpath, err := GetCertBundlePathForRepository("127.0.0.1")
		assert.NoError(t, err)
		assert.Empty(t, certpath)
	})

}
