3535#include " tscore/ink_queue.h"
3636#include " tscore/X509HostnameValidator.h"
3737
38+ #include " tscpp/util/PostScript.h"
39+
3840// clang-format off
3941
4042// A simple certificate for CN=test.sslheaders.trafficserver.apache.org.
@@ -98,13 +100,17 @@ static X509 *
98100load_cert_from_string (const char *cert_string)
99101{
100102 BIO *bio = BIO_new_mem_buf ((void *)cert_string, -1 );
103+ ts::PostScript bio_defer ([&]() -> void { BIO_free (bio); });
104+
101105 return PEM_read_bio_X509 (bio, nullptr , nullptr , nullptr );
102106}
103107
104108TEST_CASE (" CN_match" , " [libts][X509HostnameValidator]" )
105109{
106110 char *matching;
107111 X509 *x = load_cert_from_string (test_certificate_cn);
112+ ts::PostScript x_defer ([&]() -> void { X509_free (x); });
113+
108114 REQUIRE (x != nullptr );
109115 REQUIRE (validate_hostname (x, (unsigned char *)test_certificate_cn_name, false , &matching) == true );
110116 REQUIRE (strcmp (test_certificate_cn_name, matching) == 0 );
@@ -115,6 +121,8 @@ TEST_CASE("CN_match", "[libts][X509HostnameValidator]")
115121TEST_CASE (" bad_wildcard_SANs" , " [libts][X509HostnameValidator]" )
116122{
117123 X509 *x = load_cert_from_string (test_certificate_bad_sans);
124+ ts::PostScript x_defer ([&]() -> void { X509_free (x); });
125+
118126 REQUIRE (x != nullptr );
119127 REQUIRE (validate_hostname (x, (unsigned char *)" something.or.other" , false , nullptr ) == false );
120128 REQUIRE (validate_hostname (x, (unsigned char *)" a.b.c" , false , nullptr ) == false );
@@ -127,6 +135,8 @@ TEST_CASE("wildcard_SAN_and_CN", "[libts][X509HostnameValidator]")
127135{
128136 char *matching;
129137 X509 *x = load_cert_from_string (test_certificate_cn_and_SANs);
138+ ts::PostScript x_defer ([&]() -> void { X509_free (x); });
139+
130140 REQUIRE (x != nullptr );
131141 REQUIRE (validate_hostname (x, (unsigned char *)test_certificate_cn_name, false , &matching) == true );
132142 REQUIRE (strcmp (test_certificate_cn_name, matching) == 0 );
@@ -143,6 +153,8 @@ TEST_CASE("IDNA_hostnames", "[libts][X509HostnameValidator]")
143153{
144154 char *matching;
145155 X509 *x = load_cert_from_string (test_certificate_cn_and_SANs);
156+ ts::PostScript x_defer ([&]() -> void { X509_free (x); });
157+
146158 REQUIRE (x != nullptr );
147159 REQUIRE (validate_hostname (x, (unsigned char *)" xn--foobar.trafficserver.org" , false , &matching) == true );
148160 REQUIRE (strcmp (" *.trafficserver.org" , matching) == 0 );
@@ -156,6 +168,8 @@ TEST_CASE("middle_label_match", "[libts][X509HostnameValidator]")
156168{
157169 char *matching;
158170 X509 *x = load_cert_from_string (test_certificate_cn_and_SANs);
171+ ts::PostScript x_defer ([&]() -> void { X509_free (x); });
172+
159173 REQUIRE (x != nullptr );
160174 REQUIRE (validate_hostname (x, (unsigned char *)" foosomething.trafficserver.com" , false , &matching) == true );
161175 REQUIRE (strcmp (" foo*.trafficserver.com" , matching) == 0 );
0 commit comments