aboutsummaryrefslogtreecommitdiffstats
path: root/t/strip_and_check_elf_files.t
blob: 1173c6c1b3c0f26ef8e975d3139a6cd282cb74b3 (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
#!/usr/bin/perl
# $Id: gprintify 257533 2009-05-23 12:45:15Z guillomovitch $

use strict;
use warnings;
use Test::More;
use File::Temp qw/tempdir/;
use File::Path qw/make_path/;
use FindBin qw/$Bin/;
use lib "$Bin/../lib";
use Digest::MD5;

plan tests => 2;

# test the script itself
my ($buildroot, $binary, $before, $after);

($buildroot, $binary) = setup();

$before = get_md5($binary);
run($buildroot);
$after = get_md5($binary);

isnt(
    $before,
    $after,
    'binary should be modified'
);

($buildroot, $binary) = setup();

$before = get_md5($binary);
$ENV{EXCLUDE_FROM_STRIP} = 'test';
run($buildroot);
$after = get_md5($binary);

is(
    $before,
    $after,
    'EXCLUDE_FROM_STRIP should prevent binary stripping'
);

sub setup {

    my $source = File::Temp->new(UNLINK => 1, SUFFIX => '.c');
    print $source <<'EOF';
#include <stdio.h>
 
int main()
{
    printf("Hello world!\n");
    return 0;
}
EOF
    close($source);

    my $buildroot = tempdir(CLEANUP => ($ENV{TEST_DEBUG} ? 0 : 1));
    my $bindir = $buildroot . '/usr/bin';
    my $binary = $bindir . '/test';
    make_path($bindir);

    system('gcc', '-o', $binary, $source);

    return ($buildroot, $binary);
}

sub run {
    my ($buildroot) = @_;

    $ENV{RPM_BUILD_ROOT} = $buildroot;
    system("$Bin/../strip_and_check_elf_files");
}

sub get_md5 {
    my ($file) = @_;
    open(my $in, '<', $file) or die "can't read $file: $!";
    binmode($in);
    my $md5 = Digest::MD5->new();
    $md5->addfile($in);
    close($in);
    return $md5->hexdigest();
}