|
5 | 5 | Roger E. Masse
|
6 | 6 | """
|
7 | 7 |
|
8 |
| -from test.test_support import verbose, unlink |
| 8 | +from test.test_support import verbose, unlink, run_unittest |
| 9 | + |
| 10 | +import imageop, uu, os, unittest |
| 11 | + |
| 12 | +SIZES = (1, 2, 3, 4) |
| 13 | +_VALUES = (1, 2, 2**10, 2**15-1, 2**15, 2**15+1, 2**31-2, 2**31-1) |
| 14 | +VALUES = tuple( -x for x in reversed(_VALUES) ) + (0,) + _VALUES |
| 15 | +AAAAA = "A" * 1024 |
| 16 | + |
| 17 | + |
| 18 | +class InputValidationTests(unittest.TestCase): |
| 19 | + |
| 20 | + def _check(self, name, size=None, *extra): |
| 21 | + func = getattr(imageop, name) |
| 22 | + for height in VALUES: |
| 23 | + for width in VALUES: |
| 24 | + strlen = abs(width * height) |
| 25 | + if size: |
| 26 | + strlen *= size |
| 27 | + if strlen < 1024: |
| 28 | + data = "A" * strlen |
| 29 | + else: |
| 30 | + data = AAAAA |
| 31 | + if size: |
| 32 | + arguments = (data, size, width, height) + extra |
| 33 | + else: |
| 34 | + arguments = (data, width, height) + extra |
| 35 | + try: |
| 36 | + func(*arguments) |
| 37 | + except (ValueError, imageop.error): |
| 38 | + pass |
| 39 | + |
| 40 | + def check_size(self, name, *extra): |
| 41 | + for size in SIZES: |
| 42 | + self._check(name, size, *extra) |
| 43 | + |
| 44 | + def check(self, name, *extra): |
| 45 | + self._check(name, None, *extra) |
| 46 | + |
| 47 | + def test_input_validation(self): |
| 48 | + self.check_size("crop", 0, 0, 0, 0) |
| 49 | + self.check_size("scale", 1, 0) |
| 50 | + self.check_size("scale", -1, -1) |
| 51 | + self.check_size("tovideo") |
| 52 | + self.check("grey2mono", 128) |
| 53 | + self.check("grey2grey4") |
| 54 | + self.check("grey2grey2") |
| 55 | + self.check("dither2mono") |
| 56 | + self.check("dither2grey2") |
| 57 | + self.check("mono2grey", 0, 0) |
| 58 | + self.check("grey22grey") |
| 59 | + self.check("rgb2rgb8") # nlen*4 == len |
| 60 | + self.check("rgb82rgb") |
| 61 | + self.check("rgb2grey") |
| 62 | + self.check("grey2rgb") |
| 63 | + |
| 64 | + |
| 65 | +def test_main(use_rgbimg=True): |
| 66 | + run_unittest(InputValidationTests) |
9 | 67 |
|
10 |
| -import imageop, uu, os |
11 |
| - |
12 |
| -def main(use_rgbimg=1): |
| 68 | + try: |
| 69 | + import imgfile |
| 70 | + except ImportError: |
| 71 | + return |
13 | 72 |
|
14 | 73 | # Create binary test files
|
15 | 74 | uu.decode(get_qualified_path('testrgb'+os.extsep+'uue'), 'test'+os.extsep+'rgb')
|
@@ -165,7 +224,3 @@ def get_qualified_path(name):
|
165 | 224 | if os.path.exists(fullname):
|
166 | 225 | return fullname
|
167 | 226 | return name
|
168 |
| - |
169 |
| -# rgbimg (unlike imgfile) is portable to platforms other than SGI. |
170 |
| -# So we prefer to use it. |
171 |
| -main(use_rgbimg=1) |
|
0 commit comments