|
| 1 | +# vim:set ft= ts=4 sw=4 et fdm=marker: |
| 2 | + |
| 3 | +use Test::Nginx::Socket::Lua 'no_plan'; |
| 4 | +use Cwd qw(cwd); |
| 5 | + |
| 6 | + |
| 7 | +my $pwd = cwd(); |
| 8 | + |
| 9 | +my $use_luacov = $ENV{'TEST_NGINX_USE_LUACOV'} // ''; |
| 10 | + |
| 11 | +our $HttpConfig = qq{ |
| 12 | + lua_package_path "$pwd/t/openssl/?.lua;$pwd/lib/?.lua;$pwd/lib/?/init.lua;;"; |
| 13 | + init_by_lua_block { |
| 14 | + if "1" == "$use_luacov" then |
| 15 | + require 'luacov.tick' |
| 16 | + jit.off() |
| 17 | + end |
| 18 | + _G.myassert = require("helper").myassert |
| 19 | + } |
| 20 | +}; |
| 21 | + |
| 22 | +run_tests(); |
| 23 | + |
| 24 | +__DATA__ |
| 25 | +=== TEST 1: Calculate hmac correctly |
| 26 | +--- http_config eval: $::HttpConfig |
| 27 | +--- config |
| 28 | + location =/t { |
| 29 | + content_by_lua_block { |
| 30 | + if not require("resty.openssl.version").OPENSSL_30 then |
| 31 | + ngx.say("kwUMjYrP0BSJb8cIJvWYoiM1Kc4mQxZOTwSiTTLRhDM=") |
| 32 | + ngx.exit(0) |
| 33 | + end |
| 34 | +
|
| 35 | + local hmac = myassert(require("resty.openssl.mac").new("goose", "HMAC", nil, "sha256")) |
| 36 | +
|
| 37 | + myassert(hmac:update("🦢🦢🦢🦢🦢🦢")) |
| 38 | + ngx.print(ngx.encode_base64(myassert(hmac:final()))) |
| 39 | + } |
| 40 | + } |
| 41 | +--- request |
| 42 | + GET /t |
| 43 | +--- response_body_like eval |
| 44 | +"kwUMjYrP0BSJb8cIJvWYoiM1Kc4mQxZOTwSiTTLRhDM=" |
| 45 | +--- no_error_log |
| 46 | +[error] |
| 47 | + |
| 48 | +=== TEST 2: Update accepts vardiac args |
| 49 | +--- http_config eval: $::HttpConfig |
| 50 | +--- config |
| 51 | + location =/t { |
| 52 | + content_by_lua_block { |
| 53 | + if not require("resty.openssl.version").OPENSSL_30 then |
| 54 | + ngx.say("kwUMjYrP0BSJb8cIJvWYoiM1Kc4mQxZOTwSiTTLRhDM=") |
| 55 | + ngx.exit(0) |
| 56 | + end |
| 57 | +
|
| 58 | + local hmac = myassert(require("resty.openssl.mac").new("goose", "HMAC", nil, "sha256")) |
| 59 | +
|
| 60 | + hmac:update("🦢", "🦢🦢", "🦢🦢", "🦢") |
| 61 | + ngx.print(ngx.encode_base64(hmac:final())) |
| 62 | + } |
| 63 | + } |
| 64 | +--- request |
| 65 | + GET /t |
| 66 | +--- response_body_like eval |
| 67 | +"kwUMjYrP0BSJb8cIJvWYoiM1Kc4mQxZOTwSiTTLRhDM=" |
| 68 | +--- no_error_log |
| 69 | +[error] |
| 70 | + |
| 71 | +=== TEST 3: Final accepts optional arg |
| 72 | +--- http_config eval: $::HttpConfig |
| 73 | +--- config |
| 74 | + location =/t { |
| 75 | + content_by_lua_block { |
| 76 | + if not require("resty.openssl.version").OPENSSL_30 then |
| 77 | + ngx.say("kwUMjYrP0BSJb8cIJvWYoiM1Kc4mQxZOTwSiTTLRhDM=") |
| 78 | + ngx.exit(0) |
| 79 | + end |
| 80 | +
|
| 81 | + local hmac = myassert(require("resty.openssl.mac").new("goose", "HMAC", nil, "sha256")) |
| 82 | +
|
| 83 | + myassert(hmac:update("🦢", "🦢🦢", "🦢🦢")) |
| 84 | + ngx.print(ngx.encode_base64(myassert(hmac:final("🦢")))) |
| 85 | + } |
| 86 | + } |
| 87 | +--- request |
| 88 | + GET /t |
| 89 | +--- response_body_like eval |
| 90 | +"kwUMjYrP0BSJb8cIJvWYoiM1Kc4mQxZOTwSiTTLRhDM=" |
| 91 | +--- no_error_log |
| 92 | +[error] |
| 93 | + |
| 94 | +=== TEST 4: Rejects unknown hash |
| 95 | +--- http_config eval: $::HttpConfig |
| 96 | +--- config |
| 97 | + location =/t { |
| 98 | + content_by_lua_block { |
| 99 | + if not require("resty.openssl.version").OPENSSL_30 then |
| 100 | + ngx.say("mac.new: invalid cipher or digest type") |
| 101 | + ngx.exit(0) |
| 102 | + end |
| 103 | + local hmac, err = require("resty.openssl.mac").new("goose", "HMAC", nil, "sha257") |
| 104 | + ngx.print(err) |
| 105 | + } |
| 106 | + } |
| 107 | +--- request |
| 108 | + GET /t |
| 109 | +--- response_body_like eval |
| 110 | +"mac.new: invalid cipher or digest type.*" |
| 111 | +--- no_error_log |
| 112 | +[error] |
| 113 | + |
| 114 | +=== TEST 5: Returns provider |
| 115 | +--- http_config eval: $::HttpConfig |
| 116 | +--- config |
| 117 | + location =/t { |
| 118 | + content_by_lua_block { |
| 119 | + if not require("resty.openssl.version").OPENSSL_30 then |
| 120 | + ngx.say("default") |
| 121 | + ngx.exit(0) |
| 122 | + end |
| 123 | +
|
| 124 | + local mac = require("resty.openssl.mac") |
| 125 | + local m = myassert(mac.new("goose", "HMAC", nil, "sha256")) |
| 126 | + ngx.say(myassert(m:get_provider_name())) |
| 127 | + } |
| 128 | + } |
| 129 | +--- request |
| 130 | + GET /t |
| 131 | +--- response_body |
| 132 | +default |
| 133 | +--- no_error_log |
| 134 | +[error] |
0 commit comments