Skip to content

Commit c7b43a9

Browse files
committed
nginx-ssl-ja3: tests
1 parent cdab044 commit c7b43a9

File tree

4 files changed

+149
-16
lines changed

4 files changed

+149
-16
lines changed

.travis.yml

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -22,32 +22,31 @@ before_install:
2222
- sudo apt-get install -qq -y software-properties-common
2323
- sudo add-apt-repository "deb http://us.archive.ubuntu.com/ubuntu/ xenial main universe"
2424
- sudo apt-get update -qq -y --fix-missing
25-
- sudo apt-get install -qq -y --fix-missing cpanminus mercurial build-essential make clang valgrind
25+
- sudo apt-get install -qq -y --fix-missing cpanminus mercurial build-essential make clang valgrind libio-socket-ssl-perl
2626

2727
install:
2828
- if [ ! -d /opt ]; then mkdir /opt; fi
29-
- git clone https://github.com/openresty/test-nginx.git
29+
- git clone https://github.com/nginx/nginx-tests
3030
- hg clone http://hg.nginx.org/nginx
3131
- git clone https://github.com/openssl/openssl
3232

3333
script:
34-
- ls -la
3534
- cd openssl
3635
- ./config -d
3736
- make -j$JOBS > build.log 2>&1 || (cat build.log && exit 1)
3837
- sudo make install > build.log 2>&1 || (cat build.log && exit 1)
3938
- cd ..
40-
- cd test-nginx
41-
- sudo cpanm .
42-
- cd ..
43-
- export LD_LIBRARY_PATH=/usr/local/lib/
4439
- cp -v docker/debian-nginx-ssl-ja3/nginx.ssl.extensions.patch nginx/.
4540
- cd nginx
4641
- patch -p1 < nginx.ssl.extensions.patch
47-
- auto/configure --with-debug --with-stream --with-ld-opt="-Wl,-E" --prefix=$NGINX_PREFIX --with-http_ssl_module --with-stream_ssl_module --add-module=$PWD/.. > build.log 2>&1 || (cat build.log && exit 1)
42+
- auto/configure --with-debug --with-stream --with-ld-opt="-Wl,-E -L /usr/local/lib" --prefix=$NGINX_PREFIX --with-http_ssl_module --with-stream_ssl_module --add-module=.. > build.log 2>&1 || (cat build.log && exit 1)
4843
- make -j$JOBS > build.log 2>&1 || (cat build.log && exit 1)
4944
- sudo make install > build.log 2>&1 || (cat build.log && exit 1)
50-
- cd ..
5145
- export PATH=$NGINX_PREFIX/sbin:$PATH
46+
- export LD_LIBRARY_PATH=/usr/local/lib
5247
- /opt/nginx/sbin/nginx -V
5348
- ldd /opt/nginx/sbin/nginx
49+
- cd ../t
50+
- ln -sf ../nginx-tests/lib/ lib
51+
- cd ..
52+
- TEST_NGINX_BINARY=/opt/nginx/sbin/nginx prove -v

README.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,12 @@ $ make && make install
8787
```
8888
## Tests
8989

90-
Not available yet.
90+
Make sure that the lib directory for nginx-tests is available in the 't' directory.
91+
92+
93+
```
94+
$ TEST_NGINX_BINARY=/usr/local/nginx/sbin/nginx prove -v
95+
```
9196

9297
## Docker
9398

docker/debian-nginx-ssl-ja3/Dockerfile

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,9 @@ RUN cd nginx-ssl-ja3 && git checkout ${GIT_BRANCH}
6262

6363
WORKDIR /build
6464

65+
# Get test framework
66+
RUN git clone https://github.com/nginx/nginx-tests
67+
6568
# Get openssl master from git
6669
RUN git clone https://github.com/openssl/openssl
6770

@@ -81,12 +84,6 @@ COPY nginx.ssl.extensions.patch /build/nginx
8184
RUN cat nginx.ssl.extensions.patch
8285
RUN patch -p1 < nginx.ssl.extensions.patch
8386

84-
# Get test framework
85-
RUN git clone https://github.com/openresty/test-nginx.git
86-
87-
# Install test framework and dependencies
88-
RUN cd test-nginx/ && cpanm .
89-
9087
# Configure, make and install
9188
RUN ./auto/configure --add-module=/build/nginx-ssl-ja3 --with-http_ssl_module --with-stream_ssl_module --with-stream --with-debug --with-ld-opt="-L/usr/local/lib -Wl,-E"
9289
RUN make install

t/http_ssl_ja3.t

Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
#!/usr/bin/perl
2+
3+
# (C) Sergey Kandaurov
4+
# (C) Nginx, Inc.
5+
# (C) Paulo Pacheco
6+
7+
# Tests for SSL/TLS ja3 fingerprint
8+
9+
###############################################################################
10+
11+
use warnings;
12+
use strict;
13+
14+
use Test::More;
15+
16+
BEGIN { use FindBin; chdir($FindBin::Bin); }
17+
18+
use lib 'lib';
19+
use Test::Nginx;
20+
use Data::Dumper;
21+
22+
###############################################################################
23+
24+
select STDERR; $| = 1;
25+
select STDOUT; $| = 1;
26+
27+
eval { require IO::Socket::SSL; };
28+
plan(skip_all => 'IO::Socket::SSL not installed') if $@;
29+
eval { IO::Socket::SSL::SSL_VERIFY_NONE(); };
30+
plan(skip_all => 'IO::Socket::SSL too old') if $@;
31+
32+
my $t = Test::Nginx->new()->has_daemon('openssl')->plan(1);
33+
34+
$t->write_file_expand('nginx.conf', <<'EOF');
35+
36+
%%TEST_GLOBALS%%
37+
38+
daemon off;
39+
40+
events {
41+
}
42+
43+
http {
44+
%%TEST_GLOBALS_HTTP%%
45+
46+
server {
47+
listen 127.0.0.1:8080 ssl;
48+
server_name localhost;
49+
50+
ssl_certificate_key localhost.key;
51+
ssl_certificate localhost.crt;
52+
53+
location /ja3 {
54+
return 200 $http_ssl_ja3_hash;
55+
}
56+
}
57+
}
58+
59+
EOF
60+
61+
$t->write_file('openssl.conf', <<EOF);
62+
[ req ]
63+
default_bits = 1024
64+
encrypt_key = no
65+
distinguished_name = req_distinguished_name
66+
[ req_distinguished_name ]
67+
EOF
68+
69+
my $d = $t->testdir();
70+
71+
foreach my $name ('localhost') {
72+
system('openssl req -x509 -new '
73+
. "-config '$d/openssl.conf' -subj '/CN=$name/' "
74+
. "-out '$d/$name.crt' -keyout '$d/$name.key' "
75+
. ">>$d/openssl.out 2>&1") == 0
76+
or die "Can't create certificate for $name: $!\n";
77+
}
78+
79+
my $ctx = new IO::Socket::SSL::SSL_Context(
80+
SSL_verify_mode => IO::Socket::SSL::SSL_VERIFY_NONE(),
81+
SSL_session_cache_size => 100);
82+
83+
84+
$t->run();
85+
86+
###############################################################################
87+
88+
like(get('/ja3', 8080), qr/.*[0-9a-f]{32}/m, 'http_ssl_ja3 var is returned');
89+
90+
91+
###############################################################################
92+
93+
sub get {
94+
my ($uri, $port) = @_;
95+
my $s = get_ssl_socket($ctx, port($port)) or return;
96+
my $r = http_get($uri, socket => $s);
97+
98+
$s->close();
99+
return $r;
100+
}
101+
102+
sub get_ssl_socket {
103+
my ($ctx, $port, %extra) = @_;
104+
my $s;
105+
106+
eval {
107+
local $SIG{ALRM} = sub { die "timeout\n" };
108+
local $SIG{PIPE} = sub { die "sigpipe\n" };
109+
alarm(2);
110+
$s = IO::Socket::SSL->new(
111+
Proto => 'tcp',
112+
PeerAddr => '127.0.0.1',
113+
PeerPort => $port,
114+
SSL_verify_mode => IO::Socket::SSL::SSL_VERIFY_NONE(),
115+
SSL_reuse_ctx => $ctx,
116+
SSL_error_trap => sub { die $_[1] },
117+
%extra
118+
);
119+
alarm(0);
120+
};
121+
alarm(0);
122+
123+
if ($@) {
124+
log_in("died: $@");
125+
return undef;
126+
}
127+
128+
return $s;
129+
}
130+
131+
132+
###############################################################################

0 commit comments

Comments
 (0)