LCOV - code coverage report
Current view: top level - src/tds/unittests - spn.c (source / functions) Hit Total Coverage
Test: FreeTDS coverage Lines: 31 33 93.9 %
Date: 2026-11-25 04:57:11 Functions: 2 2 100.0 %

          Line data    Source code
       1             : /* FreeTDS - Library of routines accessing Sybase and Microsoft databases
       2             :  * Copyright (C) 2026  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             :  * Purpose: test tds_get_spn, computing service principal names.
      22             :  * Host names contain a dot, so no name resolution is done.
      23             :  */
      24             : #include "common.h"
      25             : #include <assert.h>
      26             : 
      27             : static TDSSOCKET *tds;
      28             : 
      29             : static void
      30          50 : check_spn(const char *expected)
      31             : {
      32          50 :         char *spn = tds_get_spn(tds);
      33             : 
      34          50 :         if (!spn || strcmp(spn, expected) != 0) {
      35           0 :                 fprintf(stderr, "Wrong SPN: got '%s' expected '%s'\n", spn ? spn : "(null)", expected);
      36           0 :                 exit(1);
      37             :         }
      38          50 :         free(spn);
      39          50 : }
      40             : 
      41          10 : TEST_MAIN()
      42             : {
      43          10 :         TDSCONTEXT *ctx = tds_alloc_context(NULL);
      44          10 :         TDSLOGIN *login = tds_alloc_login(false);
      45             : 
      46          10 :         tds = tds_alloc_socket(ctx, 512);
      47          10 :         assert(ctx && login && tds);
      48          10 :         tds->login = login;
      49          10 :         tds->conn->tds_version = 0x704;
      50             : 
      51          10 :         assert(tds_dstr_copy(&login->server_name, "MYSERVER"));
      52          10 :         assert(tds_dstr_copy(&login->server_host_name, "db.example.com"));
      53          10 :         login->port = 1433;
      54             : 
      55             :         /* port */
      56          10 :         check_spn("MSSQLSvc/db.example.com:1433");
      57             : 
      58             :         /* instance name is used instead of port */
      59          10 :         assert(tds_dstr_copy(&login->instance_name, "SQL2022"));
      60          10 :         check_spn("MSSQLSvc/db.example.com:SQL2022");
      61             : 
      62             :         /* IP address is used as is */
      63          10 :         assert(tds_dstr_copy(&login->server_host_name, "192.168.1.2"));
      64          10 :         tds_dstr_empty(&login->instance_name);
      65          10 :         check_spn("MSSQLSvc/192.168.1.2:1433");
      66             : 
      67             :         /* configured SPN */
      68          10 :         assert(tds_dstr_copy(&login->server_spn, "MSSQLSvc/other.example.com:1500"));
      69          10 :         check_spn("MSSQLSvc/other.example.com:1500");
      70          10 :         tds_dstr_empty(&login->server_spn);
      71             : 
      72             :         /* Sybase uses server name */
      73          10 :         tds->conn->tds_version = 0x500;
      74          10 :         check_spn("MYSERVER");
      75             : 
      76          10 :         tds->login = NULL;
      77          10 :         tds_free_login(login);
      78          10 :         tds_free_socket(tds);
      79          10 :         tds_free_context(ctx);
      80          10 :         return 0;
      81             : }

Generated by: LCOV version 1.13