| 1 | #include <opustags.h> |
| 2 | #include "tap.h" |
| 3 | |
| 4 | #include <string.h> |
| 5 | |
| 6 | static void check_ref_ogg() |
| 7 | { |
| 8 | ot::file input = fopen("gobble.opus", "r"); |
| 9 | if (input == nullptr) |
| 10 | throw failure("could not open gobble.opus"); |
| 11 | |
| 12 | ot::ogg_reader reader(input.get()); |
| 13 | |
| 14 | if (reader.next_page() != true) |
| 15 | throw failure("could not read the first page"); |
| 16 | if (!ot::is_opus_stream(reader.page)) |
| 17 | throw failure("failed to identify the stream as opus"); |
| 18 | reader.process_header_packet([](ogg_packet& p) { |
| 19 | if (p.bytes != 19) |
| 20 | throw failure("unexpected length for the first packet"); |
| 21 | }); |
| 22 | |
| 23 | if (reader.next_page() != true) |
| 24 | throw failure("could not read the second page"); |
| 25 | reader.process_header_packet([](ogg_packet& p) { |
| 26 | if (p.bytes != 62) |
| 27 | throw failure("unexpected length for the second packet"); |
| 28 | }); |
| 29 | |
| 30 | while (!ogg_page_eos(&reader.page)) { |
| 31 | if (reader.next_page() != true) |
| 32 | throw failure("failure reading a page"); |
| 33 | } |
| 34 | if (reader.next_page() != false) |
| 35 | throw failure("did not correctly detect the end of stream"); |
| 36 | } |
| 37 | |
| 38 | static ogg_packet make_packet(const char* contents) |
| 39 | { |
| 40 | ogg_packet op {}; |
| 41 | op.bytes = strlen(contents); |
| 42 | op.packet = (unsigned char*) contents; |
| 43 | return op; |
| 44 | } |
| 45 | |
| 46 | static bool same_packet(const ogg_packet& lhs, const ogg_packet& rhs) |
| 47 | { |
| 48 | return lhs.bytes == rhs.bytes && memcmp(lhs.packet, rhs.packet, lhs.bytes) == 0; |
| 49 | } |
| 50 | |
| 51 | /** |
| 52 | * Build an in-memory Ogg stream using ogg_writer, and then read it with ogg_reader. |
| 53 | */ |
| 54 | static void check_memory_ogg() |
| 55 | { |
| 56 | ogg_packet first_packet = make_packet("First"); |
| 57 | ogg_packet second_packet = make_packet("Second"); |
| 58 | std::vector<unsigned char> my_ogg(128); |
| 59 | size_t my_ogg_size; |
| 60 | |
| 61 | { |
| 62 | ot::file output = fmemopen(my_ogg.data(), my_ogg.size(), "w"); |
| 63 | if (output == nullptr) |
| 64 | throw failure("could not open the output stream"); |
| 65 | ot::ogg_writer writer(output.get()); |
| 66 | writer.write_header_packet(1234, 0, first_packet); |
| 67 | writer.write_header_packet(1234, 1, second_packet); |
| 68 | my_ogg_size = ftell(output.get()); |
| 69 | if (my_ogg_size != 67) |
| 70 | throw failure("unexpected output size"); |
| 71 | } |
| 72 | |
| 73 | { |
| 74 | ot::file input = fmemopen(my_ogg.data(), my_ogg_size, "r"); |
| 75 | if (input == nullptr) |
| 76 | throw failure("could not open the input stream"); |
| 77 | ot::ogg_reader reader(input.get()); |
| 78 | if (reader.next_page() != true) |
| 79 | throw failure("could not read the first page"); |
| 80 | reader.process_header_packet([&first_packet](ogg_packet &p) { |
| 81 | if (!same_packet(p, first_packet)) |
| 82 | throw failure("unexpected content in the first packet"); |
| 83 | }); |
| 84 | if (reader.next_page() != true) |
| 85 | throw failure("could not read the second page"); |
| 86 | reader.process_header_packet([&second_packet](ogg_packet &p) { |
| 87 | if (!same_packet(p, second_packet)) |
| 88 | throw failure("unexpected content in the second packet"); |
| 89 | }); |
| 90 | if (reader.next_page() != false) |
| 91 | throw failure("unexpected third page"); |
| 92 | } |
| 93 | } |
| 94 | |
| 95 | void check_bad_stream() |
| 96 | { |
| 97 | auto err_msg = "did not detect the stream is not an ogg stream"; |
| 98 | ot::file input = fmemopen((void*) err_msg, 20, "r"); |
| 99 | ot::ogg_reader reader(input.get()); |
| 100 | try { |
| 101 | reader.next_page(); |
| 102 | throw failure("did not raise an error"); |
| 103 | } catch (const ot::status& rc) { |
| 104 | if (rc != ot::st::bad_stream) |
| 105 | throw failure(err_msg); |
| 106 | } |
| 107 | } |
| 108 | |
| 109 | void check_identification() |
| 110 | { |
| 111 | auto good_header = (unsigned char*) |
| 112 | "\x4f\x67\x67\x53\x00\x02\x00\x00\x00\x00\x00\x00\x00\x00\x42\xf2" |
| 113 | "\xe6\xc7\x00\x00\x00\x00\x7e\xc3\x57\x2b\x01\x13"; |
| 114 | auto good_body = (unsigned char*) "OpusHeadABCD"; |
| 115 | |
| 116 | ogg_page id; |
| 117 | id.header = good_header; |
| 118 | id.header_len = 28; |
| 119 | id.body = good_body; |
| 120 | id.body_len = 12; |
| 121 | if (!ot::is_opus_stream(id)) |
| 122 | throw failure("could not identify opus header"); |
| 123 | |
| 124 | // Bad body |
| 125 | id.body_len = 7; |
| 126 | if (ot::is_opus_stream(id)) |
| 127 | throw failure("opus header was too short to be valid"); |
| 128 | id.body_len = 12; |
| 129 | id.body = (unsigned char*) "Not_OpusHead"; |
| 130 | if (ot::is_opus_stream(id)) |
| 131 | throw failure("was not an opus header"); |
| 132 | id.body = good_body; |
| 133 | |
| 134 | // Remove the BoS bit from the header. |
| 135 | id.header = (unsigned char*) |
| 136 | "\x4f\x67\x67\x53\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x42\xf2" |
| 137 | "\xe6\xc7\x00\x00\x00\x00\x7e\xc3\x57\x2b\x01\x13"; |
| 138 | if (ot::is_opus_stream(id)) |
| 139 | throw failure("was not the beginning of a stream"); |
| 140 | } |
| 141 | |
| 142 | void check_renumber_page() |
| 143 | { |
| 144 | ot::file input = fopen("gobble.opus", "r"); |
| 145 | if (input == nullptr) |
| 146 | throw failure("could not open gobble.opus"); |
| 147 | |
| 148 | ot::ogg_reader reader(input.get()); |
| 149 | if (reader.next_page() != true) |
| 150 | throw failure("could not read the first page"); |
| 151 | |
| 152 | long new_pageno = 1234; |
| 153 | ot::renumber_page(reader.page, new_pageno); |
| 154 | if (ogg_page_pageno(&reader.page) != new_pageno) |
| 155 | throw failure("renumbering failed"); |
| 156 | } |
| 157 | |
| 158 | int main(int argc, char **argv) |
| 159 | { |
| 160 | std::cout << "1..5\n"; |
| 161 | run(check_ref_ogg, "check a reference ogg stream"); |
| 162 | run(check_memory_ogg, "build and check a fresh stream"); |
| 163 | run(check_bad_stream, "read a non-ogg stream"); |
| 164 | run(check_identification, "stream identification"); |
| 165 | run(check_renumber_page, "page renumbering"); |
| 166 | return 0; |
| 167 | } |