blib/lib/Protocol/HAP/SetupCode.pm

filter: Statementstmt 100% 21 / 21 Branchbran 100% 8 / 8 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 5 / 5 Podpod 100% 2 / 2 totaltotal 100% 36 / 36 CC 5 SCAR 8.5
CC
5
Cov
100%
normalize_setup_code
3.0
validate_setup_code
3.0
BEGIN
1.0
CRAP 2.3
« blib/lib/Protocol/HAP/Bridge.pm ↑ Summary blib/lib/Protocol/HAP.pm »
1 # ex:ts=8 sw=4:
2 # $OpenBSD$
3 #
4 # Copyright (c) 2025 Dick Olsson <hi@senzilla.io>
5 #
6 # Permission to use, copy, modify, and distribute this software for any
7 # purpose with or without fee is hereby granted, provided that the above
8 # copyright notice and this permission notice appear in all copies.
9 #
10 # THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 # WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 # MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 # ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 # WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 # ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 # OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17  
18 4 ▶use v5.36;
Subroutine BEGIN: called CC=1 SCAR=0.0
19  
20 package Protocol::HAP::SetupCode;
21 4 our $VERSION = '0.1.0';
22 4 ▶use Exporter qw(import);
Subroutine BEGIN: called CC=1 SCAR=0.0
23 4 our @EXPORT_OK = qw(normalize_setup_code validate_setup_code);
24  
25 # Protocol::HAP::SetupCode - the rules of the 8-digit setup code.
26 #
27 # The specification says "setup code" [HAP-Pairing ยง2]. The word PIN is
28 # the one it replaced, so no name here uses it.
29  
30 # Invalid setup codes per HAP specification
31 # These are sequential or trivial patterns. Do not use them.
32 4 ▶use constant INVALID_SETUP_CODES => qw(
Subroutine BEGIN: called CC=1 SCAR=0.0
33 00000000 11111111 22222222 33333333 44444444
34 55555555 66666666 77777777 88888888 99999999
35 12345678 87654321
36 4 );
37  
38 # normalize_setup_code($code):
39 # Remove dashes and spaces from the setup code for internal use
40 # Returns: an 8-digit numeric string, or undef if the format
41 # is invalid
42 56 ▶sub normalize_setup_code ($code)
Subroutine normalize_setup_code: called CC=3 SCAR=11.0
Pod documented
43 {
44 56 ▶ return unless defined $code;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $code 2 54 56
45  
46 # Remove the dashes and spaces
47 54 $code =~ s/[-\s]//g;
48  
49 # Make sure the setup code is exactly 8 digits
50 54 ▶ return unless $code =~ /^\d{8}$/;
Branch: 1 branch
BranchTrueFalseTotal
unless $code =~ /^\d{8}$/u 12 42 54
51  
52 42 return $code;
53 }
54  
55 # validate_setup_code($code):
56 # Validate that the setup code meets the HAP requirements
57 # Returns: 1 if the setup code is valid, undef if it is invalid
58 24 ▶sub validate_setup_code ($code)
Subroutine validate_setup_code: called CC=3 SCAR=11.0
Pod documented
59 {
60 # Normalize the setup code first
61 24 my $normalized = normalize_setup_code($code);
62 24 ▶ return unless defined $normalized;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $normalized 4 20 24
63  
64 # Check the setup code against the list of invalid codes
65 20 my %invalid = map { $_ => 1 } INVALID_SETUP_CODES;
66 20 ▶ return if exists $invalid{$normalized};
Branch: 1 branch
BranchTrueFalseTotal
if exists $invalid{$normalized} 16 4 20
67  
68 4 return 1;
69 }
70  
71 4 1;
« blib/lib/Protocol/HAP/Bridge.pm ↑ Summary blib/lib/Protocol/HAP.pm »