File: testmodule.t

package info (click to toggle)
libhttp-daemon-ssl-perl 1.05-01-5
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 824 kB
  • sloc: perl: 190; makefile: 19; sh: 2
file content (145 lines) | stat: -rw-r--r-- 3,724 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
#!perl -w
# Before `make install' is performed this script should be runnable with
# `make test'. After `make install' it should work as `perl t/testmodule.t'

use HTTP::Daemon::SSL;
use HTTP::Status;
use IO::Socket::SSL::Utils;
eval {require "t/ssl_settings.req";} ||
eval {require "ssl_settings.req";};

$numtests = 14;
$|=1;
$SIG{PIPE}='IGNORE';

foreach ($^O) {
    if (/MacOS/ or /VOS/ or /vmesa/ or /riscos/ or /amigaos/) {
	print "1..0 # Skipped: fork not implemented on this platform\n";
	exit;
    }
}

print "1..$numtests\n";

$test = 0;

my ($ca_cert, $ca_key) = CERT_create(
    subject => { commonName => 'Dummy IO::Socket::SSL Certificate Authority' },
    CA => 1,
);
(defined $ca_cert and defined $ca_key) || print 'not ';
&ok('authority certificate generated');

my ($server_cert, $server_key) = CERT_create(
    subject => { commonName => 'IO::Socket::SSL Dummy Server Certificate' },
    subjectAltNames => [ [ IP => '127.0.0.1' ] ],
    CA => 0,
    issuer_cert => $ca_cert,
    issuer_key => $ca_key,
);
(defined $server_cert and defined $server_key) || print 'not ';
&ok('server certificate generated');

eval { PEM_cert2file($ca_cert, 'certs/test-ca.pem') };
(!$@ and -s 'certs/test-ca.pem') || print 'not ';
&ok('authority certificate saved');

PEM_cert2file($server_cert, 'certs/server-cert.pem');
(!$@ and -s 'certs/server-cert.pem') || print 'not ';
&ok('server certificate saved');

PEM_key2file($server_key, 'certs/server-key.pem');
(!$@ and -s 'certs/server-key.pem') || print 'not ';
&ok('server key saved');

# freeing fails now <https://bugzilla.redhat.com/show_bug.cgi?id=1097640>
#CERT_free($ca_cert);
#KEY_free($ca_key);
#CERT_free($server_cert);
#KEY_free($server_key);

my $server = new HTTP::Daemon::SSL(
				   LocalAddr => $SSL_SERVER_ADDR,
				   Listen => 5,
				   Timeout => 30,
				   ReuseAddr => 1,
				   SSL_cipher_list => 'ALL:!LOW',
				   SSL_verify_mode => 0x00,
				   SSL_ca_file => "certs/test-ca.pem",
				   SSL_key_file => "certs/server-key.pem",
				   SSL_cert_file => "certs/server-cert.pem");

if (!$server) {
    print "not ok $test\n";
    exit;
}

$SSL_SERVER_PORT = $server->sockport;
&ok("server init port=$SSL_SERVER_PORT");


unless (fork) {
    sleep 1;

    my $client = new IO::Socket::INET(PeerAddr => $SSL_SERVER_ADDR,
				      PeerPort => $SSL_SERVER_PORT);

    print $client "GET / HTTP/1.0\r\n\r\n";
    (<$client> eq "HTTP/1.1 400 Bad Request\r\n") || print "not ";
    &ok("client bad connection test");
    my @ary = <$client>;
    close $client;

    $client = new IO::Socket::SSL(PeerAddr => $SSL_SERVER_ADDR,
				  PeerPort => $SSL_SERVER_PORT,
				  SSL_verify_mode => 0x01,
				  SSL_ca_file => "certs/test-ca.pem");

    $client || (print("not ok #client failure\n") && exit);
    &ok("client good connection test");

    print $client "GET /foo HTTP/1.0\r\n\r\n";

    (<$client> eq "HTTP/1.1 403 Forbidden\r\n") || print "not ";
    &ok("client permission test");
    @ary = <$client>;

    exit(0);
}


print "not " if (!defined fileno($server));
&ok("server fileno");

print "not " unless ($server->url =~ m!^https:!);
&ok("server url test");

my $conn;
if (!($conn = $server->accept)) {
    # first client request is a bad request
    &ok("bad request handled");
} else {
    print "not ok $test # bad request returned a socket\n";
}

if ($conn = $server->accept) {
    &ok("valid request handled");
} else {
    print "not ok $test # valid request did not return a socket\n";
}

my $r = $conn->get_request();

unless ($r->method eq 'GET' and $r->url->path eq '/foo') {
    print "not ";
}
&ok("server method processing");

$conn->send_error(RC_FORBIDDEN);

close $conn;
wait;

sub ok {
    print "ok #$_[0] ", ++$test, "\n"; 
}