| 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 "tag.h" |
| 30 | #include "tstringlist.h" |
| 31 | #include "tpropertymap.h" |
| 32 | #include "oggfile.h" |
| 33 | #include "vorbisfile.h" |
| 34 | #include "oggpageheader.h" |
| 35 | #include <cppunit/extensions/HelperMacros.h> |
| 36 | #include "utils.h" |
| 37 | |
| 38 | using namespace std; |
| 39 | using namespace TagLib; |
| 40 | |
| 41 | class TestOGG : public CppUnit::TestFixture |
| 42 | { |
| 43 | CPPUNIT_TEST_SUITE(TestOGG); |
| 44 | CPPUNIT_TEST(testSimple); |
| 45 | CPPUNIT_TEST(testSplitPackets1); |
| 46 | CPPUNIT_TEST(testSplitPackets2); |
| 47 | CPPUNIT_TEST(testDictInterface1); |
| 48 | CPPUNIT_TEST(testDictInterface2); |
| 49 | CPPUNIT_TEST(testAudioProperties); |
| 50 | CPPUNIT_TEST(testPageChecksum); |
| 51 | CPPUNIT_TEST(testPageGranulePosition); |
| 52 | CPPUNIT_TEST_SUITE_END(); |
| 53 | |
| 54 | public: |
| 55 | |
| 56 | void testSimple() |
| 57 | { |
| 58 | ScopedFileCopy copy("empty", ".ogg"); |
| 59 | string newname = copy.fileName(); |
| 60 | |
| 61 | { |
| 62 | Vorbis::File f(newname.c_str()); |
| 63 | f.tag()->setArtist("The Artist"); |
| 64 | f.save(); |
| 65 | } |
| 66 | { |
| 67 | Vorbis::File f(newname.c_str()); |
| 68 | CPPUNIT_ASSERT_EQUAL(String("The Artist"), f.tag()->artist()); |
| 69 | } |
| 70 | } |
| 71 | |
| 72 | void testSplitPackets1() |
| 73 | { |
| 74 | ScopedFileCopy copy("empty", ".ogg"); |
| 75 | string newname = copy.fileName(); |
| 76 | |
| 77 | const String text = longText(128 * 1024, true); |
| 78 | |
| 79 | { |
| 80 | Vorbis::File f(newname.c_str()); |
| 81 | f.tag()->setTitle(text); |
| 82 | f.save(); |
| 83 | } |
| 84 | { |
| 85 | Vorbis::File f(newname.c_str()); |
| 86 | CPPUNIT_ASSERT(f.isValid()); |
| 87 | CPPUNIT_ASSERT_EQUAL(static_cast<offset_t>(136383), f.length()); |
| 88 | CPPUNIT_ASSERT_EQUAL(19, f.lastPageHeader()->pageSequenceNumber()); |
| 89 | CPPUNIT_ASSERT_EQUAL(30U, f.packet(0).size()); |
| 90 | CPPUNIT_ASSERT_EQUAL(131127U, f.packet(1).size()); |
| 91 | CPPUNIT_ASSERT_EQUAL(3832U, f.packet(2).size()); |
| 92 | CPPUNIT_ASSERT_EQUAL(text, f.tag()->title()); |
| 93 | |
| 94 | CPPUNIT_ASSERT(f.audioProperties()); |
| 95 | CPPUNIT_ASSERT_EQUAL(3685, f.audioProperties()->lengthInMilliseconds()); |
| 96 | |
| 97 | f.tag()->setTitle("ABCDE"); |
| 98 | f.save(); |
| 99 | } |
| 100 | { |
| 101 | Vorbis::File f(newname.c_str()); |
| 102 | CPPUNIT_ASSERT(f.isValid()); |
| 103 | CPPUNIT_ASSERT_EQUAL(static_cast<offset_t>(4370), f.length()); |
| 104 | CPPUNIT_ASSERT_EQUAL(3, f.lastPageHeader()->pageSequenceNumber()); |
| 105 | CPPUNIT_ASSERT_EQUAL(30U, f.packet(0).size()); |
| 106 | CPPUNIT_ASSERT_EQUAL(60U, f.packet(1).size()); |
| 107 | CPPUNIT_ASSERT_EQUAL(3832U, f.packet(2).size()); |
| 108 | CPPUNIT_ASSERT_EQUAL(String("ABCDE"), f.tag()->title()); |
| 109 | |
| 110 | CPPUNIT_ASSERT(f.audioProperties()); |
| 111 | CPPUNIT_ASSERT_EQUAL(3685, f.audioProperties()->lengthInMilliseconds()); |
| 112 | } |
| 113 | } |
| 114 | |
| 115 | void testSplitPackets2() |
| 116 | { |
| 117 | ScopedFileCopy copy("empty", ".ogg"); |
| 118 | string newname = copy.fileName(); |
| 119 | |
| 120 | const String text = longText(60890, true); |
| 121 | |
| 122 | { |
| 123 | Vorbis::File f(newname.c_str()); |
| 124 | f.tag()->setTitle(text); |
| 125 | f.save(); |
| 126 | } |
| 127 | { |
| 128 | Vorbis::File f(newname.c_str()); |
| 129 | CPPUNIT_ASSERT(f.isValid()); |
| 130 | CPPUNIT_ASSERT_EQUAL(text, f.tag()->title()); |
| 131 | |
| 132 | f.tag()->setTitle("ABCDE"); |
| 133 | f.save(); |
| 134 | } |
| 135 | { |
| 136 | Vorbis::File f(newname.c_str()); |
| 137 | CPPUNIT_ASSERT(f.isValid()); |
| 138 | CPPUNIT_ASSERT_EQUAL(String("ABCDE"), f.tag()->title()); |
| 139 | } |
| 140 | } |
| 141 | |
| 142 | void testDictInterface1() |
| 143 | { |
| 144 | ScopedFileCopy copy("empty", ".ogg"); |
| 145 | string newname = copy.fileName(); |
| 146 | |
| 147 | Vorbis::File f(newname.c_str()); |
| 148 | |
| 149 | CPPUNIT_ASSERT_EQUAL(static_cast<unsigned int>(0), f.tag()->properties().size()); |
| 150 | |
| 151 | PropertyMap newTags; |
| 152 | StringList values("value 1"); |
| 153 | values.append("value 2"); |
| 154 | newTags["ARTIST"] = values; |
| 155 | f.tag()->setProperties(newTags); |
| 156 | |
| 157 | PropertyMap map = f.tag()->properties(); |
| 158 | CPPUNIT_ASSERT_EQUAL(static_cast<unsigned int>(1), map.size()); |
| 159 | CPPUNIT_ASSERT_EQUAL(static_cast<unsigned int>(2), map["ARTIST"].size()); |
| 160 | CPPUNIT_ASSERT_EQUAL(String("value 1"), map["ARTIST"][0]); |
| 161 | } |
| 162 | |
| 163 | void testDictInterface2() |
| 164 | { |
| 165 | ScopedFileCopy copy("test", ".ogg"); |
| 166 | string newname = copy.fileName(); |
| 167 | |
| 168 | Vorbis::File f(newname.c_str()); |
| 169 | PropertyMap tags = f.tag()->properties(); |
| 170 | |
| 171 | CPPUNIT_ASSERT_EQUAL(static_cast<unsigned int>(2), tags["UNUSUALTAG"].size()); |
| 172 | CPPUNIT_ASSERT_EQUAL(String("usual value"), tags["UNUSUALTAG"][0]); |
| 173 | CPPUNIT_ASSERT_EQUAL(String("another value"), tags["UNUSUALTAG"][1]); |
| 174 | CPPUNIT_ASSERT_EQUAL( |
| 175 | String("\xC3\xB6\xC3\xA4\xC3\xBC\x6F\xCE\xA3\xC3\xB8", String::UTF8), |
| 176 | tags["UNICODETAG"][0]); |
| 177 | |
| 178 | tags["UNICODETAG"][0] = String( |
| 179 | "\xCE\xBD\xCE\xB5\xCF\x89\x20\xCE\xBD\xCE\xB1\xCE\xBB\xCF\x85\xCE\xB5", String::UTF8); |
| 180 | tags.erase("UNUSUALTAG"); |
| 181 | f.tag()->setProperties(tags); |
| 182 | CPPUNIT_ASSERT_EQUAL( |
| 183 | String("\xCE\xBD\xCE\xB5\xCF\x89\x20\xCE\xBD\xCE\xB1\xCE\xBB\xCF\x85\xCE\xB5", String::UTF8), |
| 184 | f.tag()->properties()["UNICODETAG"][0]); |
| 185 | CPPUNIT_ASSERT_EQUAL(false, f.tag()->properties().contains("UNUSUALTAG")); |
| 186 | } |
| 187 | |
| 188 | void testAudioProperties() |
| 189 | { |
| 190 | Ogg::Vorbis::File f(TEST_FILE_PATH_C("empty.ogg")); |
| 191 | CPPUNIT_ASSERT(f.audioProperties()); |
| 192 | CPPUNIT_ASSERT_EQUAL(3, f.audioProperties()->lengthInSeconds()); |
| 193 | CPPUNIT_ASSERT_EQUAL(3685, f.audioProperties()->lengthInMilliseconds()); |
| 194 | CPPUNIT_ASSERT_EQUAL(1, f.audioProperties()->bitrate()); |
| 195 | CPPUNIT_ASSERT_EQUAL(2, f.audioProperties()->channels()); |
| 196 | CPPUNIT_ASSERT_EQUAL(44100, f.audioProperties()->sampleRate()); |
| 197 | CPPUNIT_ASSERT_EQUAL(0, f.audioProperties()->vorbisVersion()); |
| 198 | CPPUNIT_ASSERT_EQUAL(0, f.audioProperties()->bitrateMaximum()); |
| 199 | CPPUNIT_ASSERT_EQUAL(112000, f.audioProperties()->bitrateNominal()); |
| 200 | CPPUNIT_ASSERT_EQUAL(0, f.audioProperties()->bitrateMinimum()); |
| 201 | } |
| 202 | |
| 203 | void testPageChecksum() |
| 204 | { |
| 205 | ScopedFileCopy copy("empty", ".ogg"); |
| 206 | |
| 207 | { |
| 208 | Vorbis::File f(copy.fileName().c_str()); |
| 209 | f.tag()->setArtist("The Artist"); |
| 210 | f.save(); |
| 211 | |
| 212 | f.seek(0x50); |
| 213 | CPPUNIT_ASSERT_EQUAL(static_cast<unsigned int>(0x3d3bd92d), f.readBlock(4).toUInt(0, true)); |
| 214 | } |
| 215 | { |
| 216 | Vorbis::File f(copy.fileName().c_str()); |
| 217 | f.tag()->setArtist("The Artist 2"); |
| 218 | f.save(); |
| 219 | |
| 220 | f.seek(0x50); |
| 221 | CPPUNIT_ASSERT_EQUAL(static_cast<unsigned int>(0xd985291c), f.readBlock(4).toUInt(0, true)); |
| 222 | } |
| 223 | |
| 224 | } |
| 225 | |
| 226 | void testPageGranulePosition() |
| 227 | { |
| 228 | ScopedFileCopy copy("empty", ".ogg"); |
| 229 | { |
| 230 | Vorbis::File f(copy.fileName().c_str()); |
| 231 | // Force the Vorbis comment packet to span more than one page and |
| 232 | // check if the granule position is -1 indicating that no packets |
| 233 | // finish on this page. |
| 234 | f.tag()->setComment(String(ByteVector(70000, 'A'))); |
| 235 | f.save(); |
| 236 | |
| 237 | f.seek(0x3a); |
| 238 | CPPUNIT_ASSERT_EQUAL(ByteVector("OggS\0\0", 6), f.readBlock(6)); |
| 239 | CPPUNIT_ASSERT_EQUAL(static_cast<long long>(-1), f.readBlock(8).toLongLong()); |
| 240 | } |
| 241 | { |
| 242 | Vorbis::File f(copy.fileName().c_str()); |
| 243 | // Use a small Vorbis comment package which ends on the seconds page and |
| 244 | // check if the granule position is zero. |
| 245 | f.tag()->setComment("A small comment"); |
| 246 | f.save(); |
| 247 | |
| 248 | f.seek(0x3a); |
| 249 | CPPUNIT_ASSERT_EQUAL(ByteVector("OggS\0\0", 6), f.readBlock(6)); |
| 250 | CPPUNIT_ASSERT_EQUAL(static_cast<long long>(0), f.readBlock(8).toLongLong()); |
| 251 | } |
| 252 | } |
| 253 | }; |
| 254 | |
| 255 | CPPUNIT_TEST_SUITE_REGISTRATION(TestOGG); |