Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

131 рядки
5.1KB

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