10 our @EXPORT_OK = qw(mailgroup dynamic_socket exim_binary);
15 use List::Util qw'shuffle';
19 Exim::Runtest - helper functions for the runtest script
24 my $foo = Exim::Runtest::foo('foo');
28 The B<Exim::Runtest> module provides some simple functions
29 for the F<runtest> script. No functions are exported yet.
34 my $group = shift // croak "Need a default group name.";
36 croak "Need a group *name*, not a numeric group id."
39 return $group if getgrnam $group;
42 setgrent or die "setgrent: $!\n";
43 push @groups, $_ while defined($_ = getgrent);
45 return (shuffle @groups)[0];
50 for (my $port = 1024; $port < 65000; $port++) {
51 $socket = IO::Socket::INET->new(
52 LocalHost => '127.0.0.1',
58 croak 'Can not allocate a free port.';
63 # two simple cases, absolute path or relative path and executable
64 return @_ if $_[0] =~ /^\//;
65 return Cwd::abs_path(shift), @_ if -x $_[0];
67 # so we're still here, if the simple approach didn't help.
69 # if there is '../exim-snapshot/<build-dir>/exim', use this
70 # if there is '../exim4/<build-dir>/exim', use this
71 # if there is '../exim-*.*/<build-dir>/exim', use the one with the highest version
72 # 4.84 < 4.85RC1 < 4.85RC2 < 4.85 < 4.86RC1 < … < 4.86
73 # if there is '../src/<build-dir>', use this
76 my $prefix = '..'; # was intended for testing.
78 # get a list of directories having the "scripts/{os,arch}-type"
80 my @candidates = grep { -x "$_/scripts/os-type" and -x "$_/scripts/arch-type" }
81 "$prefix/exim-snapshot", "$prefix/exim4", # highest priority
82 (reverse sort { # list of exim-*.* directories
83 # split version number from RC number
84 my @a = ($a =~ /(\d+\.\d+)(?:RC(\d+))?/);
85 my @b = ($b =~ /(\d+\.\d+)(?:RC(\d+))?/);
86 # if the versions are not equal, we're fine,
87 # but otherwise we've to compare the RC number, where an
88 # empty RC number is better than a non-empty
89 ($a[0] cmp $b[0]) || (defined $a[1] ? defined $b[1] ? $a[1] cmp $b[1] : -1 : 1)
90 } glob "$prefix/exim-*.*"),
91 "$prefix/src"; # the "normal" source
93 # binaries should be found now depending on the os-type and
94 # arch-type in the directories we got above
95 my @binaries = grep { -x }
96 map { ("$_/exim", "$_/exim4") }
98 my $os = `$_/scripts/os-type`;
99 my $arch = `$_/scripts/arch-type`;
101 "$_/build-$os-$arch" . ($ENV{EXIM_BUILD_SUFFIX} ? ".$ENV{EXIM_BUILD_SUFFIX}" : '');
104 return $binaries[0], @_;
116 =item B<mailgroup>(I<$default>)
118 Check if the mailgroup I<$default> exists. Return the checked
119 group name or some other random but existing group.
121 =item B<dynamic_socket>()
123 Return a dynamically allocated listener socket in the range
124 between 1024 and 65534;
126 =item ($binary, @argv) = B<exim_binary>(I<@argv>)
128 Find the Exim binary. Consider the first element of I<@argv>
129 and remove it from I<@argv>, if it is an executable binary.
130 Otherwise search the binary (while honouring C<EXIM_BUILD_SUFFIX>,
131 C<../scripts/os-type> and C<../os-arch>) and return the
132 the path to the binary and the unmodified I<@argv>.