| 1 | /*************************************************************************** |
| 2 | copyright : (C) 2013-2023 Stephen F. Booth |
| 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 "tbytevectorlist.h" |
| 27 | #include "tpropertymap.h" |
| 28 | #include "dsdifffile.h" |
| 29 | #include "plainfile.h" |
| 30 | #include <cppunit/extensions/HelperMacros.h> |
| 31 | #include "utils.h" |
| 32 | |
| 33 | using namespace std; |
| 34 | using namespace TagLib; |
| 35 | |
| 36 | class TestDSDIFF : public CppUnit::TestFixture |
| 37 | { |
| 38 | CPPUNIT_TEST_SUITE(TestDSDIFF); |
| 39 | CPPUNIT_TEST(testProperties); |
| 40 | CPPUNIT_TEST(testTags); |
| 41 | CPPUNIT_TEST(testSaveID3v2); |
| 42 | CPPUNIT_TEST(testSaveID3v23); |
| 43 | CPPUNIT_TEST(testStrip); |
| 44 | CPPUNIT_TEST(testRepeatedSave); |
| 45 | CPPUNIT_TEST(testId3InProp); |
| 46 | CPPUNIT_TEST_SUITE_END(); |
| 47 | |
| 48 | public: |
| 49 | |
| 50 | void testProperties() |
| 51 | { |
| 52 | DSDIFF::File f(TEST_FILE_PATH_C("empty10ms.dff")); |
| 53 | CPPUNIT_ASSERT(f.audioProperties()); |
| 54 | CPPUNIT_ASSERT_EQUAL(0, f.audioProperties()->lengthInSeconds()); |
| 55 | CPPUNIT_ASSERT_EQUAL(10, f.audioProperties()->lengthInMilliseconds()); |
| 56 | CPPUNIT_ASSERT_EQUAL(5644, f.audioProperties()->bitrate()); |
| 57 | CPPUNIT_ASSERT_EQUAL(2, f.audioProperties()->channels()); |
| 58 | CPPUNIT_ASSERT_EQUAL(2822400, f.audioProperties()->sampleRate()); |
| 59 | CPPUNIT_ASSERT_EQUAL(1, f.audioProperties()->bitsPerSample()); |
| 60 | CPPUNIT_ASSERT_EQUAL(static_cast<long long>(28224), f.audioProperties()->sampleCount()); |
| 61 | } |
| 62 | |
| 63 | void testTags() |
| 64 | { |
| 65 | ScopedFileCopy copy("empty10ms", ".dff"); |
| 66 | string newname = copy.fileName(); |
| 67 | |
| 68 | { |
| 69 | DSDIFF::File f(newname.c_str()); |
| 70 | CPPUNIT_ASSERT_EQUAL(String(""), f.tag()->artist()); |
| 71 | f.tag()->setArtist("The Artist"); |
| 72 | f.save(); |
| 73 | } |
| 74 | { |
| 75 | DSDIFF::File f(newname.c_str()); |
| 76 | CPPUNIT_ASSERT_EQUAL(String("The Artist"), f.tag()->artist()); |
| 77 | } |
| 78 | } |
| 79 | |
| 80 | void testSaveID3v2() |
| 81 | { |
| 82 | ScopedFileCopy copy("empty10ms", ".dff"); |
| 83 | string newname = copy.fileName(); |
| 84 | |
| 85 | { |
| 86 | DSDIFF::File f(newname.c_str()); |
| 87 | CPPUNIT_ASSERT(!f.hasID3v2Tag()); |
| 88 | |
| 89 | f.tag()->setTitle(L"TitleXXX"); |
| 90 | f.save(); |
| 91 | CPPUNIT_ASSERT(f.hasID3v2Tag()); |
| 92 | } |
| 93 | { |
| 94 | DSDIFF::File f(newname.c_str()); |
| 95 | CPPUNIT_ASSERT(f.hasID3v2Tag()); |
| 96 | CPPUNIT_ASSERT_EQUAL(String(L"TitleXXX"), f.tag()->title()); |
| 97 | |
| 98 | f.tag()->setTitle(""); |
| 99 | f.save(); |
| 100 | CPPUNIT_ASSERT(!f.hasID3v2Tag()); |
| 101 | } |
| 102 | { |
| 103 | DSDIFF::File f(newname.c_str()); |
| 104 | CPPUNIT_ASSERT(!f.hasID3v2Tag()); |
| 105 | f.ID3v2Tag(true)->setTitle(L"TitleXXX"); |
| 106 | f.save(); |
| 107 | CPPUNIT_ASSERT(f.hasID3v2Tag()); |
| 108 | } |
| 109 | { |
| 110 | DSDIFF::File f(newname.c_str()); |
| 111 | CPPUNIT_ASSERT(f.hasID3v2Tag()); |
| 112 | CPPUNIT_ASSERT_EQUAL(String(L"TitleXXX"), f.tag()->title()); |
| 113 | } |
| 114 | } |
| 115 | |
| 116 | void testSaveID3v23() |
| 117 | { |
| 118 | ScopedFileCopy copy("empty10ms", ".dff"); |
| 119 | string newname = copy.fileName(); |
| 120 | |
| 121 | String xxx = ByteVector(254, 'X'); |
| 122 | { |
| 123 | DSDIFF::File f(newname.c_str()); |
| 124 | CPPUNIT_ASSERT_EQUAL(false, f.hasID3v2Tag()); |
| 125 | |
| 126 | f.tag()->setTitle(xxx); |
| 127 | f.tag()->setArtist("Artist A"); |
| 128 | f.save(DSDIFF::File::AllTags, File::StripOthers, ID3v2::v3); |
| 129 | CPPUNIT_ASSERT_EQUAL(true, f.hasID3v2Tag()); |
| 130 | } |
| 131 | { |
| 132 | DSDIFF::File f2(newname.c_str()); |
| 133 | CPPUNIT_ASSERT_EQUAL(static_cast<unsigned int>(3), f2.ID3v2Tag()->header()->majorVersion()); |
| 134 | CPPUNIT_ASSERT_EQUAL(String("Artist A"), f2.tag()->artist()); |
| 135 | CPPUNIT_ASSERT_EQUAL(xxx, f2.tag()->title()); |
| 136 | } |
| 137 | } |
| 138 | |
| 139 | void testStrip() |
| 140 | { |
| 141 | { |
| 142 | ScopedFileCopy copy("empty10ms", ".dff"); |
| 143 | { |
| 144 | DSDIFF::File f(copy.fileName().c_str()); |
| 145 | f.ID3v2Tag(true)->setArtist("X"); |
| 146 | f.DIINTag(true)->setArtist("Y"); |
| 147 | f.save(); |
| 148 | } |
| 149 | { |
| 150 | DSDIFF::File f(copy.fileName().c_str()); |
| 151 | CPPUNIT_ASSERT(f.hasID3v2Tag()); |
| 152 | CPPUNIT_ASSERT(f.hasDIINTag()); |
| 153 | f.strip(); |
| 154 | } |
| 155 | { |
| 156 | DSDIFF::File f(copy.fileName().c_str()); |
| 157 | CPPUNIT_ASSERT(!f.hasID3v2Tag()); |
| 158 | CPPUNIT_ASSERT(!f.hasDIINTag()); |
| 159 | } |
| 160 | } |
| 161 | |
| 162 | { |
| 163 | ScopedFileCopy copy("empty10ms", ".dff"); |
| 164 | { |
| 165 | DSDIFF::File f(copy.fileName().c_str()); |
| 166 | f.ID3v2Tag(true); |
| 167 | f.DIINTag(true); |
| 168 | f.tag()->setArtist("X"); |
| 169 | f.save(); |
| 170 | } |
| 171 | { |
| 172 | DSDIFF::File f(copy.fileName().c_str()); |
| 173 | CPPUNIT_ASSERT(f.hasID3v2Tag()); |
| 174 | CPPUNIT_ASSERT(f.hasDIINTag()); |
| 175 | f.strip(DSDIFF::File::ID3v2); |
| 176 | } |
| 177 | { |
| 178 | DSDIFF::File f(copy.fileName().c_str()); |
| 179 | CPPUNIT_ASSERT(!f.hasID3v2Tag()); |
| 180 | CPPUNIT_ASSERT(f.hasDIINTag()); |
| 181 | } |
| 182 | } |
| 183 | |
| 184 | { |
| 185 | ScopedFileCopy copy("empty10ms", ".dff"); |
| 186 | { |
| 187 | DSDIFF::File f(copy.fileName().c_str()); |
| 188 | f.tag()->setArtist("X"); |
| 189 | f.save(); |
| 190 | } |
| 191 | { |
| 192 | DSDIFF::File f(copy.fileName().c_str()); |
| 193 | CPPUNIT_ASSERT(f.hasID3v2Tag()); |
| 194 | CPPUNIT_ASSERT(f.hasDIINTag()); |
| 195 | f.strip(DSDIFF::File::DIIN); |
| 196 | } |
| 197 | { |
| 198 | DSDIFF::File f(copy.fileName().c_str()); |
| 199 | CPPUNIT_ASSERT(f.hasID3v2Tag()); |
| 200 | CPPUNIT_ASSERT(!f.hasDIINTag()); |
| 201 | } |
| 202 | } |
| 203 | } |
| 204 | |
| 205 | void testRepeatedSave() |
| 206 | { |
| 207 | ScopedFileCopy copy("empty10ms", ".dff"); |
| 208 | string newname = copy.fileName(); |
| 209 | |
| 210 | { |
| 211 | DSDIFF::File f(newname.c_str()); |
| 212 | CPPUNIT_ASSERT_EQUAL(static_cast<offset_t>(7186), f.length()); |
| 213 | CPPUNIT_ASSERT_EQUAL(String(""), f.tag()->title()); |
| 214 | f.tag()->setTitle("NEW TITLE"); |
| 215 | f.save(); |
| 216 | CPPUNIT_ASSERT_EQUAL(String("NEW TITLE"), f.tag()->title()); |
| 217 | f.tag()->setTitle("NEW TITLE 2"); |
| 218 | f.save(); |
| 219 | CPPUNIT_ASSERT_EQUAL(String("NEW TITLE 2"), f.tag()->title()); |
| 220 | CPPUNIT_ASSERT_EQUAL(static_cast<offset_t>(8292), f.length()); |
| 221 | f.save(); |
| 222 | CPPUNIT_ASSERT_EQUAL(static_cast<offset_t>(8292), f.length()); |
| 223 | } |
| 224 | { |
| 225 | DSDIFF::File f(newname.c_str()); |
| 226 | CPPUNIT_ASSERT_EQUAL(String("NEW TITLE 2"), f.tag()->title()); |
| 227 | f.strip(); |
| 228 | CPPUNIT_ASSERT_EQUAL(static_cast<offset_t>(7186), f.length()); |
| 229 | } |
| 230 | |
| 231 | // Check if file without tags is same as original empty file |
| 232 | const ByteVector dsfData = PlainFile(TEST_FILE_PATH_C("empty10ms.dff")).readAll(); |
| 233 | const ByteVector fileData = PlainFile(newname.c_str()).readAll(); |
| 234 | CPPUNIT_ASSERT(dsfData == fileData); |
| 235 | } |
| 236 | |
| 237 | void testId3InProp() |
| 238 | { |
| 239 | PropertyMap props; |
| 240 | props["ALBUM"] = StringList("Album"); |
| 241 | props["ALBUMARTIST"] = StringList("Album Artist"); |
| 242 | props["ARTIST"] = StringList("Artist"); |
| 243 | props["COMMENT"] = StringList("Comment"); |
| 244 | props["DATE"] = StringList("2023-10-20"); |
| 245 | props["DISCNUMBER"] = StringList("1/2"); |
| 246 | props["GENRE"] = StringList("Genre"); |
| 247 | props["TITLE"] = StringList("Title"); |
| 248 | props["TRACKNUMBER"] = StringList("2/3"); |
| 249 | |
| 250 | ScopedFileCopy copy("empty10ms", ".dff"); |
| 251 | string newname = copy.fileName(); |
| 252 | |
| 253 | { |
| 254 | PlainFile id3PropFile(newname.c_str()); |
| 255 | id3PropFile.insert("\x28", 0x0b, 1); // modify FRM8 length |
| 256 | id3PropFile.insert("\x6c", 0x2b, 1); // modify PROP length |
| 257 | // insert ID3 chunk into PROP |
| 258 | id3PropFile.insert(ByteVector( |
| 259 | "ID3 \x00\x00\x00\x00\x00\x00\x00\x16ID3\x04\x00\x00\x00\x00\x00\x0c" |
| 260 | "TIT2\x00\x00\x00\x02\x00\x00\x00X", 34), 0x76, 0); |
| 261 | } |
| 262 | { |
| 263 | DSDIFF::File f(newname.c_str()); |
| 264 | CPPUNIT_ASSERT(f.hasID3v2Tag()); |
| 265 | CPPUNIT_ASSERT_EQUAL(String("X"), f.tag()->title()); |
| 266 | f.setProperties(props); |
| 267 | f.DIINTag(true)->setArtist("DIINArtist"); |
| 268 | f.save(); |
| 269 | } |
| 270 | { |
| 271 | DSDIFF::File f(newname.c_str()); |
| 272 | ID3v2::Tag *tag = f.ID3v2Tag(); |
| 273 | CPPUNIT_ASSERT(tag); |
| 274 | CPPUNIT_ASSERT_EQUAL(String("Title"), tag->title()); |
| 275 | CPPUNIT_ASSERT_EQUAL(String("Artist"), tag->artist()); |
| 276 | CPPUNIT_ASSERT_EQUAL(String("Album"), tag->album()); |
| 277 | PropertyMap properties = f.properties(); |
| 278 | if (props != properties) { |
| 279 | CPPUNIT_ASSERT_EQUAL(props.toString(), properties.toString()); |
| 280 | } |
| 281 | CPPUNIT_ASSERT(props == properties); |
| 282 | CPPUNIT_ASSERT_EQUAL(String("DIINArtist"), f.DIINTag()->artist()); |
| 283 | f.strip(); |
| 284 | } |
| 285 | { |
| 286 | // Check if file without tags is same as original empty file |
| 287 | const ByteVector dsfData = PlainFile(TEST_FILE_PATH_C("empty10ms.dff")).readAll(); |
| 288 | const ByteVector fileData = PlainFile(newname.c_str()).readAll(); |
| 289 | CPPUNIT_ASSERT(dsfData == fileData); |
| 290 | } |
| 291 | } |
| 292 | |
| 293 | void testSaveDiin() |
| 294 | { |
| 295 | ScopedFileCopy copy("empty10ms", ".dff"); |
| 296 | string newname = copy.fileName(); |
| 297 | |
| 298 | { |
| 299 | DSDIFF::File f(newname.c_str()); |
| 300 | CPPUNIT_ASSERT(!f.hasID3v2Tag()); |
| 301 | CPPUNIT_ASSERT(!f.hasDIINTag()); |
| 302 | |
| 303 | DSDIFF::DIIN::Tag *tag = f.DIINTag(true); |
| 304 | CPPUNIT_ASSERT(tag); |
| 305 | tag->setArtist("DIIN Artist"); |
| 306 | tag->setTitle("DIIN Title"); |
| 307 | tag->setAlbum("not supported"); |
| 308 | f.save(); |
| 309 | } |
| 310 | { |
| 311 | DSDIFF::File f(newname.c_str()); |
| 312 | CPPUNIT_ASSERT(!f.hasID3v2Tag()); |
| 313 | CPPUNIT_ASSERT(f.hasDIINTag()); |
| 314 | |
| 315 | DSDIFF::DIIN::Tag *tag = f.DIINTag(false); |
| 316 | CPPUNIT_ASSERT(tag); |
| 317 | CPPUNIT_ASSERT_EQUAL(String("DIIN Artist"), tag->artist()); |
| 318 | CPPUNIT_ASSERT_EQUAL(String("DIIN Title"), tag->title()); |
| 319 | CPPUNIT_ASSERT_EQUAL(String(), tag->album()); |
| 320 | CPPUNIT_ASSERT_EQUAL(String(), tag->comment()); |
| 321 | CPPUNIT_ASSERT_EQUAL(String(), tag->genre()); |
| 322 | CPPUNIT_ASSERT_EQUAL(0U, tag->year()); |
| 323 | CPPUNIT_ASSERT_EQUAL(0U, tag->track()); |
| 324 | |
| 325 | PropertyMap properties = f.properties(); |
| 326 | CPPUNIT_ASSERT_EQUAL(2U, properties.size()); |
| 327 | CPPUNIT_ASSERT_EQUAL(StringList("DIIN Artist"), properties.value("ARTIST")); |
| 328 | CPPUNIT_ASSERT_EQUAL(StringList("DIIN Title"), properties.value("TITLE")); |
| 329 | |
| 330 | tag->setArtist(""); |
| 331 | tag->setTitle(""); |
| 332 | f.save(); |
| 333 | } |
| 334 | { |
| 335 | DSDIFF::File f(newname.c_str()); |
| 336 | CPPUNIT_ASSERT(!f.hasID3v2Tag()); |
| 337 | CPPUNIT_ASSERT(!f.hasDIINTag()); |
| 338 | } |
| 339 | } |
| 340 | }; |
| 341 | |
| 342 | CPPUNIT_TEST_SUITE_REGISTRATION(TestDSDIFF); |