Class: TimingAttack::TestCase
- Inherits:
-
Object
- Object
- TimingAttack::TestCase
- Defined in:
- lib/timing_attack/test_case.rb
Instance Attribute Summary collapse
-
#input ⇒ Object
readonly
Returns the value of attribute input.
Instance Method Summary collapse
- #generate_hydra_request! ⇒ Object
-
#initialize(input:, options: {}) ⇒ TestCase
constructor
A new instance of TestCase.
- #mean ⇒ Object
- #percentile(n) ⇒ Object
- #process! ⇒ Object
- #typhoeus_basic_auth ⇒ Object
- #typhoeus_opts ⇒ Object
Constructor Details
#initialize(input:, options: {}) ⇒ TestCase
Returns a new instance of TestCase.
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
# File 'lib/timing_attack/test_case.rb', line 6 def initialize(input: , options: {}) @input = input @options = @times = [] @percentiles = [] @hydra_requests = [] @url = URI.escape( .fetch(:url). gsub(INPUT_FLAG, input) ) @params = params_from(.fetch(:params, {})) @body = params_from(.fetch(:body, {})) @headers = params_from(.fetch(:headers, {})) @basic_auth_username = params_from( .fetch(:basic_auth_username, "") ) @basic_auth_password = params_from( .fetch(:basic_auth_password, "") ) end |
Instance Attribute Details
#input ⇒ Object (readonly)
Returns the value of attribute input.
5 6 7 |
# File 'lib/timing_attack/test_case.rb', line 5 def input @input end |
Instance Method Details
#generate_hydra_request! ⇒ Object
27 28 29 30 31 |
# File 'lib/timing_attack/test_case.rb', line 27 def generate_hydra_request! req = Typhoeus::Request.new(url, **typhoeus_opts) @hydra_requests.push req req end |
#mean ⇒ Object
58 59 60 |
# File 'lib/timing_attack/test_case.rb', line 58 def mean times.reduce(:+) / times.size.to_f end |
#percentile(n) ⇒ Object
62 63 64 65 66 67 68 69 70 |
# File 'lib/timing_attack/test_case.rb', line 62 def percentile(n) raise ArgumentError.new("Can't have a percentile > 100") if n > 100 if percentiles[n].nil? position = ((times.length - 1) * (n/100.0)).to_i percentiles[n] = times.sort[position] else percentiles[n] end end |
#process! ⇒ Object
50 51 52 53 54 55 56 |
# File 'lib/timing_attack/test_case.rb', line 50 def process! @hydra_requests.each do |request| response = request.response diff = response.time - response.namelookup_time @times.push(diff) end end |
#typhoeus_basic_auth ⇒ Object
45 46 47 48 |
# File 'lib/timing_attack/test_case.rb', line 45 def typhoeus_basic_auth return "" if basic_auth_username.empty? && basic_auth_password.empty? "#{basic_auth_username}:#{basic_auth_password}" end |
#typhoeus_opts ⇒ Object
33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/timing_attack/test_case.rb', line 33 def typhoeus_opts { method: .fetch(:method), followlocation: true, }.tap do |h| h[:params] = params unless params.empty? h[:body] = body unless body.empty? h[:headers] = headers unless headers.empty? h[:userpwd] = typhoeus_basic_auth unless typhoeus_basic_auth.empty? end end |