Debian Codegraph @

t/ogg.cc

raw symbols SCIP
1 #cxx . . $ `<file>/t/ogg.cc`/include <opustags.h>cxx . . $ `<file>/src/opustags.h`/
2 #include "tap.h"cxx . . $ `<file>/t/tap.h`/
3
4 #include <string.h>
5
6 static void check_ref_oggcxx . . $ check_ref_ogg(49f6e7a06ebc5aa8).()
7 {
8 otcxx . . $ ot/::filecxx . . $ ot/file# input = fopencxx . . $ ot/file#file(8d4262d45fc644ab).("gobble.opus", "r");
9 if (input ==cxx . . $ std/`operator==`(e445595a98297a59). nullptr)
10 throw failurecxx . . $ tap/failure#("could not open gobble.opus"cxx . . $ std/__cxx11/basic_string#basic_string(e36662616dba894d).);
11
12 otcxx . . $ ot/::ogg_readercxx . . $ ot/ogg_reader# readercxx . . $ ot/ogg_reader#ogg_reader(8d4262d45fc644ab).(input.getcxx . . $ std/unique_ptr#get(ea7391e9eeeaf20e).());
13
14 if (reader.next_pagecxx . . $ ot/ogg_reader#next_page(c9857a73d68e59ba).() != true)
15 throw failurecxx . . $ tap/failure#("could not read the first page"cxx . . $ std/__cxx11/basic_string#basic_string(e36662616dba894d).);
16 if (!otcxx . . $ ot/::is_opus_streamcxx . . $ ot/is_opus_stream(f406ab5a65ea8040).(reader.pagecxx . . $ ot/ogg_reader#page.))
17 throw failurecxx . . $ tap/failure#("failed to identify the stream as opus"cxx . . $ std/__cxx11/basic_string#basic_string(e36662616dba894d).);
18 reader.process_header_packetcxx . . $ ot/ogg_reader#process_header_packet(4de049347c6f80eb).([cxx . . $ std/function#function(bdc3712e480e4d04).](ogg_packet& p) {
19 if (p.bytes != 19)
20 throw failurecxx . . $ tap/failure#("unexpected length for the first packet"cxx . . $ std/__cxx11/basic_string#basic_string(e36662616dba894d).);
21 });
22
23 if (reader.next_pagecxx . . $ ot/ogg_reader#next_page(c9857a73d68e59ba).() != true)
24 throw failurecxx . . $ tap/failure#("could not read the second page"cxx . . $ std/__cxx11/basic_string#basic_string(e36662616dba894d).);
25 reader.process_header_packetcxx . . $ ot/ogg_reader#process_header_packet(4de049347c6f80eb).([cxx . . $ std/function#function(bdc3712e480e4d04).](ogg_packet& p) {
26 if (p.bytes != 62)
27 throw failurecxx . . $ tap/failure#("unexpected length for the second packet"cxx . . $ std/__cxx11/basic_string#basic_string(e36662616dba894d).);
28 });
29
30 while (!ogg_page_eos(&reader.pagecxx . . $ ot/ogg_reader#page.)) {
31 if (reader.next_pagecxx . . $ ot/ogg_reader#next_page(c9857a73d68e59ba).() != true)
32 throw failurecxx . . $ tap/failure#("failure reading a page"cxx . . $ std/__cxx11/basic_string#basic_string(e36662616dba894d).);
33 }
34 if (reader.next_pagecxx . . $ ot/ogg_reader#next_page(c9857a73d68e59ba).() != false)
35 throw failurecxx . . $ tap/failure#("did not correctly detect the end of stream"cxx . . $ std/__cxx11/basic_string#basic_string(e36662616dba894d).);
36 }
37
38 static ogg_packet make_packetcxx . . $ make_packet(19fd28c8ccc6839f).(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_packetcxx . . $ same_packet(2148d66451d9126).(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_oggcxx . . $ check_memory_ogg(49f6e7a06ebc5aa8).()
55 {
56 ogg_packet first_packet = make_packetcxx . . $ make_packet(19fd28c8ccc6839f).("First");
57 ogg_packet second_packet = make_packetcxx . . $ make_packet(19fd28c8ccc6839f).("Second");
58 stdcxx . . $ std/::vectorcxx . . $ std/vector#<unsigned char> my_oggcxx . . $ std/vector#vector(83fdeebbe0501a83).(128);
59 size_t my_ogg_size;
60
61 {
62 otcxx . . $ ot/::filecxx . . $ ot/file# output = fmemopencxx . . $ ot/file#file(8d4262d45fc644ab).(my_ogg.datacxx . . $ std/vector#data(258ee1c9e0a18a5c).(), my_ogg.sizecxx . . $ std/vector#size(c9b1e749b64d5edc).(), "w");
63 if (output ==cxx . . $ std/`operator==`(e445595a98297a59). nullptr)
64 throw failurecxx . . $ tap/failure#("could not open the output stream"cxx . . $ std/__cxx11/basic_string#basic_string(e36662616dba894d).);
65 otcxx . . $ ot/::ogg_writercxx . . $ ot/ogg_writer# writercxx . . $ ot/ogg_writer#ogg_writer(8d4262d45fc644ab).(output.getcxx . . $ std/unique_ptr#get(ea7391e9eeeaf20e).());
66 writer.write_header_packetcxx . . $ ot/ogg_writer#write_header_packet(39685ba16f0fc2d4).(1234, 0, first_packet);
67 writer.write_header_packetcxx . . $ ot/ogg_writer#write_header_packet(39685ba16f0fc2d4).(1234, 1, second_packet);
68 my_ogg_size = ftell(output.getcxx . . $ std/unique_ptr#get(ea7391e9eeeaf20e).());
69 if (my_ogg_size != 67)
70 throw failurecxx . . $ tap/failure#("unexpected output size"cxx . . $ std/__cxx11/basic_string#basic_string(e36662616dba894d).);
71 }
72
73 {
74 otcxx . . $ ot/::filecxx . . $ ot/file# input = fmemopencxx . . $ ot/file#file(8d4262d45fc644ab).(my_ogg.datacxx . . $ std/vector#data(258ee1c9e0a18a5c).(), my_ogg_size, "r");
75 if (input ==cxx . . $ std/`operator==`(e445595a98297a59). nullptr)
76 throw failurecxx . . $ tap/failure#("could not open the input stream"cxx . . $ std/__cxx11/basic_string#basic_string(e36662616dba894d).);
77 otcxx . . $ ot/::ogg_readercxx . . $ ot/ogg_reader# readercxx . . $ ot/ogg_reader#ogg_reader(8d4262d45fc644ab).(input.getcxx . . $ std/unique_ptr#get(ea7391e9eeeaf20e).());
78 if (reader.next_pagecxx . . $ ot/ogg_reader#next_page(c9857a73d68e59ba).() != true)
79 throw failurecxx . . $ tap/failure#("could not read the first page"cxx . . $ std/__cxx11/basic_string#basic_string(e36662616dba894d).);
80 reader.process_header_packetcxx . . $ ot/ogg_reader#process_header_packet(4de049347c6f80eb).([cxx . . $ std/function#function(bdc3712e480e4d04).&first_packet](ogg_packet &p) {
81 if (!same_packetcxx . . $ same_packet(2148d66451d9126).(p, first_packet))
82 throw failurecxx . . $ tap/failure#("unexpected content in the first packet"cxx . . $ std/__cxx11/basic_string#basic_string(e36662616dba894d).);
83 });
84 if (reader.next_pagecxx . . $ ot/ogg_reader#next_page(c9857a73d68e59ba).() != true)
85 throw failurecxx . . $ tap/failure#("could not read the second page"cxx . . $ std/__cxx11/basic_string#basic_string(e36662616dba894d).);
86 reader.process_header_packetcxx . . $ ot/ogg_reader#process_header_packet(4de049347c6f80eb).([cxx . . $ std/function#function(bdc3712e480e4d04).&second_packet](ogg_packet &p) {
87 if (!same_packetcxx . . $ same_packet(2148d66451d9126).(p, second_packet))
88 throw failurecxx . . $ tap/failure#("unexpected content in the second packet"cxx . . $ std/__cxx11/basic_string#basic_string(e36662616dba894d).);
89 });
90 if (reader.next_pagecxx . . $ ot/ogg_reader#next_page(c9857a73d68e59ba).() != false)
91 throw failurecxx . . $ tap/failure#("unexpected third page"cxx . . $ std/__cxx11/basic_string#basic_string(e36662616dba894d).);
92 }
93 }
94
95 void check_bad_streamcxx . . $ check_bad_stream(49f6e7a06ebc5aa8).()
96 {
97 auto err_msg = "did not detect the stream is not an ogg stream";
98 otcxx . . $ ot/::filecxx . . $ ot/file# input = fmemopencxx . . $ ot/file#file(8d4262d45fc644ab).((void*) err_msg, 20, "r");
99 otcxx . . $ ot/::ogg_readercxx . . $ ot/ogg_reader# readercxx . . $ ot/ogg_reader#ogg_reader(8d4262d45fc644ab).(input.getcxx . . $ std/unique_ptr#get(ea7391e9eeeaf20e).());
100 try {
101 reader.next_pagecxx . . $ ot/ogg_reader#next_page(c9857a73d68e59ba).();
102 throw failurecxx . . $ tap/failure#("did not raise an error"cxx . . $ std/__cxx11/basic_string#basic_string(e36662616dba894d).);
103 } catch (const otcxx . . $ ot/::statuscxx . . $ ot/status#& rc) {
104 if (rc != otcxx . . $ ot/::stcxx . . $ ot/st#::bad_streamcxx . . $ ot/st#bad_stream.)
105 throw failurecxx . . $ tap/failure#(err_msgcxx . . $ std/__cxx11/basic_string#basic_string(e36662616dba894d).);
106 }
107 }
108
109 void check_identificationcxx . . $ check_identification(49f6e7a06ebc5aa8).()
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 (!otcxx . . $ ot/::is_opus_streamcxx . . $ ot/is_opus_stream(f406ab5a65ea8040).(id))
122 throw failurecxx . . $ tap/failure#("could not identify opus header"cxx . . $ std/__cxx11/basic_string#basic_string(e36662616dba894d).);
123
124 // Bad body
125 id.body_len = 7;
126 if (otcxx . . $ ot/::is_opus_streamcxx . . $ ot/is_opus_stream(f406ab5a65ea8040).(id))
127 throw failurecxx . . $ tap/failure#("opus header was too short to be valid"cxx . . $ std/__cxx11/basic_string#basic_string(e36662616dba894d).);
128 id.body_len = 12;
129 id.body = (unsigned char*) "Not_OpusHead";
130 if (otcxx . . $ ot/::is_opus_streamcxx . . $ ot/is_opus_stream(f406ab5a65ea8040).(id))
131 throw failurecxx . . $ tap/failure#("was not an opus header"cxx . . $ std/__cxx11/basic_string#basic_string(e36662616dba894d).);
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 (otcxx . . $ ot/::is_opus_streamcxx . . $ ot/is_opus_stream(f406ab5a65ea8040).(id))
139 throw failurecxx . . $ tap/failure#("was not the beginning of a stream"cxx . . $ std/__cxx11/basic_string#basic_string(e36662616dba894d).);
140 }
141
142 void check_renumber_pagecxx . . $ check_renumber_page(49f6e7a06ebc5aa8).()
143 {
144 otcxx . . $ ot/::filecxx . . $ ot/file# input = fopencxx . . $ ot/file#file(8d4262d45fc644ab).("gobble.opus", "r");
145 if (input ==cxx . . $ std/`operator==`(e445595a98297a59). nullptr)
146 throw failurecxx . . $ tap/failure#("could not open gobble.opus"cxx . . $ std/__cxx11/basic_string#basic_string(e36662616dba894d).);
147
148 otcxx . . $ ot/::ogg_readercxx . . $ ot/ogg_reader# readercxx . . $ ot/ogg_reader#ogg_reader(8d4262d45fc644ab).(input.getcxx . . $ std/unique_ptr#get(ea7391e9eeeaf20e).());
149 if (reader.next_pagecxx . . $ ot/ogg_reader#next_page(c9857a73d68e59ba).() != true)
150 throw failurecxx . . $ tap/failure#("could not read the first page"cxx . . $ std/__cxx11/basic_string#basic_string(e36662616dba894d).);
151
152 long new_pageno = 1234;
153 otcxx . . $ ot/::renumber_pagecxx . . $ ot/renumber_page(99e4e6107b845fea).(reader.pagecxx . . $ ot/ogg_reader#page., new_pageno);
154 if (ogg_page_pageno(&reader.pagecxx . . $ ot/ogg_reader#page.) != new_pageno)
155 throw failurecxx . . $ tap/failure#("renumbering failed"cxx . . $ std/__cxx11/basic_string#basic_string(e36662616dba894d).);
156 }
157
158 int maincxx . . $ main(afcd3168fc1188a4).(int argc, char **argv)
159 {
160 stdcxx . . $ std/::coutcxx . . $ std/cout. <<cxx . . $ std/`operator<<`(efaf8c13fc75f3c7). "1..5\n";
161 runcxx . . $ tap/run(7ee90fd1703d5d92).(check_ref_oggcxx . . $ check_ref_ogg(49f6e7a06ebc5aa8)., "check a reference ogg stream");
162 runcxx . . $ tap/run(7ee90fd1703d5d92).(check_memory_oggcxx . . $ check_memory_ogg(49f6e7a06ebc5aa8)., "build and check a fresh stream");
163 runcxx . . $ tap/run(7ee90fd1703d5d92).(check_bad_streamcxx . . $ check_bad_stream(49f6e7a06ebc5aa8)., "read a non-ogg stream");
164 runcxx . . $ tap/run(7ee90fd1703d5d92).(check_identificationcxx . . $ check_identification(49f6e7a06ebc5aa8)., "stream identification");
165 runcxx . . $ tap/run(7ee90fd1703d5d92).(check_renumber_pagecxx . . $ check_renumber_page(49f6e7a06ebc5aa8)., "page renumbering");
166 return 0;
167 }