Skip to content

Commit

Permalink
Verify configuration arguments
Browse files Browse the repository at this point in the history
  • Loading branch information
calio committed Apr 28, 2015
1 parent ce0a372 commit 8986459
Show file tree
Hide file tree
Showing 2 changed files with 124 additions and 1 deletion.
47 changes: 47 additions & 0 deletions lib/resty/logger/socket.lua
Expand Up @@ -17,6 +17,8 @@ local WARN = ngx.WARN
local ERR = ngx.ERR
local CRIT = ngx.CRIT

local MAX_PORT = 65535


local ok, new_tab = pcall(require, "table.new")
if not ok then
Expand Down Expand Up @@ -359,33 +361,78 @@ function _M.init(user_config)

for k, v in pairs(user_config) do
if k == "host" then
if type(v) ~= "string" then
return nil, '"host" must be a string'
end
host = v
elseif k == "port" then
if type(v) ~= "number" then
return nil, '"port" must be a number'
end
if v < 0 or v > MAX_PORT then
return nil, ('"port" out of range 0~%s'):format(MAX_PORT)
end
port = v
elseif k == "path" then
if type(v) ~= "string" then
return nil, '"path" must be a string'
end
path = v
elseif k == "flush_limit" then
if type(v) ~= "number" or v < 0 then
return nil, 'invalid "flush_limit"'
end
flush_limit = v
elseif k == "drop_limit" then
if type(v) ~= "number" or v < 0 then
return nil, 'invalid "drop_limit"'
end
drop_limit = v
elseif k == "timeout" then
if type(v) ~= "number" or v < 0 then
return nil, 'invalid "timeout"'
end
timeout = v
elseif k == "max_retry_times" then
if type(v) ~= "number" or v < 0 then
return nil, 'invalid "max_retry_times"'
end
max_retry_times = v
elseif k == "retry_interval" then
if type(v) ~= "number" or v < 0 then
return nil, 'invalid "retry_interval"'
end
-- ngx.sleep time is in seconds
retry_interval = v
elseif k == "pool_size" then
if type(v) ~= "number" or v < 0 then
return nil, 'invalid "pool_size"'
end
pool_size = v
elseif k == "max_buffer_reuse" then
if type(v) ~= "number" or v < 0 then
return nil, 'invalid "max_buffer_reuse"'
end
max_buffer_reuse = v
elseif k == "periodic_flush" then
if type(v) ~= "number" or v < 0 then
return nil, 'invalid "periodic_flush"'
end
periodic_flush = v
elseif k == "ssl" then
if type(v) ~= "boolean" then
return nil, '"ssl" must be a boolean value'
end
ssl = v
elseif k == "ssl_verify" then
if type(v) ~= "boolean" then
return nil, '"ssl_verify" must be a boolean value'
end
ssl_verify = v
elseif k == "sni_host" then
if type(v) ~= "string" then
return nil, '"sni_host" must be a string'
end
sni_host = v
end
end
Expand Down
78 changes: 77 additions & 1 deletion t/sanity.t
Expand Up @@ -5,7 +5,7 @@ use Cwd qw(cwd);

repeat_each(2);

plan tests => repeat_each() * (blocks() * 4 + 4);
plan tests => repeat_each() * (blocks() * 4 + 17);
our $HtmlDir = html_dir;

our $pwd = cwd();
Expand Down Expand Up @@ -838,3 +838,79 @@ wrote bytes: 77
--- error_log
Message received: Hello SSL
SNI Host: nil
=== TEST 20: Test arguments
--- http_config eval: $::HttpConfig
--- config
location /t {
content_by_lua 'ngx.say("foo")';
log_by_lua '
local logger = require "resty.logger.socket"
local ok, err = logger.init{ host = 128, port = 29999 }
ngx.log(ngx.ERR, err)
local ok, err = logger.init{ host = "google.com", port = "foo" }
ngx.log(ngx.ERR, err)
local ok, err = logger.init{ host = "google.com", port = 1234567 }
ngx.log(ngx.ERR, err)
local ok, err = logger.init{ path = 123 }
ngx.log(ngx.ERR, err)
local ok, err = logger.init{ path = "/test.sock", flush_limit = "a" }
ngx.log(ngx.ERR, err)
local ok, err = logger.init{ path = "/test.sock", drop_limit = -2.5 }
ngx.log(ngx.ERR, err)
local ok, err = logger.init{ path = "/test.sock", timeout = "bar" }
ngx.log(ngx.ERR, err)
local ok, err = logger.init{ path = "/test.sock", max_retry_times = "bar" }
ngx.log(ngx.ERR, err)
local ok, err = logger.init{ path = "/test.sock", retry_interval = "bar" }
ngx.log(ngx.ERR, err)
local ok, err = logger.init{ path = "/test.sock", pool_size = "bar" }
ngx.log(ngx.ERR, err)
local ok, err = logger.init{ path = "/test.sock", max_buffer_reuse = "bar" }
ngx.log(ngx.ERR, err)
local ok, err = logger.init{ path = "/test.sock", periodic_flush = "bar" }
ngx.log(ngx.ERR, err)
local ok, err = logger.init{ path = "/test.sock", ssl = "1" }
ngx.log(ngx.ERR, err)
local ok, err = logger.init{ path = "/test.sock", ssl_verify = 2 }
ngx.log(ngx.ERR, err)
local ok, err = logger.init{ path = "/test.sock", sni_host = true }
ngx.log(ngx.ERR, err)
';
}
--- request
GET /t?a=1&b=2
--- error_log
"host" must be a string
"port" must be a number
"port" out of range 0~65535
"path" must be a string
invalid "flush_limit"
invalid "drop_limit"
invalid "timeout"
invalid "max_retry_times"
invalid "retry_interval"
invalid "pool_size"
invalid "max_buffer_reuse"
invalid "periodic_flush"
"ssl" must be a boolean value
"ssl_verify" must be a boolean value
"sni_host" must be a string
--- response_body
foo

0 comments on commit 8986459

Please sign in to comment.