blob: 4ee778fe55216e2bb3d2e926bf8b8bbf73861e60 [file] [log] [blame]
Yann Colletdca60f22016-05-23 14:23:55 +02001#!/usr/bin/env python3
Yann Collet0d0f7e42016-05-25 10:58:11 +02002"""Test zstd interoperability between versions"""
Yann Collet9097f7b2016-08-28 16:25:56 -07003# Copyright Yann Collet, Przemyslaw Skibinski and Takayuki Matsuoka
4# License GPLv2
Yann Colletdca60f22016-05-23 14:23:55 +02005
Yann Colletdca60f22016-05-23 14:23:55 +02006import filecmp
Yann Colletebc13bc2016-05-25 10:12:39 +02007import glob
8import hashlib
Yann Colletdca60f22016-05-23 14:23:55 +02009import os
10import shutil
11import sys
inikep45456712016-06-17 13:39:43 +020012import subprocess
inikep7e3597b2016-06-17 14:43:24 +020013from subprocess import Popen, PIPE
Yann Colletdca60f22016-05-23 14:23:55 +020014
15repo_url = 'https://github.com/Cyan4973/zstd.git'
inikep9470b872016-06-09 12:54:06 +020016tmp_dir_name = 'tests/versionsTest'
Yann Colletdca60f22016-05-23 14:23:55 +020017make_cmd = 'make'
18git_cmd = 'git'
19test_dat_src = 'README.md'
20test_dat = 'test_dat'
21head = 'vdevel'
inikep24aa7b42016-06-16 14:15:32 +020022dict_source = 'dict_source'
23dict_files = './zstd/programs/*.c ./zstd/lib/common/*.c ./zstd/lib/compress/*.c ./zstd/lib/decompress/*.c ./zstd/lib/dictBuilder/*.c ./zstd/lib/legacy/*.c '
24dict_files += './zstd/programs/*.h ./zstd/lib/common/*.h ./zstd/lib/compress/*.h ./zstd/lib/dictBuilder/*.h ./zstd/lib/legacy/*.h'
25
26
inikep2ef16502016-06-17 14:07:42 +020027def execute(command, print_output=False, print_error=True, param_shell=False):
28 popen = Popen(command, stdout=PIPE, stderr=PIPE, shell=param_shell)
inikep45456712016-06-17 13:39:43 +020029 stdout_lines, stderr_lines = popen.communicate()
30 stderr_lines = stderr_lines.decode("utf-8")
31 stdout_lines = stdout_lines.decode("utf-8")
inikep24aa7b42016-06-16 14:15:32 +020032 if print_output:
33 print(stdout_lines)
inikep24aa7b42016-06-16 14:15:32 +020034 print(stderr_lines)
inikep24aa7b42016-06-16 14:15:32 +020035 if popen.returncode is not None and popen.returncode != 0:
36 if not print_output and print_error:
37 print(stderr_lines)
inikep45456712016-06-17 13:39:43 +020038 return popen.returncode
Yann Colletdca60f22016-05-23 14:23:55 +020039
Yann Colletebc13bc2016-05-25 10:12:39 +020040
Yann Colletdca60f22016-05-23 14:23:55 +020041def proc(cmd_args, pipe=True, dummy=False):
42 if dummy:
43 return
44 if pipe:
inikep45456712016-06-17 13:39:43 +020045 subproc = Popen(cmd_args, stdout=PIPE, stderr=PIPE)
Yann Colletdca60f22016-05-23 14:23:55 +020046 else:
inikep45456712016-06-17 13:39:43 +020047 subproc = Popen(cmd_args)
Yann Colletdca60f22016-05-23 14:23:55 +020048 return subproc.communicate()
49
Yann Colletebc13bc2016-05-25 10:12:39 +020050
Yann Colletdca60f22016-05-23 14:23:55 +020051def make(args, pipe=True):
52 return proc([make_cmd] + args, pipe)
53
Yann Colletebc13bc2016-05-25 10:12:39 +020054
Yann Colletdca60f22016-05-23 14:23:55 +020055def git(args, pipe=True):
56 return proc([git_cmd] + args, pipe)
57
Yann Colletebc13bc2016-05-25 10:12:39 +020058
Yann Colletdca60f22016-05-23 14:23:55 +020059def get_git_tags():
60 stdout, stderr = git(['tag', '-l', 'v[0-9].[0-9].[0-9]'])
61 tags = stdout.decode('utf-8').split()
62 return tags
63
Yann Colletebc13bc2016-05-25 10:12:39 +020064
inikep150152f2016-06-16 19:29:09 +020065def create_dict(tag, dict_source_path):
66 dict_name = 'dict.' + tag
67 if not os.path.isfile(dict_name):
68 cFiles = glob.glob(dict_source_path + "/*.c")
69 hFiles = glob.glob(dict_source_path + "/*.h")
inikep7e3597b2016-06-17 14:43:24 +020070 if tag == 'v0.5.0':
71 result = execute('./dictBuilder.' + tag + ' ' + ' '.join(cFiles) + ' ' + ' '.join(hFiles) + ' -o ' + dict_name, print_output=False, param_shell=True)
72 else:
inikepe16f6562016-06-17 15:17:35 +020073 result = execute('./zstd.' + tag + ' -f --train ' + ' '.join(cFiles) + ' ' + ' '.join(hFiles) + ' -o ' + dict_name, print_output=False, param_shell=True)
inikep7e3597b2016-06-17 14:43:24 +020074 if result == 0:
inikep45456712016-06-17 13:39:43 +020075 print(dict_name + ' created')
76 else:
77 print('ERROR: creating of ' + dict_name + ' failed')
inikep150152f2016-06-16 19:29:09 +020078 else:
79 print(dict_name + ' already exists')
80
81
82def dict_compress_sample(tag, sample):
83 dict_name = 'dict.' + tag
inikep7e3597b2016-06-17 14:43:24 +020084 DEVNULL = open(os.devnull, 'wb')
85 if subprocess.call(['./zstd.' + tag, '-D', dict_name, '-f', sample], stderr=DEVNULL) == 0:
86 os.rename(sample + '.zst', sample + '_01_64_' + tag + '_dictio.zst')
87 if subprocess.call(['./zstd.' + tag, '-D', dict_name, '-5f', sample], stderr=DEVNULL) == 0:
88 os.rename(sample + '.zst', sample + '_05_64_' + tag + '_dictio.zst')
89 if subprocess.call(['./zstd.' + tag, '-D', dict_name, '-9f', sample], stderr=DEVNULL) == 0:
90 os.rename(sample + '.zst', sample + '_09_64_' + tag + '_dictio.zst')
91 if subprocess.call(['./zstd.' + tag, '-D', dict_name, '-15f', sample], stderr=DEVNULL) == 0:
92 os.rename(sample + '.zst', sample + '_15_64_' + tag + '_dictio.zst')
93 if subprocess.call(['./zstd.' + tag, '-D', dict_name, '-18f', sample], stderr=DEVNULL) == 0:
94 os.rename(sample + '.zst', sample + '_18_64_' + tag + '_dictio.zst')
inikep150152f2016-06-16 19:29:09 +020095 # zstdFiles = glob.glob("*.zst*")
96 # print(zstdFiles)
97 print(tag + " : dict compression completed")
98
99
Yann Colletdca60f22016-05-23 14:23:55 +0200100def compress_sample(tag, sample):
inikep7e3597b2016-06-17 14:43:24 +0200101 DEVNULL = open(os.devnull, 'wb')
Yann Colletebc13bc2016-05-25 10:12:39 +0200102 if subprocess.call(['./zstd.' + tag, '-f', sample], stderr=DEVNULL) == 0:
inikepd1af4e62016-06-16 20:23:11 +0200103 os.rename(sample + '.zst', sample + '_01_64_' + tag + '_nodict.zst')
Yann Colletebc13bc2016-05-25 10:12:39 +0200104 if subprocess.call(['./zstd.' + tag, '-5f', sample], stderr=DEVNULL) == 0:
inikepd1af4e62016-06-16 20:23:11 +0200105 os.rename(sample + '.zst', sample + '_05_64_' + tag + '_nodict.zst')
Yann Colletebc13bc2016-05-25 10:12:39 +0200106 if subprocess.call(['./zstd.' + tag, '-9f', sample], stderr=DEVNULL) == 0:
inikepd1af4e62016-06-16 20:23:11 +0200107 os.rename(sample + '.zst', sample + '_09_64_' + tag + '_nodict.zst')
Yann Colletebc13bc2016-05-25 10:12:39 +0200108 if subprocess.call(['./zstd.' + tag, '-15f', sample], stderr=DEVNULL) == 0:
inikepd1af4e62016-06-16 20:23:11 +0200109 os.rename(sample + '.zst', sample + '_15_64_' + tag + '_nodict.zst')
Yann Colletebc13bc2016-05-25 10:12:39 +0200110 if subprocess.call(['./zstd.' + tag, '-18f', sample], stderr=DEVNULL) == 0:
inikepd1af4e62016-06-16 20:23:11 +0200111 os.rename(sample + '.zst', sample + '_18_64_' + tag + '_nodict.zst')
Yann Colletdca60f22016-05-23 14:23:55 +0200112 # zstdFiles = glob.glob("*.zst*")
113 # print(zstdFiles)
Yann Colleta5ad5272016-06-03 15:41:51 +0200114 print(tag + " : compression completed")
Yann Colletdca60f22016-05-23 14:23:55 +0200115
Yann Colletebc13bc2016-05-25 10:12:39 +0200116
Yann Colletdca60f22016-05-23 14:23:55 +0200117# http://stackoverflow.com/a/19711609/2132223
118def sha1_of_file(filepath):
119 with open(filepath, 'rb') as f:
120 return hashlib.sha1(f.read()).hexdigest()
121
Yann Colletebc13bc2016-05-25 10:12:39 +0200122
Yann Colletdca60f22016-05-23 14:23:55 +0200123def remove_duplicates():
inikepd1af4e62016-06-16 20:23:11 +0200124 list_of_zst = sorted(glob.glob('*.zst'))
Yann Colletdca60f22016-05-23 14:23:55 +0200125 for i, ref_zst in enumerate(list_of_zst):
126 if not os.path.isfile(ref_zst):
127 continue
Yann Collet0d0f7e42016-05-25 10:58:11 +0200128 for j in range(i + 1, len(list_of_zst)):
Yann Colletdca60f22016-05-23 14:23:55 +0200129 compared_zst = list_of_zst[j]
130 if not os.path.isfile(compared_zst):
131 continue
132 if filecmp.cmp(ref_zst, compared_zst):
133 os.remove(compared_zst)
134 print('duplicated : {} == {}'.format(ref_zst, compared_zst))
135
Yann Colletebc13bc2016-05-25 10:12:39 +0200136
inikep7e3597b2016-06-17 14:43:24 +0200137def decompress_zst(tag):
Yann Colletdca60f22016-05-23 14:23:55 +0200138 dec_error = 0
inikepd1af4e62016-06-16 20:23:11 +0200139 list_zst = sorted(glob.glob('*_nodict.zst'))
Yann Colletdca60f22016-05-23 14:23:55 +0200140 for file_zst in list_zst:
Yann Collet0d0f7e42016-05-25 10:58:11 +0200141 print(file_zst, end=' ')
142 print(tag, end=' ')
Yann Colletdca60f22016-05-23 14:23:55 +0200143 file_dec = file_zst + '_d64_' + tag + '.dec'
inikep7e3597b2016-06-17 14:43:24 +0200144 if tag <= 'v0.5.0':
inikep24aa7b42016-06-16 14:15:32 +0200145 params = ['./zstd.' + tag, '-df', file_zst, file_dec]
146 else:
147 params = ['./zstd.' + tag, '-df', file_zst, '-o', file_dec]
inikep2ef16502016-06-17 14:07:42 +0200148 if execute(params) == 0:
Yann Colletdca60f22016-05-23 14:23:55 +0200149 if not filecmp.cmp(file_dec, test_dat):
150 print('ERR !! ')
151 dec_error = 1
152 else:
153 print('OK ')
Yann Collet99b23ba2016-05-23 15:04:14 +0200154 else:
155 print('command does not work')
Yann Collet9097f7b2016-08-28 16:25:56 -0700156 dec_error = 1
Yann Colletdca60f22016-05-23 14:23:55 +0200157 return dec_error
158
Yann Colletda4fe742016-05-23 15:43:17 +0200159
inikep7e3597b2016-06-17 14:43:24 +0200160def decompress_dict(tag):
inikep150152f2016-06-16 19:29:09 +0200161 dec_error = 0
inikepd1af4e62016-06-16 20:23:11 +0200162 list_zst = sorted(glob.glob('*_dictio.zst'))
inikep150152f2016-06-16 19:29:09 +0200163 for file_zst in list_zst:
inikepd1af4e62016-06-16 20:23:11 +0200164 dict_tag = file_zst[0:len(file_zst)-11] # remove "_dictio.zst"
165 if head in dict_tag: # find vdevel
inikep150152f2016-06-16 19:29:09 +0200166 dict_tag = head
inikepd1af4e62016-06-16 20:23:11 +0200167 else:
168 dict_tag = dict_tag[dict_tag.rfind('v'):]
inikep7e3597b2016-06-17 14:43:24 +0200169 if tag == 'v0.6.0' and dict_tag < 'v0.6.0':
170 continue
inikep150152f2016-06-16 19:29:09 +0200171 dict_name = 'dict.' + dict_tag
inikepd1af4e62016-06-16 20:23:11 +0200172 print(file_zst + ' ' + tag + ' dict=' + dict_tag, end=' ')
inikep150152f2016-06-16 19:29:09 +0200173 file_dec = file_zst + '_d64_' + tag + '.dec'
inikep7e3597b2016-06-17 14:43:24 +0200174 if tag <= 'v0.5.0':
inikep150152f2016-06-16 19:29:09 +0200175 params = ['./zstd.' + tag, '-D', dict_name, '-df', file_zst, file_dec]
176 else:
177 params = ['./zstd.' + tag, '-D', dict_name, '-df', file_zst, '-o', file_dec]
inikep2ef16502016-06-17 14:07:42 +0200178 if execute(params) == 0:
inikep150152f2016-06-16 19:29:09 +0200179 if not filecmp.cmp(file_dec, test_dat):
180 print('ERR !! ')
181 dec_error = 1
182 else:
183 print('OK ')
184 else:
185 print('command does not work')
inikepd1af4e62016-06-16 20:23:11 +0200186 dec_error = 1
inikep150152f2016-06-16 19:29:09 +0200187 return dec_error
inikep24aa7b42016-06-16 14:15:32 +0200188
189
Yann Colletdca60f22016-05-23 14:23:55 +0200190if __name__ == '__main__':
191 error_code = 0
inikep24aa7b42016-06-16 14:15:32 +0200192 base_dir = os.getcwd() + '/..' # /path/to/zstd
193 tmp_dir = base_dir + '/' + tmp_dir_name # /path/to/zstd/tests/versionsTest
194 clone_dir = tmp_dir + '/' + 'zstd' # /path/to/zstd/tests/versionsTest/zstd
195 dict_source_path = tmp_dir + '/' + dict_source # /path/to/zstd/tests/versionsTest/dict_source
196 programs_dir = base_dir + '/programs' # /path/to/zstd/programs
Yann Colletdca60f22016-05-23 14:23:55 +0200197 os.makedirs(tmp_dir, exist_ok=True)
198
199 # since Travis clones limited depth, we should clone full repository
200 if not os.path.isdir(clone_dir):
201 git(['clone', repo_url, clone_dir])
202
203 shutil.copy2(base_dir + '/' + test_dat_src, tmp_dir + '/' + test_dat)
204
205 # Retrieve all release tags
206 print('Retrieve all release tags :')
207 os.chdir(clone_dir)
208 tags = get_git_tags() + [head]
Yann Colletebc13bc2016-05-25 10:12:39 +0200209 print(tags)
Yann Colletdca60f22016-05-23 14:23:55 +0200210
211 # Build all release zstd
212 for tag in tags:
213 os.chdir(base_dir)
Yann Collet803c05e2016-06-16 11:32:57 +0200214 dst_zstd = '{}/zstd.{}'.format(tmp_dir, tag) # /path/to/zstd/tests/versionsTest/zstd.<TAG>
Yann Colletdca60f22016-05-23 14:23:55 +0200215 if not os.path.isfile(dst_zstd) or tag == head:
216 if tag != head:
inikep9470b872016-06-09 12:54:06 +0200217 r_dir = '{}/{}'.format(tmp_dir, tag) # /path/to/zstd/tests/versionsTest/<TAG>
Yann Colletdca60f22016-05-23 14:23:55 +0200218 os.makedirs(r_dir, exist_ok=True)
219 os.chdir(clone_dir)
220 git(['--work-tree=' + r_dir, 'checkout', tag, '--', '.'], False)
inikep7e3597b2016-06-17 14:43:24 +0200221 if tag == 'v0.5.0':
222 os.chdir(r_dir + '/dictBuilder') # /path/to/zstd/tests/versionsTest/v0.5.0/dictBuilder
223 make(['clean', 'dictBuilder'], False)
224 shutil.copy2('dictBuilder', '{}/dictBuilder.{}'.format(tmp_dir, tag))
inikep9470b872016-06-09 12:54:06 +0200225 os.chdir(r_dir + '/programs') # /path/to/zstd/tests/versionsTest/<TAG>/programs
Yann Colletdca60f22016-05-23 14:23:55 +0200226 make(['clean', 'zstd'], False)
227 else:
228 os.chdir(programs_dir)
229 make(['zstd'], False)
230 shutil.copy2('zstd', dst_zstd)
231
232 # remove any remaining *.zst and *.dec from previous test
233 os.chdir(tmp_dir)
234 for compressed in glob.glob("*.zst"):
235 os.remove(compressed)
Yann Colletebc13bc2016-05-25 10:12:39 +0200236 for dec in glob.glob("*.dec"):
Yann Colletdca60f22016-05-23 14:23:55 +0200237 os.remove(dec)
238
inikep24aa7b42016-06-16 14:15:32 +0200239 # copy *.c and *.h to a temporary directory ("dict_source")
240 if not os.path.isdir(dict_source_path):
241 os.mkdir(dict_source_path)
242 print('cp ' + dict_files + ' ' + dict_source_path)
inikep2ef16502016-06-17 14:07:42 +0200243 execute('cp ' + dict_files + ' ' + dict_source_path, param_shell=True)
inikep24aa7b42016-06-16 14:15:32 +0200244
Yann Colletdca60f22016-05-23 14:23:55 +0200245 print('Compress test.dat by all released zstd')
246
Yann Colletebc13bc2016-05-25 10:12:39 +0200247 error_code = 0
Yann Colletdca60f22016-05-23 14:23:55 +0200248 for tag in tags:
249 print(tag)
inikep7e3597b2016-06-17 14:43:24 +0200250 if tag >= 'v0.5.0':
inikep24aa7b42016-06-16 14:15:32 +0200251 create_dict(tag, dict_source_path)
inikep150152f2016-06-16 19:29:09 +0200252 dict_compress_sample(tag, test_dat)
inikepd1af4e62016-06-16 20:23:11 +0200253 remove_duplicates()
254 error_code += decompress_dict(tag)
Yann Colletdca60f22016-05-23 14:23:55 +0200255 compress_sample(tag, test_dat)
256 remove_duplicates()
inikep7e3597b2016-06-17 14:43:24 +0200257 error_code += decompress_zst(tag)
Yann Colletdca60f22016-05-23 14:23:55 +0200258
259 print('')
260 print('Enumerate different compressed files')
261 zstds = sorted(glob.glob('*.zst'))
262 for zstd in zstds:
263 print(zstd + ' : ' + repr(os.path.getsize(zstd)) + ', ' + sha1_of_file(zstd))
264
265 if error_code != 0:
Yann Collet9097f7b2016-08-28 16:25:56 -0700266 print('====== ERROR !!! =======')
Yann Colletdca60f22016-05-23 14:23:55 +0200267
268 sys.exit(error_code)