1 # This Source Code Form is subject to the terms of the Mozilla Public
2 # License, v. 2.0. If a copy of the MPL was not distributed with this
3 # file, You can obtain one at http://mozilla.org/MPL/2.0/.
11 from string
import letters
14 Test case infrastructure for MozZipFile.
16 This isn't really a unit test, but a test case generator and runner.
17 For a given set of files, lengths, and number of writes, we create
18 a testcase for every combination of the three. There are some
19 symmetries used to reduce the number of test cases, the first file
20 written is always the first file, the second is either the first or
21 the second, the third is one of the first three. That is, if we
22 had 4 files, but only three writes, the fourth file would never even
25 The content written to the jars is pseudorandom with a fixed seed.
29 __file__
= sys
.argv
[0]
30 sys
.path
.append(os
.path
.join(os
.path
.dirname(__file__
), ".."))
34 from MozZipFile
import ZipFile
36 leafs
= ("firstdir/oneleaf", "seconddir/twoleaf", "thirddir/with/sub/threeleaf")
37 _lengths
= map(lambda n
: n
* 64, [16, 64, 80])
43 """Return a length given in the _lengths array to allow manual
44 tuning of which lengths of zip entries to use.
50 """'Tensor product of a list of iterables.
52 This generator returns lists of items, one of each given
53 iterable. It iterates over all possible combinations.
55 for item
in iterables
[0]:
56 if len(iterables
) == 1:
59 for others
in prod(*iterables
[1:]):
64 "Convert a list of ints to a string."
65 return reduce(lambda x
, y
: x
+ "{0}{1}".format(*tuple(y
)), descs
, "")
68 def getContent(length
):
69 "Get pseudo random content of given length."
71 for i
in xrange(length
):
72 rv
[i
] = random
.choice(letters
)
76 def createWriter(sizer
, *items
):
77 "Helper method to fill in tests, one set of writes, one for each item"
78 locitems
= copy
.deepcopy(items
)
80 item
["length"] = sizer(item
.pop("length", 0))
84 if os
.path
.isfile(self
.f
):
86 zf
= ZipFile(self
.f
, mode
, self
.compression
)
88 self
._write
(zf
, **item
)
95 def createTester(name
, *writes
):
96 """Helper method to fill in tests, calls into a list of write
99 _writes
= copy
.copy(writes
)
107 # unit tests get confused if the method name isn't test...
108 tester
.__name
__ = name
112 class TestExtensiveStored(unittest
.TestCase
):
113 """Unit tests for MozZipFile
115 The testcase are actually populated by code following the class
119 stage
= "mozzipfilestage"
120 compression
= zipfile
.ZIP_STORED
122 def leaf(self
, *leafs
):
123 return os
.path
.join(self
.stage
, *leafs
)
126 if os
.path
.exists(self
.stage
):
127 shutil
.rmtree(self
.stage
)
129 self
.f
= self
.leaf("test.jar")
137 def _verifyZip(self
):
138 zf
= zipfile
.ZipFile(self
.f
)
139 badEntry
= zf
.testzip()
140 self
.failIf(badEntry
, badEntry
)
141 zlist
= zf
.namelist()
143 vlist
= self
.ref
.keys()
145 self
.assertEqual(zlist
, vlist
)
146 for leaf
, content
in self
.ref
.iteritems():
147 zcontent
= zf
.read(leaf
)
148 self
.assertEqual(content
, zcontent
)
150 def _write(self
, zf
, seed
=None, leaf
=0, length
=0):
156 content
= getContent(length
)
157 self
.ref
[leaf
] = content
158 zf
.writestr(leaf
, content
)
159 dir = os
.path
.dirname(self
.leaf("stage", leaf
))
160 if not os
.path
.isdir(dir):
162 open(self
.leaf("stage", leaf
), "w").write(content
)
165 # all leafs in all lengths
166 atomics
= list(prod(xrange(len(leafs
)), xrange(lengths
)))
168 # populate TestExtensiveStore with testcases
169 for w
in xrange(writes
):
170 # Don't iterate over all files for the the first n passes,
171 # those are redundant as long as w < lengths.
172 # There are symmetries in the trailing end, too, but I don't know
173 # how to reduce those out right now.
175 list(prod(range(min(i
, len(leafs
))), xrange(lengths
))) for i
in xrange(1, w
+ 1)
177 for descs
in prod(*nonatomics
):
178 suffix
= getid(descs
)
179 dicts
= [dict(leaf
=leaf
, length
=length
) for leaf
, length
in descs
]
181 TestExtensiveStored
, "_write" + suffix
, createWriter(givenlength
, *dicts
)
186 createTester("test" + suffix
, "_write" + suffix
),
189 # now create another round of tests, with two writing passes
190 # first, write all file combinations into the jar, close it,
191 # and then write all atomics again.
192 # This should catch more or less all artifacts generated
193 # by the final ordering step when closing the jar.
194 files
= [list(prod([i
], xrange(lengths
))) for i
in xrange(len(leafs
))]
196 lambda l
, r
: l
+ r
, [list(prod(*files
[: (i
+ 1)])) for i
in xrange(len(leafs
))]
199 for first
in allfiles
:
200 testbasename
= "test{0}_".format(getid(first
))
201 test
= [None, "_write" + getid(first
), None]
202 for second
in atomics
:
203 test
[0] = testbasename
+ getid([second
])
204 test
[2] = "_write" + getid([second
])
205 setattr(TestExtensiveStored
, test
[0], createTester(*test
))
208 class TestExtensiveDeflated(TestExtensiveStored
):
209 "Test all that has been tested with ZIP_STORED with DEFLATED, too."
210 compression
= zipfile
.ZIP_DEFLATED
213 if __name__
== "__main__":