File Coverage

blib/lib/STIX/Util.pm
Criterion Covered Total %
statement 39 55 70.9
branch 6 18 33.3
condition n/a
subroutine 12 15 80.0
pod 8 8 100.0
total 65 96 67.7


line stmt bran cond sub pod time code
1             package STIX::Util;
2              
3 3     3   1214 use 5.010001;
  3         12  
4 3     3   18 use strict;
  3         6  
  3         72  
5 3     3   14 use warnings;
  3         20  
  3         222  
6 3     3   21 use utf8;
  3         7  
  3         22  
7              
8 3     3   172 use Exporter qw(import);
  3         8  
  3         188  
9              
10 3     3   19 use Carp;
  3         4  
  3         517  
11 3     3   48 use UUID::Tiny qw(:std);
  3         6  
  3         3218  
12              
13             our @EXPORT = (qw[generate_uuid get_type_from_id is_sco is_sdo is_sro is_marking file_read file_write]);
14              
15             sub generate_uuid {
16 0     0 1 0 my ($ns, $string) = @_;
17 0         0 return create_uuid_as_string(UUID_V5, $ns, $string);
18             }
19              
20             sub get_type_from_id {
21              
22 3     3 1 37 my $id = shift;
23              
24 3 50       17 Carp::croak 'Malformed identifier' unless ($id =~ /--/);
25              
26 3         14 my ($type, $uuid) = split /--/, $id;
27 3         50 return $type;
28              
29             }
30              
31             sub is_sdo {
32              
33 1     1 1 28 my $object = shift;
34              
35 1 50       9 return unless $object->can('STIX_OBJECT');
36 1         11 return $object->STIX_OBJECT eq 'SDO';
37              
38             }
39              
40             sub is_sco {
41              
42 1     1 1 5 my $object = shift;
43              
44 1 50       11 return unless $object->can('STIX_OBJECT');
45 1         9 return $object->STIX_OBJECT eq 'SCO';
46              
47             }
48              
49             sub is_sro {
50              
51 1     1 1 3 my $object = shift;
52              
53 1 50       10 return unless $object->can('STIX_OBJECT');
54 1         9 return $object->STIX_OBJECT eq 'SRO';
55              
56             }
57              
58             sub is_marking {
59              
60 0     0 1 0 my $object = shift;
61              
62 0 0       0 return unless $object->can('MARKING_TYPE');
63 0         0 return 1;
64              
65             }
66              
67             sub file_read {
68              
69 9     9 1 482092 my $file = shift;
70              
71 9 50       55 if (ref($file) eq 'GLOB') {
72 0         0 return do { local $/; <$file> };
  0         0  
  0         0  
73             }
74              
75 9         21 return do {
76 9 50       1211 open(my $fh, '<', $file) or Carp::croak qq{Failed to read file: $!};
77 9         67 local $/ = undef;
78 9         3663 <$fh>;
79             };
80              
81             }
82              
83             sub file_write {
84              
85 0     0 1   my ($file, $content) = @_;
86              
87 0           my $fh = undef;
88              
89 0 0         if (ref($file) eq 'GLOB') {
90 0           $fh = $file;
91             }
92             else {
93 0 0         open($fh, '>', $file) or Carp::croak "Can't open file: $!";
94             }
95              
96 0           $fh->autoflush(1);
97              
98 0           print $fh $content;
99 0           close($fh);
100              
101             }
102              
103              
104             1;
105              
106             1;
107              
108             =encoding utf-8
109              
110             =head1 NAME
111              
112             STIX::Util - Utility for STIX
113              
114             =head1 SYNOPSIS
115              
116             use STIX::Util qw(is_sdo);
117              
118             if (is_sdo($indicator)) {
119             say "IS STIX Domain Object"
120             }
121              
122              
123             =head1 DESCRIPTION
124              
125             Utility for L.
126              
127             =head2 FUNCTIONS
128              
129             =over
130              
131             =item file_read($file)
132              
133             Read a C<$file>.
134              
135             =item file_write($file, $content)
136              
137             Write the C<$content> in C<$file>
138              
139             =item generate_uuid($ns, $string)
140              
141             Generate UUID.
142              
143             =item get_type_from_id($identifier)
144              
145             Return STIX object type from the identifier.
146              
147             =item is_marking($object)
148              
149             Check if the provided object is a Marking object type.
150              
151             =item is_sco($object)
152              
153             Check if the provided object is a Cyber-observable object type.
154              
155             =item is_sdo($object)
156              
157             Check if the provided object is a Domain object type.
158              
159             =item is_sro($object)
160              
161             Check if the provided object is a Relationship object type.
162              
163             =back
164              
165              
166             =head1 SUPPORT
167              
168             =head2 Bugs / Feature Requests
169              
170             Please report any bugs or feature requests through the issue tracker
171             at L.
172             You will be notified automatically of any progress on your issue.
173              
174             =head2 Source Code
175              
176             This is open source software. The code repository is available for
177             public review and contribution under the terms of the license.
178              
179             L
180              
181             git clone https://github.com/giterlizzi/perl-STIX.git
182              
183              
184             =head1 AUTHOR
185              
186             =over 4
187              
188             =item * Giuseppe Di Terlizzi
189              
190             =back
191              
192              
193             =head1 LICENSE AND COPYRIGHT
194              
195             This software is copyright (c) 2024 by Giuseppe Di Terlizzi.
196              
197             This is free software; you can redistribute it and/or modify it under
198             the same terms as the Perl 5 programming language system itself.
199              
200             =cut