summaryrefslogtreecommitdiffstats
path: root/tests/test-lz4-abi.py
blob: 0c8fd0523eac46816480d7c517ea74f64e855a80 (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
#!/usr/bin/env python3
"""Test LZ4 interoperability between versions"""

#
# Copyright (C) 2011-present, Takayuki Matsuoka
# All rights reserved.
# GPL v2 License
#

import glob
import subprocess
import filecmp
import os
import shutil
import sys
import hashlib

repo_url = 'https://github.com/lz4/lz4.git'
tmp_dir_name = 'tests/abiTests'
env_flags = ' ' # '-j MOREFLAGS="-g -O0 -fsanitize=address"'
make_cmd = 'make'
git_cmd = 'git'
test_dat_src = ['README.md']
head = 'v999'

def proc(cmd_args, pipe=True, env=False):
    if env == False:
        env = os.environ.copy()
    if pipe:
        s = subprocess.Popen(cmd_args,
                             stdout=subprocess.PIPE,
                             stderr=subprocess.PIPE,
                             env = env)
    else:
        s = subprocess.Popen(cmd_args, env = env)
    r = s.communicate()
    if s.poll() != 0:
        print(' s.poll() = ', s.poll())
        sys.exit(1)
    return r

def make(args, pipe=True, env=False):
    if env == False:
        env = os.environ.copy()
        # we want the address sanitizer for abi tests
        env["MOREFLAGS"] = "-fsanitize=address"
    return proc([make_cmd] + ['-j'] + ['V=1'] + args, pipe, env)

def git(args, pipe=True):
    return proc([git_cmd] + args, pipe)

def get_git_tags():
    # Only start from first v1.7.x format release
    stdout, stderr = git(['tag', '-l', 'v[1-9].[0-9].[0-9]'])
    tags = stdout.decode('utf-8').split()
    return tags

# https://stackoverflow.com/a/19711609/2132223
def sha1_of_file(filepath):
    with open(filepath, 'rb') as f:
        return hashlib.sha1(f.read()).hexdigest()

if __name__ == '__main__':
    error_code = 0
    base_dir = os.getcwd() + '/..'           # /path/to/lz4
    tmp_dir = base_dir + '/' + tmp_dir_name  # /path/to/lz4/tests/versionsTest
    clone_dir = tmp_dir + '/' + 'lz4'        # /path/to/lz4/tests/versionsTest/lz4
    lib_dir = base_dir + '/lib'              # /path/to/lz4/lib
    test_dir = base_dir + '/tests'
    os.makedirs(tmp_dir, exist_ok=True)

    # since Travis clones limited depth, we should clone full repository
    if not os.path.isdir(clone_dir):
        git(['clone', repo_url, clone_dir])

    # Retrieve all release tags
    print('Retrieve release tags >= v1.7.5 :')
    os.chdir(clone_dir)
    tags = [head] + get_git_tags()
    tags = [x for x in tags if (x >= 'v1.7.5')]
    print(tags)

    # loop across architectures
    for march in ['-m64', '-m32', '-mx32']:
        print(' ')
        print('=====================================')
        print('Testing architecture ' + march);
        print('=====================================')

        # Build all versions of liblz4
        # note : naming scheme only works on Linux
        for tag in tags:
            print('building library ', tag)
            os.chdir(base_dir)
    #        if not os.path.isfile(dst_liblz4) or tag == head:
            if tag != head:
                r_dir = '{}/{}'.format(tmp_dir, tag)  # /path/to/lz4/test/lz4test/<TAG>
                #print('r_dir = ', r_dir)  # for debug
                os.makedirs(r_dir, exist_ok=True)
                os.chdir(clone_dir)
                git(['--work-tree=' + r_dir, 'checkout', tag, '--', '.'])
                os.chdir(r_dir + '/lib')  # /path/to/lz4/lz4test/<TAG>/lib
            else:
                # print('lib_dir = {}', lib_dir)  # for debug
                os.chdir(lib_dir)
            make(['clean'])
            build_env = os.environ.copy()
            build_env["CFLAGS"] = march
            build_env["MOREFLAGS"] = "-fsanitize=address"
            make(['liblz4'], env=build_env)

        print(' ')
        print('******************************')
        print('Round trip expecting current ABI but linking to older Dynamic Library version')
        print('******************************')
        os.chdir(test_dir)
        # Start with matching version : should be no problem
        build_env = os.environ.copy()
        build_env["CFLAGS"] = march
        build_env["LDFLAGS"] = "-L../lib"
        build_env["LDLIBS"] = "-llz4"
        # we use asan to detect any out-of-bound read or write
        build_env["MOREFLAGS"] = "-fsanitize=address"
        if os.path.isfile('abiTest'):
            os.remove('abiTest')
        make(['abiTest'], env=build_env, pipe=False)

        for tag in tags:
            print('linking to lib tag = ', tag)
            run_env = os.environ.copy()
            if tag == head:
                run_env["LD_LIBRARY_PATH"] = '../lib'
            else:
                run_env["LD_LIBRARY_PATH"] = 'abiTests/{}/lib'.format(tag)
            # check we are linking to the right library version at run time
            proc(['./check_liblz4_version.sh'] + ['./abiTest'], pipe=False, env=run_env)
            # now run with mismatched library version
            proc(['./abiTest'] + test_dat_src, pipe=False, env=run_env)

        print(' ')
        print('******************************')
        print('Round trip using current Dynamic Library expecting older ABI version')
        print('******************************')

        for tag in tags:
            print(' ')
            print('building using older lib ', tag)
            build_env = os.environ.copy()
            if tag != head:
                build_env["CPPFLAGS"] = '-IabiTests/{}/lib'.format(tag)
                build_env["LDFLAGS"] = '-LabiTests/{}/lib'.format(tag)
            else:
                build_env["CPPFLAGS"] = '-I../lib'
                build_env["LDFLAGS"] = '-L../lib'
            build_env["LDLIBS"] = "-llz4"
            build_env["CFLAGS"] = march
            build_env["MOREFLAGS"] = "-fsanitize=address"
            os.remove('abiTest')
            make(['abiTest'], pipe=False, env=build_env)

            print('run with CURRENT library version (head)')
            run_env = os.environ.copy()
            run_env["LD_LIBRARY_PATH"] = '../lib'
            # check we are linking to the right library version at run time
            proc(['./check_liblz4_version.sh'] + ['./abiTest'], pipe=False, env=run_env)
            # now run with mismatched library version
            proc(['./abiTest'] + test_dat_src, pipe=False, env=run_env)


    if error_code != 0:
        print('ERROR')

    sys.exit(error_code)