1from test.support import check_no_resource_warning, findfile, TESTFN, unlink
2import unittest
3from unittest import mock
4from test import audiotests
5from audioop import byteswap
6import io
7import sys
8import struct
9import aifc
10import warnings
11
12
13class AifcTest(audiotests.AudioWriteTests,
14               audiotests.AudioTestsWithSourceFile):
15    module = aifc
16    close_fd = True
17    test_unseekable_read = None
18
19
20class AifcPCM8Test(AifcTest, unittest.TestCase):
21    sndfilename = 'pluck-pcm8.aiff'
22    sndfilenframes = 3307
23    nchannels = 2
24    sampwidth = 1
25    framerate = 11025
26    nframes = 48
27    comptype = b'NONE'
28    compname = b'not compressed'
29    frames = bytes.fromhex("""\
30      02FF 4B00 3104 8008 CB06 4803 BF01 03FE B8FA B4F3 29EB 1AE6 \
31      EDE4 C6E2 0EE0 EFE0 57E2 FBE8 13EF D8F7 97FB F5FC 08FB DFFB \
32      11FA 3EFB BCFC 66FF CF04 4309 C10E 5112 EE17 8216 7F14 8012 \
33      490E 520D EF0F CE0F E40C 630A 080A 2B0B 510E 8B11 B60E 440A \
34      """)
35
36
37class AifcPCM16Test(AifcTest, unittest.TestCase):
38    sndfilename = 'pluck-pcm16.aiff'
39    sndfilenframes = 3307
40    nchannels = 2
41    sampwidth = 2
42    framerate = 11025
43    nframes = 48
44    comptype = b'NONE'
45    compname = b'not compressed'
46    frames = bytes.fromhex("""\
47      022EFFEA 4B5D00F6 311804EA 80E10840 CBE106B1 48A903F5 BFE601B2 036CFE7B \
48      B858FA3E B4B1F34F 299AEBCA 1A5DE6DA EDFAE491 C628E275 0E09E0B5 EF2AE029 \
49      5758E271 FB35E83F 1376EF86 D82BF727 9790FB76 F5FAFC0F 0867FB9C DF30FB43 \
50      117EFA36 3EE5FB5B BC79FCB1 66D9FF5D CF150412 431D097C C1BA0EC8 512112A1 \
51      EEE21753 82071665 7FFF1443 8004128F 49A20EAF 52BB0DBA EFB40F60 CE3C0FBF \
52      E4B30CEC 63430A5C 08C80A20 2BBB0B08 514A0E43 8BCF1139 B6F60EEB 44120A5E \
53      """)
54
55
56class AifcPCM24Test(AifcTest, unittest.TestCase):
57    sndfilename = 'pluck-pcm24.aiff'
58    sndfilenframes = 3307
59    nchannels = 2
60    sampwidth = 3
61    framerate = 11025
62    nframes = 48
63    comptype = b'NONE'
64    compname = b'not compressed'
65    frames = bytes.fromhex("""\
66      022D65FFEB9D 4B5A0F00FA54 3113C304EE2B 80DCD6084303 \
67      CBDEC006B261 48A99803F2F8 BFE82401B07D 036BFBFE7B5D \
68      B85756FA3EC9 B4B055F3502B 299830EBCB62 1A5CA7E6D99A \
69      EDFA3EE491BD C625EBE27884 0E05A9E0B6CF EF2929E02922 \
70      5758D8E27067 FB3557E83E16 1377BFEF8402 D82C5BF7272A \
71      978F16FB7745 F5F865FC1013 086635FB9C4E DF30FCFB40EE \
72      117FE0FA3438 3EE6B8FB5AC3 BC77A3FCB2F4 66D6DAFF5F32 \
73      CF13B9041275 431D69097A8C C1BB600EC74E 5120B912A2BA \
74      EEDF641754C0 8207001664B7 7FFFFF14453F 8000001294E6 \
75      499C1B0EB3B2 52B73E0DBCA0 EFB2B20F5FD8 CE3CDB0FBE12 \
76      E4B49C0CEA2D 6344A80A5A7C 08C8FE0A1FFE 2BB9860B0A0E \
77      51486F0E44E1 8BCC64113B05 B6F4EC0EEB36 4413170A5B48 \
78      """)
79
80
81class AifcPCM32Test(AifcTest, unittest.TestCase):
82    sndfilename = 'pluck-pcm32.aiff'
83    sndfilenframes = 3307
84    nchannels = 2
85    sampwidth = 4
86    framerate = 11025
87    nframes = 48
88    comptype = b'NONE'
89    compname = b'not compressed'
90    frames = bytes.fromhex("""\
91      022D65BCFFEB9D92 4B5A0F8000FA549C 3113C34004EE2BC0 80DCD680084303E0 \
92      CBDEC0C006B26140 48A9980003F2F8FC BFE8248001B07D92 036BFB60FE7B5D34 \
93      B8575600FA3EC920 B4B05500F3502BC0 29983000EBCB6240 1A5CA7A0E6D99A60 \
94      EDFA3E80E491BD40 C625EB80E27884A0 0E05A9A0E0B6CFE0 EF292940E0292280 \
95      5758D800E2706700 FB3557D8E83E1640 1377BF00EF840280 D82C5B80F7272A80 \
96      978F1600FB774560 F5F86510FC101364 086635A0FB9C4E20 DF30FC40FB40EE28 \
97      117FE0A0FA3438B0 3EE6B840FB5AC3F0 BC77A380FCB2F454 66D6DA80FF5F32B4 \
98      CF13B980041275B0 431D6980097A8C00 C1BB60000EC74E00 5120B98012A2BAA0 \
99      EEDF64C01754C060 820700001664B780 7FFFFFFF14453F40 800000001294E6E0 \
100      499C1B000EB3B270 52B73E000DBCA020 EFB2B2E00F5FD880 CE3CDB400FBE1270 \
101      E4B49CC00CEA2D90 6344A8800A5A7CA0 08C8FE800A1FFEE0 2BB986C00B0A0E00 \
102      51486F800E44E190 8BCC6480113B0580 B6F4EC000EEB3630 441317800A5B48A0 \
103      """)
104
105
106class AifcULAWTest(AifcTest, unittest.TestCase):
107    sndfilename = 'pluck-ulaw.aifc'
108    sndfilenframes = 3307
109    nchannels = 2
110    sampwidth = 2
111    framerate = 11025
112    nframes = 48
113    comptype = b'ulaw'
114    compname = b''
115    frames = bytes.fromhex("""\
116      022CFFE8 497C0104 307C04DC 8284083C CB84069C 497C03DC BE8401AC 036CFE74 \
117      B684FA24 B684F344 2A7CEC04 19FCE704 EE04E504 C584E204 0E3CE104 EF04DF84 \
118      557CE204 FB24E804 12FCEF04 D784F744 9684FB64 F5C4FC24 083CFBA4 DF84FB24 \
119      11FCFA24 3E7CFB64 BA84FCB4 657CFF5C CF84041C 417C093C C1840EBC 517C12FC \
120      EF0416FC 828415FC 7D7C13FC 828412FC 497C0EBC 517C0DBC F0040F3C CD840FFC \
121      E5040CBC 617C0A3C 08BC0A3C 2C7C0B3C 517C0E3C 8A8410FC B6840EBC 457C0A3C \
122      """)
123    if sys.byteorder != 'big':
124        frames = byteswap(frames, 2)
125
126
127class AifcALAWTest(AifcTest, unittest.TestCase):
128    sndfilename = 'pluck-alaw.aifc'
129    sndfilenframes = 3307
130    nchannels = 2
131    sampwidth = 2
132    framerate = 11025
133    nframes = 48
134    comptype = b'alaw'
135    compname = b''
136    frames = bytes.fromhex("""\
137      0230FFE8 4A0000F8 310004E0 82000840 CB0006A0 4A0003F0 BE0001A8 0370FE78 \
138      BA00FA20 B600F340 2900EB80 1A80E680 ED80E480 C700E280 0E40E080 EF80E080 \
139      5600E280 FB20E880 1380EF80 D900F740 9600FB60 F5C0FC10 0840FBA0 DF00FB20 \
140      1180FA20 3F00FB60 BE00FCB0 6600FF58 CF000420 42000940 C1000EC0 52001280 \
141      EE801780 82001680 7E001480 82001280 4A000EC0 52000DC0 EF800F40 CF000FC0 \
142      E4800CC0 62000A40 08C00A40 2B000B40 52000E40 8A001180 B6000EC0 46000A40 \
143      """)
144    if sys.byteorder != 'big':
145        frames = byteswap(frames, 2)
146
147
148class AifcMiscTest(audiotests.AudioMiscTests, unittest.TestCase):
149    module = aifc
150
151    def test_skipunknown(self):
152        #Issue 2245
153        #This file contains chunk types aifc doesn't recognize.
154        self.f = aifc.open(findfile('Sine-1000Hz-300ms.aif'))
155
156    def test_close_opened_files_on_error(self):
157        non_aifc_file = findfile('pluck-pcm8.wav', subdir='audiodata')
158        with check_no_resource_warning(self):
159            with self.assertRaises(aifc.Error):
160                # Try opening a non-AIFC file, with the expectation that
161                # `aifc.open` will fail (without raising a ResourceWarning)
162                self.f = aifc.open(non_aifc_file, 'rb')
163
164            # Aifc_write.initfp() won't raise in normal case.  But some errors
165            # (e.g. MemoryError, KeyboardInterrupt, etc..) can happen.
166            with mock.patch.object(aifc.Aifc_write, 'initfp',
167                                   side_effect=RuntimeError):
168                with self.assertRaises(RuntimeError):
169                    self.fout = aifc.open(TESTFN, 'wb')
170
171    def test_params_added(self):
172        f = self.f = aifc.open(TESTFN, 'wb')
173        f.aiff()
174        f.setparams((1, 1, 1, 1, b'NONE', b''))
175        f.close()
176
177        f = self.f = aifc.open(TESTFN, 'rb')
178        params = f.getparams()
179        self.assertEqual(params.nchannels, f.getnchannels())
180        self.assertEqual(params.sampwidth, f.getsampwidth())
181        self.assertEqual(params.framerate, f.getframerate())
182        self.assertEqual(params.nframes, f.getnframes())
183        self.assertEqual(params.comptype, f.getcomptype())
184        self.assertEqual(params.compname, f.getcompname())
185
186    def test_write_header_comptype_sampwidth(self):
187        for comptype in (b'ULAW', b'ulaw', b'ALAW', b'alaw', b'G722'):
188            fout = aifc.open(io.BytesIO(), 'wb')
189            fout.setnchannels(1)
190            fout.setframerate(1)
191            fout.setcomptype(comptype, b'')
192            fout.close()
193            self.assertEqual(fout.getsampwidth(), 2)
194            fout.initfp(None)
195
196    def test_write_markers_values(self):
197        fout = aifc.open(io.BytesIO(), 'wb')
198        self.assertEqual(fout.getmarkers(), None)
199        fout.setmark(1, 0, b'foo1')
200        fout.setmark(1, 1, b'foo2')
201        self.assertEqual(fout.getmark(1), (1, 1, b'foo2'))
202        self.assertEqual(fout.getmarkers(), [(1, 1, b'foo2')])
203        fout.initfp(None)
204
205    def test_read_markers(self):
206        fout = self.fout = aifc.open(TESTFN, 'wb')
207        fout.aiff()
208        fout.setparams((1, 1, 1, 1, b'NONE', b''))
209        fout.setmark(1, 0, b'odd')
210        fout.setmark(2, 0, b'even')
211        fout.writeframes(b'\x00')
212        fout.close()
213        f = self.f = aifc.open(TESTFN, 'rb')
214        self.assertEqual(f.getmarkers(), [(1, 0, b'odd'), (2, 0, b'even')])
215        self.assertEqual(f.getmark(1), (1, 0, b'odd'))
216        self.assertEqual(f.getmark(2), (2, 0, b'even'))
217        self.assertRaises(aifc.Error, f.getmark, 3)
218
219
220class AIFCLowLevelTest(unittest.TestCase):
221
222    def test_read_written(self):
223        def read_written(self, what):
224            f = io.BytesIO()
225            getattr(aifc, '_write_' + what)(f, x)
226            f.seek(0)
227            return getattr(aifc, '_read_' + what)(f)
228        for x in (-1, 0, 0.1, 1):
229            self.assertEqual(read_written(x, 'float'), x)
230        for x in (float('NaN'), float('Inf')):
231            self.assertEqual(read_written(x, 'float'), aifc._HUGE_VAL)
232        for x in (b'', b'foo', b'a' * 255):
233            self.assertEqual(read_written(x, 'string'), x)
234        for x in (-0x7FFFFFFF, -1, 0, 1, 0x7FFFFFFF):
235            self.assertEqual(read_written(x, 'long'), x)
236        for x in (0, 1, 0xFFFFFFFF):
237            self.assertEqual(read_written(x, 'ulong'), x)
238        for x in (-0x7FFF, -1, 0, 1, 0x7FFF):
239            self.assertEqual(read_written(x, 'short'), x)
240        for x in (0, 1, 0xFFFF):
241            self.assertEqual(read_written(x, 'ushort'), x)
242
243    def test_read_raises(self):
244        f = io.BytesIO(b'\x00')
245        self.assertRaises(EOFError, aifc._read_ulong, f)
246        self.assertRaises(EOFError, aifc._read_long, f)
247        self.assertRaises(EOFError, aifc._read_ushort, f)
248        self.assertRaises(EOFError, aifc._read_short, f)
249
250    def test_write_long_string_raises(self):
251        f = io.BytesIO()
252        with self.assertRaises(ValueError):
253            aifc._write_string(f, b'too long' * 255)
254
255    def test_wrong_open_mode(self):
256        with self.assertRaises(aifc.Error):
257            aifc.open(TESTFN, 'wrong_mode')
258
259    def test_read_wrong_form(self):
260        b1 = io.BytesIO(b'WRNG' + struct.pack('>L', 0))
261        b2 = io.BytesIO(b'FORM' + struct.pack('>L', 4) + b'WRNG')
262        self.assertRaises(aifc.Error, aifc.open, b1)
263        self.assertRaises(aifc.Error, aifc.open, b2)
264
265    def test_read_no_comm_chunk(self):
266        b = io.BytesIO(b'FORM' + struct.pack('>L', 4) + b'AIFF')
267        self.assertRaises(aifc.Error, aifc.open, b)
268
269    def test_read_no_ssnd_chunk(self):
270        b = b'FORM' + struct.pack('>L', 4) + b'AIFC'
271        b += b'COMM' + struct.pack('>LhlhhLL', 38, 1, 0, 8,
272                                   0x4000 | 12, 11025<<18, 0)
273        b += b'NONE' + struct.pack('B', 14) + b'not compressed' + b'\x00'
274        with self.assertRaisesRegex(aifc.Error, 'COMM chunk and/or SSND chunk'
275                                                ' missing'):
276            aifc.open(io.BytesIO(b))
277
278    def test_read_wrong_compression_type(self):
279        b = b'FORM' + struct.pack('>L', 4) + b'AIFC'
280        b += b'COMM' + struct.pack('>LhlhhLL', 23, 1, 0, 8,
281                                   0x4000 | 12, 11025<<18, 0)
282        b += b'WRNG' + struct.pack('B', 0)
283        self.assertRaises(aifc.Error, aifc.open, io.BytesIO(b))
284
285    def test_read_wrong_number_of_channels(self):
286        for nchannels in 0, -1:
287            b = b'FORM' + struct.pack('>L', 4) + b'AIFC'
288            b += b'COMM' + struct.pack('>LhlhhLL', 38, nchannels, 0, 8,
289                                       0x4000 | 12, 11025<<18, 0)
290            b += b'NONE' + struct.pack('B', 14) + b'not compressed' + b'\x00'
291            b += b'SSND' + struct.pack('>L', 8) + b'\x00' * 8
292            with self.assertRaisesRegex(aifc.Error, 'bad # of channels'):
293                aifc.open(io.BytesIO(b))
294
295    def test_read_wrong_sample_width(self):
296        for sampwidth in 0, -1:
297            b = b'FORM' + struct.pack('>L', 4) + b'AIFC'
298            b += b'COMM' + struct.pack('>LhlhhLL', 38, 1, 0, sampwidth,
299                                       0x4000 | 12, 11025<<18, 0)
300            b += b'NONE' + struct.pack('B', 14) + b'not compressed' + b'\x00'
301            b += b'SSND' + struct.pack('>L', 8) + b'\x00' * 8
302            with self.assertRaisesRegex(aifc.Error, 'bad sample width'):
303                aifc.open(io.BytesIO(b))
304
305    def test_read_wrong_marks(self):
306        b = b'FORM' + struct.pack('>L', 4) + b'AIFF'
307        b += b'COMM' + struct.pack('>LhlhhLL', 18, 1, 0, 8,
308                                   0x4000 | 12, 11025<<18, 0)
309        b += b'SSND' + struct.pack('>L', 8) + b'\x00' * 8
310        b += b'MARK' + struct.pack('>LhB', 3, 1, 1)
311        with self.assertWarns(UserWarning) as cm:
312            f = aifc.open(io.BytesIO(b))
313        self.assertEqual(str(cm.warning), 'Warning: MARK chunk contains '
314                                          'only 0 markers instead of 1')
315        self.assertEqual(f.getmarkers(), None)
316
317    def test_read_comm_kludge_compname_even(self):
318        b = b'FORM' + struct.pack('>L', 4) + b'AIFC'
319        b += b'COMM' + struct.pack('>LhlhhLL', 18, 1, 0, 8,
320                                   0x4000 | 12, 11025<<18, 0)
321        b += b'NONE' + struct.pack('B', 4) + b'even' + b'\x00'
322        b += b'SSND' + struct.pack('>L', 8) + b'\x00' * 8
323        with self.assertWarns(UserWarning) as cm:
324            f = aifc.open(io.BytesIO(b))
325        self.assertEqual(str(cm.warning), 'Warning: bad COMM chunk size')
326        self.assertEqual(f.getcompname(), b'even')
327
328    def test_read_comm_kludge_compname_odd(self):
329        b = b'FORM' + struct.pack('>L', 4) + b'AIFC'
330        b += b'COMM' + struct.pack('>LhlhhLL', 18, 1, 0, 8,
331                                   0x4000 | 12, 11025<<18, 0)
332        b += b'NONE' + struct.pack('B', 3) + b'odd'
333        b += b'SSND' + struct.pack('>L', 8) + b'\x00' * 8
334        with self.assertWarns(UserWarning) as cm:
335            f = aifc.open(io.BytesIO(b))
336        self.assertEqual(str(cm.warning), 'Warning: bad COMM chunk size')
337        self.assertEqual(f.getcompname(), b'odd')
338
339    def test_write_params_raises(self):
340        fout = aifc.open(io.BytesIO(), 'wb')
341        wrong_params = (0, 0, 0, 0, b'WRNG', '')
342        self.assertRaises(aifc.Error, fout.setparams, wrong_params)
343        self.assertRaises(aifc.Error, fout.getparams)
344        self.assertRaises(aifc.Error, fout.setnchannels, 0)
345        self.assertRaises(aifc.Error, fout.getnchannels)
346        self.assertRaises(aifc.Error, fout.setsampwidth, 0)
347        self.assertRaises(aifc.Error, fout.getsampwidth)
348        self.assertRaises(aifc.Error, fout.setframerate, 0)
349        self.assertRaises(aifc.Error, fout.getframerate)
350        self.assertRaises(aifc.Error, fout.setcomptype, b'WRNG', '')
351        fout.aiff()
352        fout.setnchannels(1)
353        fout.setsampwidth(1)
354        fout.setframerate(1)
355        fout.setnframes(1)
356        fout.writeframes(b'\x00')
357        self.assertRaises(aifc.Error, fout.setparams, (1, 1, 1, 1, 1, 1))
358        self.assertRaises(aifc.Error, fout.setnchannels, 1)
359        self.assertRaises(aifc.Error, fout.setsampwidth, 1)
360        self.assertRaises(aifc.Error, fout.setframerate, 1)
361        self.assertRaises(aifc.Error, fout.setnframes, 1)
362        self.assertRaises(aifc.Error, fout.setcomptype, b'NONE', '')
363        self.assertRaises(aifc.Error, fout.aiff)
364        self.assertRaises(aifc.Error, fout.aifc)
365
366    def test_write_params_singles(self):
367        fout = aifc.open(io.BytesIO(), 'wb')
368        fout.aifc()
369        fout.setnchannels(1)
370        fout.setsampwidth(2)
371        fout.setframerate(3)
372        fout.setnframes(4)
373        fout.setcomptype(b'NONE', b'name')
374        self.assertEqual(fout.getnchannels(), 1)
375        self.assertEqual(fout.getsampwidth(), 2)
376        self.assertEqual(fout.getframerate(), 3)
377        self.assertEqual(fout.getnframes(), 0)
378        self.assertEqual(fout.tell(), 0)
379        self.assertEqual(fout.getcomptype(), b'NONE')
380        self.assertEqual(fout.getcompname(), b'name')
381        fout.writeframes(b'\x00' * 4 * fout.getsampwidth() * fout.getnchannels())
382        self.assertEqual(fout.getnframes(), 4)
383        self.assertEqual(fout.tell(), 4)
384
385    def test_write_params_bunch(self):
386        fout = aifc.open(io.BytesIO(), 'wb')
387        fout.aifc()
388        p = (1, 2, 3, 4, b'NONE', b'name')
389        fout.setparams(p)
390        self.assertEqual(fout.getparams(), p)
391        fout.initfp(None)
392
393    def test_write_header_raises(self):
394        fout = aifc.open(io.BytesIO(), 'wb')
395        self.assertRaises(aifc.Error, fout.close)
396        fout = aifc.open(io.BytesIO(), 'wb')
397        fout.setnchannels(1)
398        self.assertRaises(aifc.Error, fout.close)
399        fout = aifc.open(io.BytesIO(), 'wb')
400        fout.setnchannels(1)
401        fout.setsampwidth(1)
402        self.assertRaises(aifc.Error, fout.close)
403
404    def test_write_header_comptype_raises(self):
405        for comptype in (b'ULAW', b'ulaw', b'ALAW', b'alaw', b'G722'):
406            fout = aifc.open(io.BytesIO(), 'wb')
407            fout.setsampwidth(1)
408            fout.setcomptype(comptype, b'')
409            self.assertRaises(aifc.Error, fout.close)
410            fout.initfp(None)
411
412    def test_write_markers_raises(self):
413        fout = aifc.open(io.BytesIO(), 'wb')
414        self.assertRaises(aifc.Error, fout.setmark, 0, 0, b'')
415        self.assertRaises(aifc.Error, fout.setmark, 1, -1, b'')
416        self.assertRaises(aifc.Error, fout.setmark, 1, 0, None)
417        self.assertRaises(aifc.Error, fout.getmark, 1)
418        fout.initfp(None)
419
420    def test_write_aiff_by_extension(self):
421        sampwidth = 2
422        filename = TESTFN + '.aiff'
423        fout = self.fout = aifc.open(filename, 'wb')
424        self.addCleanup(unlink, filename)
425        fout.setparams((1, sampwidth, 1, 1, b'ULAW', b''))
426        frames = b'\x00' * fout.getnchannels() * sampwidth
427        fout.writeframes(frames)
428        fout.close()
429        f = self.f = aifc.open(filename, 'rb')
430        self.assertEqual(f.getcomptype(), b'NONE')
431        f.close()
432
433
434if __name__ == "__main__":
435    unittest.main()
436