]> git.kaiwu.me - njs.git/commitdiff
Tests: distinguish WebCrypto absence from probe failures
authorDmitry Volyntsev <xeioex@nginx.com>
Sat, 24 Oct 2026 05:45:29 +0000 (22:45 -0700)
committerDmitry Volyntsev <xeioexception@gmail.com>
Thu, 19 Nov 2026 06:44:10 +0000 (22:44 -0800)
The capability guards introduced in 4ea1356a skipped whenever the HTTP
response did not contain "true".  A startup error, HTTP 500 or malformed
response could therefore hide a regression.  Referencing an absent
crypto global also raised ReferenceError instead of returning false.

Use typeof to check the global and accept only a successful response
with an exact boolean body.  Probe WebCrypto through stream in the
stream test, independently of HTTP engine selection.  Treat startup
and malformed probe responses as failures.

Require WebCrypto when the existing CI_SSL setting is yes.  Keep a
capability skip for a valid false response outside SSL-enabled CI,
so local builds without crypto remain supported.

nginx/t/js_webcrypto.t
nginx/t/stream_js_webcrypto.t

index 94f916a46476070f56d3f6fb62cd75bc52ef87e3..9ebcd241879b58a28d838cd01ff2436670d55250 100644 (file)
@@ -59,7 +59,7 @@ EOF
 
 $t->write_file('test.js', <<EOF);
     function has_crypto(r) {
-        r.return(200, (crypto !== undefined).toString());
+        r.return(200, (typeof crypto !== 'undefined').toString());
     }
 
     function count1(v) {
@@ -151,10 +151,21 @@ $t->write_file('test.js', <<EOF);
 
 EOF
 
-$t->try_run('no njs');
+$t->run();
 
-plan(skip_all => 'njs crypto module not available')
-       if http_get('/has_crypto') !~ /true/;
+my $response = http_get('/has_crypto');
+my ($headers, $has_crypto) = split(/\x0d\x0a\x0d\x0a/, $response // '', 2);
+
+die "invalid WebCrypto capability response\n"
+       unless defined $has_crypto && $headers =~ /\AHTTP\/1\.[01] 200 /
+       && $has_crypto =~ /\A(?:true|false)\z/;
+
+if ($has_crypto eq 'false') {
+       die "WebCrypto is unavailable in an SSL-enabled CI job\n"
+               if defined $ENV{CI_SSL} && $ENV{CI_SSL} eq 'yes';
+
+       plan(skip_all => 'njs crypto module not available');
+}
 
 $t->plan(4);
 
index 0d9272c7c41d1ab82b6454e1b2686e5d91033a42..286486de19a9cd9454ad57bbf7afb56c05c280c4 100644 (file)
@@ -23,7 +23,7 @@ use Test::Nginx::Stream qw/ stream /;
 select STDERR; $| = 1;
 select STDOUT; $| = 1;
 
-my $t = Test::Nginx->new()->has(qw/http stream stream_return/)
+my $t = Test::Nginx->new()->has(qw/stream stream_return/)
        ->write_file_expand('nginx.conf', <<'EOF');
 
 %%TEST_GLOBALS%%
@@ -33,28 +33,19 @@ daemon off;
 events {
 }
 
-http {
-    %%TEST_GLOBALS_HTTP%%
-
-    js_import test.js;
-
-    server {
-        listen       127.0.0.1:8080;
-        server_name  localhost;
-
-        location /has_crypto {
-            js_content test.has_crypto;
-        }
-    }
-}
-
 stream {
     %%TEST_GLOBALS_STREAM%%
 
     js_import test.js;
 
+    js_set $has_crypto test.has_crypto;
     js_set $test test.random_values_test;
 
+    server {
+        listen  127.0.0.1:8080;
+        return  $has_crypto;
+    }
+
     server {
         listen  127.0.0.1:8081;
         return  $test;
@@ -85,8 +76,8 @@ stream {
 EOF
 
 $t->write_file('test.js', <<EOF);
-    function has_crypto(r) {
-        r.return(200, (crypto !== undefined).toString());
+    function has_crypto(s) {
+        return (typeof crypto !== 'undefined').toString();
     }
 
     function count1(v) {
@@ -180,10 +171,19 @@ $t->write_file('test.js', <<EOF);
                     test_ed25519, test_x25519, test_aes_kw};
 EOF
 
-$t->try_run('no stream js_var');
+$t->run();
 
-plan(skip_all => 'njs crypto module not available')
-       if http_get('/has_crypto') !~ /true/;
+my $has_crypto = stream('127.0.0.1:' . port(8080))->read();
+
+die "invalid WebCrypto capability response\n"
+       unless defined $has_crypto && $has_crypto =~ /\A(true|false)\z/;
+
+if ($has_crypto eq 'false') {
+       die "WebCrypto is unavailable in an SSL-enabled CI job\n"
+               if defined $ENV{CI_SSL} && $ENV{CI_SSL} eq 'yes';
+
+       plan(skip_all => 'njs crypto module not available');
+}
 
 $t->plan(4);