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
25
# 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, {}))
  @headers = params_from(options.fetch(:headers, {}))
  @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



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

#meanObject



58
59
60
# File 'lib/timing_attack/test_case.rb', line 58

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

#percentile(n) ⇒ Object

Raises:

  • (ArgumentError)


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_authObject



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_optsObject



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

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[:headers] = headers unless headers.empty?
    h[:userpwd] = typhoeus_basic_auth unless typhoeus_basic_auth.empty?
  end
end