blib/lib/Oracle/TNS.pm

filter: Statementstmt 100% 254 / 254 Branchbran 99.0% 208 / 210 Conditioncond 85.7% 60 / 70 MC/DCmcdc 73.6% 28 / 38 Subroutinesub 100% 19 / 19 Podpod 100% 7 / 7 totaltotal 96.3% 576 / 598 CC 141 SCAR 26.2
CC
141
Cov
98%
tns_parse_connect
23.0
__build_ezconnect
17.1
__ordered_items
17.0
CRAP 13.8
↑ Summary
1 ##############################################################################
2 ##
3 ## Oracle::TNS -- parse Oracle TNS descriptors / tnsnames.ora into a hashtree
4 ## and build descriptor strings back from a hashtree
5 ## Copyright (c) 2026 Vladi Belperchinov-Shabanski "Cade"
6 ## <cade@noxrun.com>
7 ## http://cade.noxrun.com
8 ##
9 ## LICENSE: GPLv2
10 ##
11 ##############################################################################
12 ##
13 ## Hashtree layout:
14 ##
15 ## (DESCRIPTION=(ADDRESS=(HOST=a)(PORT=1521))(ADDRESS=(HOST=b)(PORT=1521))
16 ## (CONNECT_DATA=(SERVICE_NAME=X)))
17 ##
18 ## becomes
19 ##
20 ## {
21 ## DESCRIPTION => {
22 ## ADDRESS => [ { HOST => 'a', PORT => 1521 },
23 ## { HOST => 'b', PORT => 1521 } ],
24 ## CONNECT_DATA => { SERVICE_NAME => 'X' },
25 ## }
26 ## }
27 ##
28 ## Repeated keys at the same level are collected into an array reference.
29 ## Keys are uppercased on parse. A single-element value may be given either
30 ## as a scalar/hashref or as a one-element arrayref when building.
31 ##
32 ##############################################################################
33 package Oracle::TNS;
34 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
35 3 ▶use Exporter;
Subroutine BEGIN: called CC=1 SCAR=0.0
36 3 ▶use Hash::Util::FieldHash qw( fieldhash );
Subroutine BEGIN: called CC=1 SCAR=0.0
37 3 ▶use Scalar::Util qw( weaken );
Subroutine BEGIN: called CC=1 SCAR=0.0
38  
39 3 our $VERSION = '1.04';
40  
41 3 our @ISA = qw( Exporter );
42 3 our @EXPORT = qw(
43 tns_parse
44 tns_parse_names
45  
46 tns_build
47 tns_build_names
48  
49 tns_parse_connect
50 tns_build_connect
51 tns_mask_connect
52  
53 );
54  
55 # preferred output order for well-known keys, everything else sorted after
56 3 our @KEY_ORDER = qw(
57 DESCRIPTION_LIST DESCRIPTION
58 LOAD_BALANCE FAILOVER SOURCE_ROUTE ENABLE
59 ADDRESS_LIST ADDRESS PROTOCOL HOST PORT
60 CONNECT_DATA SERVICE_NAME SID SERVER INSTANCE_NAME
61 SECURITY
62 );
63 3 my %KEY_RANK;
64 3 @KEY_RANK{ @KEY_ORDER } = ( 1 .. @KEY_ORDER );
65  
66 # parsed hashref => [ [ KEY, value ], ... ] in parsed order, nested hashrefs
67 # weakened, used by tns_build() to keep the original order of values,
68 # entries vanish with their hashes
69 3 fieldhash my %KEY_SEEN_ORDER;
70  
71 ##############################################################################
72 ##
73 ## PARSING
74 ##
75 ##############################################################################
76  
77 # parses a single descriptor string, e.g. "(DESCRIPTION=...)"
78 # returns ( $hashtree, undef ) on success, ( undef, $error_message ) on error
79 sub tns_parse
80 39 ▶{
Subroutine tns_parse: called CC=4 SCAR=13.9
Pod documented
81 39 my $str = __strip_comments( shift );
82  
83 39 my $tree = {};
84 39 my $err = __parse_items( \$str, $tree );
85 39 ▶ return ( undef, $err ) if $err;
Branch: 1 branch
BranchTrueFalseTotal
if $err 7 32 39
86  
87 32 $str =~ /\G\s*/gc;
88 32 ▶ return ( undef, "trailing data at offset " . pos( $str ) ) if pos( $str ) < length( $str );
Branch: 1 branch
BranchTrueFalseTotal
if pos $str < length $str 3 29 32
89 29 ▶ return ( undef, "empty descriptor" ) unless %$tree;
Branch: 1 branch
BranchTrueFalseTotal
unless %$tree 3 26 29
90  
91 26 return ( $tree, undef );
92 }
93  
94 # parses tnsnames.ora content: "ALIAS[, ALIAS2] = (DESCRIPTION=...)" entries
95 # and "NAME = value" entries (e.g. IFILE), one top-level group per alias
96 # returns ( { ALIAS => $hashtree, NAME => $value, ... }, undef ) or ( undef, $error_message )
97 sub tns_parse_names
98 12 ▶{
Subroutine tns_parse_names: called CC=10 SCAR=23.0
Pod documented
99 12 my $str = __strip_comments( shift );
100  
101 12 my $names = {};
102 12 ▶ while( $str =~ /\G\s*([A-Za-z0-9_.\-]+(?:\s*,\s*[A-Za-z0-9_.\-]+)*)\s*=/gc )
Branch: 1 branch
BranchTrueFalseTotal
if ($str =~ /\G\s*([A-Za-z0-9_.\-]+(?:\s*,\s*[A-Za-z0-9_.\-]+)*)\s*=/cg) 25 10 35
103 {
104 25 my @aliases = map { uc } split /\s*,\s*/, $1;
105  
106 25 ▶ if( $str =~ /\G[^\S\n]*(?:"([^"\n]*)"|([^\s()"=](?:[^\n()"=]*[^\s()"=])?))[^\S\n]*(?=\n|\z)/gc )
Branch: 1 branch
BranchTrueFalseTotal
if ($str =~ /\G[^\S\n]*(?:"([^"\n]*)"|([^\s()"=](?:[^\n()"=]*[^\s()"=])?))[^\S\n]*(?=\n|\z)/cg) 10 15 25
107 {
108 # "NAME = value" entry, repeated names collected into arrayref
109 10 ▶ my $val = defined $1 ? $1 : $2;
Branch: 1 branch
BranchTrueFalseTotal
defined $1 ? : 4 6 10
110 10 for my $alias ( @aliases )
111 {
112 # same alias given as descriptor before, last one wins as for descriptors
113 10 ▶ delete $names->{ $alias } if ref( $names->{ $alias } ) eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
if ref $names->{$alias} eq "HASH" 1 9 10
114 10 __add_item( $names, $alias, $val );
115 }
116 10 next;
117 }
118  
119 15 my $tree = {};
120 15 my $err = __parse_items( \$str, $tree, 1 );
121 15 ▶ return ( undef, "alias $aliases[0]: $err" ) if $err;
Branch: 1 branch
BranchTrueFalseTotal
if $err 1 14 15
122 14 ▶ return ( undef, "alias $aliases[0]: empty descriptor" ) unless %$tree;
Branch: 1 branch
BranchTrueFalseTotal
unless %$tree 1 13 14
123 13 $names->{ $_ } = $tree for @aliases;
124 }
125  
126 10 $str =~ /\G\s*/gc;
127 10 ▶ return ( undef, "unparsed data at offset " . pos( $str ) ) if pos( $str ) < length( $str );
Branch: 1 branch
BranchTrueFalseTotal
if pos $str < length $str 2 8 10
128  
129 8 return ( $names, undef );
130 }
131  
132 # removes "# ..." comments up to the end of line, outside of quoted values
133 sub __strip_comments
134 51 ▶{
Subroutine __strip_comments: called CC=2 SCAR=6.9
135 51 my $str = shift;
136  
137 51 ▶ $str =~ s/("[^"]*")|#[^\n]*/defined $1 ? $1 : ''/ge;
Branch: 1 branch
BranchTrueFalseTotal
defined $1 ? : 14 8 22
138 51 pos( $str ) = 0;
139  
140 51 return $str;
141 }
142  
143 # parses "(KEY=VALUE)" / "(KEY=(...)(...))" groups into $hr, at most $max if given
144 # returns undef on success, error message on failure
145 sub __parse_items
146 176 ▶{
Subroutine __parse_items: called CC=12 SCAR=24.9
147 176 my ( $sr, $hr, $max ) = @_;
148  
149 176 ▶ my $order = $KEY_SEEN_ORDER{ $hr } ||= [];
Condition50%1/2
$KEY_SEEN_ORDER{$hr} ||= []
l!l
0176
MC/DC0%0/1
$KEY_SEEN_ORDER{$hr} ||= []
$KEY_SEEN_ORDER{$hr}
Truth table50%1/2
A
A= $KEY_SEEN_ORDER{$hr}
Aresult
11
00
150 176 my $count = 0;
151 176 ▶ while( ( ! $max or $count++ < $max ) and $$sr =~ /\G\s*\(/gc )
Branch: 1 branch
BranchTrueFalseTotal
if (!$max || $count++ < $max and $$sr =~ /\G\s*\(/cg) 299 168 467
Condition100%6/6
!$max || $count++ < $max and $$sr =~ /\G\s*\(/cg
!ll&&!rl&&r
13155299
not $max or $count++ < $max
l!l&&r!l&&!r
4391513
MC/DC100%3/3
!$max || $count++ < $max and $$sr =~ /\G\s*\(/cg
not $max $count++ < $max $$sr =~ /\G\s*\(/cg
Truth table100%5/5
not(A) or B and C
A= not $maxB= $count++ < $maxC= $$sr =~ /\G\s*\(/cg
ABCresult
00X0
1X00
0100
1X11
0111
152 {
153 299 ▶ return "expected KEY= at offset " . pos( $$sr )
Branch: 1 branch
BranchTrueFalseTotal
unless $$sr =~ /\G\s*([A-Za-z0-9_.\-]+)\s*=/cg 2 297 299
154 unless $$sr =~ /\G\s*([A-Za-z0-9_.\-]+)\s*=/gc;
155 297 my $key = uc $1;
156 297 my $val;
157  
158 297 ▶ if( $$sr =~ /\G(?=\s*\()/gc )
Branch: 3 branches
BranchTrueFalseTotal
if ($$sr =~ /\G(?=\s*\()/cg) { } 122 175 297
elsif ($$sr =~ /\G\s*"([^"]*)"\s*/cg) { } 10 165 175
elsif ($$sr =~ /\G\s*([^()"]*?)\s*(?=\))/cg) { } 163 2 165
159 {
160 # nested groups
161 122 $val = {};
162 122 my $err = __parse_items( $sr, $val );
163 122 ▶ return $err if $err;
Branch: 1 branch
BranchTrueFalseTotal
if $err 0 122 122
164 }
165 elsif( $$sr =~ /\G\s*"([^"]*)"\s*/gc )
166 {
167 # quoted scalar value
168 10 $val = $1;
169 }
170 elsif( $$sr =~ /\G\s*([^()"]*?)\s*(?=\))/gc )
171 {
172 # bare scalar value
173 163 $val = $1;
174 }
175 else
176 {
177 2 return "bad value for $key at offset " . pos( $$sr );
178 }
179  
180 295 ▶ return "expected ) after $key at offset " . pos( $$sr )
Branch: 1 branch
BranchTrueFalseTotal
unless $$sr =~ /\G\s*\)/cg 4 291 295
181 unless $$sr =~ /\G\s*\)/gc;
182  
183 291 __add_item( $hr, $key, $val );
184 291 push @$order, [ $key, $val ];
185 291 ▶ weaken( $order->[ -1 ][ 1 ] ) if ref( $val );
Branch: 1 branch
BranchTrueFalseTotal
if ref $val 119 172 291
186 }
187  
188 168 return undef;
189 }
190  
191 # adds a value under key, promoting to arrayref on repeated keys
192 sub __add_item
193 301 ▶{
Subroutine __add_item: called CC=3 SCAR=11.0
194 301 my ( $hr, $key, $val ) = @_;
195  
196 301 ▶ if( ! exists $hr->{ $key } )
Branch: 2 branches
BranchTrueFalseTotal
if (not exists $hr->{$key}) { } 272 29 301
elsif (ref $hr->{$key} eq 'ARRAY') { } 2 27 29
197 {
198 272 $hr->{ $key } = $val;
199 }
200 elsif( ref( $hr->{ $key } ) eq 'ARRAY' )
201 {
202 2 push @{ $hr->{ $key } }, $val;
203 }
204 else
205 {
206 27 $hr->{ $key } = [ $hr->{ $key }, $val ];
207 }
208 }
209  
210 ##############################################################################
211 ##
212 ## BUILDING
213 ##
214 ##############################################################################
215  
216 # builds descriptor string from hashtree
217 # $indent: undef -> single line, otherwise number of spaces per nesting level
218 # returns ( $string, undef ) or ( undef, $error_message )
219 sub tns_build
220 51 ▶{
Subroutine tns_build: called CC=4 SCAR=13.9
Pod documented
221 51 my ( $tree, $indent ) = @_;
222  
223 51 ▶ return ( undef, "hashref expected" ) unless ref( $tree ) eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $tree eq "HASH" 4 47 51
224 47 ▶ return ( undef, "empty hashtree" ) unless %$tree;
Branch: 1 branch
BranchTrueFalseTotal
unless %$tree 2 45 47
225  
226 45 my $out = '';
227 45 my $err = __build_items( $tree, \$out, $indent, 0 );
228 45 ▶ return ( undef, $err ) if $err;
Branch: 1 branch
BranchTrueFalseTotal
if $err 10 35 45
229  
230 35 $out =~ s/^\n//;
231 35 return ( $out, undef );
232 }
233  
234 # builds tnsnames.ora content from { ALIAS => $hashtree, NAME => $value, ... }
235 # returns ( $string, undef ) or ( undef, $error_message )
236 sub tns_build_names
237 14 ▶{
Subroutine tns_build_names: called CC=15 SCAR=27.1
Pod documented
238 14 my ( $names, $indent ) = @_;
239  
240 14 ▶ return ( undef, "hashref expected" ) unless ref( $names ) eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $names eq "HASH" 1 13 14
241 13 ▶ $indent = 2 unless defined $indent;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $indent 12 1 13
242  
243 13 my $out = '';
244 13 for my $alias ( sort keys %$names )
245 {
246 19 my $v = $names->{ $alias };
247 19 ▶ if( ! ref( $v ) or ref( $v ) eq 'ARRAY' )
Branch: 1 branch
BranchTrueFalseTotal
if (not ref $v or ref $v eq "ARRAY") 8 11 19
Condition100%3/3
not ref $v or ref $v eq "ARRAY"
l!l&&r!l&&!r
5311
MC/DC100%2/2
not ref $v or ref $v eq "ARRAY"
not ref $v ref $v eq "ARRAY"
Truth table100%3/3
not(A) or B
A= not ref $vB= ref $v eq "ARRAY"
ABresult
1X1
011
000
248 {
249 # "NAME = value" entries
250 8 ▶ for my $item ( ref( $v ) ? @$v : ( $v ) )
Branch: 1 branch
BranchTrueFalseTotal
ref $v ? : 3 5 8
251 {
252 11 my $val = $item;
253 11 ▶ return ( undef, "alias $alias: undefined value" ) unless defined $val;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $val 2 9 11
254 9 ▶ return ( undef, "alias $alias: hashref or scalar expected" ) if ref( $val );
Branch: 1 branch
BranchTrueFalseTotal
if ref $val 1 8 9
255 8 ▶ return ( undef, "alias $alias: double quote in value is not supported" ) if $val =~ /"/;
Branch: 1 branch
BranchTrueFalseTotal
if $val =~ /"/ 1 7 8
256 7 ▶ return ( undef, "alias $alias: multiline value is not supported" ) if $val =~ /\n/;
Branch: 1 branch
BranchTrueFalseTotal
if $val =~ /\n/ 1 6 7
257 6 ▶ $val = qq["$val"] if $val eq '' or $val =~ /[()=#]|^\s|\s$/;
Branch: 1 branch
BranchTrueFalseTotal
if $val eq "" or $val =~ /[()=#]|^\s|\s$/ 3 3 6
Condition100%3/3
$val eq "" or $val =~ /[()=#]|^\s|\s$/
l!l&&r!l&&!r
123
MC/DC100%2/2
$val eq "" or $val =~ /[()=#]|^\s|\s$/
$val eq "" $val =~ /[()=#]|^\s|\s$/
Truth table100%3/3
A or B
A= $val eq ""B= $val =~ /[()=#]|^\s|\s$/
ABresult
1X1
011
000
258 6 $out .= "$alias = $val\n\n";
259 }
260 3 next;
261 }
262 11 my ( $str, $err ) = tns_build( $v, $indent );
263 11 ▶ return ( undef, "alias $alias: $err" ) if $err;
Branch: 1 branch
BranchTrueFalseTotal
if $err 2 9 11
264 9 $str =~ s/^/ /mg;
265 9 $out .= "$alias =\n$str\n\n";
266 }
267  
268 6 return ( $out, undef );
269 }
270  
271 # emits all "(KEY=...)" groups of $hr into $$out
272 sub __build_items
273 165 ▶{
Subroutine __build_items: called CC=11 SCAR=24.0
274 165 my ( $hr, $out, $indent, $level ) = @_;
275  
276 165 ▶ my $pad = defined $indent ? "\n" . ( ' ' x ( $indent * $level ) ) : '';
Branch: 1 branch
BranchTrueFalseTotal
defined $indent ? : 44 121 165
277  
278 165 for my $item ( __ordered_items( $hr ) )
279 {
280 299 my ( $key, $val ) = @$item;
281 299 $$out .= "$pad($key=";
282 299 ▶ if( ref( $val ) eq 'HASH' )
Branch: 2 branches
BranchTrueFalseTotal
if (ref $val eq 'HASH') { } 122 177 299
elsif (not ref $val) { } 174 3 177
283 {
284 122 ▶ return "empty hashref for $key" unless %$val;
Branch: 1 branch
BranchTrueFalseTotal
unless %$val 2 120 122
285 120 my $err = __build_items( $val, $out, $indent, $level + 1 );
286 120 ▶ return $err if $err;
Branch: 1 branch
BranchTrueFalseTotal
if $err 2 118 120
287 118 ▶ $$out .= $pad if defined $indent;
Branch: 1 branch
BranchTrueFalseTotal
if defined $indent 28 90 118
288 }
289 elsif( ! ref( $val ) )
290 {
291 174 ▶ return "undefined value for $key" unless defined $val;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $val 3 171 174
292 171 ▶ return "double quote in value for $key is not supported" if $val =~ /"/;
Branch: 1 branch
BranchTrueFalseTotal
if $val =~ /"/ 2 169 171
293 169 ▶ $val = qq["$val"] if $val =~ /[()\s=#]/;
Branch: 1 branch
BranchTrueFalseTotal
if $val =~ /[()\s=#]/ 9 160 169
294 169 $$out .= $val;
295 }
296 else
297 {
298 3 return "unsupported value type " . ref( $val ) . " for $key";
299 }
300 287 $$out .= ")";
301 }
302  
303 153 return undef;
304 }
305  
306 # returns ( [ KEY, value ], ... ) in output order: values found at parse
307 # time go to their parsed position, other values of a parsed key follow the
308 # previous value of that key (or take its first position), keys not seen at
309 # parse time (or all, if not parsed) follow in predefined order
310 sub __ordered_items
311 165 ▶{
Subroutine __ordered_items: called CC=17 SCAR=28.4
312 165 my $hr = shift;
313  
314 165 ▶ my $slots = $KEY_SEEN_ORDER{ $hr } || [];
Condition100%2/2
$KEY_SEEN_ORDER{$hr} || []
l!l
9471
MC/DC100%1/1
$KEY_SEEN_ORDER{$hr} || []
$KEY_SEEN_ORDER{$hr}
Truth table100%2/2
A
A= $KEY_SEEN_ORDER{$hr}
Aresult
11
00
315  
316 165 my %first; # key => first slot of that key
317 165 for my $i ( reverse 0 .. $#$slots )
318 {
319 175 $first{ $slots->[ $i ][ 0 ] } = $i;
320 }
321  
322 165 my @at; # slot => [ items placed there ]
323 my @rest; # items of keys not seen at parse time
324 165 for my $key ( __sorted_keys( $hr ) )
325 {
326 271 my $v = $hr->{ $key };
327 271 ▶ my @vals = ref( $v ) eq 'ARRAY' ? @$v : ( $v );
Branch: 1 branch
BranchTrueFalseTotal
ref $v eq 'ARRAY' ? : 30 241 271
328  
329 271 ▶ if( ! exists $first{ $key } )
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $first{$key}) 118 153 271
330 {
331 118 push @rest, map { [ $key, $_ ] } @vals;
332 118 next;
333 }
334  
335 153 my $prev = $first{ $key };
336 153 my %taken;
337 153 for my $val ( @vals )
338 {
339 174 for my $i ( $prev .. $#$slots )
340 {
341 215 my ( $skey, $sval ) = @{ $slots->[ $i ] };
342 215 ▶ next if $skey ne $key or $taken{ $i } or ! defined $sval or ! defined $val;
Branch: 1 branch
BranchTrueFalseTotal
if $skey ne $key or $taken{$i} or not defined $sval or not defined $val 43 172 215
Condition88%8/9
$skey ne $key or $taken{$i} or not defined $sval or not defined $val
l!l&&r!l&&!r
430172
$skey ne $key or $taken{$i} or not defined $sval
l!l&&r!l&&!r
394172
$skey ne $key or $taken{$i}
l!l&&r!l&&!r
1920176
MC/DC75%3/4
$skey ne $key or $taken{$i} or not defined $sval or not defined $val
$skey ne $key $taken{$i} not defined $sval not defined $val
Truth table80%4/5
A or B or not(C) or not(D)
A= $skey ne $keyB= $taken{$i}C= not defined $svalD= not defined $val
ABCDresult
1XXX1
01XX1
001X1
00011
00000
343 172 ▶ next unless ref( $val ) ? ref( $sval ) && $sval == $val : ! ref( $sval ) && $sval eq $val;
Branch: 2 branches
BranchTrueFalseTotal
unless ref $val ? ref $sval && $sval == $val : !ref($sval) && $sval eq $val 4 168 172
ref $val ? : 77 95 172
Condition66%4/6
ref $sval and $sval == $val
!ll&&!rl&&r
0374
not ref $sval and $sval eq $val
!ll&&!rl&&r
0194
MC/DC50%1/2
ref $sval and $sval == $val
ref $sval $sval == $val
MC/DC50%1/2
not ref $sval and $sval eq $val
not ref $sval $sval eq $val
Truth table66%2/3
A and B
A= ref $svalB= $sval == $val
ABresult
0X0
100
111
Truth table66%2/3
not(A) and B
A= not ref $svalB= $sval eq $val
ABresult
0X0
100
111
344 168 $taken{ $i } = 1;
345 168 $prev = $i;
346 168 last;
347 }
348 174 push @{ $at[ $prev ] }, [ $key, $val ];
349 }
350 }
351  
352 165 ▶ return ( map( { @{ $_ || [] } } @at ), @rest );
Condition100%2/2
$_ || []
l!l
1714
MC/DC100%1/1
$_ || []
$_
Truth table100%2/2
A
A= $_
Aresult
11
00
353 }
354  
355 # well-known keys first in canonical order, rest alphabetically
356 sub __sorted_keys
357 165 ▶{
Subroutine __sorted_keys: called CC=4 SCAR=13.9
358 165 my $hr = shift;
359  
360 165 ▶ return sort { ( $KEY_RANK{ $a } || 999 ) <=> ( $KEY_RANK{ $b } || 999 ) or $a cmp $b } keys %$hr;
Condition85%6/7
($KEY_RANK{$a} || 999) <=> ($KEY_RANK{$b} || 999) || $a cmp $b
l!l&&r!l&&!r
126180
$KEY_RANK{$a} || 999
l!l
11925
$KEY_RANK{$b} || 999
l!l
12321
MC/DC0%0/2
($KEY_RANK{$a} || 999) <=> ($KEY_RANK{$b} || 999) || $a cmp $b
$KEY_RANK{$a} $a cmp $b
MC/DC100%1/1
$KEY_RANK{$b} || 999
$KEY_RANK{$b}
Truth table66%2/3
A || B
A= $KEY_RANK{$a}B= $a cmp $b
ABresult
1X1
011
000
Truth table100%2/2
A
A= $KEY_RANK{$b}
Aresult
11
00
361 }
362  
363 ##############################################################################
364 ##
365 ## CONNECT STRINGS
366 ##
367 ##############################################################################
368  
369 3 my $ALIAS_RE = qr/[A-Za-z0-9_.\-]+/;
370 3 my $EZCONNECT_RE = qr{
371 ^(?://)?
372 (?: \[ ( [^\]\s/\@]+ ) \] | ( [^:/\[\]\s\@]+ ) ) # host or [ipv6]
373 (?: : ( \d+ ) )? # port
374 (?: / ( [^:/\s\@]+ ) # service name
375 (?: : ( [A-Za-z]+ ) )? # server
376 (?: / ( [^/\s\@]+ ) )? # instance name
377 )?$
378 }x;
379  
380 # parses DBI/sqlplus style connect string: user/pass@target, user@target,
381 # user/pass, /@target, target is a descriptor, an alias or EZConnect
382 # returns ( { USER, PASS, TNS | ALIAS | EZCONNECT }, undef ) or ( undef, $error_message )
383 sub tns_parse_connect
384 67 ▶{
Subroutine tns_parse_connect: called CC=23 SCAR=31.4
Pod documented
385 67 my $str = shift;
386  
387 67 ▶ return ( undef, "empty connect string" ) unless defined $str and $str =~ /\S/;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $str and $str =~ /\S/ 2 65 67
Condition100%3/3
defined $str and $str =~ /\S/
!ll&&!rl&&r
1165
MC/DC100%2/2
defined $str and $str =~ /\S/
defined $str $str =~ /\S/
Truth table100%3/3
A and B
A= defined $strB= $str =~ /\S/
ABresult
0X0
100
111
388 65 $str =~ s/\s+$//;
389 65 pos( $str ) = 0;
390 65 $str =~ /\G\s*/gc;
391  
392 65 my $conn = {};
393  
394 65 ▶ $conn->{ USER } = defined $1 ? $1 : $2 if $str =~ /\G(?:"([^"]*)"|([^\/\@"\s]+))/gc;
Branch: 2 branches
BranchTrueFalseTotal
if $str =~ m[\G(?:"([^"]*)"|([^/\@"\s]+))]cg 57 8 65
defined $1 ? : 5 52 57
395  
396 65 ▶ if( $str =~ /\G\//gc )
Branch: 2 branches
BranchTrueFalseTotal
if ($str =~ m[\G/]cg) { } 53 12 65
elsif (not exists $conn->{'USER'}) { } 2 10 12
397 {
398 53 ▶ if( $str =~ /\G(?:"([^"]*)"|([^\@"\s]+))/gc )
Branch: 2 branches
BranchTrueFalseTotal
if ($str =~ /\G(?:"([^"]*)"|([^\@"\s]+))/cg) { } 45 8 53
elsif (exists $conn->{'USER'}) { } 3 5 8
399 {
400 45 ▶ return ( undef, "password without user" ) unless exists $conn->{ USER };
Branch: 1 branch
BranchTrueFalseTotal
unless exists $conn->{"USER"} 1 44 45
401 44 ▶ $conn->{ PASS } = defined $1 ? $1 : $2;
Branch: 1 branch
BranchTrueFalseTotal
defined $1 ? : 5 39 44
402 }
403 elsif( exists $conn->{ USER } )
404 {
405 3 return ( undef, "missing password after / at offset " . pos( $str ) );
406 }
407 }
408 elsif( ! exists $conn->{ USER } )
409 {
410 2 return ( undef, "expected user or / at offset " . pos( $str ) );
411 }
412  
413 59 ▶ if( $str =~ /\G\@\s*/gc )
Branch: 2 branches
BranchTrueFalseTotal
if ($str =~ /\G\@\s*/cg) { } 51 8 59
elsif (pos $str < length $str) { } 1 7 8
414 {
415 51 my $target = substr( $str, pos( $str ) );
416 51 ▶ return ( undef, "missing connect target after \@" ) if $target eq '';
Branch: 1 branch
BranchTrueFalseTotal
if $target eq "" 2 49 51
417  
418 49 ▶ if( $target =~ /^\(/ )
Branch: 3 branches
BranchTrueFalseTotal
if ($target =~ /^\(/) { } 7 42 49
elsif ($target =~ /^$ALIAS_RE$/) { } 19 23 42
elsif (() = $target =~ /$EZCONNECT_RE/) { } 15 8 23
419 {
420 7 my ( $tree, $err ) = tns_parse( $target );
421 7 ▶ return ( undef, "descriptor: $err" ) if $err;
Branch: 1 branch
BranchTrueFalseTotal
if $err 2 5 7
422 5 $conn->{ TNS } = $tree;
423 }
424 elsif( $target =~ /^$ALIAS_RE$/ )
425 {
426 19 $conn->{ ALIAS } = $target;
427 }
428 elsif( my ( $host6, $host, $port, $service, $server, $instance ) = $target =~ $EZCONNECT_RE )
429 {
430 15 ▶ my %ez = ( HOST => defined $host6 ? $host6 : $host );
Branch: 1 branch
BranchTrueFalseTotal
defined $host6 ? : 4 11 15
431 15 ▶ $ez{ PORT } = $port if defined $port;
Branch: 1 branch
BranchTrueFalseTotal
if defined $port 8 7 15
432 15 ▶ $ez{ SERVICE_NAME } = $service if defined $service;
Branch: 1 branch
BranchTrueFalseTotal
if defined $service 8 7 15
433 15 ▶ $ez{ SERVER } = $server if defined $server;
Branch: 1 branch
BranchTrueFalseTotal
if defined $server 2 13 15
434 15 ▶ $ez{ INSTANCE_NAME } = $instance if defined $instance;
Branch: 1 branch
BranchTrueFalseTotal
if defined $instance 3 12 15
435 15 $conn->{ EZCONNECT } = \%ez;
436 }
437 else
438 {
439 8 return ( undef, "bad connect target: $target" );
440 }
441 }
442 elsif( pos( $str ) < length( $str ) )
443 {
444 1 return ( undef, "trailing data at offset " . pos( $str ) );
445 }
446  
447 46 return ( $conn, undef );
448 }
449  
450 # builds connect string from { USER, PASS, TNS | ALIAS | EZCONNECT }
451 # $indent is passed to tns_build() for TNS, default single line
452 # returns ( $string, undef ) or ( undef, $error_message )
453 sub tns_build_connect
454 51 ▶{
Subroutine tns_build_connect: called CC=17 SCAR=28.3
Pod documented
455 51 my ( $conn, $indent ) = @_;
456  
457 51 ▶ return ( undef, "hashref expected" ) unless ref( $conn ) eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $conn eq "HASH" 2 49 51
458  
459 49 my %known = map { $_ => 1 } qw( USER PASS TNS ALIAS EZCONNECT );
460 49 my @bad = sort grep { ! $known{ $_ } } keys %$conn;
461 49 ▶ return ( undef, "unknown key @bad" ) if @bad;
Branch: 1 branch
BranchTrueFalseTotal
if @bad 1 48 49
462  
463 48 my @targets = grep { exists $conn->{ $_ } } qw( TNS ALIAS EZCONNECT );
464 48 ▶ return ( undef, "only one of @targets expected" ) if @targets > 1;
Branch: 1 branch
BranchTrueFalseTotal
if @targets > 1 1 47 48
465  
466 47 my $out;
467 47 ▶ if( exists $conn->{ USER } )
Branch: 1 branch
BranchTrueFalseTotal
if (exists $conn->{'USER'}) { } 43 4 47
468 {
469 43 my ( $user, $err ) = __connect_quote( USER => $conn->{ USER }, qr/[\/\@\s]/ );
470 43 ▶ return ( undef, $err ) if $err;
Branch: 1 branch
BranchTrueFalseTotal
if $err 3 40 43
471 40 $out = $user;
472 40 ▶ if( exists $conn->{ PASS } )
Branch: 1 branch
BranchTrueFalseTotal
if (exists $conn->{"PASS"}) 17 23 40
473 {
474 17 my ( $pass, $err ) = __connect_quote( PASS => $conn->{ PASS }, qr/[\@\s]/ );
475 17 ▶ return ( undef, $err ) if $err;
Branch: 1 branch
BranchTrueFalseTotal
if $err 2 15 17
476 15 $out .= "/$pass";
477 }
478 }
479 else
480 {
481 4 ▶ return ( undef, "PASS without USER" ) if exists $conn->{ PASS };
Branch: 1 branch
BranchTrueFalseTotal
if exists $conn->{"PASS"} 1 3 4
482 3 $out = '/';
483 }
484  
485 41 ▶ return ( $out, undef ) unless @targets;
Branch: 1 branch
BranchTrueFalseTotal
unless @targets 4 37 41
486  
487 37 my $target;
488 37 ▶ if( exists $conn->{ TNS } )
Branch: 2 branches
BranchTrueFalseTotal
if (exists $conn->{'TNS'}) { } 6 31 37
elsif (exists $conn->{'ALIAS'}) { } 13 18 31
489 {
490 6 my $err;
491 6 ( $target, $err ) = tns_build( $conn->{ TNS }, $indent );
492 6 ▶ return ( undef, "TNS: $err" ) if $err;
Branch: 1 branch
BranchTrueFalseTotal
if $err 2 4 6
493 }
494 elsif( exists $conn->{ ALIAS } )
495 {
496 13 $target = $conn->{ ALIAS };
497 13 ▶ return ( undef, "bad ALIAS" ) unless defined $target and ! ref( $target ) and $target =~ /^$ALIAS_RE$/;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $target and not ref $target and $target =~ /^$ALIAS_RE$/ 2 11 13
Condition83%5/6
defined $target and not ref $target and $target =~ /^$ALIAS_RE$/
!ll&&!rl&&r
1111
defined $target and not ref $target
!ll&&!rl&&r
1012
MC/DC66%2/3
defined $target and not ref $target and $target =~ /^$ALIAS_RE$/
defined $target not ref $target $target =~ /^$ALIAS_RE$/
Truth table75%3/4
A and not(B) and C
A= defined $targetB= not ref $targetC= $target =~ /^$ALIAS_RE$/
ABCresult
0XX0
10X0
1100
1111
498 }
499 else
500 {
501 18 my $err;
502 18 ( $target, $err ) = __build_ezconnect( $conn->{ EZCONNECT } );
503 18 ▶ return ( undef, "EZCONNECT: $err" ) if $err;
Branch: 1 branch
BranchTrueFalseTotal
if $err 11 7 18
504 }
505  
506 22 return ( "$out\@$target", undef );
507 }
508  
509 # replaces USER and PASS in connect string with $mask (default "***"), e.g. for logging
510 # returns masked string, never fails: unparsable strings without quotes keep only the part after the last @
511 sub tns_mask_connect
512 14 ▶{
Subroutine tns_mask_connect: called CC=10 SCAR=23.1
Pod documented
513 14 my $str = shift;
514 14 ▶ my $mask = @_ ? shift : '***';
Branch: 1 branch
BranchTrueFalseTotal
@_ ? : 1 13 14
515  
516 14 ▶ return $str unless defined $str and $str =~ /\S/;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $str and $str =~ /\S/ 2 12 14
Condition100%3/3
defined $str and $str =~ /\S/
!ll&&!rl&&r
1112
MC/DC100%2/2
defined $str and $str =~ /\S/
defined $str $str =~ /\S/
Truth table100%3/3
A and B
A= defined $strB= $str =~ /\S/
ABresult
0X0
100
111
517  
518 12 my ( $conn, $err ) = tns_parse_connect( $str );
519 12 ▶ if( ! $err )
Branch: 1 branch
BranchTrueFalseTotal
unless ($err) 9 3 12
520 {
521 9 ▶ $conn->{ USER } = $mask if exists $conn->{ USER };
Branch: 1 branch
BranchTrueFalseTotal
if exists $conn->{"USER"} 8 1 9
522 9 ▶ $conn->{ PASS } = $mask if exists $conn->{ PASS };
Branch: 1 branch
BranchTrueFalseTotal
if exists $conn->{"PASS"} 6 3 9
523 9 my $out;
524 9 ( $out, $err ) = tns_build_connect( $conn );
525 9 ▶ return $out unless $err;
Branch: 1 branch
BranchTrueFalseTotal
unless $err 9 0 9
526 }
527  
528 # quoted user/pass may contain @, so nothing can be kept safely
529 3 ▶ return $str !~ /"/ && $str =~ /\@([^\@]*)$/ ? "$mask\@$1" : $mask;
Branch: 1 branch
BranchTrueFalseTotal
!($str =~ /"/) && $str =~ /\@([^\@]*)$/ ? : 2 1 3
Condition66%2/3
not $str =~ /"/ and $str =~ /\@([^\@]*)$/
!ll&&!rl&&r
102
MC/DC50%1/2
not $str =~ /"/ and $str =~ /\@([^\@]*)$/
not $str =~ /"/ $str =~ /\@([^\@]*)$/
Truth table66%2/3
not(A) and B
A= not $str =~ /"/B= $str =~ /\@([^\@]*)$/
ABresult
0X0
100
111
530 }
531  
532 # quotes user or password if empty or matching $re
533 sub __connect_quote
534 60 ▶{
Subroutine __connect_quote: called CC=6 SCAR=17.9
535 60 my ( $name, $val, $re ) = @_;
536  
537 60 ▶ return ( undef, "undefined $name" ) unless defined $val;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $val 2 58 60
538 58 ▶ return ( undef, "unsupported $name type " . ref( $val ) ) if ref( $val );
Branch: 1 branch
BranchTrueFalseTotal
if ref $val 1 57 58
539 57 ▶ return ( undef, "double quote in $name is not supported" ) if $val =~ /"/;
Branch: 1 branch
BranchTrueFalseTotal
if $val =~ /"/ 2 55 57
540 55 ▶ $val = qq["$val"] if $val eq '' or $val =~ $re;
Branch: 1 branch
BranchTrueFalseTotal
if $val eq "" or $val =~ /$re/ 5 50 55
Condition100%3/3
$val eq "" or $val =~ /$re/
l!l&&r!l&&!r
2350
MC/DC100%2/2
$val eq "" or $val =~ /$re/
$val eq "" $val =~ /$re/
Truth table100%3/3
A or B
A= $val eq ""B= $val =~ /$re/
ABresult
1X1
011
000
541  
542 55 return ( $val, undef );
543 }
544  
545 # builds [//]host[:port][/service[:server][/instance]]
546 sub __build_ezconnect
547 18 ▶{
Subroutine __build_ezconnect: called CC=17 SCAR=28.4
548 18 my $ez = shift;
549  
550 18 ▶ return ( undef, "hashref expected" ) unless ref( $ez ) eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $ez eq "HASH" 1 17 18
551  
552 17 my %check = (
553 HOST => qr/^(?:[^:\/\[\]\s\@]+|[^\[\]\s\/\@]*:[^\[\]\s\/\@]*)$/,
554 PORT => qr/^\d+$/,
555 SERVICE_NAME => qr/^[^:\/\s\@]+$/,
556 SERVER => qr/^[A-Za-z]+$/,
557 INSTANCE_NAME => qr/^[^\/\s\@]+$/,
558 );
559 17 for my $key ( sort keys %$ez )
560 {
561 32 ▶ return ( undef, "unknown key $key" ) unless $check{ $key };
Branch: 1 branch
BranchTrueFalseTotal
unless $check{$key} 1 31 32
562 31 my $val = $ez->{ $key };
563 31 ▶ return ( undef, "bad $key" ) unless defined $val and ! ref( $val ) and $val =~ $check{ $key };
Branch: 1 branch
BranchTrueFalseTotal
unless defined $val and not ref $val and $val =~ /$check{$key}/ 6 25 31
Condition50%3/6
defined $val and not ref $val and $val =~ /$check{$key}/
!ll&&!rl&&r
0625
defined $val and not ref $val
!ll&&!rl&&r
0031
MC/DC33%1/3
defined $val and not ref $val and $val =~ /$check{$key}/
defined $val not ref $val $val =~ /$check{$key}/
Truth table50%2/4
A and not(B) and C
A= defined $valB= not ref $valC= $val =~ /$check{$key}/
ABCresult
0XX0
10X0
1100
1111
564 }
565 10 ▶ return ( undef, "missing HOST" ) unless exists $ez->{ HOST };
Branch: 1 branch
BranchTrueFalseTotal
unless exists $ez->{"HOST"} 1 9 10
566 return ( undef, "SERVER and INSTANCE_NAME need SERVICE_NAME" )
567 9 ▶ if ! exists $ez->{ SERVICE_NAME } and ( exists $ez->{ SERVER } or exists $ez->{ INSTANCE_NAME } );
Branch: 1 branch
BranchTrueFalseTotal
if not exists $ez->{"SERVICE_NAME"} and exists $ez->{"SERVER"} || exists $ez->{"INSTANCE_NAME"} 2 7 9
Condition100%6/6
not exists $ez->{"SERVICE_NAME"} and exists $ez->{"SERVER"} || exists $ez->{"INSTANCE_NAME"}
!ll&&!rl&&r
342
exists $ez->{"SERVER"} or exists $ez->{"INSTANCE_NAME"}
l!l&&r!l&&!r
114
MC/DC100%3/3
not exists $ez->{"SERVICE_NAME"} and exists $ez->{"SERVER"} || exists $ez->{"INSTANCE_NAME"}
not exists $ez->{"SERVICE_NAME"} exists $ez->{"SERVER"} exists $ez->{"INSTANCE_NAME"}
Truth table100%4/4
not(A) and B or C
A= not exists $ez->{"SERVICE_NAME"}B= exists $ez->{"SERVER"}C= exists $ez->{"INSTANCE_NAME"}
ABCresult
0XX0
1000
11X1
1011
568  
569 7 ▶ my $out = $ez->{ HOST } =~ /:/ ? "[$ez->{ HOST }]" : $ez->{ HOST };
Branch: 1 branch
BranchTrueFalseTotal
$ez->{'HOST'} =~ /:/ ? : 2 5 7
570 7 ▶ $out .= ":$ez->{ PORT }" if exists $ez->{ PORT };
Branch: 1 branch
BranchTrueFalseTotal
if exists $ez->{"PORT"} 4 3 7
571 7 ▶ if( exists $ez->{ SERVICE_NAME } )
Branch: 1 branch
BranchTrueFalseTotal
if (exists $ez->{"SERVICE_NAME"}) 3 4 7
572 {
573 3 $out .= "/$ez->{ SERVICE_NAME }";
574 3 ▶ $out .= ":$ez->{ SERVER }" if exists $ez->{ SERVER };
Branch: 1 branch
BranchTrueFalseTotal
if exists $ez->{"SERVER"} 1 2 3
575 3 ▶ $out .= "/$ez->{ INSTANCE_NAME }" if exists $ez->{ INSTANCE_NAME };
Branch: 1 branch
BranchTrueFalseTotal
if exists $ez->{"INSTANCE_NAME"} 1 2 3
576 }
577 # a lone host would be read back as alias
578 7 ▶ $out = "//$out" if $out =~ /^$ALIAS_RE$/;
Branch: 1 branch
BranchTrueFalseTotal
if $out =~ /^$ALIAS_RE$/ 2 5 7
579  
580 7 return ( $out, undef );
581 }
582  
583 3 1;
584  
585 __END__
↑ Summary