summaryrefslogtreecommitdiffstats
path: root/Lib/test
diff options
context:
space:
mode:
authorSerhiy Storchaka <storchaka@gmail.com>2018-03-18 07:55:53 (GMT)
committerGitHub <noreply@github.com>2018-03-18 07:55:53 (GMT)
commit134cb01cda50f02725575808130b05d2d776693f (patch)
tree25cf0f7f9b0404c2e9c6addc7584a24f63f64ca6 /Lib/test
parentbc300ce205f99acb1ef92c37de06dc76147e073b (diff)
downloadcpython-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')
-rw-r--r--Lib/test/test_aifc.py35
-rw-r--r--Lib/test/test_sunau.py37
-rw-r--r--Lib/test/test_wave.py62
3 files changed, 129 insertions, 5 deletions
diff --git a/Lib/test/test_aifc.py b/Lib/test/test_aifc.py
index 8fd306a..ff52f5b 100644
--- a/Lib/test/test_aifc.py
+++ b/Lib/test/test_aifc.py
@@ -268,7 +268,8 @@ class AIFCLowLevelTest(unittest.TestCase):
def test_read_no_ssnd_chunk(self):
b = b'FORM' + struct.pack('>L', 4) + b'AIFC'
- b += b'COMM' + struct.pack('>LhlhhLL', 38, 0, 0, 0, 0, 0, 0)
+ b += b'COMM' + struct.pack('>LhlhhLL', 38, 1, 0, 8,
+ 0x4000 | 12, 11025<<18, 0)
b += b'NONE' + struct.pack('B', 14) + b'not compressed' + b'\x00'
with self.assertRaisesRegex(aifc.Error, 'COMM chunk and/or SSND chunk'
' missing'):
@@ -276,13 +277,35 @@ class AIFCLowLevelTest(unittest.TestCase):
def test_read_wrong_compression_type(self):
b = b'FORM' + struct.pack('>L', 4) + b'AIFC'
- b += b'COMM' + struct.pack('>LhlhhLL', 23, 0, 0, 0, 0, 0, 0)
+ b += b'COMM' + struct.pack('>LhlhhLL', 23, 1, 0, 8,
+ 0x4000 | 12, 11025<<18, 0)
b += b'WRNG' + struct.pack('B', 0)
self.assertRaises(aifc.Error, aifc.open, io.BytesIO(b))
+ def test_read_wrong_number_of_channels(self):
+ for nchannels in 0, -1:
+ b = b'FORM' + struct.pack('>L', 4) + b'AIFC'
+ b += b'COMM' + struct.pack('>LhlhhLL', 38, nchannels, 0, 8,
+ 0x4000 | 12, 11025<<18, 0)
+ b += b'NONE' + struct.pack('B', 14) + b'not compressed' + b'\x00'
+ b += b'SSND' + struct.pack('>L', 8) + b'\x00' * 8
+ with self.assertRaisesRegex(aifc.Error, 'bad # of channels'):
+ aifc.open(io.BytesIO(b))
+
+ def test_read_wrong_sample_width(self):
+ for sampwidth in 0, -1:
+ b = b'FORM' + struct.pack('>L', 4) + b'AIFC'
+ b += b'COMM' + struct.pack('>LhlhhLL', 38, 1, 0, sampwidth,
+ 0x4000 | 12, 11025<<18, 0)
+ b += b'NONE' + struct.pack('B', 14) + b'not compressed' + b'\x00'
+ b += b'SSND' + struct.pack('>L', 8) + b'\x00' * 8
+ with self.assertRaisesRegex(aifc.Error, 'bad sample width'):
+ aifc.open(io.BytesIO(b))
+
def test_read_wrong_marks(self):
b = b'FORM' + struct.pack('>L', 4) + b'AIFF'
- b += b'COMM' + struct.pack('>LhlhhLL', 18, 0, 0, 0, 0, 0, 0)
+ b += b'COMM' + struct.pack('>LhlhhLL', 18, 1, 0, 8,
+ 0x4000 | 12, 11025<<18, 0)
b += b'SSND' + struct.pack('>L', 8) + b'\x00' * 8
b += b'MARK' + struct.pack('>LhB', 3, 1, 1)
with self.assertWarns(UserWarning) as cm:
@@ -293,7 +316,8 @@ class AIFCLowLevelTest(unittest.TestCase):
def test_read_comm_kludge_compname_even(self):
b = b'FORM' + struct.pack('>L', 4) + b'AIFC'
- b += b'COMM' + struct.pack('>LhlhhLL', 18, 0, 0, 0, 0, 0, 0)
+ b += b'COMM' + struct.pack('>LhlhhLL', 18, 1, 0, 8,
+ 0x4000 | 12, 11025<<18, 0)
b += b'NONE' + struct.pack('B', 4) + b'even' + b'\x00'
b += b'SSND' + struct.pack('>L', 8) + b'\x00' * 8
with self.assertWarns(UserWarning) as cm:
@@ -303,7 +327,8 @@ class AIFCLowLevelTest(unittest.TestCase):
def test_read_comm_kludge_compname_odd(self):
b = b'FORM' + struct.pack('>L', 4) + b'AIFC'
- b += b'COMM' + struct.pack('>LhlhhLL', 18, 0, 0, 0, 0, 0, 0)
+ b += b'COMM' + struct.pack('>LhlhhLL', 18, 1, 0, 8,
+ 0x4000 | 12, 11025<<18, 0)
b += b'NONE' + struct.pack('B', 3) + b'odd'
b += b'SSND' + struct.pack('>L', 8) + b'\x00' * 8
with self.assertWarns(UserWarning) as cm:
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()
diff --git a/Lib/test/test_wave.py b/Lib/test/test_wave.py
index c5d2e02..8a42f8e4 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()