diff options
| author | Serhiy Storchaka <storchaka@gmail.com> | 2018-03-18 09:55:53 +0200 | 
|---|---|---|
| committer | GitHub <noreply@github.com> | 2018-03-18 09:55:53 +0200 | 
| commit | 134cb01cda50f02725575808130b05d2d776693f (patch) | |
| tree | 25cf0f7f9b0404c2e9c6addc7584a24f63f64ca6 /Lib/test/test_wave.py | |
| parent | bc300ce205f99acb1ef92c37de06dc76147e073b (diff) | |
| download | cpython-git-134cb01cda50f02725575808130b05d2d776693f.tar.gz | |
bpo-32056: Improve exceptions in aifc, wave and sunau. (GH-5951)
Diffstat (limited to 'Lib/test/test_wave.py')
| -rw-r--r-- | Lib/test/test_wave.py | 62 | 
1 files changed, 62 insertions, 0 deletions
| diff --git a/Lib/test/test_wave.py b/Lib/test/test_wave.py index c5d2e02450..8a42f8e471 100644 --- a/Lib/test/test_wave.py +++ b/Lib/test/test_wave.py @@ -2,6 +2,8 @@ import unittest  from test import audiotests  from test import support  from audioop import byteswap +import io +import struct  import sys  import wave @@ -111,5 +113,65 @@ class MiscTestCase(audiotests.AudioMiscTests, unittest.TestCase):          support.check__all__(self, wave, blacklist=blacklist) +class WaveLowLevelTest(unittest.TestCase): + +    def test_read_no_chunks(self): +        b = b'SPAM' +        with self.assertRaises(EOFError): +            wave.open(io.BytesIO(b)) + +    def test_read_no_riff_chunk(self): +        b = b'SPAM' + struct.pack('<L', 0) +        with self.assertRaisesRegex(wave.Error, +                                    'file does not start with RIFF id'): +            wave.open(io.BytesIO(b)) + +    def test_read_not_wave(self): +        b = b'RIFF' + struct.pack('<L', 4) + b'SPAM' +        with self.assertRaisesRegex(wave.Error, +                                    'not a WAVE file'): +            wave.open(io.BytesIO(b)) + +    def test_read_no_fmt_no_data_chunk(self): +        b = b'RIFF' + struct.pack('<L', 4) + b'WAVE' +        with self.assertRaisesRegex(wave.Error, +                                    'fmt chunk and/or data chunk missing'): +            wave.open(io.BytesIO(b)) + +    def test_read_no_data_chunk(self): +        b = b'RIFF' + struct.pack('<L', 28) + b'WAVE' +        b += b'fmt ' + struct.pack('<LHHLLHH', 16, 1, 1, 11025, 11025, 1, 8) +        with self.assertRaisesRegex(wave.Error, +                                    'fmt chunk and/or data chunk missing'): +            wave.open(io.BytesIO(b)) + +    def test_read_no_fmt_chunk(self): +        b = b'RIFF' + struct.pack('<L', 12) + b'WAVE' +        b += b'data' + struct.pack('<L', 0) +        with self.assertRaisesRegex(wave.Error, 'data chunk before fmt chunk'): +            wave.open(io.BytesIO(b)) + +    def test_read_wrong_form(self): +        b = b'RIFF' + struct.pack('<L', 36) + b'WAVE' +        b += b'fmt ' + struct.pack('<LHHLLHH', 16, 2, 1, 11025, 11025, 1, 1) +        b += b'data' + struct.pack('<L', 0) +        with self.assertRaisesRegex(wave.Error, 'unknown format: 2'): +            wave.open(io.BytesIO(b)) + +    def test_read_wrong_number_of_channels(self): +        b = b'RIFF' + struct.pack('<L', 36) + b'WAVE' +        b += b'fmt ' + struct.pack('<LHHLLHH', 16, 1, 0, 11025, 11025, 1, 8) +        b += b'data' + struct.pack('<L', 0) +        with self.assertRaisesRegex(wave.Error, 'bad # of channels'): +            wave.open(io.BytesIO(b)) + +    def test_read_wrong_sample_width(self): +        b = b'RIFF' + struct.pack('<L', 36) + b'WAVE' +        b += b'fmt ' + struct.pack('<LHHLLHH', 16, 1, 1, 11025, 11025, 1, 0) +        b += b'data' + struct.pack('<L', 0) +        with self.assertRaisesRegex(wave.Error, 'bad sample width'): +            wave.open(io.BytesIO(b)) + +  if __name__ == '__main__':      unittest.main() | 
