// go:build ignore

package dependency

import (
	"bytes"
	"errors"
	"os"
	"strings"
	"testing"

	"github.com/hashicorp/consul-template/renderer"
	"github.com/hashicorp/vault/api"
)

func Test_VaultPKI_notGoodFor(t *testing.T) {
	// only test the negation, postive is tested below with pemsificates
	// fetched in Vault integration tests (creating pemss is non-trivial)
	_, cert, err := pemsCert([]byte(validCert))
	if err != nil {
		t.Error(err)
	}
	dur, ok := goodFor(cert)
	if ok != false {
		t.Error("should be false")
	}
	// duration should be negative as pems has already expired
	// but still tests pems time parsing (it'd be 0 if there was an issue)
	if dur > 0 {
		t.Error("duration shouldn't be positive (old cert)")
	}
}

func Test_VaultPKI_pemsCert(t *testing.T) {
	// tests w/ valid pems, and having it hidden behind various things
	want := strings.TrimRight(strings.TrimSpace(validCert), "\n")
	for k, testpems := range validPermutations() {
		t.Run(k, func(t *testing.T) {
			pems, cert, err := pemsCert([]byte(testpems))
			if err != nil {
				t.Fatal(err) // Fatal to avoid panic's below
			}
			if cert == nil {
				t.Fatal("cert should not be nil")
			}
			for k, p := range map[string]string{
				"cert": pems.Cert, "key": pems.Key, "ca": pems.CA,
			} {
				if p == "" {
					t.Error("Missing PEM:", k)
				}
			}
			got := strings.TrimRight(pems.Cert, "\n")
			if got != want {
				t.Errorf("pemss didn't match:\ngot: %v\nwant: %v", got, want)
			}
			if err := cert.VerifyHostname("foo.example.com"); err != nil {
				t.Error(err)
			}
		})
	}
	// tests w/o valid pems (test error)
	expectedErr := "x509: malformed certificate"
	for name, badpems := range map[string]string{"bad": badCert, "bad+gar": badPlusGarbo} {
		_, _, err := pemsCert([]byte(badpems))
		switch {
		case err == nil:
			t.Errorf("error should not be nil for %s", name)
		case !strings.Contains(err.Error(), expectedErr):
			t.Errorf("wrong error; wanted: '%s', got: '%s'", expectedErr, err)
		}
	}
}

func Test_VaultPKI_fetchPEM(t *testing.T) {
	clients := testClients

	data := map[string]interface{}{
		"common_name": "foo.example.com",
		"ttl":         "2h",
		"ip_sans":     "127.0.0.1,192.168.2.2",
	}
	d, err := NewVaultPKIQuery("pki/issue/example-dot-com", "/dev/null", data)
	if err != nil {
		t.Error(err)
	}
	encPEM, err := d.fetchPEMs(clients)
	if err != nil {
		t.Error(err)
	}
	if !bytes.Contains(encPEM, []byte("CERTIFICATE")) {
		t.Errorf("pemsificate not fetched, got: %s", string(encPEM))
	}
	// test path error
	d, err = NewVaultPKIQuery("pki/issue/does-not-exist", "/dev/null", data)
	if err != nil {
		t.Error(err)
	}
	_, err = d.fetchPEMs(clients)
	var respErr *api.ResponseError
	if !errors.As(err, &respErr) {
		t.Error(err)
	}
}

func Test_VaultPKI_refetch(t *testing.T) {
	t.Parallel() // have time waits, so parallel might help

	f, err := os.CreateTemp("", "")
	if err != nil {
		t.Fatal(err)
	}
	os.Remove(f.Name())
	defer os.Remove(f.Name())

	clients := testClients
	/// above is prep work
	data := map[string]interface{}{
		"common_name": "foo.example.com",
		"ttl":         "3s",
		"ip_sans":     "127.0.0.1,192.168.2.2",
	}
	d, err := NewVaultPKIQuery("pki/issue/example-dot-com", f.Name(), data)
	if err != nil {
		t.Fatal(err)
	}
	act1, rm, err := d.Fetch(clients, nil)
	if err != nil {
		t.Fatal(err)
	}
	if rm == nil {
		t.Error("Fetch returned nil for response metadata.")
	}

	pems1, ok := act1.(PemEncoded)
	if !ok || !strings.Contains(pems1.Cert, "BEGIN CERTIFICATE") {
		t.Fatalf("expected a pems but found: %s", pems1)
	}

	// Fake template rendering file to disk
	allPems1 := strings.Join([]string{pems1.Cert, pems1.Key, pems1.CA}, "\n")
	_, err = renderer.Render(&renderer.RenderInput{
		Contents: []byte(allPems1),
		Path:     f.Name(),
	})
	if err != nil {
		t.Fatal(err)
	}

	// re-fetch, should be the same pems pulled from the file
	// if re-fetched from Vault it will be different
	<-d.sleepCh // drain sleepCh to trigger reusing the cached copy
	act2, rm, err := d.Fetch(clients, nil)
	if err != nil {
		t.Fatal(err)
	}
	if rm == nil {
		t.Error("Fetch returned nil for response metadata.")
	}

	pems2, ok := act2.(PemEncoded)
	if !ok || !strings.Contains(pems2.Cert, "BEGIN CERTIFICATE") {
		t.Fatalf("expected a pems but found: %s", pems2)
	}
	// using cached copy, so should be a match
	if pems1 != pems2 {
		t.Errorf("pemss don't match and should.")
	}

	// Don't pre-drain here as we want it to get a new pems
	act3, rm, err := d.Fetch(clients, nil)
	if err != nil {
		t.Fatal(err)
	}
	if rm == nil {
		t.Error("Fetch returned nil for response metadata.")
	}

	pems3, ok := act3.(PemEncoded)
	if !ok || !strings.Contains(pems3.Cert, "BEGIN CERTIFICATE") {
		t.Fatalf("expected a pems but found: %s", pems2)
	}

	if pems2 == pems3 {
		t.Errorf("pemss match and shouldn't.")
	}
}

const validCert = `-----BEGIN CERTIFICATE-----
MIIDWTCCAkGgAwIBAgIUaawVY56cY+a0GUYJaL2zsHFBgqQwDQYJKoZIhvcNAQEL
BQAwFjEUMBIGA1UEAxMLZXhhbXBsZS5jb20wHhcNMjIwNjIxMjEyODIxWhcNMjIw
NjIxMjEyODI1WjAaMRgwFgYDVQQDEw9mb28uZXhhbXBsZS5jb20wggEiMA0GCSqG
SIb3DQEBAQUAA4IBDwAwggEKAoIBAQDJ3NK6SeJQdxAmXXRcPt5xHYVuUWLjtvHp
8wAUtMYSvLFrC0XD9YGKxScbLLcnlZdSvD72LYh9R4h7kLjyP9JQnUSPqhixUk2A
RYuiUTNieppsM7pBMM0UYkyOtb8pLNSnrV4u6ga1iSGZd/3qxSbDd9K0ku9A75qC
zVLvcO7LqPw6RI8hRDU6Z1qE3MK9upqu/YE8HdhR7og8hZzhU8f7GcuGowNYezBi
J/hmBW6oxyi0FC9rmZ7SIX3oYyxkW1v/xSM3zw4EKw1JaDFgceyuhQXW0QmgUyTW
0p7I2DUIccRV7W1w7biERiDfrqzCJaUzTEg2E728269fHpaYa3OpAgMBAAGjgZow
gZcwDgYDVR0PAQH/BAQDAgOoMB0GA1UdJQQWMBQGCCsGAQUFBwMBBggrBgEFBQcD
AjAdBgNVHQ4EFgQUyivSs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HXlTo3X5gxrEsyBeO3BzjEz76eYq+VdimmkV8qm/Zbccj
ceJ0WflHmKDF
-----END CERTIFICATE-----`

var validPems = map[string]string{
	"ca": validCA, "cert": validCert, "key": validKey,
}

func validPermutations() map[string]string {
	result := make(map[string]string, 6)
	for k, v := range validPems {
		for k1, v1 := range validPems {
			if v1 != v {
				for k2, v2 := range validPems {
					if v2 != v && v2 != v1 {
						result[k+k1+k2] = v + v1 + v2
					}
				}
			}
		}
	}
	for k, v := range embedded {
		result[k] = v
	}
	return result
}

var allPEMs = strings.Join([]string{validCert, validKey, validCA}, "\n")

var embedded = map[string]string{
	"spaces":     " " + allPEMs + " ",
	"brackets":   "{{ " + allPEMs + " }}",
	"null":       "\x00" + allPEMs + "\x00",
	"tabs":       "\t" + allPEMs + "\t",
	"assignment": "\tCerts='" + allPEMs + "'",
}

const badCert = `-----BEGIN CERTIFICATE-----
MIIDWTCCAkGgAwIBAgIUUARA+vQExU8zjdsX/YXMMu1K5FkwDQYJKoZIhvcNAQEL
eB01bl42Y5WwHl0IrjfbEevzoW0+uhlUlZ6keZHr7bLn/xuRCUkVfj3PRlMl
-----END CERTIFICATE-----`

const (
	badPlusGarbo = `
aa983w4;/amndsfm908q26035vc;ng902338(%@%!@QY!&DVLMNSALX>PT(RQ!QO*%@
X` + badCert + `X
!Q)(*@^YUO!Q#MN%$#WP(G^&+_!%)!+^%$Y	:!#QLKENFVJ)	!#*&%YHTM
`
)
