diff options
| author | Serhiy Storchaka <storchaka@gmail.com> | 2018-03-18 07:55:53 (GMT) |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2018-03-18 07:55:53 (GMT) |
| commit | 134cb01cda50f02725575808130b05d2d776693f (patch) | |
| tree | 25cf0f7f9b0404c2e9c6addc7584a24f63f64ca6 /Lib/test/test_sunau.py | |
| parent | bc300ce205f99acb1ef92c37de06dc76147e073b (diff) | |
| download | cpython-134cb01cda50f02725575808130b05d2d776693f.zip cpython-134cb01cda50f02725575808130b05d2d776693f.tar.gz cpython-134cb01cda50f02725575808130b05d2d776693f.tar.bz2 | |
bpo-32056: Improve exceptions in aifc, wave and sunau. (GH-5951)
Diffstat (limited to 'Lib/test/test_sunau.py')
| -rw-r--r-- | Lib/test/test_sunau.py | 37 |
1 files changed, 37 insertions, 0 deletions
diff --git a/Lib/test/test_sunau.py b/Lib/test/test_sunau.py index 966224b..470a100 100644 --- a/Lib/test/test_sunau.py +++ b/Lib/test/test_sunau.py @@ -1,6 +1,8 @@ import unittest from test import audiotests from audioop import byteswap +import io +import struct import sys import sunau @@ -121,5 +123,40 @@ class SunauMiscTests(audiotests.AudioMiscTests, unittest.TestCase): module = sunau +class SunauLowLevelTest(unittest.TestCase): + + def test_read_bad_magic_number(self): + b = b'SPA' + with self.assertRaises(EOFError): + sunau.open(io.BytesIO(b)) + b = b'SPAM' + with self.assertRaisesRegex(sunau.Error, 'bad magic number'): + sunau.open(io.BytesIO(b)) + + def test_read_too_small_header(self): + b = struct.pack('>LLLLL', sunau.AUDIO_FILE_MAGIC, 20, 0, + sunau.AUDIO_FILE_ENCODING_LINEAR_8, 11025) + with self.assertRaisesRegex(sunau.Error, 'header size too small'): + sunau.open(io.BytesIO(b)) + + def test_read_too_large_header(self): + b = struct.pack('>LLLLLL', sunau.AUDIO_FILE_MAGIC, 124, 0, + sunau.AUDIO_FILE_ENCODING_LINEAR_8, 11025, 1) + b += b'\0' * 100 + with self.assertRaisesRegex(sunau.Error, 'header size ridiculously large'): + sunau.open(io.BytesIO(b)) + + def test_read_wrong_encoding(self): + b = struct.pack('>LLLLLL', sunau.AUDIO_FILE_MAGIC, 24, 0, 0, 11025, 1) + with self.assertRaisesRegex(sunau.Error, r'encoding not \(yet\) supported'): + sunau.open(io.BytesIO(b)) + + def test_read_wrong_number_of_channels(self): + b = struct.pack('>LLLLLL', sunau.AUDIO_FILE_MAGIC, 24, 0, + sunau.AUDIO_FILE_ENCODING_LINEAR_8, 11025, 0) + with self.assertRaisesRegex(sunau.Error, 'bad # of channels'): + sunau.open(io.BytesIO(b)) + + if __name__ == "__main__": unittest.main() |
