2018-08-02 00:09:29 +02:00
|
|
|
# Copyright (C) 2018 Colin Darie <colin@darie.eu>, 2018 Evolix <info@evolix.fr>
|
|
|
|
# License: GNU AGPL-3+ (see full text in LICENSE file)
|
|
|
|
|
2018-07-02 17:21:08 +02:00
|
|
|
require "test_helper"
|
|
|
|
|
|
|
|
module SSL
|
|
|
|
class ServiceTest < ActiveSupport::TestCase
|
|
|
|
test "should run the command, return the result" do
|
|
|
|
result = OpenStruct.new(exit_status: 0)
|
|
|
|
|
2018-08-29 16:51:21 +02:00
|
|
|
mock_system_klass("check_http", standard_args, result) do |system_klass|
|
2019-03-03 22:47:05 +01:00
|
|
|
service = SSL::Service.new("example.org", system_klass: system_klass)
|
2018-07-02 17:21:08 +02:00
|
|
|
assert_equal result, service.run_command
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
test "should raise an exception if exit status > 0" do
|
|
|
|
result = OpenStruct.new(exit_status: 1)
|
|
|
|
|
2018-08-29 16:51:21 +02:00
|
|
|
mock_system_klass("check_http", standard_args, result) do |system_klass|
|
2019-03-03 22:47:05 +01:00
|
|
|
service = SSL::Service.new("example.org", system_klass: system_klass)
|
2018-07-02 17:21:08 +02:00
|
|
|
|
2019-03-03 22:47:05 +01:00
|
|
|
assert_raises SSL::CommandError do
|
2018-07-02 17:21:08 +02:00
|
|
|
service.run_command
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
test "should parse from a command result" do
|
|
|
|
result = OpenStruct.new(
|
|
|
|
exit_status: 0,
|
|
|
|
stdout: file_fixture("ssl/ssl0.domain.org.txt").read,
|
|
|
|
)
|
|
|
|
|
2019-03-03 22:47:05 +01:00
|
|
|
service = SSL::Service.new("ssl0.domain.org")
|
2019-03-03 11:05:05 +01:00
|
|
|
assert_kind_of SSL::Response, service.parse(result)
|
2018-07-02 17:21:08 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
test "should uses the command line arguments of the configuration" do
|
|
|
|
result = OpenStruct.new(exit_status: 0)
|
2018-07-05 16:04:17 +02:00
|
|
|
config = OpenStruct.new(check_http_args: ["-f", "-I 127.0.0.1"])
|
2018-07-02 17:21:08 +02:00
|
|
|
|
2018-08-29 16:51:21 +02:00
|
|
|
expected_args = standard_args.concat ["-f", "-I 127.0.0.1"]
|
2018-07-02 17:21:08 +02:00
|
|
|
mock_system_klass("check_http", expected_args, result) do |system_klass|
|
2019-03-03 22:47:05 +01:00
|
|
|
service = SSL::Service.new("example.org", configuration: config, system_klass: system_klass)
|
2018-07-02 17:21:08 +02:00
|
|
|
assert_equal result, service.run_command
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-07-05 16:04:17 +02:00
|
|
|
test "should raise an error when check_http_args is not an array" do
|
|
|
|
black_hole = Naught.build(&:black_hole)
|
|
|
|
config = OpenStruct.new(check_http_args: "-f")
|
|
|
|
|
2019-03-03 22:47:05 +01:00
|
|
|
assert_raises SSL::ConfigurationError do
|
|
|
|
service = SSL::Service.new("example.org", configuration: config, system_klass: black_hole)
|
2018-07-05 16:04:17 +02:00
|
|
|
service.run_command
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-07-02 17:21:08 +02:00
|
|
|
test "should uses the program path from the configuration" do
|
|
|
|
result = OpenStruct.new(exit_status: 0)
|
|
|
|
config = OpenStruct.new(check_http_path: "/usr/local/custom/path")
|
|
|
|
|
2018-08-29 16:51:21 +02:00
|
|
|
mock_system_klass("/usr/local/custom/path", standard_args, result) do |sys|
|
2019-03-03 22:47:05 +01:00
|
|
|
service = SSL::Service.new("example.org", configuration: config, system_klass: sys)
|
2018-07-02 17:21:08 +02:00
|
|
|
assert_equal result, service.run_command
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-08-29 16:51:21 +02:00
|
|
|
private
|
|
|
|
|
|
|
|
def standard_args
|
|
|
|
["-C 0", "--sni", "-H", "example.org"]
|
|
|
|
end
|
|
|
|
|
2018-07-02 17:21:08 +02:00
|
|
|
def mock_system_klass(program, command_args, result)
|
|
|
|
system_klass = Minitest::Mock.new
|
|
|
|
system_command = Minitest::Mock.new.expect(:execute, result)
|
|
|
|
system_klass.expect(:new, system_command) do |arg1, arg2, logger:|
|
|
|
|
arg1 == program &&
|
|
|
|
arg2 == command_args &&
|
|
|
|
logger.class == NullLogger
|
|
|
|
end
|
|
|
|
|
|
|
|
yield system_klass
|
|
|
|
|
|
|
|
system_klass.verify
|
|
|
|
system_command.verify
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|