| 1 | /*************************************************************************** |
| 2 | copyright : (C) 2023 by Urs Fleisch |
| 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 <unordered_map> |
| 28 | #include <list> |
| 29 | |
| 30 | #include "taglib_config.h" |
| 31 | #include "tag_c.h" |
| 32 | #include "tbytevector.h" |
| 33 | #include "tstring.h" |
| 34 | #include "plainfile.h" |
| 35 | #include <cppunit/extensions/HelperMacros.h> |
| 36 | #include "utils.h" |
| 37 | |
| 38 | using namespace TagLib; |
| 39 | |
| 40 | namespace { |
| 41 | |
| 42 | void propertiesToMap( |
| 43 | const TagLib_File *file, |
| 44 | std::unordered_map<std::string, std::list<std::string>> &propertyMap) |
| 45 | { |
| 46 | if(char **keys = taglib_property_keys(file)) { |
| 47 | char **keyPtr = keys; |
| 48 | while(*keyPtr) { |
| 49 | char **values = taglib_property_get(file, *keyPtr); |
| 50 | char **valuePtr = values; |
| 51 | std::list<std::string> valueList; |
| 52 | while(*valuePtr) { |
| 53 | valueList.push_back(*valuePtr++); |
| 54 | } |
| 55 | taglib_property_free(values); |
| 56 | propertyMap[*keyPtr++] = valueList; |
| 57 | } |
| 58 | taglib_property_free(keys); |
| 59 | } |
| 60 | } |
| 61 | |
| 62 | void complexPropertyKeysToList( |
| 63 | const TagLib_File *file, |
| 64 | std::list<std::string> &keyList) |
| 65 | { |
| 66 | if(char **complexKeys = taglib_complex_property_keys(file)) { |
| 67 | char **complexKeyPtr = complexKeys; |
| 68 | while(*complexKeyPtr) { |
| 69 | keyList.push_back(*complexKeyPtr++); |
| 70 | } |
| 71 | taglib_complex_property_free_keys(complexKeys); |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | } // namespace |
| 76 | |
| 77 | class TestTagC : public CppUnit::TestFixture |
| 78 | { |
| 79 | CPPUNIT_TEST_SUITE(TestTagC); |
| 80 | CPPUNIT_TEST(testMp3); |
| 81 | #ifdef TAGLIB_WITH_VORBIS |
| 82 | CPPUNIT_TEST(testStream); |
| 83 | #endif |
| 84 | CPPUNIT_TEST_SUITE_END(); |
| 85 | |
| 86 | public: |
| 87 | void testMp3() |
| 88 | { |
| 89 | ScopedFileCopy copy("xing", ".mp3"); |
| 90 | |
| 91 | { |
| 92 | TagLib_File *file = taglib_file_new(copy.fileName().c_str()); |
| 93 | CPPUNIT_ASSERT(taglib_file_is_valid(file)); |
| 94 | const TagLib_AudioProperties *audioProperties = taglib_file_audioproperties(file); |
| 95 | CPPUNIT_ASSERT_EQUAL(32, taglib_audioproperties_bitrate(audioProperties)); |
| 96 | CPPUNIT_ASSERT_EQUAL(2, taglib_audioproperties_channels(audioProperties)); |
| 97 | CPPUNIT_ASSERT_EQUAL(2, taglib_audioproperties_length(audioProperties)); |
| 98 | CPPUNIT_ASSERT_EQUAL(44100, taglib_audioproperties_samplerate(audioProperties)); |
| 99 | |
| 100 | TagLib_Tag *tag = taglib_file_tag(file); |
| 101 | CPPUNIT_ASSERT_EQUAL(""s, std::string(taglib_tag_album(tag))); |
| 102 | taglib_tag_set_album(tag, "Album"); |
| 103 | taglib_tag_set_artist(tag, "Artist"); |
| 104 | taglib_tag_set_comment(tag, "Comment"); |
| 105 | taglib_tag_set_genre(tag, "Genre"); |
| 106 | taglib_tag_set_title(tag, "Title"); |
| 107 | taglib_tag_set_track(tag, 2); |
| 108 | taglib_tag_set_year(tag, 2023); |
| 109 | |
| 110 | taglib_property_set(file, "COMPOSER", "Composer 1"); |
| 111 | taglib_property_set_append(file, "COMPOSER", "Composer 2"); |
| 112 | taglib_property_set(file, "ALBUMARTIST", "Album Artist"); |
| 113 | taglib_property_set(file, "COMMENT:\xE2\x80\xBB", "Comment (with description)"); |
| 114 | |
| 115 | // cppcheck-suppress cstyleCast |
| 116 | TAGLIB_COMPLEX_PROPERTY_PICTURE(props, "JPEG Data", 9, "Written by TagLib", |
| 117 | "image/jpeg", "Front Cover"); |
| 118 | taglib_complex_property_set(file, "PICTURE", props); |
| 119 | |
| 120 | taglib_file_save(file); |
| 121 | taglib_file_free(file); |
| 122 | } |
| 123 | { |
| 124 | TagLib_File *file = taglib_file_new(copy.fileName().c_str()); |
| 125 | CPPUNIT_ASSERT(taglib_file_is_valid(file)); |
| 126 | |
| 127 | TagLib_Tag *tag = taglib_file_tag(file); |
| 128 | CPPUNIT_ASSERT_EQUAL("Album"s, std::string(taglib_tag_album(tag))); |
| 129 | CPPUNIT_ASSERT_EQUAL("Artist"s, std::string(taglib_tag_artist(tag))); |
| 130 | CPPUNIT_ASSERT_EQUAL("Comment"s, std::string(taglib_tag_comment(tag))); |
| 131 | CPPUNIT_ASSERT_EQUAL("Genre"s, std::string(taglib_tag_genre(tag))); |
| 132 | CPPUNIT_ASSERT_EQUAL("Title"s, std::string(taglib_tag_title(tag))); |
| 133 | CPPUNIT_ASSERT_EQUAL(2U, taglib_tag_track(tag)); |
| 134 | CPPUNIT_ASSERT_EQUAL(2023U, taglib_tag_year(tag)); |
| 135 | |
| 136 | std::unordered_map<std::string, std::list<std::string>> propertyMap; |
| 137 | propertiesToMap(file, propertyMap); |
| 138 | const std::unordered_map<std::string, std::list<std::string>> expected { |
| 139 | {"TRACKNUMBER"s, {"2"s}}, |
| 140 | {"TITLE"s, {"Title"s}}, |
| 141 | {"GENRE"s, {"Genre"s}}, |
| 142 | {"DATE"s, {"2023"s}}, |
| 143 | {"COMPOSER"s, {"Composer 1"s, "Composer 2"s}}, |
| 144 | {"COMMENT"s, {"Comment"s}}, |
| 145 | {"COMMENT:\xE2\x80\xBB"s, {"Comment (with description)"s}}, |
| 146 | {"ARTIST"s, {"Artist"s}}, |
| 147 | {"ALBUMARTIST"s, {"Album Artist"s}}, |
| 148 | {"ALBUM"s, {"Album"s}} |
| 149 | }; |
| 150 | CPPUNIT_ASSERT(expected == propertyMap); |
| 151 | |
| 152 | std::list<std::string> keyList; |
| 153 | complexPropertyKeysToList(file, keyList); |
| 154 | CPPUNIT_ASSERT(std::list{"PICTURE"s} == keyList); |
| 155 | |
| 156 | TagLib_Complex_Property_Attribute*** properties = |
| 157 | taglib_complex_property_get(file, "PICTURE"); |
| 158 | TagLib_Complex_Property_Picture_Data picture; |
| 159 | taglib_picture_from_complex_property(properties, &picture); |
| 160 | CPPUNIT_ASSERT_EQUAL("image/jpeg"s, std::string(picture.mimeType)); |
| 161 | CPPUNIT_ASSERT_EQUAL("Written by TagLib"s, std::string(picture.description)); |
| 162 | CPPUNIT_ASSERT_EQUAL("Front Cover"s, std::string(picture.pictureType)); |
| 163 | CPPUNIT_ASSERT_EQUAL(ByteVector("JPEG Data"), ByteVector(picture.data, 9)); |
| 164 | CPPUNIT_ASSERT_EQUAL(9U, picture.size); |
| 165 | taglib_complex_property_free(properties); |
| 166 | |
| 167 | taglib_file_free(file); |
| 168 | } |
| 169 | |
| 170 | taglib_tag_free_strings(); |
| 171 | } |
| 172 | |
| 173 | #ifdef TAGLIB_WITH_VORBIS |
| 174 | void testStream() |
| 175 | { |
| 176 | // Only fetch the beginning of a FLAC file |
| 177 | const ByteVector data = PlainFile(TEST_FILE_PATH_C("silence-44-s.flac")) |
| 178 | .readBlock(4200); |
| 179 | |
| 180 | { |
| 181 | TagLib_IOStream *stream = taglib_memory_iostream_new(data.data(), data.size()); |
| 182 | TagLib_File *file = taglib_file_new_iostream(stream); |
| 183 | CPPUNIT_ASSERT(taglib_file_is_valid(file)); |
| 184 | const TagLib_AudioProperties *audioProperties = taglib_file_audioproperties(file); |
| 185 | CPPUNIT_ASSERT_EQUAL(2, taglib_audioproperties_channels(audioProperties)); |
| 186 | CPPUNIT_ASSERT_EQUAL(3, taglib_audioproperties_length(audioProperties)); |
| 187 | CPPUNIT_ASSERT_EQUAL(44100, taglib_audioproperties_samplerate(audioProperties)); |
| 188 | |
| 189 | TagLib_Tag *tag = taglib_file_tag(file); |
| 190 | CPPUNIT_ASSERT_EQUAL("Quod Libet Test Data"s, std::string(taglib_tag_album(tag))); |
| 191 | CPPUNIT_ASSERT_EQUAL("piman / jzig"s, std::string(taglib_tag_artist(tag))); |
| 192 | CPPUNIT_ASSERT_EQUAL("Silence"s, std::string(taglib_tag_genre(tag))); |
| 193 | CPPUNIT_ASSERT_EQUAL(""s, std::string(taglib_tag_comment(tag))); |
| 194 | CPPUNIT_ASSERT_EQUAL("Silence"s, std::string(taglib_tag_title(tag))); |
| 195 | CPPUNIT_ASSERT_EQUAL(2U, taglib_tag_track(tag)); |
| 196 | CPPUNIT_ASSERT_EQUAL(2004U, taglib_tag_year(tag)); |
| 197 | |
| 198 | std::unordered_map<std::string, std::list<std::string>> propertyMap; |
| 199 | propertiesToMap(file, propertyMap); |
| 200 | const std::unordered_map<std::string, std::list<std::string>> expected { |
| 201 | {"TRACKNUMBER"s, {"02/10"s}}, |
| 202 | {"TITLE"s, {"Silence"s}}, |
| 203 | {"GENRE"s, {"Silence"s}}, |
| 204 | {"DATE"s, {"2004"s}}, |
| 205 | {"ARTIST"s, {"piman"s, "jzig"s}}, |
| 206 | {"ALBUM"s, {"Quod Libet Test Data"s}} |
| 207 | }; |
| 208 | CPPUNIT_ASSERT(expected == propertyMap); |
| 209 | |
| 210 | std::list<std::string> keyList; |
| 211 | complexPropertyKeysToList(file, keyList); |
| 212 | CPPUNIT_ASSERT(std::list{"PICTURE"s} == keyList); |
| 213 | |
| 214 | TagLib_Complex_Property_Attribute*** properties = |
| 215 | taglib_complex_property_get(file, "PICTURE"); |
| 216 | TagLib_Complex_Property_Picture_Data picture; |
| 217 | taglib_picture_from_complex_property(properties, &picture); |
| 218 | ByteVector expectedData( |
| 219 | "\x89PNG\x0d\x0a\x1a\x0a\x00\x00\x00\x0dIHDR" |
| 220 | "\x00\x00\x00\x01\x00\x00\x00\x01\x08\x02\x00\x00\x00\x90\x77\x53" |
| 221 | "\xde\x00\x00\x00\x09pHYs\x00\x00\x0b\x13\x00\x00\x0b" |
| 222 | "\x13\x01\x00\x9a\x9c\x18\x00\x00\x00\x07\x74\x49\x4d\x45\x07\xd6" |
| 223 | "\x0b\x1c\x0a\x36\x06\x08\x44\x3d\x32\x00\x00\x00\x1dtEXt" |
| 224 | "Comment\0Created" |
| 225 | " with The GIMP\xef\x64" |
| 226 | "\x25\x6e\x00\x00\x00\x0cIDAT\x08\xd7\x63\xf8\xff\xff" |
| 227 | "\x3f\x00\x05\xfe\x02\xfe\xdc\xcc\x59\xe7\x00\x00\x00\x00IEND" |
| 228 | "\xae\x42\x60\x82", 150); |
| 229 | CPPUNIT_ASSERT_EQUAL("image/png"s, std::string(picture.mimeType)); |
| 230 | CPPUNIT_ASSERT_EQUAL("A pixel."s, std::string(picture.description)); |
| 231 | CPPUNIT_ASSERT_EQUAL("Front Cover"s, std::string(picture.pictureType)); |
| 232 | CPPUNIT_ASSERT_EQUAL(expectedData, ByteVector(picture.data, 150)); |
| 233 | CPPUNIT_ASSERT_EQUAL(150U, picture.size); |
| 234 | taglib_complex_property_free(properties); |
| 235 | |
| 236 | taglib_file_free(file); |
| 237 | taglib_iostream_free(stream); |
| 238 | } |
| 239 | |
| 240 | taglib_tag_free_strings(); |
| 241 | } |
| 242 | #endif |
| 243 | }; |
| 244 | |
| 245 | CPPUNIT_TEST_SUITE_REGISTRATION(TestTagC); |