1 | #!/usr/bin/python2.4
|
---|
2 | # xdelta 3 - delta compression tools and library
|
---|
3 | # Copyright (C) 2003, 2006, 2007. Joshua P. MacDonald
|
---|
4 | #
|
---|
5 | # This program is free software; you can redistribute it and/or modify
|
---|
6 | # it under the terms of the GNU General Public License as published by
|
---|
7 | # the Free Software Foundation; either version 2 of the License, or
|
---|
8 | # (at your option) any later version.
|
---|
9 | #
|
---|
10 | # This program is distributed in the hope that it will be useful,
|
---|
11 | # but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
12 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
13 | # GNU General Public License for more details.
|
---|
14 | #
|
---|
15 | # You should have received a copy of the GNU General Public License
|
---|
16 | # along with this program; if not, write to the Free Software
|
---|
17 | # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
---|
18 |
|
---|
19 | import xdelta3
|
---|
20 |
|
---|
21 | # the test data section is expected to be len('target')
|
---|
22 | source = 'source source input0 source source'
|
---|
23 | target = 'source source target source source'
|
---|
24 |
|
---|
25 | #
|
---|
26 | #
|
---|
27 |
|
---|
28 | print 'encode: basic ...'
|
---|
29 | result, patch = xdelta3.xd3_encode_memory(target, source, 50)
|
---|
30 |
|
---|
31 | assert result == 0
|
---|
32 | assert len(patch) < len(source)
|
---|
33 |
|
---|
34 | print 'encode: adler32 ...'
|
---|
35 | result, patch_adler32 = xdelta3.xd3_encode_memory(target, source, 50,
|
---|
36 | xdelta3.XD3_ADLER32)
|
---|
37 |
|
---|
38 | assert result == 0
|
---|
39 | assert len(patch_adler32) < len(source)
|
---|
40 | assert len(patch_adler32) > len(patch)
|
---|
41 |
|
---|
42 | print 'encode: secondary ...'
|
---|
43 | result, patch_djw = xdelta3.xd3_encode_memory(target, source, 50,
|
---|
44 | xdelta3.XD3_SEC_DJW)
|
---|
45 |
|
---|
46 | assert result == 0
|
---|
47 | # secondary compression doesn't help
|
---|
48 | assert len(patch_djw) > len(patch)
|
---|
49 |
|
---|
50 | print 'encode: exact ...'
|
---|
51 | result, ignore = xdelta3.xd3_encode_memory(target, source, len(patch))
|
---|
52 |
|
---|
53 | assert result == 0
|
---|
54 | assert len(ignore) < len(source)
|
---|
55 |
|
---|
56 | print 'encode: out of space ...'
|
---|
57 | result, ignore = xdelta3.xd3_encode_memory(target, source, len(patch) - 1)
|
---|
58 |
|
---|
59 | assert result == 28
|
---|
60 | assert ignore == None
|
---|
61 |
|
---|
62 | print 'encode: zero space ...'
|
---|
63 | result, ignore = xdelta3.xd3_encode_memory(target, source, 0)
|
---|
64 |
|
---|
65 | assert result == 28
|
---|
66 | assert ignore == None
|
---|
67 |
|
---|
68 | print 'encode: no source ...'
|
---|
69 | result, zdata = xdelta3.xd3_encode_memory(target, None, 50)
|
---|
70 |
|
---|
71 | assert result == 0
|
---|
72 | assert len(zdata) > len(patch)
|
---|
73 |
|
---|
74 | print 'encode: no input ...'
|
---|
75 | result, ignore = xdelta3.xd3_encode_memory(None, None, 50)
|
---|
76 |
|
---|
77 | assert result != 0
|
---|
78 |
|
---|
79 | print 'decode: basic ...'
|
---|
80 | result, target1 = xdelta3.xd3_decode_memory(patch, source, len(target))
|
---|
81 |
|
---|
82 | assert result == 0
|
---|
83 | assert len(target1) == len(target)
|
---|
84 | assert target1 == target
|
---|
85 |
|
---|
86 | print 'decode: out of space ...'
|
---|
87 | result, ignore = xdelta3.xd3_decode_memory(patch, source, len(target) - 1)
|
---|
88 |
|
---|
89 | assert result == 28
|
---|
90 | assert ignore == None
|
---|
91 |
|
---|
92 | print 'decode: zero space ...'
|
---|
93 | result, ignore = xdelta3.xd3_decode_memory(patch, source, 0)
|
---|
94 |
|
---|
95 | assert result == 28
|
---|
96 | assert ignore == None
|
---|
97 |
|
---|
98 | print 'decode: single byte error ...'
|
---|
99 | # a few expected single-byte errors, e.g., unused address cache bits, see
|
---|
100 | # xdelta3-test.h's single-bit error tests
|
---|
101 | extra_count = 4
|
---|
102 | noverify_count = 0
|
---|
103 | for corrupt_pos in range(len(patch_adler32)):
|
---|
104 | input = ''.join([j == corrupt_pos and '\xff' or patch_adler32[j]
|
---|
105 | for j in range(len(patch_adler32))])
|
---|
106 |
|
---|
107 | result, ignore = xdelta3.xd3_decode_memory(input, source, len(target), 0)
|
---|
108 | assert result == -17712
|
---|
109 | assert ignore == None
|
---|
110 |
|
---|
111 | # without adler32 verification, the error may be in the data section which
|
---|
112 | # in this case is 6 bytes 'target'
|
---|
113 | result, corrupt = xdelta3.xd3_decode_memory(input, source, len(target),
|
---|
114 | xdelta3.XD3_ADLER32_NOVER)
|
---|
115 | if result == 0:
|
---|
116 | noverify_count = noverify_count + 1
|
---|
117 | #print "got %s" % corrupt
|
---|
118 | #end
|
---|
119 | #end
|
---|
120 | assert noverify_count == len('target') + extra_count
|
---|
121 |
|
---|
122 | print 'decode: no source ...'
|
---|
123 | result, target2 = xdelta3.xd3_decode_memory(zdata, None, len(target))
|
---|
124 |
|
---|
125 | assert result == 0
|
---|
126 | assert target == target2
|
---|
127 |
|
---|
128 | # Test compression level setting via flags. assumes a 9 byte checksum
|
---|
129 | # and that level 9 steps 2, level 1 steps 15:
|
---|
130 | # 01234567890123456789012345678901
|
---|
131 | # level 1 only indexes 2 checksums "abcdefghi" and "ABCDEFGHI"
|
---|
132 | # outputs 43 vs. 23 bytes
|
---|
133 | print 'encode: compression level ...'
|
---|
134 |
|
---|
135 | source = '_la_la_abcdefghi_la_la_ABCDEFGHI'
|
---|
136 | target = 'la_la_ABCDEFGH__la_la_abcdefgh__'
|
---|
137 |
|
---|
138 | result1, level1 = xdelta3.xd3_encode_memory(target, source, 50, xdelta3.XD3_COMPLEVEL_1)
|
---|
139 | result9, level9 = xdelta3.xd3_encode_memory(target, source, 50, xdelta3.XD3_COMPLEVEL_9)
|
---|
140 |
|
---|
141 | assert result1 == 0 and result9 == 0
|
---|
142 | assert len(level1) > len(level9)
|
---|
143 |
|
---|
144 | #
|
---|
145 | #
|
---|
146 |
|
---|
147 | print 'PASS'
|
---|