// Copyright 2021 Liuxiangchao iwind.liu@gmail.com. All rights reserved.

package sslconfigs_test

import (
	"context"
	"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs/sslconfigs"
	"github.com/iwind/TeaGo/assert"
	"testing"
	"time"
)

func TestSSLPolicy_MatchDomain(t *testing.T) {
	var a = assert.NewAssertion(t)

	var policy = &sslconfigs.SSLPolicy{}
	policy.Certs = []*sslconfigs.SSLCertConfig{
		{
			Id:       1,
			DNSNames: []string{"a.com", "b.com"},
		},
		{
			Id:       2,
			DNSNames: []string{"c.com", "d.com"},
		},
		{
			Id:       3,
			DNSNames: []string{"e.com", "f.com"},
		},
	}

	{
		_, ok := policy.MatchDomain("c.com")
		a.IsTrue(ok)
	}
}

func TestSSLPolicy_CheckOCSP(t *testing.T) {
	var certData = []byte(`-----BEGIN CERTIFICATE-----
MIIEcTCCA9qgAwIBAgIDbhMuMA0GCSqGSIb3DQEBBQUAMIGKMQswCQYDVQQGEwJD
TjESMBAGA1UECBMJR3Vhbmdkb25nMREwDwYDVQQHEwhTaGVuemhlbjEQMA4GA1UE
ChMHVGVuY2VudDEMMAoGA1UECxMDV1hHMRMwEQYDVQQDEwpNbXBheW1jaENBMR8w
HQYJKoZIhvcNAQkBFhBtbXBheW1jaEB0ZW5jZW50MB4XDTE2MTIxMjA5NDAwM1oX
DTI2MTIxMDA5NDAwM1owgaExCzAJBgNVBAYTAkNOMRIwEAYDVQQIEwlHdWFuZ2Rv
bmcxETAPBgNVBAcTCFNoZW56aGVuMRAwDgYDVQQKEwdUZW5jZW50MQ4wDAYDVQQL
EwVNTVBheTE2MDQGA1UEAxQt5YyX5Lqs5LiJ55m+5YWt5Y2B6KGM5LqS6IGU56eR
5oqA5pyJ6ZmQ5YWs5Y+4MREwDwYDVQQEEwgxNzIyNzc0NDCCASIwDQYJKoZIhvcN
AQEBBQADggEPADCCAQoCggEBAN2/1axdFhLKgMAGpkM9kpBfz88IvVYLFLaRrsIO
aM4RLDup5ye0GrOvQtq8gvP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5WHLUBEQxCPiwsvCjccSRAyzDNQZfG7xuOXJlZR/P9ms/ce8Ry6hyO+nYEzb
qNXddQHSD+RjjAxUwCxdw3XNgFTQimE03EarO5zZdMT57RKa3AaBWePpbQKBgQCq
D4fcMNFrfaqqt8FUEgAlLiZw7En5Hz+Ufrr0/Kt6LNnj6EFiTYgfcjcMQ6mHJzKV
XL5SY4mg2D+RUectH4mJdae74QPNVTJcVQuv6wbOw45+PZbtsYddYenwwqWjDADd
IExdaoXHctjDMcVmWTozCg38I48biC5Pl0WHi86bAQKBgGBK6XUJPRYOsQFshunq
edxSbZBiYFDUj6SfOdaTSuU61KOWRTXJyuOBaB77usmZdwOrB4vy1XUT1uuPWKlx
SKmNoe/mk2xYiGdKvFDRRHh25zCxDWsQ2nMQfUFczTZ9wBwGs40wzm36fSgHZybq
Z3NIV2eNt6YBwkC69DzdazXT
-----END PRIVATE KEY-----
`)

	var policy = &sslconfigs.SSLPolicy{
		OCSPIsOn: true,
	}

	var nowTime = time.Now().Unix()

	policy.Certs = append(policy.Certs, &sslconfigs.SSLCertConfig{
		Id:            1,
		CertData:      certData,
		KeyData:       keyData,
		OCSP:          []byte("ocsp"),
		OCSPExpiresAt: nowTime + 1,
	})
	policy.Certs = append(policy.Certs, &sslconfigs.SSLCertConfig{
		Id:            1,
		CertData:      certData,
		KeyData:       keyData,
		OCSP:          []byte("ocsp"),
		OCSPExpiresAt: nowTime + 3,
	})
	policy.Certs = append(policy.Certs, &sslconfigs.SSLCertConfig{
		Id:            1,
		CertData:      certData,
		KeyData:       keyData,
		OCSP:          []byte("ocsp"),
		OCSPExpiresAt: nowTime + 2,
	})

	err := policy.Init(context.TODO())
	if err != nil {
		t.Fatal(err)
	}

	t.Log(policy.OcspExpiresAt(), policy.OcspExpiresAt() == nowTime+1)

	time.Sleep(1 * time.Second)
	policy.CheckOCSP()
	t.Log(policy.OcspExpiresAt(), policy.OcspExpiresAt() == nowTime+2)
}
