xref: /openbsd-src/gnu/usr.bin/perl/cpan/IPC-SysV/t/shm.t (revision eac174f2741a08d8deb8aae59a7f778ef9b5d770)
1################################################################################
2#
3#  Version 2.x, Copyright (C) 2007-2013, Marcus Holland-Moritz <mhx@cpan.org>.
4#  Version 1.x, Copyright (C) 1999, Graham Barr <gbarr@pobox.com>.
5#
6#  This program is free software; you can redistribute it and/or
7#  modify it under the same terms as Perl itself.
8#
9################################################################################
10
11use strict;
12use warnings;
13
14our %Config;
15BEGIN {
16  if ($ENV{'PERL_CORE'}) {
17    chdir 't' if -d 't';
18    @INC = '../lib' if -d '../lib' && -d '../ext';
19  }
20
21  require Test::More; Test::More->import;
22  require Config; Config->import;
23
24  if ($ENV{'PERL_CORE'} && $Config{'extensions'} !~ m[\bIPC/SysV\b]) {
25    plan(skip_all => 'IPC::SysV was not built');
26  }
27}
28
29if ($Config{'d_shm'} ne 'define') {
30  plan(skip_all => '$Config{d_shm} undefined');
31}
32
33use IPC::SysV qw( IPC_PRIVATE S_IRWXU );
34use IPC::SharedMem;
35
36my $shm = sub {
37  my $code = shift;
38  if (exists $SIG{SYS}) {
39    local $SIG{SYS} = sub { plan(skip_all => "SIGSYS caught") };
40    return $code->();
41  }
42  return $code->();
43}->(sub { IPC::SharedMem->new(IPC_PRIVATE, 8, S_IRWXU) });
44
45unless (defined $shm) {
46  my $info = "IPC::SharedMem->new failed: $!";
47  if ($! == &IPC::SysV::ENOSPC || $! == &IPC::SysV::ENOSYS ||
48      $! == &IPC::SysV::ENOMEM || $! == &IPC::SysV::EACCES) {
49    plan(skip_all => $info);
50  }
51  else {
52    die $info;
53  }
54}
55
56plan(tests => 23);
57
58pass('acquired shared mem');
59
60my $st = $shm->stat;
61
62ok($st, 'stat it');
63is($st->nattch, 0, 'st->nattch');
64is($st->cpid, $$, 'cpid');
65ok($st->segsz >= 8, 'segsz');
66
67ok($shm->write(pack("N", 4711), 0, 4), 'write(offs=0)');
68ok($shm->write(pack("N", 210577), 4, 4), 'write(offs=4)');
69
70is($shm->read(0, 4), pack("N", 4711), 'read(offs=0)');
71is($shm->read(4, 4), pack("N", 210577), 'read(offs=4)');
72
73ok($shm->attach, 'attach');
74
75$st = $shm->stat;
76
77ok($st, 'stat it');
78is($st->nattch, 1, 'st->nattch');
79is($st->cpid, $$, 'lpid');
80
81is($shm->read(0, 4), pack("N", 4711), 'read(offs=0)');
82is($shm->read(4, 4), pack("N", 210577), 'read(offs=4)');
83
84ok($shm->write("Shared", 1, 6), 'write(offs=1)');
85
86ok(!$shm->is_removed, '!is_removed');
87ok($shm->remove, 'remove');
88ok($shm->is_removed, 'is_removed');
89
90is($shm->read(1, 6), 'Shared', 'read(offs=1)');
91ok($shm->write("Memory", 0, 6), 'write(offs=0)');
92is(unpack("P6", $shm->addr), 'Memory', 'read using unpack');
93
94ok($shm->detach, 'detach');
95
96