Path: blob/master/test/jdk/javax/sound/sampled/spi/AudioFileReader/ReadersExceptions.java
40527 views
/*1* Copyright (c) 2013, 2016, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223import java.io.ByteArrayInputStream;24import java.io.IOException;25import java.io.InputStream;2627import javax.sound.sampled.AudioSystem;28import javax.sound.sampled.UnsupportedAudioFileException;29import javax.sound.sampled.spi.AudioFileReader;3031import static java.util.ServiceLoader.load;3233/**34* @test35* @bug 7058662 7058666 7058672 813030536* @author Sergey Bylokhov37*/38public final class ReadersExceptions {3940// empty channels41static byte[] wrongAIFFCh =42{0x46, 0x4f, 0x52, 0x4d, // AiffFileFormat.AIFF_MAGIC430, 0, 0, 0, // length440, 0, 0, 0, // iffType450x43, 0x4f, 0x4d, 0x4d, // chunkName460, 0, 0, 100, // chunkLen470, 0, // channels480, 0, 0, 0, //490, 10 // sampleSize50, 0, 0, 0, 0};51// empty sampleSize52static byte[] wrongAIFFSSL =53{0x46, 0x4f, 0x52, 0x4d, //AiffFileFormat.AIFF_MAGIC540, 0, 0, 0, // length550, 0, 0, 0, // iffType560x43, 0x4f, 0x4d, 0x4d, // chunkName570, 0, 0, 100, // chunkLen580, 10, // channels590, 0, 0, 0, //600, 0 // sampleSize61, 0, 0, 0, 0};62// big sampleSize63static byte[] wrongAIFFSSH =64{0x46, 0x4f, 0x52, 0x4d, //AiffFileFormat.AIFF_MAGIC650, 0, 0, 0, // length660, 0, 0, 0, // iffType670x43, 0x4f, 0x4d, 0x4d, // chunkName680, 0, 0, 100, // chunkLen690, 10, // channels700, 0, 0, 0, //710, 33 // sampleSize72, 0, 0, 0, 0};73// empty channels74static byte[] wrongAUCh =75{0x2e, 0x73, 0x6e, 0x64,//AiffFileFormat.AU_SUN_MAGIC760, 0, 0, 24, // headerSize770, 0, 0, 0, // dataSize780, 0, 0, 1, // encoding_local AuFileFormat.AU_ULAW_8790, 0, 0, 1, // sampleRate800, 0, 0, 0 // channels81};82// empty sample rate83static byte[] wrongAUSR =84{0x2e, 0x73, 0x6e, 0x64,//AiffFileFormat.AU_SUN_MAGIC850, 0, 0, 24, // headerSize860, 0, 0, 0, // dataSize870, 0, 0, 1, // encoding_local AuFileFormat.AU_ULAW_8880, 0, 0, 0, // sampleRate890, 0, 0, 1 // channels90};91// empty header size92static byte[] wrongAUEmptyHeader =93{0x2e, 0x73, 0x6e, 0x64,//AiffFileFormat.AU_SUN_MAGIC940, 0, 0, 0, // headerSize950, 0, 0, 0, // dataSize960, 0, 0, 1, // encoding_local AuFileFormat.AU_ULAW_8970, 0, 0, 1, // sampleRate980, 0, 0, 1 // channels99};100// small header size101static byte[] wrongAUSmallHeader =102{0x2e, 0x73, 0x6e, 0x64,//AiffFileFormat.AU_SUN_MAGIC1030, 0, 0, 7, // headerSize1040, 0, 0, 0, // dataSize1050, 0, 0, 1, // encoding_local AuFileFormat.AU_ULAW_81060, 0, 0, 1, // sampleRate1070, 0, 0, 1 // channels108};109// frame size overflow, when result negative110static byte[] wrongAUFrameSizeOverflowNegativeResult =111{0x2e, 0x73, 0x6e, 0x64,//AiffFileFormat.AU_SUN_MAGIC1120, 0, 0, 24, // headerSize1130, 0, 0, 0, // dataSize1140, 0, 0, 5, // encoding_local AuFileFormat.AU_LINEAR_321150, 0, 0, 1, // sampleRate1160x7F, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF // channels117};118// frame size overflow, when result positive119static byte[] wrongAUFrameSizeOverflowPositiveResult =120{0x2e, 0x73, 0x6e, 0x64,//AiffFileFormat.AU_SUN_MAGIC1210, 0, 0, 24, // headerSize1220, 0, 0, 0, // dataSize1230, 0, 0, 4, // encoding_local AuFileFormat.AU_LINEAR_241240, 0, 0, 1, // sampleRate1250x7F, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF // channels126};127// empty channels128static byte[] wrongWAVCh =129{0x52, 0x49, 0x46, 0x46, // WaveFileFormat.RIFF_MAGIC1301, 1, 1, 1, // fileLength1310x57, 0x41, 0x56, 0x45, // waveMagic1320x66, 0x6d, 0x74, 0x20, // FMT_MAGIC1333, 0, 0, 0, // length1341, 0, // wav_type WAVE_FORMAT_PCM1350, 0, // channels1360, 0, 0, 0, // sampleRate1370, 0, 0, 0, // avgBytesPerSec1380, 0, // blockAlign1391, 0, // sampleSizeInBits1400x64, 0x61, 0x74, 0x61, // WaveFileFormat.DATA_MAGIC1410, 0, 0, 0, // dataLength142};143// empty sampleSizeInBits144static byte[] wrongWAVSSB =145{0x52, 0x49, 0x46, 0x46, // WaveFileFormat.RIFF_MAGIC1461, 1, 1, 1, // fileLength1470x57, 0x41, 0x56, 0x45, // waveMagic1480x66, 0x6d, 0x74, 0x20, // FMT_MAGIC1493, 0, 0, 0, // length1501, 0, // wav_type WAVE_FORMAT_PCM1511, 0, // channels1520, 0, 0, 0, // sampleRate1530, 0, 0, 0, // avgBytesPerSec1540, 0, // blockAlign1550, 0, // sampleSizeInBits1560x64, 0x61, 0x74, 0x61, // WaveFileFormat.DATA_MAGIC1570, 0, 0, 0, // dataLength158};159160static byte[][] data = {161wrongAIFFCh, wrongAIFFSSL, wrongAIFFSSH, wrongAUCh, wrongAUSR,162wrongAUEmptyHeader, wrongAUSmallHeader,163wrongAUFrameSizeOverflowNegativeResult,164wrongAUFrameSizeOverflowPositiveResult, wrongWAVCh, wrongWAVSSB165};166167public static void main(final String[] args) throws IOException {168for (final byte[] bytes : data) {169testAS(bytes);170testAFR(bytes);171}172}173174private static void testAS(final byte[] buffer) throws IOException {175// AudioSystem API176final InputStream is = new ByteArrayInputStream(buffer);177try {178AudioSystem.getAudioFileFormat(is);179} catch (UnsupportedAudioFileException ignored) {180// Expected.181return;182}183throw new RuntimeException("Test Failed");184}185186private static void testAFR(final byte[] buffer) throws IOException {187// AudioFileReader API188final InputStream is = new ByteArrayInputStream(buffer);189for (final AudioFileReader afr : load(AudioFileReader.class)) {190for (int i = 0; i < 10; ++i) {191try {192afr.getAudioFileFormat(is);193throw new RuntimeException("UAFE expected");194} catch (final UnsupportedAudioFileException ignored) {195// Expected.196}197}198}199}200}201202203