Class: TimingAttack::TestCase

Inherits:
Object
  • Object
show all
Defined in:
lib/timing_attack/test_case.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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
# File 'lib/timing_attack/test_case.rb', line 6

def initialize(input: , options: {})
  @input = input
  @options = options
  @times = []
  @percentiles = []
  @hydra_requests = []
  @url = URI.escape(
    options.fetch(:url).
    gsub(INPUT_FLAG, input)
  )
  @params = params_from(options.fetch :params, {})
  @body = params_from(options.fetch :body, {})
  @basic_auth_username = params_from(
    options.fetch(:basic_auth_username, "")
  )
  @basic_auth_password = params_from(
    options.fetch(:basic_auth_password, "")
  )
end

Instance Attribute Details

#inputObject (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



26
27
28
29
30
# File 'lib/timing_attack/test_case.rb', line 26

def generate_hydra_request!
  req = Typhoeus::Request.new(url, **typhoeus_opts)
  @hydra_requests.push req
  req
end

#meanObject



56
57
58
# File 'lib/timing_attack/test_case.rb', line 56

def mean
  times.reduce(:+) / times.size.to_f
end

#percentile(n) ⇒ Object

Raises:

  • (ArgumentError)


60
61
62
63
64
65
66
67
68
# File 'lib/timing_attack/test_case.rb', line 60

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



48
49
50
51
52
53
54
# File 'lib/timing_attack/test_case.rb', line 48

def process!
  @hydra_requests.each do |request|
    response = request.response
    diff = response.time - response.namelookup_time
    @times.push(diff)
  end
end

#typhoeus_basic_authObject



43
44
45
46
# File 'lib/timing_attack/test_case.rb', line 43

def typhoeus_basic_auth
  return "" if basic_auth_username.empty? && basic_auth_password.empty?
  "#{basic_auth_username}:#{basic_auth_password}"
end

#typhoeus_optsObject



32
33
34
35
36
37
38
39
40
41
# File 'lib/timing_attack/test_case.rb', line 32

def typhoeus_opts
  {
    method: options.fetch(:method),
    followlocation: true,
  }.tap do |h|
    h[:params] = params unless params.empty?
    h[:body] = body unless body.empty?
    h[:userpwd] = typhoeus_basic_auth unless typhoeus_basic_auth.empty?
  end
end