1 /* FreeTDS - Library of routines accessing Sybase and Microsoft databases
2 * Copyright (C) 2019 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 #include "common.h"
20 #include <freetds/replacements.h>
21
22 static void
set_interface(void)23 set_interface(void)
24 {
25 const char *in_file = FREETDS_SRCDIR "/portconf.in";
26
27 FILE *f = fopen(in_file, "r");
28 if (!f) {
29 in_file = "portconf.in";
30 f = fopen(in_file, "r");
31 }
32 if (!f) {
33 fprintf(stderr, "error opening test file\n");
34 exit(1);
35 }
36 fclose(f);
37 tds_set_interfaces_file_loc(in_file);
38 }
39
40 static void
dump_login(const char * name,const TDSLOGIN * login)41 dump_login(const char *name, const TDSLOGIN *login)
42 {
43 printf("Dump login %s\n", name);
44 #define STR(name) printf(" " #name ": %s\n", tds_dstr_cstr(&login->name));
45 #define INT(name) printf(" " #name ": %d\n", login->name);
46 STR(server_name);
47 STR(server_host_name);
48 STR(instance_name);
49 INT(port);
50 }
51
52 static void
test0(TDSCONTEXT * ctx,TDSSOCKET * tds,const char * input,const char * expected,int line)53 test0(TDSCONTEXT *ctx, TDSSOCKET *tds, const char *input, const char *expected, int line)
54 {
55 TDSLOGIN *login, *connection;
56 char *ret = NULL;
57
58 login = tds_alloc_login(1);
59 if (!login || !tds_set_server(login, input)) {
60 fprintf(stderr, "Error setting login!\n");
61 exit(1);
62 }
63
64 connection = tds_read_config_info(tds, login, ctx->locale);
65 dump_login("input", login);
66 dump_login("final", connection);
67 if (asprintf(&ret, "%s,%s,%s,%d",
68 tds_dstr_cstr(&connection->server_name),
69 tds_dstr_cstr(&connection->server_host_name),
70 tds_dstr_cstr(&connection->instance_name),
71 connection->port) < 0)
72 exit(1);
73 if (strcmp(ret, expected) != 0) {
74 fprintf(stderr, "Mismatch line %d:\n OUT: %s\n EXP: %s\n",
75 line, ret, expected);
76 exit(1);
77 }
78 tds_free_login(connection);
79 tds_free_login(login);
80 free(ret);
81 }
82 #define test(in, out) test0(ctx, tds, in, out, __LINE__)
83
84 int
main(void)85 main(void)
86 {
87 TDSCONTEXT *ctx = tds_alloc_context(NULL);
88 TDSSOCKET *tds = tds_alloc_socket(ctx, 512);
89 FILE *f;
90
91 /* set an empty base configuration */
92 f = fopen("empty.conf", "w");
93 if (f)
94 fclose(f);
95 putenv("FREETDSCONF=empty.conf");
96 unsetenv("TDSPORT");
97
98 set_interface();
99 if (!ctx || !tds) {
100 fprintf(stderr, "Error creating socket!\n");
101 return 1;
102 }
103
104 test("NotExistingServer1:1234", "my_server,8.7.6.5,,1234");
105 test("localhost:1234", "localhost,localhost,,1234");
106 test("NotExistingServer1\\named", "my_server,8.7.6.5,named,0");
107 test("localhost\\named", "localhost,localhost,named,0");
108
109 tds_free_socket(tds);
110 tds_free_context(ctx);
111 tds_set_interfaces_file_loc(NULL);
112
113 return 0;
114 }
115
116