No Description https://cryptcheck.fr/
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

analysis.rb 4.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. RSpec.shared_examples :analysis do
  2. describe '#analyze' do
  3. it 'return 1 grade with IPv4' do
  4. grades = server host: '127.0.0.1' do
  5. analyze '127.0.0.1', 5000
  6. end
  7. expect(grades.size).to be 1
  8. expect_grade grades, '127.0.0.1', '127.0.0.1', 5000, :ipv4
  9. end
  10. it 'return 1 grade with IPv6' do
  11. grades = server host: '::1' do
  12. analyze '::1', 5000
  13. end
  14. expect(grades.size).to be 1
  15. expect_grade grades, '::1', '::1', 5000, :ipv6
  16. end
  17. it 'return 2 grades with hostname (IPv4 & IPv6)' do
  18. addresses = %w(127.0.0.1 ::1)
  19. allow(Addrinfo).to receive(:getaddrinfo).with('localhost', nil, nil, :STREAM) do
  20. addresses.collect { |a| Addrinfo.new Socket.sockaddr_in(nil, a) }
  21. end
  22. grades = server host: '::' do
  23. analyze 'localhost', 5000
  24. end
  25. expect_grade grades, 'localhost', '127.0.0.1', 5000, :ipv4
  26. expect_grade grades, 'localhost', '::1', 5000, :ipv6
  27. end
  28. it 'return error if DNS resolution problem' do
  29. allow(Addrinfo).to receive(:getaddrinfo).with('localhost', nil, nil, :STREAM)
  30. .and_raise SocketError, 'getaddrinfo: Name or service not known'
  31. grades = server do
  32. analyze 'localhost', 5000
  33. end
  34. expect_grade_error grades, 'localhost', nil, 5000, 'Unable to resolve localhost'
  35. end
  36. it 'return error if analysis too long' do
  37. stub_const 'CryptCheck::MAX_ANALYSIS_DURATION', 1
  38. allow(CryptCheck::Tls::Server).to receive(:new) { sleep 2 }
  39. grades = server do
  40. analyze 'localhost', 5000
  41. end
  42. expect_grade_error grades, 'localhost', '127.0.0.1', 5000,
  43. 'Too long analysis (max 1 second)'
  44. end
  45. it 'return error if unable to connect' do
  46. addresses = %w(127.0.0.1 ::1)
  47. allow(Addrinfo).to receive(:getaddrinfo).with('localhost', nil, nil, :STREAM) do
  48. addresses.collect { |a| Addrinfo.new Socket.sockaddr_in(nil, a) }
  49. end
  50. grades = server host: '::1' do
  51. analyze 'localhost', 5000
  52. end
  53. expect_grade_error grades, 'localhost', '127.0.0.1', 5000,
  54. 'Connection refused - connect(2) for 127.0.0.1:5000'
  55. expect_grade grades, 'localhost', '::1', 5000, :ipv6
  56. end
  57. it 'return error if TCP timeout' do
  58. stub_const 'CryptCheck::Tls::Server::TCP_TIMEOUT', 1
  59. addresses = %w(127.0.0.1 ::1)
  60. allow(Addrinfo).to receive(:getaddrinfo).with('localhost', nil, nil, :STREAM) do
  61. addresses.collect { |a| Addrinfo.new Socket.sockaddr_in(nil, a) }
  62. end
  63. original = IO.method :select
  64. allow(IO).to receive(:select) do |*args, &block|
  65. socket = [args[0]&.first, args[1]&.first].compact.first
  66. next nil if socket.is_a?(Socket) && (socket.local_address.afamily == Socket::AF_INET)
  67. original.call *args, &block
  68. end
  69. grades = server host: '::' do
  70. analyze 'localhost', 5000
  71. end
  72. expect_grade_error grades, 'localhost', '127.0.0.1', 5000,
  73. 'Timeout when connect to 127.0.0.1:5000 (max 1 second)'
  74. expect_grade grades, 'localhost', '::1', 5000, :ipv6
  75. end
  76. it 'return error if TLS timeout' do
  77. stub_const 'CryptCheck::Tls::Server::SSL_TIMEOUT', 1
  78. addresses = %w(127.0.0.1 ::1)
  79. allow(Addrinfo).to receive(:getaddrinfo).with('localhost', nil, nil, :STREAM) do
  80. addresses.collect { |a| Addrinfo.new Socket.sockaddr_in(nil, a) }
  81. end
  82. original = IO.method :select
  83. allow(IO).to receive(:select) do |*args, &block|
  84. socket = [args[0]&.first, args[1]&.first].compact.first
  85. next nil if socket.is_a?(OpenSSL::SSL::SSLSocket) && (socket.io.local_address.afamily == Socket::AF_INET)
  86. original.call *args, &block
  87. end
  88. grades = server host: '::' do
  89. analyze 'localhost', 5000
  90. end
  91. expect_grade_error grades, 'localhost', '127.0.0.1', 5000,
  92. 'Timeout when TLS connect to 127.0.0.1:5000 (max 1 second)'
  93. expect_grade grades, 'localhost', '::1', 5000, :ipv6
  94. end
  95. it 'return error if plain server' do
  96. stub_const 'CryptCheck::Tls::Server::SSL_TIMEOUT', 1
  97. addresses = %w(127.0.0.1 ::1)
  98. allow(Addrinfo).to receive(:getaddrinfo).with('localhost', nil, nil, :STREAM) do
  99. addresses.collect { |a| Addrinfo.new Socket.sockaddr_in(nil, a) }
  100. end
  101. grades = plain_server host: '127.0.0.1' do
  102. server host: '::1' do
  103. analyze 'localhost', 5000
  104. end
  105. end
  106. expect_grade_error grades, 'localhost', '127.0.0.1', 5000,
  107. 'TLS seems not supported on this server'
  108. expect_grade grades, 'localhost', '::1', 5000, :ipv6
  109. end
  110. end
  111. end