@@ -120,3 +120,147 @@ impl ToBox for PlaceholderDataBox {
120120 Ok ( ( ) )
121121 }
122122}
123+
124+ #[ cfg( test) ]
125+ mod tests {
126+ #![ allow( clippy:: expect_used) ]
127+ #![ allow( clippy:: panic) ]
128+ #![ allow( clippy:: unwrap_used) ]
129+
130+ use std:: io:: { Cursor , Write } ;
131+
132+ use hex_literal:: hex;
133+
134+ use crate :: {
135+ builder:: {
136+ to_box:: { jumbf_size, write_jumbf} ,
137+ PlaceholderDataBox , ToBox ,
138+ } ,
139+ BoxType ,
140+ } ;
141+
142+ const RANDOM_BOX_TYPE : BoxType = BoxType ( * b"abcd" ) ;
143+
144+ #[ test]
145+ fn simple_case ( ) {
146+ let expected_jumbf = hex ! (
147+ "00000018" // box size
148+ "61626364" // box type = 'abcd'
149+ "00000000000000000000000000000000" // placeholder
150+ ) ;
151+
152+ let pbox = PlaceholderDataBox :: new ( RANDOM_BOX_TYPE , 16 ) ;
153+
154+ assert_eq ! ( pbox. box_type( ) , RANDOM_BOX_TYPE ) ;
155+ assert_eq ! ( pbox. payload_size( ) . unwrap( ) , 16 ) ;
156+ assert_eq ! ( jumbf_size( & pbox) . unwrap( ) , 24 ) ;
157+
158+ let mut jumbf = Cursor :: new ( Vec :: < u8 > :: new ( ) ) ;
159+ write_jumbf ( & pbox, & mut jumbf) . unwrap ( ) ;
160+ assert_eq ! ( * jumbf. get_ref( ) , expected_jumbf) ;
161+
162+ let expected_jumbf = hex ! (
163+ "00000018" // box size
164+ "61626364" // box type = 'abcd'
165+ "31323334353637383930000000000000" // replacement payload
166+ ) ;
167+
168+ pbox. replace_payload ( & mut jumbf, & expected_jumbf[ 8 ..18 ] )
169+ . unwrap ( ) ;
170+ assert_eq ! ( * jumbf. get_ref( ) , expected_jumbf) ;
171+ }
172+
173+ #[ test]
174+ fn error_write_payload_only ( ) {
175+ // PlaceholderDataBox reports an error if its .write_payload() method
176+ // is called by itself.
177+
178+ let pbox = PlaceholderDataBox :: new ( RANDOM_BOX_TYPE , 16 ) ;
179+
180+ let mut payload = Cursor :: new ( Vec :: < u8 > :: new ( ) ) ;
181+ let err = pbox. write_payload ( & mut payload) . unwrap_err ( ) ;
182+ assert_eq ! (
183+ "Custom { kind: Other, error: \" placeholder stream should have some data already\" }" ,
184+ format!( "{err:?}" )
185+ ) ;
186+ }
187+
188+ #[ test]
189+ fn error_payload_too_large ( ) {
190+ let expected_jumbf = hex ! (
191+ "00000018" // box size
192+ "61626364" // box type = 'abcd'
193+ "00000000000000000000000000000000" // placeholder
194+ ) ;
195+
196+ let pbox = PlaceholderDataBox :: new ( RANDOM_BOX_TYPE , 16 ) ;
197+
198+ let mut jumbf = Cursor :: new ( Vec :: < u8 > :: new ( ) ) ;
199+ write_jumbf ( & pbox, & mut jumbf) . unwrap ( ) ;
200+ assert_eq ! ( * jumbf. get_ref( ) , expected_jumbf) ;
201+
202+ let payload_too_large = [ 1u8 ; 17 ] ;
203+ let err = pbox
204+ . replace_payload ( & mut jumbf, & payload_too_large)
205+ . unwrap_err ( ) ;
206+
207+ assert_eq ! (
208+ "Custom { kind: Other, error: \" replace_payload: payload (17 bytes) is larger than reserved capacity (16 bytes)\" }" ,
209+ format!( "{err:?}" )
210+ ) ;
211+
212+ // No part of the original JUMBF as written should have been changed.
213+ assert_eq ! ( * jumbf. get_ref( ) , expected_jumbf) ;
214+ }
215+
216+ #[ test]
217+ fn error_write_jumbf_not_called ( ) {
218+ let pbox = PlaceholderDataBox :: new ( RANDOM_BOX_TYPE , 16 ) ;
219+
220+ let mut jumbf = Cursor :: new ( Vec :: < u8 > :: new ( ) ) ;
221+ let payload = [ 1u8 ; 16 ] ;
222+ let err = pbox. replace_payload ( & mut jumbf, & payload) . unwrap_err ( ) ;
223+
224+ assert_eq ! (
225+ "Custom { kind: Other, error: \" replace_payload: no offset recorded; call write_jumbf() first\" }" ,
226+ format!( "{err:?}" )
227+ ) ;
228+
229+ // No part of the original JUMBF as written should have been changed.
230+ assert_eq ! ( * jumbf. get_ref( ) , [ ] ) ;
231+ }
232+
233+ #[ test]
234+ fn offset ( ) {
235+ let expected_jumbf = hex ! (
236+ "41424344" // arbitrary prefix = 'ABCD'
237+ "00000018" // box size
238+ "61626364" // box type = 'abcd'
239+ "00000000000000000000000000000000" // placeholder
240+ ) ;
241+
242+ let pbox = PlaceholderDataBox :: new ( RANDOM_BOX_TYPE , 16 ) ;
243+
244+ assert_eq ! ( pbox. box_type( ) , RANDOM_BOX_TYPE ) ;
245+ assert_eq ! ( pbox. payload_size( ) . unwrap( ) , 16 ) ;
246+ assert_eq ! ( jumbf_size( & pbox) . unwrap( ) , 24 ) ;
247+
248+ let mut jumbf = Cursor :: new ( Vec :: < u8 > :: new ( ) ) ;
249+ jumbf. write_all ( b"ABCD" ) . unwrap ( ) ;
250+
251+ write_jumbf ( & pbox, & mut jumbf) . unwrap ( ) ;
252+ assert_eq ! ( * jumbf. get_ref( ) , expected_jumbf) ;
253+
254+ assert_eq ! ( pbox. offset( ) , Some ( 12 ) ) ;
255+ }
256+
257+ #[ test]
258+ fn offset_before_write ( ) {
259+ let pbox = PlaceholderDataBox :: new ( RANDOM_BOX_TYPE , 16 ) ;
260+
261+ assert_eq ! ( pbox. box_type( ) , RANDOM_BOX_TYPE ) ;
262+ assert_eq ! ( pbox. payload_size( ) . unwrap( ) , 16 ) ;
263+ assert_eq ! ( jumbf_size( & pbox) . unwrap( ) , 24 ) ;
264+ assert_eq ! ( pbox. offset( ) , None ) ;
265+ }
266+ }
0 commit comments