package topdown

import (
	"crypto"
	"crypto/ecdsa"
	"crypto/ed25519"
	"crypto/rsa"
	"crypto/x509"
	"encoding/base64"
	"errors"
	"fmt"
	"strconv"
	"strings"
	"testing"
	"time"

	"github.com/google/go-cmp/cmp"
	"github.com/google/go-cmp/cmp/cmpopts"
	"github.com/open-policy-agent/opa/v1/ast"
	"github.com/open-policy-agent/opa/v1/topdown/builtins"
)

var rootCA = `-----BEGIN CERTIFICATE-----
MIIBoDCCAUagAwIBAgIRAJXcMYZALXooNq/VV/grXhMwCgYIKoZIzj0EAwIwLjER
MA8GA1UEChMIT1BBIFRlc3QxGTAXBgNVBAMTEE9QQSBUZXN0IFJvb3QgQ0EwHhcN
MjEwNzAxMTc0MTUzWhcNMzEwNjI5MTc0MTUzWjAuMREwDwYDVQQKEwhPUEEgVGVz
dDEZMBcGA1UEAxMQT1BBIFRlc3QgUm9vdCBDQTBZMBMGByqGSM49AgEGCCqGSM49
AwEHA0IABFqhdZA5LjsJgzsBvhgzfayZFOk+C7PmGCi7xz6zOC3xWORJZSNOyZeJ
YzSKFmoMZkcFMfslTW1jp9fwe1xl3HWjRTBDMA4GA1UdDwEB/wQEAwIBBjASBgNV
HRMBAf8ECDAGAQH/AgEBMB0GA1UdDgQWBBTch60qxQvLl+AfDfcaXmjvT8GvpzAK
BggqhkjOPQQDAgNIADBFAiBqraIP0l2U0oNuH0+rf36hDks94wSB5EGlGH3lYNMR
ugIhANkbukX5hOP8pJDRWP/pYuv6MBnRY4BS8gpp9Vu31qOb
-----END CERTIFICATE-----`
var intermediateCA = `-----BEGIN CERTIFICATE-----
MIIByDCCAW6gAwIBAgIQC0k4DPGrh9me73EJX5zntTAKBggqhkjOPQQDAjAuMREw
DwYDVQQKEwhPUEEgVGVzdDEZMBcGA1UEAxMQT1BBIFRlc3QgUm9vdCBDQTAeFw0y
MTA3MDExNzQxNTNaFw0zMTA2MjkxNzQxNTNaMDYxETAPBgNVBAoTCE9QQSBUZXN0
MSEwHwYDVQQDExhPUE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xWRXyHkWtSqQtDcb8MDDp5/kzkfUgnUCgYEAv8CVWPKTuw83/nnqZg26
URXJ/C7hN/1uU21BuyCTMV/fLiSAsV0ucDV2spqCl3VAXcsECavERVppluVylBcR
DFa6BZS0N0x374JRidFWV0a+Mz7pTqC0TO/M3+y6yaDd766J3bkdh2sq8pnhAnXc
qPYXB5U6tdTrexzaYBKr4gQ=
-----END PRIVATE KEY-----`

var keyPemEC = `-----BEGIN EC PRIVATE KEY-----
MHcCAQEEIIrYSSNQFaA2Hwf1duRSxKtLYX5CB04fSeQ6tF1aY/PuoAoGCCqGSM49
AwEHoUQDQgAEPR3tU2Fta9ktY+6P9G0cWO+0kETA6SFs38GecTyudlHz6xvCdz8q
EKTcWGekdmdDPsHloRNtsiCa697B2O9IFA==
-----END EC PRIVATE KEY-----`

var keyEd25519 = `-----BEGIN PRIVATE KEY-----
MC4CAQAwBQYDK2VwBCIEIJHG93jlLLLTF6Stky5+8Q7mMpgCkYYTO12NDAzlJn3w
-----END PRIVATE KEY-----
`

var partiallyValidPEMString = `
something else
-----BEGIN PRIVATE KEY-----
 MC4CAQAwBQYDK2VwBCIEIJHG93jlLLLTF6Stky5+8Q7mMpgCkYYTO12NDAzlJn3w
-----END PRIVATE KEY-----
something else
-----BEGIN CERTIFICATE-----
MIIBcDCCARagAwIBAgIJAMZmuGSIfvgzMAoGCCqGSM49BAMCMBMxETAPBgNVBAMM
CHdoYXRldmVyMB4XDTE4MDgxMDE0Mjg1NFoXDTE4MDkwOTE0Mjg1NFowEzERMA8G
A1UEAwwId2hhdGV2ZXIwWTATBgcqhkjOPQIBBggqhkjOPQMBBwNCAATPwn3WCEXL
mjp/bFniDwuwsfu7bASlPae2PyWhqGeWwe23Xlyx+tSqxlkXYe4pZ23BkAAscpGj
yn5gXHExyDlKo1MwUTAdBgNVHQ4EFgQUElRjSoVgKjUqY5AXz2o74cLzzS8wHwYD
VR0jBBgwFoAUElRjSoVgKjUqY5AXz2o74cLzzS8wDwYDVR0TAQH/BAUwAwEB/zAK
BggqhkjOPQQDAgNIADBFAiEA4yQ/88ZrUX68c6kOe9G11u8NUaUzd8pLOtkKhniN
OHoCIHmNX37JOqTcTzGn2u9+c8NlnvZ0uDvsd1BmKPaUmjmm
-----END CERTIFICATE-----
something else
-----BEGIN PRIVATE KEY-----
 MC4CAQAwBQYDK2VwBCIEIJHG93jlLLLTF6Stky5+8Q7mMpgCkYYTO12NDAzlJn3w
-----END PRIVATE KEY-----
something else
`
var invalidData = `nothingtoseehere`

func TestX509ParseAndVerify(t *testing.T) {
	t.Parallel()

	t.Run("TestFullChainPEM", func(t *testing.T) {
		t.Parallel()

		chain := strings.Join([]string{rootCA, intermediateCA, leaf}, "\n")

		parsed, err := getX509CertsFromString(chain)
		if err != nil {
			t.Fatalf("failed to parse PEM cert chain: %v", err)
		}

		if _, err := verifyX509CertificateChain(parsed, x509.VerifyOptions{}); err != nil {
			t.Error("x509 verification failed when it was expected to succeed")
		}
	})

	t.Run("TestFullChainBase64", func(t *testing.T) {
		t.Parallel()

		chain := strings.Join([]string{rootCA, intermediateCA, leaf}, "\n")
		b64 := base64.StdEncoding.EncodeToString([]byte(chain))

		parsed, err := getX509CertsFromString(b64)
		if err != nil {
			t.Fatalf("failed to parse base64 cert chain: %v", err)
		}

		if _, err := verifyX509CertificateChain(parsed, x509.VerifyOptions{}); err != nil {
			t.Error("x509 verification failed when it was expected to succeed")
		}
	})

	t.Run("TestWrongOrder", func(t *testing.T) {
		t.Parallel()

		chain := strings.Join([]string{leaf, intermediateCA, rootCA}, "\n")

		parsed, err := getX509CertsFromString(chain)
		if err != nil {
			t.Fatalf("failed to parse PEM cert chain: %v", err)
		}

		if _, err := verifyX509CertificateChain(parsed, x509.VerifyOptions{}); err == nil {
			t.Error("x509 verification succeeded when it was expected to fail")
		}
	})

	t.Run("TestMissingIntermediate", func(t *testing.T) {
		t.Parallel()

		chain := rootCA + "\n" + leaf

		parsed, err := getX509CertsFromString(chain)
		if err != nil {
			t.Fatalf("failed to parse PEM cert chain: %v", err)
		}

		if _, err := verifyX509CertificateChain(parsed, x509.VerifyOptions{}); err == nil {
			t.Error("x509 verification succeeded when it was expected to fail")
		}
	})

	t.Run("TestTooFewCerts", func(t *testing.T) {
		t.Parallel()

		parsed, err := getX509CertsFromString(leaf)
		if err != nil {
			t.Fatalf("failed to parse leaf cert: %v", err)
		}

		if _, err := verifyX509CertificateChain(parsed, x509.VerifyOptions{}); err == nil {
			t.Error("x509 verification succeeded when it was expected to fail")
		}
	})
}

func Test_parsex509KeyPair(t *testing.T) {
	t.Parallel()

	certPemEC := []byte(`-----BEGIN CERTIFICATE-----
MIIBhTCCASugAwIBAgIQIRi6zePL6mKjOipn+dNuaTAKBggqhkjOPQQDAjASMRAw
DgYDVQQKEwdBY21lIENvMB4XDTE3MTAyMDE5NDMwNloXDTE4MTAyMDE5NDMwNlow
EjEQMA4GA1UEChMHQWNtZSBDbzBZMBMGByqGSM49AgEGCCqGSM49AwEHA0IABD0d
7VNhbWvZLWPuj/RtHFjvtJBEwOkhbN/BnnE8rnZR8+sbwnc/KhCk3FhnpHZnQz7B
5aETbbIgmuvewdjvSBSjYzBhMA4GA1UdDwEB/wQEAwICpDATBgNVHSUEDDAKBggr
BgEFBQcDATAPBgNVHRMBAf8EBTADAQH/MCkGA1UdEQQiMCCCDmxvY2FsaG9zdDo1
NDUzgg4xMjcuMC4wLjE6NTQ1MzAKBggqhkjOPQQDAgNIADBFAiEA2zpJEPQyz6/l
Wf86aX6PepsntZv2GYlA5UpabfT2EZICICpJ5h/iI+i341gBmLiAFQOyTDT+/wQc
6MF9+Yw1Yy0t
-----END CERTIFICATE-----`)
	keyPemEC := []byte(`-----BEGIN EC PRIVATE KEY-----
MHcCAQEEIIrYSSNQFaA2Hwf1duRSxKtLYX5CB04fSeQ6tF1aY/PuoAoGCCqGSM49
AwEHoUQDQgAEPR3tU2Fta9ktY+6P9G0cWO+0kETA6SFs38GecTyudlHz6xvCdz8q
EKTcWGekdmdDPsHloRNtsi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3ClUyeTB4OHkvYzNqdzlNZksxWDBHWWhTY1owcFYzeTN4dGxxckhuTmpTUmFaM3BmUGw3NGFjMGJndEU5cThKdDUKbzNEMGdmWmkwOFJrdW5ua2dKMS9QZDVpYUgvWEdnVG56NXdTRUF6VFJCWEhLWVlvRnBGV0wwcnowa0pvamZDdwpSdXQ2T21uL0w2blFXL1I1endzcXZrQUR1by9LWFkyTjY5L1J4UHcwejhFcTZZTVR0OHBXOGU1ZHpSRlFUS2N2CktUdDJCNTB1VFVEUlBRbW92dkYzRmRqcllaYkhyRlhHUXIyaGV6bDU2QkRSSkJaTzhIQ0g0NEIxc3d6K3QySWYKb3V2MlBIZDA1V3ZDVHIvM0dFL2ZiVGpabFVWeDhaYTJUWXdWakhTUzl3OWtRQ1V2dzNXM25OdVF6TklHVGJzYQpEdlQzeVN0MjJkRDQwTVlrNHpOc3o4d1YwUVYwdmJ5enorMUZUSXJlVldJSXhpMHVveWdNaHVGTDdJem4xeHNBClQ4TWVsbHQzN20wM2V2QkxwUXdPWHF0N1JXWlNaMllydVQ2clpkZ2RBOXRlK2Y3VUxhYUNGRDJxYzloRFRLOGoKemh3Mk41cGNIMVpYZGZBRjNNUWo1VmErSElDRElaL3BtTmNkVlJnWWxWN0hkOWc9Ci0tLS0tRU5EIENFUlRJRklDQVRFLS0tLS0K`)
	certPemRSAKeyB64 := []byte(` `)

	t.Run("ParseX509KeyPairEC", func(t *testing.T) {
		t.Parallel()

		validKeyPairEc, err := getTLSx509KeyPairFromString(certPemEC, keyPemEC)
		if err != nil {
			t.Fatal("failed to parse x509 key pair")
		}
		if validKeyPairEc == nil {
			t.Fatal("expected certificate but got nil")
		}

	})

	t.Run("ParseX509KeyPairRSA", func(t *testing.T) {
		t.Parallel()

		validCertPair, err := getTLSx509KeyPairFromString(certPemx509, certPemRSA)
		if err != nil {
			t.Fatal("failed to parse x509 Pair with RSAkey")
		}
		if validCertPair == nil {
			t.Fatal("expected certificate but got nil")
		}

	})

	t.Run("ParseX509KeyPairRSABase64", func(t *testing.T) {
		t.Parallel()

		validCertPair, err := getTLSx509KeyPairFromString(certPemRSACrtB64, certPemRSAKeyB64)
		if err != nil {
			t.Fatal("failed to parse x509 Pair with RSAkey")
		}
		if validCertPair == nil {
			t.Fatal("expected certificate but got nil")
		}

	})

	t.Run("ParseX509KeyPairDERx509", func(t *testing.T) {
		t.Parallel()

		validCertPair, err := getTLSx509KeyPairFromString(certDERx509, certDERRSA)
		if err != nil {
			t.Fatal("failed to parse x509 Pair with RSAkey", err)
		}
		if validCertPair == nil {
			t.Fatal("expected certificate but got nil")
		}
	})

	t.Run("ParseX509KeyPairPEMstringB64Key", func(t *testing.T) {
		t.Parallel()

		validCertPair, err := getTLSx509KeyPairFromString(certPemx509, certPemRSAKeyB64)
		if err != nil {
			t.Fatal("failed to parse x509 Pair with RSAkey")
		}
		if validCertPair == nil {
			t.Fatal("expected certificate but got nil")
		}

	})

	t.Run("ParseX509KeyPairB64CRTBPEMKey", func(t *testing.T) {
		t.Parallel()

		validCertPair, err := getTLSx509KeyPairFromString(certPemRSACrtB64, certPemRSA)
		if err != nil {
			t.Fatal("failed to parse x509 Pair with RSAkey")
		}
		if validCertPair == nil {
			t.Fatal("expected certificate but got nil")
		}

	})

	t.Run("ParseX509KeyPairMisMatchedTypes", func(t *testing.T) {
		t.Parallel()

		certPair, err := getTLSx509KeyPairFromString(certPemEC, certPemRSA)
		if err == nil {
			t.Fatal("expected error but got nil")
		}
		if certPair != nil {
			t.Fatalf("expected no certificate but got %v\n", certPair)
		}
	})

}

func Test_getPrivateKeyFromPEMData(t *testing.T) {
	t.Parallel()

	tests := map[string]struct {
		input    string
		wantErr  string
		keyCheck func(t *testing.T, keys []crypto.PrivateKey)
	}{
		"invalid data": {
			input: invalidData,
			keyCheck: func(t *testing.T, keys []crypto.PrivateKey) {
				if len(keys) != 0 {
					t.Fatalf("expected no keys but got %d", len(keys))
				}
			},
		},
		"rsa key": {
			input: rsaPrivateKey,
			keyCheck: func(t *testing.T, keys []crypto.PrivateKey) {
				if len(keys) != 1 {
					t.Fatalf("expected 1 key but got %d", len(keys))
				}
				if _, ok := keys[0].(*rsa.PrivateKey); !ok {
					t.Fatalf("expected rsa key but got %T", keys[0])
				}
			},
		},
		"base64 rsa key": {
			input: base64.StdEncoding.EncodeToString([]byte(rsaPrivateKey)),
			keyCheck: func(t *testing.T, keys []crypto.PrivateKey) {
				if len(keys) != 1 {
					t.Fatalf("expected 1 key but got %d", len(keys))
				}
				if _, ok := keys[0].(*rsa.PrivateKey); !ok {
					t.Fatalf("expected rsa key but got %T", keys[0])
				}
			},
		},
		"rsa key pkcs8": {
			input: rsaPrivateKeyPKCS8,
			keyCheck: func(t *testing.T, keys []crypto.PrivateKey) {
				if len(keys) != 1 {
					t.Fatalf("expected 1 key but got %d", len(keys))
				}
				if _, ok := keys[0].(*rsa.PrivateKey); !ok {
					t.Fatalf("expected rsa key but got %T", keys[0])
				}
			},
		},
		"base64 rsa key pkcs8": {
			input: base64.StdEncoding.EncodeToString([]byte(rsaPrivateKeyPKCS8)),
			keyCheck: func(t *testing.T, keys []crypto.PrivateKey) {
				if len(keys) != 1 {
					t.Fatalf("expected 1 key but got %d", len(keys))
				}
				if _, ok := keys[0].(*rsa.PrivateKey); !ok {
					t.Fatalf("expected rsa key but got %T", keys[0])
				}
			},
		},
		"ec key": {
			input: keyPemEC,
			keyCheck: func(t *testing.T, keys []crypto.PrivateKey) {
				if len(keys) != 1 {
					t.Fatalf("expected 1 key but got %d", len(keys))
				}
				if _, ok := keys[0].(*ecdsa.PrivateKey); !ok {
					t.Fatalf("expected ecdsa key but got %T", keys[0])
				}
			},
		},
		"base64 ec key": {
			input: base64.StdEncoding.EncodeToString([]byte(keyPemEC)),
			keyCheck: func(t *testing.T, keys []crypto.PrivateKey) {
				if len(keys) != 1 {
					t.Fatalf("expected 1 key but got %d", len(keys))
				}
				if _, ok := keys[0].(*ecdsa.PrivateKey); !ok {
					t.Fatalf("expected ecdsa key but got %T", keys[0])
				}
			},
		},
		"ed key": {
			input: keyEd25519,
			keyCheck: func(t *testing.T, keys []crypto.PrivateKey) {
				if len(keys) != 1 {
					t.Fatalf("expected 1 key but got %d", len(keys))
				}
				if _, ok := keys[0].(ed25519.PrivateKey); !ok {
					t.Fatalf("expected ed25519 key but got %T", keys[0])
				}
			},
		},
		"base64 ed key": {
			input: base64.StdEncoding.EncodeToString([]byte(keyEd25519)),
			keyCheck: func(t *testing.T, keys []crypto.PrivateKey) {
				if len(keys) != 1 {
					t.Fatalf("expected 1 key but got %d", len(keys))
				}
				if _, ok := keys[0].(ed25519.PrivateKey); !ok {
					t.Fatalf("expected ed25519 key but got %T", keys[0])
				}
			},
		},
		"other PEM data, no keys": {
			input: fmt.Sprintf("%s\n%s\n%s\n", rootCA, intermediateCA, leaf),
			keyCheck: func(t *testing.T, keys []crypto.PrivateKey) {
				if len(keys) != 0 {
					t.Fatalf("expected no keys but got %d", len(keys))
				}
			},
		},
		"partially valid PEM data": {
			input: partiallyValidPEMString,
			keyCheck: func(t *testing.T, keys []crypto.PrivateKey) {
				if len(keys) != 2 {
					t.Fatalf("expected 1 key but got %d", len(keys))
				}
				if _, ok := keys[0].(ed25519.PrivateKey); !ok {
					t.Fatalf("expected ed25519 key but got %T", keys[0])
				}
				if _, ok := keys[1].(ed25519.PrivateKey); !ok {
					t.Fatalf("expected ed25519 key but got %T", keys[0])
				}
			},
		},
		"mixed PEM data": {
			input: fmt.Sprintf("%s\n%s\n%s\n%s", rootCA, intermediateCA, leaf, keyPemEC),
			keyCheck: func(t *testing.T, keys []crypto.PrivateKey) {
				if len(keys) != 1 {
					t.Fatalf("expected 1 key but got %d", len(keys))
				}
				if _, ok := keys[0].(*ecdsa.PrivateKey); !ok {
					t.Fatalf("expected ecdsa key but got %T", keys[0])
				}
			},
		},
		"mixed PEM data, two keys": {
			input: fmt.Sprintf("%s\n%s\n%s\n%s\n%s", rootCA, intermediateCA, leaf, keyPemEC, rsaPrivateKey),
			keyCheck: func(t *testing.T, keys []crypto.PrivateKey) {
				if len(keys) != 2 {
					t.Fatalf("expected 2 keys but got %d", len(keys))
				}
				if _, ok := keys[0].(*ecdsa.PrivateKey); !ok {
					t.Fatalf("expected ecdsa key but got %T", keys[0])
				}
				if _, ok := keys[1].(*rsa.PrivateKey); !ok {
					t.Fatalf("expected rsa key but got %T", keys[0])
				}
			},
		},
		"corrupted key": {
			input: `-----BEGIN PRIVATE KEY-----
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
-----END PRIVATE KEY-----
`,
			wantErr: "asn1: structure error",
			keyCheck: func(t *testing.T, keys []crypto.PrivateKey) {
				if len(keys) != 0 {
					t.Fatalf("expected no keys but got %d", len(keys))
				}
			},
		},
	}
	for name, testData := range tests {
		t.Run(name, func(t *testing.T) {
			t.Parallel()

			keys, err := getPrivateKeysFromPEMData(testData.input)
			if testData.wantErr != "" {
				if err != nil && !strings.Contains(err.Error(), testData.wantErr) {
					t.Fatalf("got error: %v, want error: %v", err, testData.wantErr)
				} else if err == nil {
					t.Fatalf("expected error: %v", testData.wantErr)
				}
			} else {
				if err != nil {
					t.Fatalf("unexpected error: %v", err)
				}
			}
			testData.keyCheck(t, keys)
		})
	}
}

func TestParseAndVerifyX509CertsWithOptions(t *testing.T) {
	t.Parallel()

	chain := strings.Join([]string{rootCA, intermediateCA, leaf}, "\n")

	parsed, err := getX509CertsFromString(chain)
	if err != nil {
		t.Fatalf("failed to parse PEM cert chain: %v", err)
	}

	dnsName := parsed[2].DNSNames[0]
	notBefore, notAfter := parsed[2].NotBefore, parsed[2].NotAfter
	invalidBefore := notBefore.Add(-time.Minute).UnixNano()
	invalidAfter := notAfter.Add(time.Minute).UnixNano()
	// This certificate has "KeyUsageServerAuth", "KeyUsageClientAuth" as key usages. So these are used
	// in following tests.

	tests := []struct {
		jsonOption *ast.Term
		expectErr  bool
	}{
		{
			jsonOption: ast.MustParseTerm(`{"DNSName": "bad.dns.com"}`),
			expectErr:  true,
		},
		{
			jsonOption: ast.MustParseTerm(`{"CurrentTime": ` + strconv.FormatInt(invalidBefore, 10) + `}`),
			expectErr:  true,
		},
		{
			jsonOption: ast.MustParseTerm(`{"CurrentTime": ` + strconv.FormatInt(invalidAfter, 10) + `}`),
			expectErr:  true,
		},
		{
			jsonOption: ast.MustParseTerm(`{"CurrentTime": ` + strconv.FormatInt(notBefore.UnixNano(), 10) + `}`),
			expectErr:  false,
		},
		{
			jsonOption: ast.MustParseTerm(`{"DNSName": "` + dnsName + `" }`),
			expectErr:  false,
		},
		{
			jsonOption: ast.MustParseTerm(`{"KeyUsages": ["KeyUsageServerAuth", "KeyUsageClientAuth", "KeyUsageCodeSigning"] }`),
			expectErr:  false,
		},
		{
			jsonOption: ast.MustParseTerm(`{"KeyUsages": ["KeyUsageCodeSigning"] }`),
			expectErr:  true,
		},
		{
			jsonOption: ast.MustParseTerm(`{"DNSName": "` + dnsName + `", "CurrentTime": ` + strconv.FormatInt(notBefore.UnixNano(), 10) + `, "KeyUsages": ["KeyUsageServerAuth", "KeyUsageCodeSigning"] }`),
			expectErr:  false,
		},
	}

	for _, testCase := range tests {
		options, _ := builtins.ObjectOperand(testCase.jsonOption.Value, 0)
		vo, err := extractVerifyOpts(options)
		if err != nil {
			t.Fatalf("Unexpected error in extracting options: %s", err)
		}

		_, err = verifyX509CertificateChain(parsed, vo)
		if testCase.expectErr {
			if err == nil {
				t.Fatalf("expected error in verifying cert chain, but got nil error")
			}
		} else {
			if err != nil {
				t.Fatalf("did not expect error, but got error: %s", err)
			}
		}
	}

}

func TestExtractX509VerifyOptions(t *testing.T) {
	t.Parallel()

	tests := []struct {
		jsonOption      *ast.Term
		expectErr       error
		expectVerifyOpt x509.VerifyOptions
	}{
		{
			jsonOption: ast.MustParseTerm(`{"DNSName": 1}`),
			expectErr:  errors.New("'DNSName' should be a string"),
		},
		{
			jsonOption: ast.MustParseTerm(`{CurrentTime: "string"}`),
			expectErr:  errors.New("'CurrentTime' should be a number"),
		},
		{
			jsonOption: ast.MustParseTerm(`{MaxConstraintComparisons: "string"}`),
			expectErr:  errors.New("'MaxConstraintComparisons' should be a number"),
		},
		{
			jsonOption: ast.MustParseTerm(`{"KeyUsages" : "true"}`),
			expectErr:  errors.New("'KeyUsages' should be an Array or Set"),
		},
		{
			jsonOption: ast.MustParseTerm(`{"DNSName": 1, CurrentTime: "string", "KeyUsages" : {1,2}}`),
			expectErr:  errors.New("'DNSName' should be a string"),
		},
		{
			jsonOption: ast.MustParseTerm(`{"InvalidKey": "test.com"}`),
			expectErr:  errors.New("invalid key option"),
		},
		{
			jsonOption:      ast.MustParseTerm(`{}`),
			expectVerifyOpt: x509.VerifyOptions{},
		},
		{
			jsonOption: ast.MustParseTerm(`{"KeyUsages" : {"KeyUsageAny", "InvalidKeyUsage", "KeyUsageServerAuth"}}`),
			expectErr:  fmt.Errorf("invalid entries for 'KeyUsages' found: %s", []string{"InvalidKeyUsage"}),
		},
		{
			jsonOption: ast.MustParseTerm(`{"KeyUsages" : ["1","KeyUsageAny", "InvalidKeyUsage", "KeyUsageServerAuth", "2"]}`),
			expectErr:  fmt.Errorf("invalid entries for 'KeyUsages' found: %s", []string{"1", "InvalidKeyUsage", "2"}),
		},
		{
			jsonOption: ast.MustParseTerm(`{"DNSName": "test.com", "CurrentTime": 1708447636000000000, ` +
				`"MaxConstraintComparisons": 5, "KeyUsages" : {"KeyUsageAny", "KeyUsageServerAuth","KeyUsageClientAuth"}}`),
			expectVerifyOpt: x509.VerifyOptions{
				DNSName:                   "test.com",
				CurrentTime:               time.Unix(0, 1708447636000000000),
				MaxConstraintComparisions: 5,
				KeyUsages:                 []x509.ExtKeyUsage{x509.ExtKeyUsageAny, x509.ExtKeyUsageServerAuth, x509.ExtKeyUsageClientAuth},
			},
		},
		{
			jsonOption: ast.MustParseTerm(`{"DNSName": "test.com", "CurrentTime": 1708447636000000000, ` +
				`"MaxConstraintComparisons": 5, "KeyUsages" : {"KeyUsageAny", "KeyUsageAny", 1, 2, "KeyUsageServerAuth","KeyUsageClientAuth"}}`),
			expectVerifyOpt: x509.VerifyOptions{
				DNSName:                   "test.com",
				CurrentTime:               time.Unix(0, 1708447636000000000),
				MaxConstraintComparisions: 5,
				KeyUsages:                 []x509.ExtKeyUsage{x509.ExtKeyUsageAny, x509.ExtKeyUsageServerAuth, x509.ExtKeyUsageClientAuth},
			},
		},
		{ // KeyUsages as an array
			jsonOption: ast.MustParseTerm(`{"DNSName": "test.com", "CurrentTime": 1708447636000000000,
				"MaxConstraintComparisons": 5,
				"KeyUsages" : ["KeyUsageAny", "KeyUsageAny", 1, 2,
				"KeyUsageServerAuth","KeyUsageClientAuth"]}`),
			expectVerifyOpt: x509.VerifyOptions{
				DNSName:                   "test.com",
				CurrentTime:               time.Unix(0, 1708447636000000000),
				MaxConstraintComparisions: 5,
				KeyUsages:                 []x509.ExtKeyUsage{x509.ExtKeyUsageAny, x509.ExtKeyUsageAny, x509.ExtKeyUsageServerAuth, x509.ExtKeyUsageClientAuth},
			},
		},
	}

	for _, testCase := range tests {
		options, _ := builtins.ObjectOperand(testCase.jsonOption.Value, 0)
		if testCase.expectErr == nil {
			vo, err := extractVerifyOpts(options)
			if err != nil {
				t.Fatalf("did not expect error but got %s", err)
			}
			// ignore the order of ExtKeyUsage values
			if !cmp.Equal(vo, testCase.expectVerifyOpt, cmpopts.SortSlices(
				func(ku1, ku2 x509.ExtKeyUsage) bool {
					return ku1 < ku2
				}), cmpopts.IgnoreUnexported(x509.VerifyOptions{})) {

				t.Fatalf("expected x509.VerifyOptions: %+v \n"+
					"got: %+v", testCase.expectVerifyOpt, vo)
			}
		} else {
			_, err := extractVerifyOpts(options)
			if err == nil {
				t.Fatalf("expected error: %s, got nil error", testCase.expectErr)
			}
		}
	}
}

// Before/after replacing sprintf("%x", ...) with hex.EncodeToString(...), and using
// util.ByteSliceToString to convert the resulting byte slice:
// BenchmarkMd5-10    	 3294998	       435.2 ns/op	     128 B/op	       5 allocs/op
// BenchmarkMd5-10    	 6193455	       180.9 ns/op	      96 B/op	       3 allocs/op
// ...
func BenchmarkMd5(b *testing.B) {
	bctx := BuiltinContext{}
	operands := []*ast.Term{ast.StringTerm("hello")}
	expect := ast.String("5d41402abc4b2a76b9719d911017c592")
	iter := func(result *ast.Term) error {
		if !expect.Equal(result.Value) {
			return fmt.Errorf("unexpected result: %v", result.Value)
		}
		return nil
	}

	b.ResetTimer()

	for range b.N {
		err := builtinCryptoMd5(bctx, operands, iter)
		if err != nil {
			b.Fatalf("unexpected error: %v", err)
		}
	}
}
