| 1 | package Blitz::Validate; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use warnings; | ||
| 5 | use Regexp::Common qw/URI/; | ||
| 6 | |||
| 7 | =head1 NAME | ||
| 8 | |||
| 9 | Blitz::Validate - Perl module for assisting with the validation of calls to Blitz.io | ||
| 10 | |||
| 11 | =cut | ||
| 12 | |||
| 13 | =head2 validate | ||
| 14 | |||
| 15 | Takes a hash reference of options to be sent to the blitz.io servers | ||
| 16 | and validates its content. Called before any tests are executed. | ||
| 17 | |||
| 18 | Detailed API docs for communicating with the blitz servers can be found here: | ||
| 19 | https://github.com/mudynamics/blitz-app/wiki/API-Docs | ||
| 20 | |||
| 21 | =cut | ||
| 22 | |||
| 23 | sub validate { | ||
| 24 | my $options = shift; | ||
| 25 | my $test_type = shift; | ||
| 26 | my $reasons = []; | ||
| 27 | |||
| 28 | # url | ||
| 29 | if (!$options->{url}) { | ||
| 30 | push @$reasons, "No URL given"; | ||
| 31 | } | ||
| 32 | elsif (! _is_url($options->{url}) ) { | ||
| 33 | push @$reasons, "Invalid URL: $options->{url}"; | ||
| 34 | } | ||
| 35 | |||
| 36 | # pattern | ||
| 37 | if ($options->{pattern} && $test_type eq 'rush') { | ||
| 38 | if (!$options->{pattern}{iterations} || ! _is_integer($options->{pattern}{iterations}) ) { | ||
| 39 | push @$reasons, "Pattern iterations not given or not an integer"; | ||
| 40 | } | ||
| 41 | else { | ||
| 42 | $options->{pattern}{iterations} += 0; | ||
| 43 | } | ||
| 44 | if (! $options->{pattern}{intervals} || ! _is_array($options->{pattern}{intervals}) ) { | ||
| 45 | push @$reasons, "Intervals is not an array"; | ||
| 46 | } | ||
| 47 | } | ||
| 48 | elsif ($options->{pattern} && $test_type eq 'sprint') { | ||
| 49 | push @$reasons, 'Pattern is not a valid options for sprints'; | ||
| 50 | } | ||
| 51 | |||
| 52 | # region | ||
| 53 | if ($options->{region} && $options->{region} !~ /(california|virginia|ireland|singapore|japan)/) { | ||
| 54 | |||
| 55 | push @$reasons, "Invalid region: $options->{region}"; | ||
| 56 | } | ||
| 57 | |||
| 58 | # ssl | ||
| 59 | if ($options->{ssl} && ! | ||
| 60 | ( $options->{ssl} eq 'tlsv1' || | ||
| 61 | $options->{ssl} eq 'sslv2' || | ||
| 62 | $options->{ssl} eq 'sslv3' ) | ||
| 63 | ){ | ||
| 64 | push @$reasons, "SSL needs to be one of the following: tlsv1, sslv2, or sslv3"; | ||
| 65 | } | ||
| 66 | |||
| 67 | # data | ||
| 68 | if ($options->{content}) { | ||
| 69 | if (! _is_hash($options->{content})) { | ||
| 70 | push @$reasons, "Content must be a hash"; | ||
| 71 | } | ||
| 72 | elsif ( ! $options->{content}{data} || ! _is_array($options->{content}{data}) ) { | ||
| 73 | push @$reasons, "Content hash must have a data key with an array value"; | ||
| 74 | } | ||
| 75 | } | ||
| 76 | |||
| 77 | # referer | ||
| 78 | if ($options->{referer} && ! _is_url($options->{referer} )) { | ||
| 79 | push @$reasons, "Invalid referer URL: " . $options->{referer}; | ||
| 80 | } | ||
| 81 | # string params | ||
| 82 | for my $key ('user-agent', 'user') { | ||
| 83 | if ($options->{$key} && | ||
| 84 | ( _is_array($options->{$key}) || | ||
| 85 | _is_hash($options->{$key}) ) | ||
| 86 | ) { | ||
| 87 | push @$reasons, "Invalid " . $key . ": " . "$options->{$key}"; | ||
| 88 | } | ||
| 89 | } | ||
| 90 | |||
| 91 | # integer params | ||
| 92 | for my $key ('status', 'timeout') { | ||
| 93 | if ($options->{$key} && $options->{$key} !~ /^\d+$/g) { | ||
| 94 | push @$reasons, "Invalid " . $key . ": " . "$options->{$key}"; | ||
| 95 | } | ||
| 96 | } | ||
| 97 | |||
| 98 | # array params | ||
| 99 | for my $key ('cookies', 'headers') { | ||
| 100 | if ($options->{$key} && ! _is_array($options->{$key})) { | ||
| 101 | push @$reasons, "values to $key not given as an array: " . $options->{$key}; | ||
| 102 | } | ||
| 103 | } | ||
| 104 | |||
| 105 | # XXX: variables | ||
| 106 | # variables must be a hash | ||
| 107 | # key/val pairs of hash must be name, followed by a hash of params | ||
| 108 | # each params hash must have a type key, with the value being one of "list|alpha|number|udid" | ||
| 109 | # if list, must have an entries key with a value of an array | ||
| 110 | # if alpha, must have keys min and max, with integer values | ||
| 111 | # if number, must have keys min and max, with integer values | ||
| 112 | if ($options->{variables}) { | ||
| 113 | if (! _is_hash($options->{variables})) { | ||
| 114 | push @$reasons, "values to variables not given as a hash: " . $options->{variables}; | ||
| 115 | } | ||
| 116 | else { | ||
| 117 | for my $key (keys %{$options->{variables}}) { | ||
| 118 | if (! _is_hash($options->{variables}{$key})) { | ||
| 119 | push @$reasons, | ||
| 120 | "values to variables key $key not given as a hash: " . | ||
| 121 | $options->{variables}{$key}; | ||
| 122 | } | ||
| 123 | else { | ||
| 124 | my $type = $options->{variables}{$key}{type}; | ||
| 125 | if (! $type) { | ||
| 126 | push @$reasons, "all variables must have a type definition"; | ||
| 127 | } | ||
| 128 | elsif (! $type =~ /^(list|alpha|number|udid)$/) { | ||
| 129 | push @$reasons, "all variables must have type of list, alpha, number, or udid"; | ||
| 130 | } | ||
| 131 | else { | ||
| 132 | if ($type eq 'list') { | ||
| 133 | if (! $options->{variables}{$key}{entries} || | ||
| 134 | ! _is_array($options->{variables}{$key}{entries}) ) { | ||
| 135 | push @$reasons, "all variables of type list need an array of values"; | ||
| 136 | } | ||
| 137 | } | ||
| 138 | elsif ($type eq 'number' || $type eq 'alpha') { | ||
| 139 | for my $int ('min', 'max') { | ||
| 140 | if (! $options->{variables}{$key}{$int} || | ||
| 141 | $options->{variables}{$key}{$int} !~ /^\-*\d+$/g) { | ||
| 142 | push @$reasons, "$int value must be an integer for $key"; | ||
| 143 | } | ||
| 144 | else { | ||
| 145 | $options->{variables}{$key}{$int} += 0; | ||
| 146 | } | ||
| 147 | } | ||
| 148 | } | ||
| 149 | } | ||
| 150 | } | ||
| 151 | } | ||
| 152 | } | ||
| 153 | } | ||
| 154 | |||
| 155 | if ($reasons->[0]) { | ||
| 156 | my $reason = join(', ', @$reasons); | ||
| 157 | return (0, { error => 'Invalid parameters', reason => $reason }); | ||
| 158 | } | ||
| 159 | else { | ||
| 160 | return (1, {}); | ||
| 161 | } | ||
| 162 | } | ||
| 163 | |||
| 164 | sub _is_integer { | ||
| 165 | my $val = shift; | ||
| 166 | if ($val && $val =~ /^\d+$/g) { | ||
| 167 | return 1; | ||
| 168 | } | ||
| 169 | else { | ||
| 170 | return 0; | ||
| 171 | } | ||
| 172 | } | ||
| 173 | |||
| 174 | sub _is_foo { | ||
| 175 | my ($val, $type) = @_; | ||
| 176 | if ( ref($val) =~ $type) { | ||
| 177 | return 1; | ||
| 178 | } | ||
| 179 | else { | ||
| 180 | return 0; | ||
| 181 | } | ||
| 182 | } | ||
| 183 | sub _is_array { | ||
| 184 | return _is_foo($_[0], 'ARRAY'); | ||
| 185 | } | ||
| 186 | |||
| 187 | sub _is_hash { | ||
| 188 | return _is_foo($_[0], 'HASH'); | ||
| 189 | } | ||
| 190 | |||
| 191 | sub _is_url { | ||
| 192 | my $url = shift; | ||
| 193 | return $url =~ /$RE{URI}{HTTP}{ -scheme => 'https?' }/g; | ||
| 194 | } | ||
| 195 | |||
| 196 | return 1; |