LCOV - code coverage report
Current view: top level - src/tds/unittests - tls.c (source / functions) Hit Total Coverage
Test: FreeTDS coverage Lines: 24 30 80.0 %
Date: 2025-01-18 11:50:39 Functions: 2 2 100.0 %

          Line data    Source code
       1             : /* FreeTDS - Library of routines accessing Sybase and Microsoft databases
       2             :  * Copyright (C) 2023 Frediano Ziglio
       3             :  *
       4             :  * This library is free software; you can redistribute it and/or
       5             :  * modify it under the terms of the GNU Library General Public
       6             :  * License as published by the Free Software Foundation; either
       7             :  * version 2 of the License, or (at your option) any later version.
       8             :  *
       9             :  * This library is distributed in the hope that it will be useful,
      10             :  * but WITHOUT ANY WARRANTY; without even the implied warranty of
      11             :  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
      12             :  * Library General Public License for more details.
      13             :  *
      14             :  * You should have received a copy of the GNU Library General Public
      15             :  * License along with this library; if not, write to the
      16             :  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
      17             :  * Boston, MA 02111-1307, USA.
      18             :  */
      19             : 
      20             : /*
      21             :  * Check check_hostname function
      22             :  */
      23             : #include "../tls.c"
      24             : 
      25             : #include <freetds/data.h>
      26             : 
      27             : #if defined(HAVE_OPENSSL)
      28             : 
      29             : #include <freetds/bool.h>
      30             : 
      31             : /* This certificate has common name as "www.abc.com" and alternate names
      32             :    as "xyz.org", "127.0.0.1", "::2:3:4:5:6" and "192.168.127.1". */
      33             : static const char certificate[] =
      34             : "-----BEGIN CERTIFICATE-----\n"
      35             : "MIIE0jCCA7qgAwIBAgIUbIV2n53RPAMttnVuFQlE9C0tPvAwDQYJKoZIhvcNAQEL\n"
      36             : "BQAwgYwxCzAJBgNVBAYTAlVLMQswCQYDVQQIDAJDQTESMBAGA1UEBwwJQ2FtYnJp\n"
      37             : "ZGdlMRMwEQYDVQQKDApFeGFtcGxlIENvMRAwDgYDVQQLDAd0ZWNob3BzMRMwEQYD\n"
      38             : "VQQDDApUZXN0aW5nIENBMSAwHgYJKoZIhvcNAQkBFhFjZXJ0c0BleGFtcGxlLmNv\n"
      39             : "bTAeFw0yMzA5MjYxOTI2MjZaFw0yNjA2MjExOTI2MjZaMIGLMQswCQYDVQQGEwJV\n"
      40             : "SzELMAkGA1UECAwCQ0ExEjAQBgNVBAcMCUNhbWJyaWRnZTERMA8GA1UECgwIRnJl\n"
      41             : "ZGlhbm8xEDAOBgNVBAsMB3RlY2hvcHMxFDASBgNVBAMMC3d3dy5hYmMuY29tMSAw\n"
      42             : "HgYJKoZIhvcNAQkBFhFjZXJ0c0BleGFtcGxlLmNvbTCCASIwDQYJKoZIhvcNAQEB\n"
      43             : "BQADggEPADCCAQoCggEBAMcXWlvCeX//9wxvaTP9qD1RaFYUhxOppC/+JDBHnn8Y\n"
      44             : "T9915OYzctoAoVrcThMsg5GNWTB0/OkXz0/IHgxJZ9HFFsTJSUFvVKSD2UrG2ypF\n"
      45             : "aSLdJOD2CpqNbrr0cNhIFfRBrJ7KC3F3PHKB7BoROiSCgTTz46Hx29fRLW3Rqxh0\n"
      46             : "tz/tj7Yt5vesqByWo5zj3vha/F4+eK1hNNuP93i8wkZIOPStWNOO2OQ/ULh8MZON\n"
      47             : "qpvJHw6NveDmVFIVGtutrA+5w30Wp2vUJI60erRSailsMpXFyElYdnYZ+24/hA7P\n"
      48             : "Hfx3v5cQ+DHF3+AKFU7G2bcS/kB48vLSZzDz82/5O88CAwEAAaOCASkwggElMAwG\n"
      49             : "A1UdEwEB/wQCMAAwCwYDVR0PBAQDAgXgMDAGA1UdEQQpMCeCB3h5ei5vcmeHBH8A\n"
      50             : "AAGHEAAAAAAAAAACAAMABAAFAAaHBMCofwEwHQYDVR0OBBYEFDWbwRVMZvyOL8oA\n"
      51             : "nVpuRW2xkeeoMIG2BgNVHSMEga4wgauhgZKkgY8wgYwxCzAJBgNVBAYTAlVLMQsw\n"
      52             : "CQYDVQQIDAJDQTESMBAGA1UEBwwJQ2FtYnJpZGdlMRMwEQYDVQQKDApFeGFtcGxl\n"
      53             : "IENvMRAwDgYDVQQLDAd0ZWNob3BzMRMwEQYDVQQDDApUZXN0aW5nIENBMSAwHgYJ\n"
      54             : "KoZIhvcNAQkBFhFjZXJ0c0BleGFtcGxlLmNvbYIUW7YAeQBh0HFi6VWbFh9+tG2F\n"
      55             : "8NAwDQYJKoZIhvcNAQELBQADggEBAL0SfWxEufOYqg9e3vnLJj5Jxv1arayEWHrt\n"
      56             : "hL64GmEw4DltxX2DXAlPnQvpMvYGV3ynnAdnvDaFlBceG0iZzu9ZQTw0bdB12L30\n"
      57             : "PETIYUN1uHPaIXA8cCtLFi0BNVIeGH8WYbOVEu0Kl7JX+WSbZqnC9+wqpGrQv578\n"
      58             : "Ml+EIP8L1ZLaJx7W1U+A/WW+xtWmpTnHVNyOAWdX3c+GE4kSYzsW+6D3Ha2EYAno\n"
      59             : "R46tD+akLPNKjYETaB+MU72xF7h4crpEqfOZx2WVwMKjRsZed33xG4kG3P8SItYI\n"
      60             : "UJdPxDazCiKJYJ/JlRLZTW7+LisH33QcRRmbtS7KHrBDXveDez8=\n"
      61             : "-----END CERTIFICATE-----\n";
      62             : 
      63             : static bool got_failure = false;
      64             : 
      65             : static void
      66          36 : test_hostname(X509 *cert, const char *hostname, bool expected)
      67             : {
      68          36 :         bool got = !!check_hostname(cert, hostname);
      69          36 :         if (got != expected) {
      70           0 :                 fprintf(stderr, "unexpected result, got %d expected %d for '%s'\n", got, expected, hostname);
      71           0 :                 got_failure = true;
      72             :         }
      73          36 : }
      74             : 
      75             : int
      76           4 : main(void)
      77             : {
      78             :         BIO *bufio;
      79             :         X509 *cert;
      80             :         bool expected;
      81             : #define test(hostname) test_hostname(cert, hostname, expected)
      82             : 
      83           4 :         bufio = BIO_new_mem_buf((void*)certificate, -1);
      84           4 :         if (!bufio) {
      85           0 :                 fprintf(stderr, "error allocating BIO\n");
      86           0 :                 return 1;
      87             :         }
      88           4 :         cert = PEM_read_bio_X509(bufio, NULL, 0, NULL);
      89           4 :         if (!cert) {
      90           0 :                 fprintf(stderr, "error creating certificate\n");
      91           0 :                 return 1;
      92             :         }
      93             : 
      94             :         /* check valid names are accepted */
      95           4 :         expected = true;
      96           4 :         test("xyz.org");
      97           4 :         test("127.0.0.1");
      98           4 :         test("::2:3:4:5:6");
      99           4 :         test("192.168.127.1");
     100           4 :         test("www.abc.com");
     101             : 
     102             :         /* check some invalid names */
     103           4 :         expected = false;
     104           4 :         test("fail.com");
     105           4 :         test("127.0.0.2");
     106           4 :         test("::2:3:4:5:7");
     107           4 :         test("::1");
     108             : 
     109           4 :         X509_free(cert);
     110           4 :         BIO_free(bufio);
     111           4 :         return got_failure ? 1 : 0;
     112             : }
     113             : #else
     114             : int
     115           4 : main(void)
     116             : {
     117             :         return 0;
     118             : }
     119             : #endif

Generated by: LCOV version 1.13