summaryrefslogtreecommitdiff
path: root/test/functional/sample_test.rb
blob: 1bfe8b5910a37fc8415e648f66e574dec9b9e9b2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
require 'English'
require 'support/functional_test'

class SampleTest < FunctionalTest
  # don't parallize me... Hard to get that right with nickserver start & stop

  def run(*args)
    nickserver :start
    super
  ensure
    nickserver :stop
  end

  def test_running
    assert_running
  end

  # def test_invalid
  #   assert_lookup_status 400, 'invalid'
  # end

  def test_nicknym_success
    assert_lookup_status 200, 'test@mail.bitmask.net'
  end

  # Regression Tests

  # #3 handle missing A records
  def test_nicknym_handles_missing_a_record
    assert_lookup_status 404, 'postmaster@cs.ucl.ac.uk'
  end

  # platform/#8674 handle nonexisting domains
  def test_nicknym_handles_missing_domain
    assert_lookup_status 404, 'postmaster@dont-you-dare-register-this.coop'
  end

  def test_no_file_descriptors_leak
    lookup 'test@mail.bitmask.net'
    before = open_files_count
    lookup 'test@mail.bitmask.net'
    assert_equal before, open_files_count, 'Filedescriptors leaked'
    assert (before > 0), 'Could not get filedescriptor count'
  end

  protected

  def assert_lookup_status(status, address)
    assert_equal status, lookup(address).to_i
  end

  def lookup(address)
    run_command %(curl localhost:6425 #{curl_opts} -d "address=#{address}")
  end

  def curl_opts
    '--silent -w "%{http_code}" -o /dev/null'
  end

  def open_files_count
    run_command(%(lsof | grep " #{nickserver_pid} " | wc -l)).to_i
  end

  def run_command(command)
    `#{command} 2>&1`.tap do |out|
      assert $CHILD_STATUS.exitstatus.zero?,
             "failed to run '#{command}':\n #{out}"
    end
  end
end