summaryrefslogtreecommitdiff
path: root/ext/Socket/t/Socket.t
blob: 326fa7a9effbd0995139d26bc9ea99c91e3961f3 (plain)
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
#!./perl

BEGIN {
    require Config; import Config;
    if ($Config{'extensions'} !~ /\bSocket\b/ && 
        !(($^O eq 'VMS') && $Config{d_socket})) {
	print "1..0\n";
	exit 0;
    }
    $has_alarm = $Config{d_alarm};
}
	
use Socket qw(:all);

print "1..26\n";

$has_echo = $^O ne 'MSWin32';
$alarmed = 0;
sub arm      { $alarmed = 0; alarm(shift) if $has_alarm }
sub alarmed  { $alarmed = 1 }
$SIG{ALRM} = 'alarmed'                    if $has_alarm;

if (socket(T, PF_INET, SOCK_STREAM, IPPROTO_TCP)) {
  print "ok 1\n";
  
  arm(5);
  my $host = $^O eq 'MacOS' || ($^O eq 'irix' && $Config{osvers} == 5) ?
                 '127.0.0.1' : 'localhost';
  my $localhost = inet_aton($host);

  if ($has_echo && defined $localhost && connect(T,pack_sockaddr_in(7,$localhost))){
	arm(0);

	print "ok 2\n";

	print "# Connected to " .
		inet_ntoa((unpack_sockaddr_in(getpeername(T)))[1])."\n";

	arm(5);
	syswrite(T,"hello",5);
	arm(0);

	arm(5);
	$read = sysread(T,$buff,10);	# Connection may be granted, then closed!
	arm(0);

	while ($read > 0 && length($buff) < 5) {
	    # adjust for fact that TCP doesn't guarantee size of reads/writes
	    arm(5);
	    $read = sysread(T,$buff,10,length($buff));
	    arm(0);
	}
	print(($read == 0 || $buff eq "hello") ? "ok 3\n" : "not ok 3\n");
  }
  else {
	print "# You're allowed to fail tests 2 and 3 if\n";
	print "# the echo service has been disabled or if your\n";
        print "# gethostbyname() cannot resolve your localhost.\n";
	print "# 'Connection refused' indicates disabled echo service.\n";
	print "# 'Interrupted system call' indicates a hanging echo service.\n";
	print "# Error: $!\n";
	print "ok 2 - skipped\n";
	print "ok 3 - skipped\n";
  }
}
else {
	print "# Error: $!\n";
	print "not ok 1\n";
}

if( socket(S, PF_INET,SOCK_STREAM, IPPROTO_TCP) ){
  print "ok 4\n";

  arm(5);
  if ($has_echo && connect(S,pack_sockaddr_in(7,INADDR_LOOPBACK))){
        arm(0);

	print "ok 5\n";

	print "# Connected to " .
		inet_ntoa((unpack_sockaddr_in(getpeername(S)))[1])."\n";

	arm(5);
	syswrite(S,"olleh",5);
	arm(0);

	arm(5);
	$read = sysread(S,$buff,10);	# Connection may be granted, then closed!
	arm(0);

	while ($read > 0 && length($buff) < 5) {
	    # adjust for fact that TCP doesn't guarantee size of reads/writes
	    arm(5);
	    $read = sysread(S,$buff,10,length($buff));
	    arm(0);
	}
	print(($read == 0 || $buff eq "olleh") ? "ok 6\n" : "not ok 6\n");
  }
  else {
	print "# You're allowed to fail tests 5 and 6 if\n";
	print "# the echo service has been disabled.\n";
	print "# 'Interrupted system call' indicates a hanging echo service.\n";
	print "# Error: $!\n";
	print "ok 5 - skipped\n";
	print "ok 6 - skipped\n";
  }
}
else {
	print "# Error: $!\n";
	print "not ok 4\n";
}

# warnings
$SIG{__WARN__} = sub {
    ++ $w if $_[0] =~ /^6-ARG sockaddr_in call is deprecated/ ;
} ;
$w = 0 ;
sockaddr_in(1,2,3,4,5,6) ;
print ($w == 1 ? "not ok 7\n" : "ok 7\n") ;
use warnings 'Socket' ;
sockaddr_in(1,2,3,4,5,6) ;
print ($w == 1 ? "ok 8\n" : "not ok 8\n") ;

# Thest that whatever we give into pack/unpack_sockaddr retains
# the value thru the entire chain.
if((inet_ntoa((unpack_sockaddr_in(pack_sockaddr_in(100,inet_aton("10.250.230.10"))))[1])) eq '10.250.230.10') {
    print "ok 9\n"; 
} else {
    print "not ok 9\n"; 
}
print ((inet_ntoa(inet_aton("10.20.30.40")) eq "10.20.30.40") ? "ok 10\n" : "not ok 10\n");
print ((inet_ntoa(v10.20.30.40) eq "10.20.30.40") ? "ok 11\n" : "not ok 11\n");
{
    my ($port,$addr) = unpack_sockaddr_in(pack_sockaddr_in(100,v10.10.10.10));
    print (($port == 100) ? "ok 12\n" : "not ok 12\n");
    print ((inet_ntoa($addr) eq "10.10.10.10") ? "ok 13\n" : "not ok 13\n");
}
				     
eval { inet_ntoa(v10.20.30.400) };
print (($@ =~ /^Wide character in Socket::inet_ntoa at/) ? "ok 14\n" : "not ok 14\n");

if (sockaddr_family(pack_sockaddr_in(100,inet_aton("10.250.230.10"))) == AF_INET) {
    print "ok 15\n";
} else {
    print "not ok 15\n";
}

eval { sockaddr_family("") };
print (($@ =~ /^Bad arg length for Socket::sockaddr_family, length is 0, should be at least \d+/) ? "ok 16\n" : "not ok 16\n");

if ($^O eq 'linux') {
    # see if we can handle abstract sockets
    my $test_abstract_socket = chr(0) . '/org/perl/hello'. chr(0) . 'world';
    my $addr = sockaddr_un ($test_abstract_socket);
    my ($path) = sockaddr_un ($addr);
    if ($test_abstract_socket eq $path) {
        print "ok 17\n";
    }
    else {
	$path =~ s/\0/\\0/g;
	print "# got <$path>\n";
        print "not ok 17\n";
    }

    # see if we calculate the address structure length correctly
    if (length ($test_abstract_socket) + 2 == length $addr) {
        print "ok 18\n";
    } else {
	print "# got ".(length $addr)."\n";
        print "not ok 18\n";
    }

} else {
    # doesn't have abstract socket support
    print "ok 17 - skipped on this platform\n";
    print "ok 18 - skipped on this platform\n";
}

if($Config{d_inetntop} && $Config{d_inetaton}){
    print ((inet_ntop(AF_INET, inet_pton(AF_INET, "10.20.30.40")) eq "10.20.30.40") ? "ok 19\n" : "not ok 19\n");
    print ((inet_ntop(AF_INET, inet_aton("10.20.30.40")) eq "10.20.30.40") ? "ok 20\n" : "not ok 20\n");
    if(defined eval { AF_INET6() } ) {
        print (lc(inet_ntop(AF_INET6, inet_pton(AF_INET6, "2001:503:BA3E::2:30")) eq "2001:503:ba3e::2:30") ? "ok 21\n" : "not ok 21\n");
    }
    else {
        print "ok 21 - skipped - no AF_INET6\n";
    }
} else {
    # no IPv6 
    print "ok $_ - skipped on this platform\n" for 19 .. 21;
}

if(defined eval { AF_INET6() } ) {
   my $sin6 = pack_sockaddr_in6(0x1234, "0123456789abcdef", 0, 89);

   print "not " unless sockaddr_family($sin6) == AF_INET6;
   print "ok 22\n";

   print "not " unless (unpack_sockaddr_in6($sin6))[0] == 0x1234;
   print "ok 23\n";

   print "not " unless (unpack_sockaddr_in6($sin6))[1] == "0123456789abcdef";
   print "ok 24\n";

   print "not " unless (unpack_sockaddr_in6($sin6))[2] == 0;
   print "ok 25\n";

   print "not " unless (unpack_sockaddr_in6($sin6))[3] == 89;
   print "ok 26\n";
}
else {
   print "ok $_ - skipped - no AF_INET6\n" for 22 .. 26;
}