/*
 * ZLint Copyright 2018 Regents of the University of Michigan
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy
 * of the License at http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
 * implied. See the License for the specific language governing
 * permissions and limitations under the License.
 */

package zlint

import (
	"encoding/pem"
	"testing"

	"github.com/smallstep/zcrypto/x509"
	"github.com/smallstep/zlint/lints"
)

var (
	globalLintResult       *ResultSet
	globalSingleLintResult *lints.LintResult
)

const bigCertificatePem = `-----BEGIN CERTIFICATE-----
MIILajCCClKgAwIBAgIMOp/m5bdkZ2+oPevRMA0GCSqGSIb3DQEBCwUAMGIxCzAJ
BgNVBAYTAkJFMRkwFwYDVQQKExBHbG9iYWxTaWduIG52LXNhMTgwNgYDVQQDEy9H
bG9iYWxTaWduIEV4dGVuZGVkIFZhbGlkYXRpb24gQ0EgLSBTSEEyNTYgLSBHMzAe
Fw0xNzA2MjIwNjU2MDNaFw0xOTA2MjMwNjU2MDNaMIH9MR0wGwYDVQQPDBRQcml2
YXRlIE9yZ2FuaXphdGlvbjEPMA0GA1UEBRMGNTc4NjExMRMwEQYLKwYBBAGCNzwC
AQMTAlVTMR4wHAYLKwYBBAGCNzwCAQITDU5ldyBIYW1wc2hpcmUxCzAJBgNVBAYT
AlVTMRYwFAYDVQQIEw1OZXcgSGFtcHNoaXJlMRMwEQYDVQQHEwpQb3J0c21vdXRo
MSAwHgYDVQQJExdUd28gSW50ZXJuYXRpb25hbCBEcml2ZTEdMBsGA1UEChMUR01P
IEdsb2JhbFNpZ24sIEluYy4xGzAZBgNVBAMTEnd3dy5nbG9iYWxzaWduLmNvbTCC
ASIwDQYJKoZIhvcNAQEBBQADggEPADCCAQoCggEBAKaVk8nelrMqQmTSBju68D8B
MO7GGHtuQU8bfvuGNTUe6HiAxHYR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aapxlaAs29Y4SE/RvRR2CJ0V/gvq9GUorY4OF2
2HEky394KiGDZDYUUDArx2+9w+yPikV5llF7lm2o84kZifnBO6SE9+4zdBExzg==
-----END CERTIFICATE-----
`

func BenchmarkZlint(b *testing.B) {
	certDerBlock, _ := pem.Decode([]byte(bigCertificatePem))
	x509Cert, err := x509.ParseCertificate(certDerBlock.Bytes)
	if err != nil {
		b.Fatalf("Error parsing certificate: %s", err.Error())
	}

	b.ResetTimer()
	b.Run("All lints", func(b *testing.B) {
		var lintResult *ResultSet
		for i := 0; i < b.N; i++ {
			lintResult = LintCertificate(x509Cert)
		}

		globalLintResult = lintResult
	})

	b.Run("Fast lints", func(b *testing.B) {
		globalLintResult = &ResultSet{}
		globalLintResult.Results = make(map[string]*lints.LintResult, len(lints.Lints))
		b.ResetTimer()
		for i := 0; i < b.N; i++ {
			for key, value := range lints.Lints {
				switch key {
				case "w_dnsname_underscore_in_trd", "e_dnsname_underscore_in_sld", "e_dnsname_hyphen_in_sld",
					"w_dnsname_wildcard_left_of_public_suffix", "w_san_iana_pub_suffix_empty":
					continue
				}

				if !value.Lint.CheckApplies(x509Cert) {
					continue
				}
				globalLintResult.Results[key] = value.Lint.Execute(x509Cert)
			}
		}
	})

	b.Run("Fastest lints", func(b *testing.B) {
		globalLintResult = &ResultSet{}
		globalLintResult.Results = make(map[string]*lints.LintResult, len(lints.Lints))
		b.ResetTimer()
		for i := 0; i < b.N; i++ {
			for key, value := range lints.Lints {
				switch key {
				case "w_dnsname_underscore_in_trd", "e_dnsname_underscore_in_sld", "e_dnsname_hyphen_in_sld",
					"w_dnsname_wildcard_left_of_public_suffix", "w_san_iana_pub_suffix_empty",
					"w_rsa_mod_factors_smaller_than_752", "e_dnsname_bad_character_in_label", "w_subject_dn_leading_whitespace",
					"w_subject_dn_trailing_whitespace", "w_multiple_subject_rdn", "e_ext_san_dns_not_ia5_string",
					"e_ext_san_empty_name", "e_dnsname_not_valid_tld", "e_dnsname_contains_bare_iana_suffix",
					"e_dnsname_wildcard_only_in_left_label", "e_international_dns_name_not_nfc", "e_dnsname_left_label_wildcard_correct",
					"e_international_dns_name_not_unicode", "w_issuer_dn_trailing_whitespace", "w_issuer_dn_leading_whitespace",
					"w_multiple_issuer_rdn", "e_dnsname_empty_label", "e_dnsname_label_too_long", "e_distribution_point_incomplete",
					"e_wrong_time_format_pre2050", "e_utc_time_does_not_include_seconds", "e_sub_cert_not_is_ca", "w_rsa_mod_not_odd",
					"e_path_len_constraint_zero_or_less", "e_san_dns_name_includes_null_char":
					continue
				}

				if !value.Lint.CheckApplies(x509Cert) {
					continue
				}
				globalLintResult.Results[key] = value.Lint.Execute(x509Cert)
			}
		}
	})

	for key, value := range lints.Lints {
		b.Run(key, func(b *testing.B) {
			if !value.Lint.CheckApplies(x509Cert) {
				b.Skip("Check doesn't apply")
			}

			var result *lints.LintResult
			for i := 0; i < b.N; i++ {
				result = value.Lint.Execute(x509Cert)
			}

			globalSingleLintResult = result
		})
	}
}
