blob: a5a713009a93f1f00113b6d998c0e4570acd26cc [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 Collet4ded9e52016-08-30 10:04:33 -07003
4#
5# Copyright (c) 2016-present, Yann Collet, Facebook, Inc.
6# All rights reserved.
7#
8# This source code is licensed under the BSD-style license found in the
9# LICENSE file in the root directory of this source tree. An additional grant
10# of patent rights can be found in the PATENTS file in the same directory.
11#
Yann Colletdca60f22016-05-23 14:23:55 +020012
Yann Colletdca60f22016-05-23 14:23:55 +020013import filecmp
Yann Colletebc13bc2016-05-25 10:12:39 +020014import glob
15import hashlib
Yann Colletdca60f22016-05-23 14:23:55 +020016import os
17import shutil
18import sys
inikep45456712016-06-17 13:39:43 +020019import subprocess
inikep7e3597b2016-06-17 14:43:24 +020020from subprocess import Popen, PIPE
Yann Colletdca60f22016-05-23 14:23:55 +020021
Yann Collet33a04652016-09-02 22:11:49 -070022repo_url = 'https://github.com/facebook/zstd.git'
inikep9470b872016-06-09 12:54:06 +020023tmp_dir_name = 'tests/versionsTest'
Yann Colletdca60f22016-05-23 14:23:55 +020024make_cmd = 'make'
25git_cmd = 'git'
26test_dat_src = 'README.md'
27test_dat = 'test_dat'
28head = 'vdevel'
inikep24aa7b42016-06-16 14:15:32 +020029dict_source = 'dict_source'
30dict_files = './zstd/programs/*.c ./zstd/lib/common/*.c ./zstd/lib/compress/*.c ./zstd/lib/decompress/*.c ./zstd/lib/dictBuilder/*.c ./zstd/lib/legacy/*.c '
31dict_files += './zstd/programs/*.h ./zstd/lib/common/*.h ./zstd/lib/compress/*.h ./zstd/lib/dictBuilder/*.h ./zstd/lib/legacy/*.h'
32
33
inikep2ef16502016-06-17 14:07:42 +020034def execute(command, print_output=False, print_error=True, param_shell=False):
35 popen = Popen(command, stdout=PIPE, stderr=PIPE, shell=param_shell)
inikep45456712016-06-17 13:39:43 +020036 stdout_lines, stderr_lines = popen.communicate()
37 stderr_lines = stderr_lines.decode("utf-8")
38 stdout_lines = stdout_lines.decode("utf-8")
inikep24aa7b42016-06-16 14:15:32 +020039 if print_output:
40 print(stdout_lines)
inikep24aa7b42016-06-16 14:15:32 +020041 print(stderr_lines)
inikep24aa7b42016-06-16 14:15:32 +020042 if popen.returncode is not None and popen.returncode != 0:
43 if not print_output and print_error:
44 print(stderr_lines)
inikep45456712016-06-17 13:39:43 +020045 return popen.returncode
Yann Colletdca60f22016-05-23 14:23:55 +020046
Yann Colletebc13bc2016-05-25 10:12:39 +020047
Yann Colletdca60f22016-05-23 14:23:55 +020048def proc(cmd_args, pipe=True, dummy=False):
49 if dummy:
50 return
51 if pipe:
inikep45456712016-06-17 13:39:43 +020052 subproc = Popen(cmd_args, stdout=PIPE, stderr=PIPE)
Yann Colletdca60f22016-05-23 14:23:55 +020053 else:
inikep45456712016-06-17 13:39:43 +020054 subproc = Popen(cmd_args)
Yann Colletdca60f22016-05-23 14:23:55 +020055 return subproc.communicate()
56
Yann Colletebc13bc2016-05-25 10:12:39 +020057
Yann Colletdca60f22016-05-23 14:23:55 +020058def make(args, pipe=True):
59 return proc([make_cmd] + args, pipe)
60
Yann Colletebc13bc2016-05-25 10:12:39 +020061
Yann Colletdca60f22016-05-23 14:23:55 +020062def git(args, pipe=True):
63 return proc([git_cmd] + args, pipe)
64
Yann Colletebc13bc2016-05-25 10:12:39 +020065
Yann Colletdca60f22016-05-23 14:23:55 +020066def get_git_tags():
67 stdout, stderr = git(['tag', '-l', 'v[0-9].[0-9].[0-9]'])
68 tags = stdout.decode('utf-8').split()
69 return tags
70
Yann Colletebc13bc2016-05-25 10:12:39 +020071
inikep150152f2016-06-16 19:29:09 +020072def create_dict(tag, dict_source_path):
73 dict_name = 'dict.' + tag
74 if not os.path.isfile(dict_name):
75 cFiles = glob.glob(dict_source_path + "/*.c")
76 hFiles = glob.glob(dict_source_path + "/*.h")
inikep7e3597b2016-06-17 14:43:24 +020077 if tag == 'v0.5.0':
78 result = execute('./dictBuilder.' + tag + ' ' + ' '.join(cFiles) + ' ' + ' '.join(hFiles) + ' -o ' + dict_name, print_output=False, param_shell=True)
79 else:
inikepe16f6562016-06-17 15:17:35 +020080 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 +020081 if result == 0:
inikep45456712016-06-17 13:39:43 +020082 print(dict_name + ' created')
83 else:
84 print('ERROR: creating of ' + dict_name + ' failed')
inikep150152f2016-06-16 19:29:09 +020085 else:
86 print(dict_name + ' already exists')
87
88
89def dict_compress_sample(tag, sample):
90 dict_name = 'dict.' + tag
inikep7e3597b2016-06-17 14:43:24 +020091 DEVNULL = open(os.devnull, 'wb')
92 if subprocess.call(['./zstd.' + tag, '-D', dict_name, '-f', sample], stderr=DEVNULL) == 0:
93 os.rename(sample + '.zst', sample + '_01_64_' + tag + '_dictio.zst')
94 if subprocess.call(['./zstd.' + tag, '-D', dict_name, '-5f', sample], stderr=DEVNULL) == 0:
95 os.rename(sample + '.zst', sample + '_05_64_' + tag + '_dictio.zst')
96 if subprocess.call(['./zstd.' + tag, '-D', dict_name, '-9f', sample], stderr=DEVNULL) == 0:
97 os.rename(sample + '.zst', sample + '_09_64_' + tag + '_dictio.zst')
98 if subprocess.call(['./zstd.' + tag, '-D', dict_name, '-15f', sample], stderr=DEVNULL) == 0:
99 os.rename(sample + '.zst', sample + '_15_64_' + tag + '_dictio.zst')
100 if subprocess.call(['./zstd.' + tag, '-D', dict_name, '-18f', sample], stderr=DEVNULL) == 0:
101 os.rename(sample + '.zst', sample + '_18_64_' + tag + '_dictio.zst')
inikep150152f2016-06-16 19:29:09 +0200102 # zstdFiles = glob.glob("*.zst*")
103 # print(zstdFiles)
104 print(tag + " : dict compression completed")
105
106
Yann Colletdca60f22016-05-23 14:23:55 +0200107def compress_sample(tag, sample):
inikep7e3597b2016-06-17 14:43:24 +0200108 DEVNULL = open(os.devnull, 'wb')
Yann Colletebc13bc2016-05-25 10:12:39 +0200109 if subprocess.call(['./zstd.' + tag, '-f', sample], stderr=DEVNULL) == 0:
inikepd1af4e62016-06-16 20:23:11 +0200110 os.rename(sample + '.zst', sample + '_01_64_' + tag + '_nodict.zst')
Yann Colletebc13bc2016-05-25 10:12:39 +0200111 if subprocess.call(['./zstd.' + tag, '-5f', sample], stderr=DEVNULL) == 0:
inikepd1af4e62016-06-16 20:23:11 +0200112 os.rename(sample + '.zst', sample + '_05_64_' + tag + '_nodict.zst')
Yann Colletebc13bc2016-05-25 10:12:39 +0200113 if subprocess.call(['./zstd.' + tag, '-9f', sample], stderr=DEVNULL) == 0:
inikepd1af4e62016-06-16 20:23:11 +0200114 os.rename(sample + '.zst', sample + '_09_64_' + tag + '_nodict.zst')
Yann Colletebc13bc2016-05-25 10:12:39 +0200115 if subprocess.call(['./zstd.' + tag, '-15f', sample], stderr=DEVNULL) == 0:
inikepd1af4e62016-06-16 20:23:11 +0200116 os.rename(sample + '.zst', sample + '_15_64_' + tag + '_nodict.zst')
Yann Colletebc13bc2016-05-25 10:12:39 +0200117 if subprocess.call(['./zstd.' + tag, '-18f', sample], stderr=DEVNULL) == 0:
inikepd1af4e62016-06-16 20:23:11 +0200118 os.rename(sample + '.zst', sample + '_18_64_' + tag + '_nodict.zst')
Yann Colletdca60f22016-05-23 14:23:55 +0200119 # zstdFiles = glob.glob("*.zst*")
120 # print(zstdFiles)
Yann Colleta5ad5272016-06-03 15:41:51 +0200121 print(tag + " : compression completed")
Yann Colletdca60f22016-05-23 14:23:55 +0200122
Yann Colletebc13bc2016-05-25 10:12:39 +0200123
Yann Colletdca60f22016-05-23 14:23:55 +0200124# http://stackoverflow.com/a/19711609/2132223
125def sha1_of_file(filepath):
126 with open(filepath, 'rb') as f:
127 return hashlib.sha1(f.read()).hexdigest()
128
Yann Colletebc13bc2016-05-25 10:12:39 +0200129
Yann Colletdca60f22016-05-23 14:23:55 +0200130def remove_duplicates():
inikepd1af4e62016-06-16 20:23:11 +0200131 list_of_zst = sorted(glob.glob('*.zst'))
Yann Colletdca60f22016-05-23 14:23:55 +0200132 for i, ref_zst in enumerate(list_of_zst):
133 if not os.path.isfile(ref_zst):
134 continue
Yann Collet0d0f7e42016-05-25 10:58:11 +0200135 for j in range(i + 1, len(list_of_zst)):
Yann Colletdca60f22016-05-23 14:23:55 +0200136 compared_zst = list_of_zst[j]
137 if not os.path.isfile(compared_zst):
138 continue
139 if filecmp.cmp(ref_zst, compared_zst):
140 os.remove(compared_zst)
141 print('duplicated : {} == {}'.format(ref_zst, compared_zst))
142
Yann Colletebc13bc2016-05-25 10:12:39 +0200143
inikep7e3597b2016-06-17 14:43:24 +0200144def decompress_zst(tag):
Yann Colletdca60f22016-05-23 14:23:55 +0200145 dec_error = 0
inikepd1af4e62016-06-16 20:23:11 +0200146 list_zst = sorted(glob.glob('*_nodict.zst'))
Yann Colletdca60f22016-05-23 14:23:55 +0200147 for file_zst in list_zst:
Yann Collet0d0f7e42016-05-25 10:58:11 +0200148 print(file_zst, end=' ')
149 print(tag, end=' ')
Yann Colletdca60f22016-05-23 14:23:55 +0200150 file_dec = file_zst + '_d64_' + tag + '.dec'
inikep7e3597b2016-06-17 14:43:24 +0200151 if tag <= 'v0.5.0':
inikep24aa7b42016-06-16 14:15:32 +0200152 params = ['./zstd.' + tag, '-df', file_zst, file_dec]
153 else:
154 params = ['./zstd.' + tag, '-df', file_zst, '-o', file_dec]
inikep2ef16502016-06-17 14:07:42 +0200155 if execute(params) == 0:
Yann Colletdca60f22016-05-23 14:23:55 +0200156 if not filecmp.cmp(file_dec, test_dat):
157 print('ERR !! ')
158 dec_error = 1
159 else:
160 print('OK ')
Yann Collet99b23ba2016-05-23 15:04:14 +0200161 else:
162 print('command does not work')
Yann Collet9097f7b2016-08-28 16:25:56 -0700163 dec_error = 1
Yann Colletdca60f22016-05-23 14:23:55 +0200164 return dec_error
165
Yann Colletda4fe742016-05-23 15:43:17 +0200166
inikep7e3597b2016-06-17 14:43:24 +0200167def decompress_dict(tag):
inikep150152f2016-06-16 19:29:09 +0200168 dec_error = 0
inikepd1af4e62016-06-16 20:23:11 +0200169 list_zst = sorted(glob.glob('*_dictio.zst'))
inikep150152f2016-06-16 19:29:09 +0200170 for file_zst in list_zst:
inikepd1af4e62016-06-16 20:23:11 +0200171 dict_tag = file_zst[0:len(file_zst)-11] # remove "_dictio.zst"
172 if head in dict_tag: # find vdevel
inikep150152f2016-06-16 19:29:09 +0200173 dict_tag = head
inikepd1af4e62016-06-16 20:23:11 +0200174 else:
175 dict_tag = dict_tag[dict_tag.rfind('v'):]
inikep7e3597b2016-06-17 14:43:24 +0200176 if tag == 'v0.6.0' and dict_tag < 'v0.6.0':
177 continue
inikep150152f2016-06-16 19:29:09 +0200178 dict_name = 'dict.' + dict_tag
inikepd1af4e62016-06-16 20:23:11 +0200179 print(file_zst + ' ' + tag + ' dict=' + dict_tag, end=' ')
inikep150152f2016-06-16 19:29:09 +0200180 file_dec = file_zst + '_d64_' + tag + '.dec'
inikep7e3597b2016-06-17 14:43:24 +0200181 if tag <= 'v0.5.0':
inikep150152f2016-06-16 19:29:09 +0200182 params = ['./zstd.' + tag, '-D', dict_name, '-df', file_zst, file_dec]
183 else:
184 params = ['./zstd.' + tag, '-D', dict_name, '-df', file_zst, '-o', file_dec]
inikep2ef16502016-06-17 14:07:42 +0200185 if execute(params) == 0:
inikep150152f2016-06-16 19:29:09 +0200186 if not filecmp.cmp(file_dec, test_dat):
187 print('ERR !! ')
188 dec_error = 1
189 else:
190 print('OK ')
191 else:
192 print('command does not work')
inikepd1af4e62016-06-16 20:23:11 +0200193 dec_error = 1
inikep150152f2016-06-16 19:29:09 +0200194 return dec_error
inikep24aa7b42016-06-16 14:15:32 +0200195
196
Yann Colletdca60f22016-05-23 14:23:55 +0200197if __name__ == '__main__':
198 error_code = 0
inikep24aa7b42016-06-16 14:15:32 +0200199 base_dir = os.getcwd() + '/..' # /path/to/zstd
200 tmp_dir = base_dir + '/' + tmp_dir_name # /path/to/zstd/tests/versionsTest
201 clone_dir = tmp_dir + '/' + 'zstd' # /path/to/zstd/tests/versionsTest/zstd
202 dict_source_path = tmp_dir + '/' + dict_source # /path/to/zstd/tests/versionsTest/dict_source
203 programs_dir = base_dir + '/programs' # /path/to/zstd/programs
Yann Colletdca60f22016-05-23 14:23:55 +0200204 os.makedirs(tmp_dir, exist_ok=True)
205
206 # since Travis clones limited depth, we should clone full repository
207 if not os.path.isdir(clone_dir):
208 git(['clone', repo_url, clone_dir])
209
210 shutil.copy2(base_dir + '/' + test_dat_src, tmp_dir + '/' + test_dat)
211
212 # Retrieve all release tags
213 print('Retrieve all release tags :')
214 os.chdir(clone_dir)
Yann Colletdea67a62016-08-28 16:56:17 -0700215 alltags = get_git_tags() + [head]
216 tags = [t for t in alltags if t >= 'v0.4.0']
Yann Colletebc13bc2016-05-25 10:12:39 +0200217 print(tags)
Yann Colletdca60f22016-05-23 14:23:55 +0200218
219 # Build all release zstd
220 for tag in tags:
221 os.chdir(base_dir)
Yann Collet803c05e2016-06-16 11:32:57 +0200222 dst_zstd = '{}/zstd.{}'.format(tmp_dir, tag) # /path/to/zstd/tests/versionsTest/zstd.<TAG>
Yann Colletdca60f22016-05-23 14:23:55 +0200223 if not os.path.isfile(dst_zstd) or tag == head:
224 if tag != head:
inikep9470b872016-06-09 12:54:06 +0200225 r_dir = '{}/{}'.format(tmp_dir, tag) # /path/to/zstd/tests/versionsTest/<TAG>
Yann Colletdca60f22016-05-23 14:23:55 +0200226 os.makedirs(r_dir, exist_ok=True)
227 os.chdir(clone_dir)
228 git(['--work-tree=' + r_dir, 'checkout', tag, '--', '.'], False)
inikep7e3597b2016-06-17 14:43:24 +0200229 if tag == 'v0.5.0':
230 os.chdir(r_dir + '/dictBuilder') # /path/to/zstd/tests/versionsTest/v0.5.0/dictBuilder
231 make(['clean', 'dictBuilder'], False)
232 shutil.copy2('dictBuilder', '{}/dictBuilder.{}'.format(tmp_dir, tag))
inikep9470b872016-06-09 12:54:06 +0200233 os.chdir(r_dir + '/programs') # /path/to/zstd/tests/versionsTest/<TAG>/programs
Yann Colletdca60f22016-05-23 14:23:55 +0200234 make(['clean', 'zstd'], False)
235 else:
236 os.chdir(programs_dir)
237 make(['zstd'], False)
238 shutil.copy2('zstd', dst_zstd)
239
240 # remove any remaining *.zst and *.dec from previous test
241 os.chdir(tmp_dir)
242 for compressed in glob.glob("*.zst"):
243 os.remove(compressed)
Yann Colletebc13bc2016-05-25 10:12:39 +0200244 for dec in glob.glob("*.dec"):
Yann Colletdca60f22016-05-23 14:23:55 +0200245 os.remove(dec)
246
inikep24aa7b42016-06-16 14:15:32 +0200247 # copy *.c and *.h to a temporary directory ("dict_source")
248 if not os.path.isdir(dict_source_path):
249 os.mkdir(dict_source_path)
250 print('cp ' + dict_files + ' ' + dict_source_path)
inikep2ef16502016-06-17 14:07:42 +0200251 execute('cp ' + dict_files + ' ' + dict_source_path, param_shell=True)
inikep24aa7b42016-06-16 14:15:32 +0200252
Yann Colletdca60f22016-05-23 14:23:55 +0200253 print('Compress test.dat by all released zstd')
254
Yann Colletebc13bc2016-05-25 10:12:39 +0200255 error_code = 0
Yann Colletdca60f22016-05-23 14:23:55 +0200256 for tag in tags:
257 print(tag)
inikep7e3597b2016-06-17 14:43:24 +0200258 if tag >= 'v0.5.0':
inikep24aa7b42016-06-16 14:15:32 +0200259 create_dict(tag, dict_source_path)
inikep150152f2016-06-16 19:29:09 +0200260 dict_compress_sample(tag, test_dat)
inikepd1af4e62016-06-16 20:23:11 +0200261 remove_duplicates()
262 error_code += decompress_dict(tag)
Yann Colletdca60f22016-05-23 14:23:55 +0200263 compress_sample(tag, test_dat)
264 remove_duplicates()
inikep7e3597b2016-06-17 14:43:24 +0200265 error_code += decompress_zst(tag)
Yann Colletdca60f22016-05-23 14:23:55 +0200266
267 print('')
268 print('Enumerate different compressed files')
269 zstds = sorted(glob.glob('*.zst'))
270 for zstd in zstds:
271 print(zstd + ' : ' + repr(os.path.getsize(zstd)) + ', ' + sha1_of_file(zstd))
272
273 if error_code != 0:
Yann Collet9097f7b2016-08-28 16:25:56 -0700274 print('====== ERROR !!! =======')
Yann Colletdca60f22016-05-23 14:23:55 +0200275
276 sys.exit(error_code)