Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

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