lib/DR/Tnt/Proto.pm

filter: Statementstmt 81.5% 137 / 168 Branchbran 60.2% 53 / 88 Conditioncond 31.2% 5 / 16 MC/DCmcdc 0.0% 0 / 10 Subroutinesub 82.7% 24 / 29 Podpod 0.0% 0 / 14 totaltotal 67.3% 219 / 325 SCAR 47.6
CC
53
Cov
72%
parse_greeting
20.0
raw_response
18.1
select
14.1
CRAP 116.7
« blib/lib/DR/Tnt/Test.pm ↑ Summary blib/lib/DR/Tnt/LowLevel/Connector/Sync.pm »
1 8 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
2 8 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 8 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 package DR::Tnt::Proto;
6 8 ▶use base qw(Exporter);
Subroutine BEGIN: called CC=1 SCAR=0.0
7 8 our @EXPORT_OK = qw(
8 call_lua
9 response
10 insert
11 replace
12 del
13 update
14 select
15 auth
16 handshake
17 ping
18 );
19 8 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 8 $Carp::Internal{ (__PACKAGE__) }++;
21 8 ▶use DR::Tnt::Msgpack;
Subroutine BEGIN: called CC=1 SCAR=0.0
22 8 ▶use Digest::SHA;
Subroutine BEGIN: called CC=1 SCAR=0.0
23 8 ▶use Scalar::Util 'looks_like_number';
Subroutine BEGIN: called CC=1 SCAR=0.0
24 8 ▶use Digest::SHA 'sha1';
Subroutine BEGIN: called CC=1 SCAR=0.0
25 8 ▶use MIME::Base64;
Subroutine BEGIN: called CC=1 SCAR=0.0
26 8 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
27  
28 sub parse_greeting {
29 0 ▶ my ($str) = @_;
Subroutine parse_greeting: not called CC=4 SCAR=30.0
Pod undocumented
30 0 ▶ croak "strlen is not 128 bytes" unless $str and 128 == length $str;
Branch: 1 branch
BranchTrueFalseTotal
unless $str and 128 == length $str 0 0 0
Condition0%0/3
$str and 128 == length $str
!ll&&!rl&&r
000
MC/DC0%0/2
$str and 128 == length $str
$str 128 == length $str
Truth table0%0/3
A and B
A= $strB= 128 == length $str
ABresult
0X0
100
111
31  
32 0 ▶ my $salt = eval { substr decode_base64(substr $str, 64, 44), 0, 20; } || undef;
Condition0%0/2
eval { do { substr &decode_base64(substr($str, 64, 44)), 0, 20 } } || undef
l!l
00
MC/DC0%0/1
eval { do { substr &decode_base64(substr($str, 64, 44)), 0, 20 } } || undef
eval { do { substr &decode_base64(substr($str, 64, 44)), 0, 20 } }
Truth table0%0/2
A
A= eval { do { substr &decode_base64(substr($str, 64, 44)), 0, 20 } }
Aresult
11
00
33 0 my $grstr = substr $str, 0, 64;
34  
35 0 my ($title, $v, $pt, $uid) = split /\s+/, $grstr, 5;
36  
37 return {
38 0 salt => $salt,
39 gr => $grstr,
40 title => $title,
41 version => $v,
42 uuid => $uid,
43 proto => $pt,
44 }
45 }
46  
47 8 my (%resolve, %tresolve);
48  
49 8 my %iter = (
50 EQ => 0,
51 REQ => 1,
52 ALL => 2,
53 LT => 3,
54 LE => 4,
55 GE => 5,
56 GT => 6,
57 BITS_ALL_SET => 7,
58 BITS_ANY_SET => 8,
59 BITS_ALL_NOT_SET => 9
60 );
61  
62 8 my %riter = reverse %iter;
63  
64 BEGIN {
65 8 ▶ my %types = (
Subroutine BEGIN: called CC=3 SCAR=11.0
66 IPROTO_SELECT => 1,
67 IPROTO_INSERT => 2,
68 IPROTO_REPLACE => 3,
69 IPROTO_UPDATE => 4,
70 IPROTO_DELETE => 5,
71 IPROTO_CALL => 6,
72 IPROTO_AUTH => 7,
73 IPROTO_EVAL => 8,
74 IPROTO_PING => 64,
75 );
76 8 my %attrs = (
77 IPROTO_CODE => 0x00,
78 IPROTO_SYNC => 0x01,
79 IPROTO_SERVER_ID => 0x02,
80 IPROTO_LSN => 0x03,
81 IPROTO_TIMESTAMP => 0x04,
82 IPROTO_SCHEMA_ID => 0x05,
83 IPROTO_SPACE_ID => 0x10,
84 IPROTO_INDEX_ID => 0x11,
85 IPROTO_LIMIT => 0x12,
86 IPROTO_OFFSET => 0x13,
87 IPROTO_ITERATOR => 0x14,
88 IPROTO_KEY => 0x20,
89 IPROTO_TUPLE => 0x21,
90 IPROTO_FUNCTION_NAME => 0x22,
91 IPROTO_USER_NAME => 0x23,
92 IPROTO_EXPRESSION => 0x27,
93 IPROTO_DATA => 0x30,
94 IPROTO_ERROR => 0x31,
95 );
96  
97 8 ▶ use constant;
Subroutine BEGIN: called CC=1 SCAR=0.0
98 8 ▶ while (my ($n, $v) = each %types) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %types) 72 8 80
99 72 constant->import($n => $v);
100 72 $n =~ s/^IPROTO_//;
101 72 $tresolve{$v} = $n;
102 }
103 8 ▶ while (my ($n, $v) = each %attrs) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %attrs) 144 8 152
104 144 constant->import($n => $v);
105 144 $n =~ s/^IPROTO_//;
106 144 $resolve{$v} = $n;
107 }
108 }
109  
110  
111 sub raw_response($$) {
112 174 ▶ my ($response, $utf8) = @_;
Subroutine raw_response: called CC=15 SCAR=29.0
Pod undocumented
113  
114 174 my $len;
115 {
116 174 ▶ return unless defined $response;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $response 0 174 174
117 174 ▶ my $lenheader = length $response > 10 ?
Branch: 1 branch
BranchTrueFalseTotal
length $response > 10 ? : 97 77 174
118 substr $response, 0, 10 : $response;
119 174 ▶ return unless my $lenlen = DR::Tnt::Msgpack::msgunpack_check $lenheader;
Branch: 1 branch
BranchTrueFalseTotal
unless my $lenlen = &DR::Tnt::Msgpack::msgunpack_check($lenheader) 7 167 174
120  
121 167 $len = DR::Tnt::Msgpack::msgunpack $lenheader;
122 167 ▶ croak 'Unexpected msgpack object ' . ref($len) if ref $len;
Branch: 1 branch
BranchTrueFalseTotal
if ref $len 0 167 167
123 167 $len += $lenlen;
124 }
125
126 167 ▶ return if length $response < $len;
Branch: 1 branch
BranchTrueFalseTotal
if length $response < $len 160 7 167
127  
128 7 my @r;
129 7 my $off = 0;
130  
131 7 for (1 .. 3) {
132 21 ▶ my $sp = $off ? substr $response, $off : $response;
Branch: 1 branch
BranchTrueFalseTotal
$off ? : 14 7 21
133 21 my $len_item = DR::Tnt::Msgpack::msgunpack_check $sp;
134 21 ▶ croak sprintf('Broken %s section of response', $_)
Branch: 1 branch
BranchTrueFalseTotal
unless $len_item and $len_item + $off <= length $response 0 21 21
Condition33%1/3
$len_item and $len_item + $off <= length $response
!ll&&!rl&&r
0021
MC/DC0%0/2
$len_item and $len_item + $off <= length $response
$len_item $len_item + $off <= length $response
Truth table33%1/3
A and B
A= $len_itemB= $len_item + $off <= length $response
ABresult
0X0
100
111
135 unless $len_item and $len_item + $off <= length $response;
136 21 ▶ if ($utf8) {
Branch: 1 branch
BranchTrueFalseTotal
if ($utf8) { } 0 21 21
137 0 push @r => DR::Tnt::Msgpack::msgunpack_utf8 $sp;
138 } else {
139 21 push @r => DR::Tnt::Msgpack::msgunpack $sp;
140 }
141 21 $off += $len_item;
142  
143 21 ▶ if ($_ eq 2 and $off == length $response) {
Branch: 1 branch
BranchTrueFalseTotal
if ($_ eq 2 and $off == length $response) 0 21 21
Condition66%2/3
$_ eq 2 and $off == length $response
!ll&&!rl&&r
1470
MC/DC0%0/2
$_ eq 2 and $off == length $response
$_ eq 2 $off == length $response
Truth table66%2/3
A and B
A= $_ eq 2B= $off == length $response
ABresult
0X0
100
111
144 0 push @r => {};
145 0 last;
146 }
147 }
148  
149 7 ▶ croak 'Broken response header' unless 'HASH' eq ref $r[1];
Branch: 1 branch
BranchTrueFalseTotal
unless "HASH" eq ref $r[1] 0 7 7
150 7 ▶ croak 'Broken response body' unless 'HASH' eq ref $r[2];
Branch: 1 branch
BranchTrueFalseTotal
unless "HASH" eq ref $r[2] 0 7 7
151  
152 7 return [ $r[1], $r[2] ], substr $response, $off;
153 }
154  
155 sub response($;$) {
156  
157 174 ▶ my ($buffer, $utf8) = @_;
Subroutine response: called CC=10 SCAR=23.3
Pod undocumented
158 174 my ($resp, $tail) = raw_response($buffer => $utf8);
159 174 ▶ return unless $resp;
Branch: 1 branch
BranchTrueFalseTotal
unless $resp 167 7 174
160 7 my ($h, $b) = @$resp;
161  
162 7 my $res = {};
163  
164 7 ▶ while(my ($k, $v) = each %$h) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %$h) 14 7 21
165 14 my $name = $resolve{$k};
166 14 ▶ $name = $k unless defined $name;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $name 0 14 14
167 14 $res->{$name} = $v;
168 }
169 7 ▶ while(my ($k, $v) = each %$b) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %$b) 17 7 24
170 17 my $name = $resolve{$k};
171 17 ▶ $name = $k unless defined $name;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $name 0 17 17
172 17 $res->{$name} = $v;
173 }
174  
175 7 ▶ if (defined $res->{CODE}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $res->{"CODE"}) 7 0 7
176 7 my $n = $tresolve{ $res->{CODE} };
177 7 ▶ $res->{CODE} = $n if defined $n;
Branch: 1 branch
BranchTrueFalseTotal
if defined $n 7 0 7
178 }
179  
180 7 ▶ if (defined $res->{ITERATOR}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $res->{"ITERATOR"}) 1 6 7
181 1 my $n = $riter{ $res->{ITERATOR} };
182 1 ▶ $res->{ITERATOR} = $n if defined $n;
Branch: 1 branch
BranchTrueFalseTotal
if defined $n 1 0 1
183 }
184  
185 7 return $res, $tail;
186
187 }
188  
189 sub request($$) {
190 7 ▶ my ($header, $body) = @_;
Subroutine request: called CC=1 SCAR=0.0
Pod undocumented
191 7 my $pkt = msgpack($header) . msgpack($body);
192  
193 7 return join '',
194 msgpack(length $pkt),
195 $pkt;
196 }
197  
198 sub _mk_header($$$) {
199 7 ▶ my ($code, $sync, $schema_id) = @_;
Subroutine _mk_header: called CC=2 SCAR=8.1
200
201  
202 return {
203 7 ▶ IPROTO_SYNC, $sync,
Branch: 1 branch
BranchTrueFalseTotal
unless defined $schema_id 7 0 7
204 IPROTO_CODE, $code,
205 } unless defined $schema_id;
206  
207 return {
208 0 IPROTO_SYNC, $sync,
209 IPROTO_CODE, $code,
210 IPROTO_SCHEMA_ID, $schema_id
211 }
212 }
213  
214 sub _call_lua($$$$) {
215 0 ▶ my ($sync, $schema_id, $proc, $tuple) = @_;
Subroutine _call_lua: not called CC=1 SCAR=6.9
216 0 request
217 _mk_header(IPROTO_CALL, $sync, $schema_id),
218 {
219 IPROTO_FUNCTION_NAME, $proc,
220 IPROTO_TUPLE, $tuple,
221 }
222 ;
223 }
224  
225 sub call_lua($$$@) {
226 0 ▶ my ($sync, $schema_id, $proc, @args) = @_;
Subroutine call_lua: not called CC=1 SCAR=6.9
Pod undocumented
227 0 return _call_lua($sync, $schema_id, $proc, \@args);
228 }
229  
230  
231 sub eval_lua($$$@) {
232 1 ▶ my ($sync, $schema_id, $lua, @args) = @_;
Subroutine eval_lua: called CC=1 SCAR=0.0
Pod undocumented
233 1 request
234 _mk_header(IPROTO_EVAL, $sync, $schema_id),
235 {
236 IPROTO_EXPRESSION, $lua,
237 IPROTO_TUPLE, \@args,
238 }
239 ;
240 }
241  
242 sub insert($$$$) {
243 1 ▶ my ($sync, $schema_id, $space, $tuple) = @_;
Subroutine insert: called CC=4 SCAR=15.2
Pod undocumented
244  
245 1 ▶ $tuple = [ $tuple ] unless ref $tuple;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $tuple 0 1 1
246 1 ▶ croak "Cant convert HashRef to tuple" if 'HASH' eq ref $tuple;
Branch: 1 branch
BranchTrueFalseTotal
if "HASH" eq ref $tuple 0 1 1
247  
248 1 ▶ if (looks_like_number $space) {
Branch: 1 branch
BranchTrueFalseTotal
if (&looks_like_number($space)) 1 0 1
249 1 return request
250 _mk_header(IPROTO_INSERT, $sync, $schema_id),
251 {
252 IPROTO_SPACE_ID, $space,
253 IPROTO_TUPLE, $tuple,
254 }
255 ;
256 }
257  
258 # HACK
259 0 _call_lua($sync, $schema_id, "box.space.$space:insert", $tuple);
260 }
261  
262 sub replace($$$$) {
263 1 ▶ my ($sync, $schema_id, $space, $tuple) = @_;
Subroutine replace: called CC=4 SCAR=15.2
Pod undocumented
264  
265 1 ▶ $tuple = [ $tuple ] unless ref $tuple;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $tuple 0 1 1
266 1 ▶ croak "Cant convert HashRef to tuple" if 'HASH' eq ref $tuple;
Branch: 1 branch
BranchTrueFalseTotal
if "HASH" eq ref $tuple 0 1 1
267  
268 1 ▶ if (looks_like_number $space) {
Branch: 1 branch
BranchTrueFalseTotal
if (&looks_like_number($space)) 1 0 1
269 1 return request
270 _mk_header(IPROTO_REPLACE, $sync, $schema_id),
271 {
272 IPROTO_SPACE_ID, $space,
273 IPROTO_TUPLE, $tuple,
274 }
275 ;
276 }
277 # HACK
278 0 _call_lua($sync, $schema_id, "box.space.$space:replace", $tuple);
279 }
280 sub del($$$$) {
281 1 ▶ my ($sync, $schema_id, $space, $key) = @_;
Subroutine del: called CC=4 SCAR=15.2
Pod undocumented
282  
283 1 ▶ $key = [ $key ] unless ref $key;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $key 1 0 1
284 1 ▶ croak "Cant convert HashRef to key" if 'HASH' eq ref $key;
Branch: 1 branch
BranchTrueFalseTotal
if "HASH" eq ref $key 0 1 1
285  
286 1 ▶ if (looks_like_number $space) {
Branch: 1 branch
BranchTrueFalseTotal
if (&looks_like_number($space)) 1 0 1
287 1 return request
288 _mk_header(IPROTO_DELETE, $sync, $schema_id),
289 {
290 IPROTO_SPACE_ID, $space,
291 IPROTO_KEY, $key,
292 }
293 ;
294 }
295 # HACK
296 0 _call_lua($sync, $schema_id, "box.space.$space:delete", $key);
297 }
298  
299  
300 sub update($$$$$) {
301 1 ▶ my ($sync, $schema_id, $space, $key, $ops) = @_;
Subroutine update: called CC=5 SCAR=17.8
Pod undocumented
302 1 ▶ croak 'Oplist must be Arrayref' unless 'ARRAY' eq ref $ops;
Branch: 1 branch
BranchTrueFalseTotal
unless "ARRAY" eq ref $ops 0 1 1
303 1 ▶ $key = [ $key ] unless ref $key;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $key 1 0 1
304 1 ▶ croak "Cant convert HashRef to key" if 'HASH' eq ref $key;
Branch: 1 branch
BranchTrueFalseTotal
if "HASH" eq ref $key 0 1 1
305  
306 1 ▶ if (looks_like_number $space) {
Branch: 1 branch
BranchTrueFalseTotal
if (&looks_like_number($space)) 1 0 1
307 1 return request
308 _mk_header(IPROTO_UPDATE, $sync, $schema_id),
309 {
310 IPROTO_SPACE_ID, $space,
311 IPROTO_KEY, $key,
312 IPROTO_TUPLE, $ops,
313 }
314 ;
315 }
316 # HACK
317 0 _call_lua($sync, $schema_id, "box.space.$space:update", [ $key, $ops ]);
318 }
319  
320 sub select($$$$$;$$$) {
321 1 ▶ my ($sync, $schema_id, $space, $index, $key, $limit, $offset, $iterator) = @_;
Subroutine select: called CC=10 SCAR=26.4
Pod undocumented
322 1 ▶ $iterator = 'EQ' unless defined $iterator;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $iterator 0 1 1
323 1 ▶ $offset ||= 0;
Condition50%1/2
$offset ||= 0
l!l
10
MC/DC0%0/1
$offset ||= 0
$offset
Truth table50%1/2
A
A= $offset
Aresult
11
00
324 1 ▶ $limit = 0xFFFF_FFFF unless defined $limit;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $limit 0 1 1
325 1 ▶ $key = [ $key ] unless ref $key;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $key 0 1 1
326 1 ▶ croak "Cant convert HashRef to key" if 'HASH' eq ref $key;
Branch: 1 branch
BranchTrueFalseTotal
if "HASH" eq ref $key 0 1 1
327  
328 1 ▶ unless(looks_like_number $iterator) {
Branch: 1 branch
BranchTrueFalseTotal
unless (&looks_like_number($iterator)) 1 0 1
329 1 my $i = $iter{$iterator};
330 1 ▶ croak "Wrong iterator type: $iterator" unless defined $i;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $i 0 1 1
331 1 $iterator = $i;
332 }
333  
334 1 ▶ if (looks_like_number $space and looks_like_number $index) {
Branch: 1 branch
BranchTrueFalseTotal
if (&looks_like_number($space) and &looks_like_number($index)) 1 0 1
Condition33%1/3
&looks_like_number($space) and &looks_like_number($index)
!ll&&!rl&&r
001
MC/DC0%0/2
&looks_like_number($space) and &looks_like_number($index)
&looks_like_number($space) &looks_like_number($index)
Truth table33%1/3
A and B
A= &looks_like_number($space)B= &looks_like_number($index)
ABresult
0X0
100
111
335 1 return request
336 _mk_header(IPROTO_SELECT, $sync, $schema_id),
337 {
338 IPROTO_KEY, $key,
339 IPROTO_SPACE_ID, $space,
340 IPROTO_OFFSET, $offset,
341 IPROTO_INDEX_ID, $index,
342 IPROTO_LIMIT, $limit,
343 IPROTO_ITERATOR, $iterator,
344 }
345 ;
346 }
347  
348 # HACK
349 0 _call_lua($sync, $schema_id, "box.space.$space.index.$index:select", [
350 $key,
351 {
352 offset => $offset,
353 limit => $limit,
354 iterator => $iterator
355 }
356 ]
357 );
358 }
359  
360 sub ping($$) {
361 1 ▶ my ($sync, $schema_id) = @_;
Subroutine ping: called CC=1 SCAR=0.0
Pod undocumented
362 1 request
363 _mk_header(IPROTO_PING, $sync, $schema_id),
364 {
365 }
366 ;
367 }
368  
369  
370 sub strxor($$) {
371 0 ▶ my ($x, $y) = @_;
Subroutine strxor: not called CC=2 SCAR=17.9
Pod undocumented
372  
373 0 my @x = unpack 'C*', $x;
374 0 my @y = unpack 'C*', $y;
375 0 $x[$_] ^= $y[$_] for 0 .. $#x;
376 0 return pack 'C*', @x;
377 }
378  
379 sub auth($$$$$) {
380 0 ▶ my ($sync, $schema_id, $user, $password, $salt) = @_;
Subroutine auth: not called CC=1 SCAR=6.9
Pod undocumented
381  
382 0 my $hpasswd = sha1 $password;
383 0 my $hhpasswd = sha1 $hpasswd;
384 0 my $scramble = sha1 $salt . $hhpasswd;
385  
386 0 my $hash = strxor $hpasswd, $scramble;
387 0 request
388 _mk_header(IPROTO_AUTH, $sync, $schema_id),
389 {
390 IPROTO_USER_NAME, $user,
391 IPROTO_TUPLE, [ 'chap-sha1', $hash ],
392 }
393 ;
394 }
395  
396 8 1;
« blib/lib/DR/Tnt/Test.pm ↑ Summary blib/lib/DR/Tnt/LowLevel/Connector/Sync.pm »