Skip to content

Commit 2abdfe2

Browse files
tests: Add tests for ngx_http_lua_ffi_get_ssl_pointer
1 parent cc205f2 commit 2abdfe2

File tree

1 file changed

+123
-0
lines changed

1 file changed

+123
-0
lines changed

t/140-ssl-c-api.t

Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,8 @@ ffi.cdef[[
5757
int ngx_http_lua_ffi_set_priv_key(void *r,
5858
void *cdata, char **err);
5959
60+
void *ngx_http_lua_ffi_get_ssl_pointer(void *r);
61+
6062
void ngx_http_lua_ffi_free_cert(void *cdata);
6163
6264
void ngx_http_lua_ffi_free_priv_key(void *cdata);
@@ -811,3 +813,124 @@ lua ssl server name: "test.com"
811813
--- no_error_log
812814
[error]
813815
[alert]
816+
817+
818+
819+
=== TEST 6: Raw SSL pointer
820+
--- http_config
821+
server {
822+
listen unix:$TEST_NGINX_HTML_DIR/nginx.sock ssl;
823+
server_name test.com;
824+
825+
ssl_certificate_by_lua_block {
826+
collectgarbage()
827+
828+
local ffi = require "ffi"
829+
require "defines"
830+
831+
local r = getfenv(0).__ngx_req
832+
if not r then
833+
ngx.log(ngx.ERR, "no request found")
834+
return
835+
end
836+
837+
local ssl = ffi.C.ngx_http_lua_ffi_get_ssl_pointer(r);
838+
if ssl == nil then
839+
ngx.log(ngx.ERR, "failed to retrieve SSL*")
840+
return
841+
end
842+
843+
ffi.cdef[[
844+
const char *SSL_get_servername(const void *, const int);
845+
]]
846+
local libssl = ffi.load "ssl"
847+
local TLSEXT_NAMETYPE_host_name = 0
848+
local sni = ffi.string(libssl.SSL_get_servername(ssl, TLSEXT_NAMETYPE_host_name))
849+
ngx.log(ngx.ERR, "SNI is ", sni)
850+
}
851+
852+
ssl_certificate ../../cert/test.crt;
853+
ssl_certificate_key ../../cert/test.key;
854+
855+
server_tokens off;
856+
location /foo {
857+
default_type 'text/plain';
858+
content_by_lua_block { ngx.status = 201 ngx.say("foo") ngx.exit(201) }
859+
more_clear_headers Date;
860+
}
861+
}
862+
--- config
863+
server_tokens off;
864+
lua_ssl_trusted_certificate ../../cert/test.crt;
865+
866+
location /t {
867+
content_by_lua_block {
868+
do
869+
local sock = ngx.socket.tcp()
870+
871+
sock:settimeout(2000)
872+
873+
local ok, err = sock:connect("unix:$TEST_NGINX_HTML_DIR/nginx.sock")
874+
if not ok then
875+
ngx.say("failed to connect: ", err)
876+
return
877+
end
878+
879+
ngx.say("connected: ", ok)
880+
881+
local sess, err = sock:sslhandshake(nil, "test.com", true)
882+
if not sess then
883+
ngx.say("failed to do SSL handshake: ", err)
884+
return
885+
end
886+
887+
ngx.say("ssl handshake: ", type(sess))
888+
889+
local req = "GET /foo HTTP/1.0\r\nHost: test.com\r\nConnection: close\r\n\r\n"
890+
local bytes, err = sock:send(req)
891+
if not bytes then
892+
ngx.say("failed to send http request: ", err)
893+
return
894+
end
895+
896+
ngx.say("sent http request: ", bytes, " bytes.")
897+
898+
while true do
899+
local line, err = sock:receive()
900+
if not line then
901+
-- ngx.say("failed to receive response status line: ", err)
902+
break
903+
end
904+
905+
ngx.say("received: ", line)
906+
end
907+
908+
local ok, err = sock:close()
909+
ngx.say("close: ", ok, " ", err)
910+
end -- do
911+
-- collectgarbage()
912+
}
913+
}
914+
915+
--- request
916+
GET /t
917+
--- response_body
918+
connected: 1
919+
ssl handshake: userdata
920+
sent http request: 56 bytes.
921+
received: HTTP/1.1 201 Created
922+
received: Server: nginx
923+
received: Content-Type: text/plain
924+
received: Content-Length: 4
925+
received: Connection: close
926+
received:
927+
received: foo
928+
close: 1 nil
929+
930+
--- error_log
931+
failed to retrieve SSL*
932+
933+
--- no_error_log
934+
SNI is test.com
935+
[error]
936+
[alert]

0 commit comments

Comments
 (0)