1 /*
2    Copyright (C) 2003 Commonwealth Scientific and Industrial Research
3    Organisation (CSIRO) Australia
4 
5    Redistribution and use in source and binary forms, with or without
6    modification, are permitted provided that the following conditions
7    are met:
8 
9    - Redistributions of source code must retain the above copyright
10    notice, this list of conditions and the following disclaimer.
11 
12    - Redistributions in binary form must reproduce the above copyright
13    notice, this list of conditions and the following disclaimer in the
14    documentation and/or other materials provided with the distribution.
15 
16    - Neither the name of CSIRO Australia nor the names of its
17    contributors may be used to endorse or promote products derived from
18    this software without specific prior written permission.
19 
20    THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21    ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22    LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
23    PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE ORGANISATION OR
24    CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
25    EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
26    PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
27    PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
28    LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
29    NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
30    SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 */
32 
33 #include "oggz/oggz.h"
34 
35 #include "oggz_tests.h"
36 
37 /* #define DEBUG */
38 
39 static long serialno;
40 
41 static int
hungry(OGGZ * oggz,int empty,void * user_data)42 hungry (OGGZ * oggz, int empty, void * user_data)
43 {
44   unsigned char buf[1];
45   ogg_packet op;
46   static int iter = 0;
47   static long b_o_s = 1;
48   static long e_o_s = 0;
49 
50   if (iter > 10) return 1;
51 
52   buf[0] = 'a' + iter;
53 
54   op.packet = buf;
55   op.bytes = 1;
56   op.b_o_s = b_o_s;
57   op.e_o_s = e_o_s;
58   op.granulepos = iter;
59   op.packetno = iter;
60 
61   /* Main check */
62    if (oggz_write_feed (oggz, &op, serialno, 0, NULL) != 0)
63     FAIL ("Oggz write failed");
64 
65   iter++;
66   b_o_s = 0;
67   if (iter == 10) e_o_s = 1;
68 
69   return 0;
70 }
71 
72 static int
read_packet(OGGZ * oggz,oggz_packet * zp,long serialno,void * user_data)73 read_packet (OGGZ * oggz, oggz_packet * zp, long serialno, void * user_data)
74 {
75   ogg_packet * op = &zp->op;
76   static int iter = 0;
77   static long b_o_s = 1;
78   static long e_o_s = 0;
79 
80 #ifdef DEBUG
81   printf ("%08" PRI_OGGZ_OFF_T "x: serialno %010lu, "
82 	  "granulepos %" PRId64 ", packetno %" PRId64,
83 	  oggz_tell (oggz), serialno, op->granulepos, op->packetno);
84 
85   if (op->b_o_s) {
86     printf (" *** bos");
87   }
88 
89   if (op->e_o_s) {
90     printf (" *** eos");
91   }
92 
93   printf ("\n");
94 #endif
95 
96   if (op->bytes != 1)
97     FAIL ("Packet too long");
98 
99   if (op->packet[0] != 'a' + iter)
100     FAIL ("Packet contains incorrect data");
101 
102   if ((op->b_o_s == 0) != (b_o_s == 0))
103     FAIL ("Packet has incorrect b_o_s");
104 
105   if ((op->e_o_s == 0) != (e_o_s == 0))
106     FAIL ("Packet has incorrect e_o_s");
107 
108   if (op->granulepos != -1 && op->granulepos != iter)
109     FAIL ("Packet has incorrect granulepos");
110 
111   if (op->packetno != iter)
112     FAIL ("Packet has incorrect packetno");
113 
114   iter++;
115   b_o_s = 0;
116   if (iter == 10) e_o_s = 1;
117 
118   return 0;
119 }
120 
121 int
main(int argc,char * argv[])122 main (int argc, char * argv[])
123 {
124   OGGZ * reader, * writer;
125   unsigned char buf[1024];
126   long n;
127 
128   INFO ("Testing read of generated packet sequence");
129 
130   writer = oggz_new (OGGZ_WRITE);
131   if (writer == NULL)
132     FAIL("newly created OGGZ writer == NULL");
133 
134   serialno = oggz_serialno_new (writer);
135 
136   if (oggz_write_set_hungry_callback (writer, hungry, 1, NULL) == -1)
137     FAIL("Could not set hungry callback");
138 
139   reader = oggz_new (OGGZ_READ);
140   if (reader == NULL)
141     FAIL("newly created OGGZ reader == NULL");
142 
143   oggz_set_read_callback (reader, -1, read_packet, NULL);
144 
145   while ((n = oggz_write_output (writer, buf, 1024)) > 0) {
146     oggz_read_input (reader, buf, n);
147   }
148 
149   if (oggz_close (reader) != 0)
150     FAIL("Could not close OGGZ reader");
151 
152   if (oggz_close (writer) != 0)
153     FAIL("Could not close OGGZ writer");
154 
155   exit (0);
156 }
157