| 1 | #include <opustags.h> |
| 2 | #include "tap.h" |
| 3 | |
| 4 | #include <string.h> |
| 5 | |
| 6 | static const char standard_OpusTags[] = |
| 7 | "OpusTags" |
| 8 | "\x14\x00\x00\x00" "opustags test packet" |
| 9 | "\x02\x00\x00\x00" |
| 10 | "\x09\x00\x00\x00" "TITLE=Foo" |
| 11 | "\x0a\x00\x00\x00" "ARTIST=Bar"; |
| 12 | |
| 13 | static void parse_standard() |
| 14 | { |
| 15 | ogg_packet op; |
| 16 | op.bytes = sizeof(standard_OpusTags) - 1; |
| 17 | op.packet = (unsigned char*) standard_OpusTags; |
| 18 | ot::opus_tags tags = ot::parse_tags(op); |
| 19 | if (tags.vendor != u8"opustags test packet") |
| 20 | throw failure("bad vendor string"); |
| 21 | if (tags.comments.size() != 2) |
| 22 | throw failure("bad number of comments"); |
| 23 | auto it = tags.comments.begin(); |
| 24 | if (*it != u8"TITLE=Foo") |
| 25 | throw failure("bad title"); |
| 26 | ++it; |
| 27 | if (*it != u8"ARTIST=Bar") |
| 28 | throw failure("bad artist"); |
| 29 | if (tags.extra_data.size() != 0) |
| 30 | throw failure("found mysterious padding data"); |
| 31 | } |
| 32 | |
| 33 | static ot::status try_parse_tags(const ogg_packet& packet) |
| 34 | { |
| 35 | try { |
| 36 | ot::parse_tags(packet); |
| 37 | return ot::st::ok; |
| 38 | } catch (const ot::status& rc) { |
| 39 | return rc; |
| 40 | } |
| 41 | } |
| 42 | |
| 43 | /** |
| 44 | * Try parse_tags with packets that should not valid, or that might even |
| 45 | * corrupt the memory. Run this one with valgrind to ensure we're not |
| 46 | * overflowing. |
| 47 | */ |
| 48 | static void parse_corrupted() |
| 49 | { |
| 50 | size_t size = sizeof(standard_OpusTags); |
| 51 | char packet[size]; |
| 52 | memcpy(packet, standard_OpusTags, size); |
| 53 | ot::opus_tags tags; |
| 54 | ogg_packet op; |
| 55 | op.packet = (unsigned char*) packet; |
| 56 | op.bytes = size; |
| 57 | |
| 58 | char* header_data = packet; |
| 59 | char* vendor_length = header_data + 8; |
| 60 | char* vendor_string = vendor_length + 4; |
| 61 | char* comment_count = vendor_string + *vendor_length; |
| 62 | char* first_comment_length = comment_count + 4; |
| 63 | char* first_comment_data = first_comment_length + 4; |
| 64 | char* end = packet + size; |
| 65 | |
| 66 | op.bytes = 7; |
| 67 | if (try_parse_tags(op) != ot::st::cut_magic_number) |
| 68 | throw failure("did not detect the overflowing magic number"); |
| 69 | op.bytes = 11; |
| 70 | if (try_parse_tags(op) != ot::st::cut_vendor_length) |
| 71 | throw failure("did not detect the overflowing vendor string length"); |
| 72 | op.bytes = size; |
| 73 | |
| 74 | header_data[0] = 'o'; |
| 75 | if (try_parse_tags(op) != ot::st::bad_magic_number) |
| 76 | throw failure("did not detect the bad magic number"); |
| 77 | header_data[0] = 'O'; |
| 78 | |
| 79 | *vendor_length = end - vendor_string + 1; |
| 80 | if (try_parse_tags(op) != ot::st::cut_vendor_data) |
| 81 | throw failure("did not detect the overflowing vendor string"); |
| 82 | *vendor_length = end - vendor_string - 3; |
| 83 | if (try_parse_tags(op) != ot::st::cut_comment_count) |
| 84 | throw failure("did not detect the overflowing comment count"); |
| 85 | *vendor_length = comment_count - vendor_string; |
| 86 | |
| 87 | ++*comment_count; |
| 88 | if (try_parse_tags(op) != ot::st::cut_comment_length) |
| 89 | throw failure("did not detect the overflowing comment length"); |
| 90 | *first_comment_length = end - first_comment_data + 1; |
| 91 | if (try_parse_tags(op) != ot::st::cut_comment_data) |
| 92 | throw failure("did not detect the overflowing comment data"); |
| 93 | } |
| 94 | |
| 95 | static void recode_standard() |
| 96 | { |
| 97 | ogg_packet op; |
| 98 | op.bytes = sizeof(standard_OpusTags) - 1; |
| 99 | op.packet = (unsigned char*) standard_OpusTags; |
| 100 | ot::opus_tags tags = ot::parse_tags(op); |
| 101 | auto packet = ot::render_tags(tags); |
| 102 | if (packet.b_o_s != 0) |
| 103 | throw failure("b_o_s should not be set"); |
| 104 | if (packet.e_o_s != 0) |
| 105 | throw failure("e_o_s should not be set"); |
| 106 | if (packet.granulepos != 0) |
| 107 | throw failure("granule_post should be 0"); |
| 108 | if (packet.packetno != 1) |
| 109 | throw failure("packetno should be 1"); |
| 110 | if (packet.bytes != sizeof(standard_OpusTags) - 1) |
| 111 | throw failure("the packet is not the right size"); |
| 112 | if (memcmp(packet.packet, standard_OpusTags, packet.bytes) != 0) |
| 113 | throw failure("the rendered packet is not what we expected"); |
| 114 | } |
| 115 | |
| 116 | static void recode_padding() |
| 117 | { |
| 118 | std::string padded_OpusTags(standard_OpusTags, sizeof(standard_OpusTags)); |
| 119 | // ^ note: padded_OpusTags ends with a null byte here |
| 120 | padded_OpusTags += "hello"; |
| 121 | ogg_packet op; |
| 122 | op.bytes = padded_OpusTags.size(); |
| 123 | op.packet = (unsigned char*) padded_OpusTags.data(); |
| 124 | |
| 125 | ot::opus_tags tags = ot::parse_tags(op); |
| 126 | if (tags.extra_data != "\0hello"_bsv) |
| 127 | throw failure("corrupted extra data"); |
| 128 | // recode the packet and ensure it's exactly the same |
| 129 | auto packet = ot::render_tags(tags); |
| 130 | if (static_cast<size_t>(packet.bytes) < padded_OpusTags.size()) |
| 131 | throw failure("the packet was truncated"); |
| 132 | if (static_cast<size_t>(packet.bytes) > padded_OpusTags.size()) |
| 133 | throw failure("the packet got too big"); |
| 134 | if (memcmp(packet.packet, padded_OpusTags.data(), packet.bytes) != 0) |
| 135 | throw failure("the rendered packet is not what we expected"); |
| 136 | } |
| 137 | |
| 138 | static void extract_cover() |
| 139 | { |
| 140 | ot::byte_string_view picture_data = ""_bsv |
| 141 | "\x00\x00\x00\x03" // Picture type 3. |
| 142 | "\x00\x00\x00\x09" "image/foo" // MIME type. |
| 143 | "\x00\x00\x00\x00" "" // Description. |
| 144 | "\x00\x00\x00\x00" // Width. |
| 145 | "\x00\x00\x00\x00" // Height. |
| 146 | "\x00\x00\x00\x00" // Color depth. |
| 147 | "\x00\x00\x00\x00" // Palette size. |
| 148 | "\x00\x00\x00\x0C" "Picture data"; |
| 149 | |
| 150 | ot::opus_tags tags; |
| 151 | tags.comments = { u8"METADATA_BLOCK_PICTURE=" + ot::encode_base64(picture_data) }; |
| 152 | std::optional<ot::picture> cover = ot::extract_cover(tags); |
| 153 | if (!cover) |
| 154 | throw failure("could not extract the cover"); |
| 155 | if (cover->mime_type != "image/foo"_bsv) |
| 156 | throw failure("bad extracted MIME type"); |
| 157 | if (cover->picture_data != "Picture data"_bsv) |
| 158 | throw failure("bad extracted picture data"); |
| 159 | |
| 160 | ot::byte_string_view truncated_data = picture_data.substr(0, picture_data.size() - 1); |
| 161 | tags.comments = { u8"METADATA_BLOCK_PICTURE=" + ot::encode_base64(truncated_data) }; |
| 162 | try { |
| 163 | ot::extract_cover(tags); |
| 164 | throw failure("accepted a bad picture block"); |
| 165 | } catch (const ot::status& rc) {} |
| 166 | } |
| 167 | |
| 168 | static void make_cover() |
| 169 | { |
| 170 | ot::byte_string_view picture_block = ""_bsv |
| 171 | "\x00\x00\x00\x03" // Picture type 3. |
| 172 | "\x00\x00\x00\x09" "image/png" // MIME type. |
| 173 | "\x00\x00\x00\x00" "" // Description. |
| 174 | "\x00\x00\x00\x00" // Width. |
| 175 | "\x00\x00\x00\x00" // Height. |
| 176 | "\x00\x00\x00\x00" // Color depth. |
| 177 | "\x00\x00\x00\x00" // Palette size. |
| 178 | "\x00\x00\x00\x11" "\x89PNG Picture data"; |
| 179 | |
| 180 | std::u8string expected = u8"METADATA_BLOCK_PICTURE=" + ot::encode_base64(picture_block); |
| 181 | opaque_is(ot::make_cover("\x89PNG Picture data"_bsv), expected, "build the picture tag"); |
| 182 | } |
| 183 | |
| 184 | int main() |
| 185 | { |
| 186 | std::cout << "1..6\n"; |
| 187 | run(parse_standard, "parse a standard OpusTags packet"); |
| 188 | run(parse_corrupted, "correctly reject invalid packets"); |
| 189 | run(recode_standard, "recode a standard OpusTags packet"); |
| 190 | run(recode_padding, "recode a OpusTags packet with padding"); |
| 191 | run(extract_cover, "extract the cover art"); |
| 192 | run(make_cover, "encode the cover art"); |
| 193 | return 0; |
| 194 | } |