| 1 | /*************************************************************************** |
| 2 | copyright : (C) 2026 by Antoine Colombier |
| 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 | |
| 28 | #include "tag.h" |
| 29 | #include "mp4file.h" |
| 30 | #include <cppunit/extensions/HelperMacros.h> |
| 31 | #include "utils.h" |
| 32 | |
| 33 | using namespace std; |
| 34 | using namespace TagLib; |
| 35 | |
| 36 | namespace { |
| 37 | const String STEM_KEY("STEM"); |
| 38 | } |
| 39 | |
| 40 | class TestMP4Stem : public CppUnit::TestFixture |
| 41 | { |
| 42 | CPPUNIT_TEST_SUITE(TestMP4Stem); |
| 43 | CPPUNIT_TEST(testCreate); |
| 44 | CPPUNIT_TEST(testUpdate); |
| 45 | CPPUNIT_TEST(testRemove); |
| 46 | CPPUNIT_TEST_SUITE_END(); |
| 47 | |
| 48 | protected: |
| 49 | static void createTestFile(ScopedFileCopy ©){ |
| 50 | MP4::File f(copy.fileName().c_str()); |
| 51 | CPPUNIT_ASSERT(!f.hasMP4Tag()); |
| 52 | auto &tag = *f.tag(); |
| 53 | CPPUNIT_ASSERT_EQUAL(0U, tag.complexProperties(STEM_KEY).size()); |
| 54 | CPPUNIT_ASSERT(tag.setComplexProperties(STEM_KEY, List<VariantMap>({{{"manifest", ByteVector("{some text data}")}}}))); |
| 55 | f.save(); |
| 56 | } |
| 57 | |
| 58 | public: |
| 59 | |
| 60 | void testCreate() |
| 61 | { |
| 62 | ScopedFileCopy copy("no-tags", ".m4a"); |
| 63 | createTestFile(copy); |
| 64 | |
| 65 | // Assert whether the newly created stem content is as expected |
| 66 | { |
| 67 | MP4::File f(copy.fileName().c_str()); |
| 68 | auto &tag = *f.tag(); |
| 69 | CPPUNIT_ASSERT(f.hasMP4Tag()); |
| 70 | auto stems = tag.complexProperties(STEM_KEY); |
| 71 | CPPUNIT_ASSERT_EQUAL(1U, stems.size()); |
| 72 | CPPUNIT_ASSERT(stems.front().contains("manifest")); |
| 73 | CPPUNIT_ASSERT_EQUAL(Variant(ByteVector("{some text data}")), stems.front().find("manifest")->second); |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | void testUpdate() |
| 78 | { |
| 79 | ScopedFileCopy copy("no-tags", ".m4a"); |
| 80 | createTestFile(copy); |
| 81 | |
| 82 | // Prepare so large test data, to ensure that free padding is correctly used |
| 83 | char *buffer = new char[1025]{'X'}; |
| 84 | buffer[1024] = '\0'; |
| 85 | String artist(buffer); |
| 86 | std::memset(buffer, 'Y', 1024); |
| 87 | String title(buffer); |
| 88 | std::memset(buffer, 'Z', 1024); |
| 89 | ByteVector newStem(buffer); |
| 90 | delete [] buffer; |
| 91 | |
| 92 | // Update tags and stems to force atom offset recalculation |
| 93 | { |
| 94 | MP4::File f(copy.fileName().c_str()); |
| 95 | CPPUNIT_ASSERT(f.setComplexProperties("PICTURE", List<VariantMap>({ |
| 96 | { |
| 97 | {"data", ByteVector("DummyData")}, |
| 98 | {"pictureType", "Front Cover"}, |
| 99 | {"mimeType", String("image/png")}, |
| 100 | {"description", String("Test")} |
| 101 | } |
| 102 | }))); |
| 103 | CPPUNIT_ASSERT(f.tag()->setComplexProperties(STEM_KEY, List<VariantMap>({{{"manifest", newStem}}}))); |
| 104 | f.tag()->setArtist(artist); |
| 105 | f.tag()->setTitle(title); |
| 106 | f.save(); |
| 107 | } |
| 108 | // Reload the file to assert its tags |
| 109 | { |
| 110 | MP4::File f(copy.fileName().c_str()); |
| 111 | CPPUNIT_ASSERT(f.hasMP4Tag()); |
| 112 | |
| 113 | auto stems = f.tag()->complexProperties(STEM_KEY); |
| 114 | CPPUNIT_ASSERT_EQUAL(1U, stems.size()); |
| 115 | CPPUNIT_ASSERT(stems.front().contains("manifest")); |
| 116 | CPPUNIT_ASSERT_EQUAL(Variant(newStem), stems.front().find("manifest")->second); |
| 117 | |
| 118 | auto pictures = f.tag()->complexProperties("PICTURE"); |
| 119 | CPPUNIT_ASSERT_EQUAL(1U, pictures.size()); |
| 120 | CPPUNIT_ASSERT_EQUAL(VariantMap({ |
| 121 | { |
| 122 | {"data", ByteVector("DummyData")}, |
| 123 | {"mimeType", String("image/png")}, |
| 124 | } |
| 125 | }), pictures.front()); |
| 126 | |
| 127 | CPPUNIT_ASSERT_EQUAL(title, f.tag()->title()); |
| 128 | CPPUNIT_ASSERT_EQUAL(artist, f.tag()->artist()); |
| 129 | } |
| 130 | } |
| 131 | |
| 132 | void testRemove() |
| 133 | { |
| 134 | ScopedFileCopy copy("no-tags", ".m4a"); |
| 135 | createTestFile(copy); |
| 136 | |
| 137 | // Remove the stem |
| 138 | { |
| 139 | MP4::File f(copy.fileName().c_str()); |
| 140 | CPPUNIT_ASSERT(f.hasMP4Tag()); |
| 141 | CPPUNIT_ASSERT(f.tag()->setComplexProperties(STEM_KEY, List<VariantMap>())); |
| 142 | f.save(); |
| 143 | } |
| 144 | { |
| 145 | MP4::File f(copy.fileName().c_str()); |
| 146 | CPPUNIT_ASSERT(!f.hasMP4Tag()); |
| 147 | CPPUNIT_ASSERT(!f.tag()->complexPropertyKeys().contains(STEM_KEY)); |
| 148 | } |
| 149 | } |
| 150 | |
| 151 | }; |
| 152 | |
| 153 | CPPUNIT_TEST_SUITE_REGISTRATION(TestMP4Stem); |