| 1 | /*************************************************************************** |
| 2 | copyright : (C) 2009 by Lukas Lalinsky |
| 3 | email : [email protected] |
| 4 | ***************************************************************************/ |
| 5 | |
| 6 | /*************************************************************************** |
| 7 | * This library is free software; you can redistribute it and/or modify * |
| 8 | * it under the terms of the GNU Lesser General Public License version * |
| 9 | * 2.1 as published by the Free Software Foundation. * |
| 10 | * * |
| 11 | * This library is distributed in the hope that it will be useful, but * |
| 12 | * WITHOUT ANY WARRANTY; without even the implied warranty of * |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * |
| 14 | * Lesser General Public License for more details. * |
| 15 | * * |
| 16 | * You should have received a copy of the GNU Lesser General Public * |
| 17 | * License along with this library; if not, write to the Free Software * |
| 18 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA * |
| 19 | * 02110-1301 USA * |
| 20 | * * |
| 21 | * Alternatively, this file is available under the Mozilla Public * |
| 22 | * License Version 1.1. You may obtain a copy of the License at * |
| 23 | * http://www.mozilla.org/MPL/ * |
| 24 | ***************************************************************************/ |
| 25 | |
| 26 | #include <string> |
| 27 | #include <cstdio> |
| 28 | |
| 29 | #include "tbytevectorlist.h" |
| 30 | #include "tag.h" |
| 31 | #include "aifffile.h" |
| 32 | #include <cppunit/extensions/HelperMacros.h> |
| 33 | #include "utils.h" |
| 34 | |
| 35 | using namespace std; |
| 36 | using namespace TagLib; |
| 37 | |
| 38 | class TestAIFF : public CppUnit::TestFixture |
| 39 | { |
| 40 | CPPUNIT_TEST_SUITE(TestAIFF); |
| 41 | CPPUNIT_TEST(testAiffProperties); |
| 42 | CPPUNIT_TEST(testAiffCProperties); |
| 43 | CPPUNIT_TEST(testSaveID3v2); |
| 44 | CPPUNIT_TEST(testSaveID3v23); |
| 45 | CPPUNIT_TEST(testDuplicateID3v2); |
| 46 | CPPUNIT_TEST(testFuzzedFile1); |
| 47 | CPPUNIT_TEST(testFuzzedFile2); |
| 48 | CPPUNIT_TEST_SUITE_END(); |
| 49 | |
| 50 | public: |
| 51 | |
| 52 | void testAiffProperties() |
| 53 | { |
| 54 | RIFF::AIFF::File f(TEST_FILE_PATH_C("empty.aiff")); |
| 55 | CPPUNIT_ASSERT(f.audioProperties()); |
| 56 | CPPUNIT_ASSERT_EQUAL(0, f.audioProperties()->lengthInSeconds()); |
| 57 | CPPUNIT_ASSERT_EQUAL(67, f.audioProperties()->lengthInMilliseconds()); |
| 58 | CPPUNIT_ASSERT_EQUAL(706, f.audioProperties()->bitrate()); |
| 59 | CPPUNIT_ASSERT_EQUAL(44100, f.audioProperties()->sampleRate()); |
| 60 | CPPUNIT_ASSERT_EQUAL(1, f.audioProperties()->channels()); |
| 61 | CPPUNIT_ASSERT_EQUAL(16, f.audioProperties()->bitsPerSample()); |
| 62 | CPPUNIT_ASSERT_EQUAL(2941U, f.audioProperties()->sampleFrames()); |
| 63 | CPPUNIT_ASSERT_EQUAL(false, f.audioProperties()->isAiffC()); |
| 64 | } |
| 65 | |
| 66 | void testAiffCProperties() |
| 67 | { |
| 68 | RIFF::AIFF::File f(TEST_FILE_PATH_C("alaw.aifc")); |
| 69 | CPPUNIT_ASSERT(f.audioProperties()); |
| 70 | CPPUNIT_ASSERT_EQUAL(0, f.audioProperties()->lengthInSeconds()); |
| 71 | CPPUNIT_ASSERT_EQUAL(37, f.audioProperties()->lengthInMilliseconds()); |
| 72 | CPPUNIT_ASSERT_EQUAL(355, f.audioProperties()->bitrate()); |
| 73 | CPPUNIT_ASSERT_EQUAL(44100, f.audioProperties()->sampleRate()); |
| 74 | CPPUNIT_ASSERT_EQUAL(1, f.audioProperties()->channels()); |
| 75 | CPPUNIT_ASSERT_EQUAL(16, f.audioProperties()->bitsPerSample()); |
| 76 | CPPUNIT_ASSERT_EQUAL(1622U, f.audioProperties()->sampleFrames()); |
| 77 | CPPUNIT_ASSERT_EQUAL(true, f.audioProperties()->isAiffC()); |
| 78 | CPPUNIT_ASSERT_EQUAL(ByteVector("ALAW"), f.audioProperties()->compressionType()); |
| 79 | CPPUNIT_ASSERT_EQUAL(String("SGI CCITT G.711 A-law"), f.audioProperties()->compressionName()); |
| 80 | } |
| 81 | |
| 82 | void testSaveID3v2() |
| 83 | { |
| 84 | ScopedFileCopy copy("empty", ".aiff"); |
| 85 | string newname = copy.fileName(); |
| 86 | |
| 87 | { |
| 88 | RIFF::AIFF::File f(newname.c_str()); |
| 89 | CPPUNIT_ASSERT(!f.hasID3v2Tag()); |
| 90 | |
| 91 | f.tag()->setTitle(L"TitleXXX"); |
| 92 | f.save(); |
| 93 | CPPUNIT_ASSERT(f.hasID3v2Tag()); |
| 94 | } |
| 95 | { |
| 96 | RIFF::AIFF::File f(newname.c_str()); |
| 97 | CPPUNIT_ASSERT(f.hasID3v2Tag()); |
| 98 | CPPUNIT_ASSERT_EQUAL(String(L"TitleXXX"), f.tag()->title()); |
| 99 | |
| 100 | f.tag()->setTitle(""); |
| 101 | f.save(); |
| 102 | CPPUNIT_ASSERT(!f.hasID3v2Tag()); |
| 103 | } |
| 104 | { |
| 105 | RIFF::AIFF::File f(newname.c_str()); |
| 106 | CPPUNIT_ASSERT(!f.hasID3v2Tag()); |
| 107 | } |
| 108 | } |
| 109 | |
| 110 | void testSaveID3v23() |
| 111 | { |
| 112 | ScopedFileCopy copy("empty", ".aiff"); |
| 113 | string newname = copy.fileName(); |
| 114 | |
| 115 | String xxx = ByteVector(254, 'X'); |
| 116 | { |
| 117 | RIFF::AIFF::File f(newname.c_str()); |
| 118 | CPPUNIT_ASSERT_EQUAL(false, f.hasID3v2Tag()); |
| 119 | |
| 120 | f.tag()->setTitle(xxx); |
| 121 | f.tag()->setArtist("Artist A"); |
| 122 | f.save(ID3v2::v3); |
| 123 | CPPUNIT_ASSERT_EQUAL(true, f.hasID3v2Tag()); |
| 124 | } |
| 125 | { |
| 126 | RIFF::AIFF::File f2(newname.c_str()); |
| 127 | CPPUNIT_ASSERT_EQUAL(static_cast<unsigned int>(3), f2.tag()->header()->majorVersion()); |
| 128 | CPPUNIT_ASSERT_EQUAL(String("Artist A"), f2.tag()->artist()); |
| 129 | CPPUNIT_ASSERT_EQUAL(xxx, f2.tag()->title()); |
| 130 | } |
| 131 | } |
| 132 | |
| 133 | void testDuplicateID3v2() |
| 134 | { |
| 135 | ScopedFileCopy copy("duplicate_id3v2", ".aiff"); |
| 136 | |
| 137 | // duplicate_id3v2.aiff has duplicate ID3v2 tag chunks. |
| 138 | // title() returns "Title2" if can't skip the second tag. |
| 139 | |
| 140 | RIFF::AIFF::File f(copy.fileName().c_str()); |
| 141 | CPPUNIT_ASSERT(f.hasID3v2Tag()); |
| 142 | CPPUNIT_ASSERT_EQUAL(String("Title1"), f.tag()->title()); |
| 143 | |
| 144 | f.save(); |
| 145 | CPPUNIT_ASSERT_EQUAL(static_cast<offset_t>(7030), f.length()); |
| 146 | CPPUNIT_ASSERT_EQUAL(static_cast<offset_t>(-1), f.find("Title2")); |
| 147 | } |
| 148 | |
| 149 | void testFuzzedFile1() |
| 150 | { |
| 151 | RIFF::AIFF::File f(TEST_FILE_PATH_C("segfault.aif")); |
| 152 | CPPUNIT_ASSERT(f.isValid()); |
| 153 | } |
| 154 | |
| 155 | void testFuzzedFile2() |
| 156 | { |
| 157 | RIFF::AIFF::File f(TEST_FILE_PATH_C("excessive_alloc.aif")); |
| 158 | CPPUNIT_ASSERT(f.isValid()); |
| 159 | } |
| 160 | |
| 161 | }; |
| 162 | |
| 163 | CPPUNIT_TEST_SUITE_REGISTRATION(TestAIFF); |