@@ -104,9 +104,9 @@ pub fn get_text_section<'a>(
104104 std:: str:: from_utf8 ( bytes) . or ( Err ( UkiError :: UnicodeError ( section_name. into ( ) ) ) )
105105}
106106
107- /// Buffered version of [`get_text_section`].
107+ /// Buffered version of [`get_text_section`].
108108///
109- /// See [`get_text_section`] for details. This version works with any [`Read`] + [`Seek`]
109+ /// See [`get_text_section`] for details. This version works with any [`Read`] + [`Seek`]
110110/// source instead of requiring the entire image in memory.
111111pub fn get_text_section_buffered < ' a , R : Read + Seek > (
112112 image : & ' a mut R ,
@@ -179,7 +179,7 @@ pub fn get_section<'a>(
179179
180180/// Buffered version of [`get_section`].
181181///
182- /// See [`get_section`] for details. This version works with any [`Read`] + [`Seek`]
182+ /// See [`get_section`] for details. This version works with any [`Read`] + [`Seek`]
183183/// source and returns owned data instead of borrowed slices.
184184pub fn get_section_buffered < R : Read + Seek > (
185185 image : & mut R ,
@@ -352,10 +352,18 @@ ID=pretty-os
352352"# ,
353353 ) ;
354354
355+ // Test slice-based functions
355356 assert_eq ! (
356357 get_boot_label( uki. as_ref( ) ) . unwrap( ) ,
357358 "prettyOS Rocky Racoon"
358359 ) ;
360+
361+ // Test buffered functions produce same results
362+ let mut cursor = std:: io:: Cursor :: new ( & uki) ;
363+ assert_eq ! (
364+ get_boot_label_buffered( & mut cursor) . unwrap( ) ,
365+ "prettyOS Rocky Racoon"
366+ ) ;
359367 }
360368
361369 #[ test]
@@ -371,6 +379,13 @@ ID=pretty-os
371379 get_boot_label( img) ,
372380 Err ( UkiError :: MissingSection ( s) ) if s == ".osrel"
373381 ) ) ;
382+
383+ // Test buffered version
384+ let mut cursor = std:: io:: Cursor :: new ( img) ;
385+ assert ! ( matches!(
386+ get_boot_label_buffered( & mut cursor) ,
387+ Err ( UkiError :: MissingSection ( s) ) if s == ".osrel"
388+ ) ) ;
374389 }
375390
376391 pe_err ( b"" ) ;
@@ -413,4 +428,93 @@ ID=pretty-os
413428 & [ ] ,
414429 ) ) ;
415430 }
431+
432+ #[ test]
433+ fn test_section_functions ( ) {
434+ let osrel_data = b"PRETTY_NAME='TestOS'\n VERSION_ID=1.0\n " ;
435+ let cmdline_data = b"root=/dev/sda1 quiet" ;
436+
437+ let osrel_offset = data_offset ( 2 ) ;
438+ let cmdline_offset = osrel_offset + osrel_data. len ( ) ;
439+
440+ let uki = peify (
441+ b"" ,
442+ & [
443+ SectionHeader {
444+ name : * b".osrel\0 \0 " ,
445+ virtual_size : U32 :: new ( osrel_data. len ( ) as u32 ) ,
446+ pointer_to_raw_data : U32 :: new ( osrel_offset as u32 ) ,
447+ ..Default :: default ( )
448+ } ,
449+ SectionHeader {
450+ name : * b".cmdline" ,
451+ virtual_size : U32 :: new ( cmdline_data. len ( ) as u32 ) ,
452+ pointer_to_raw_data : U32 :: new ( cmdline_offset as u32 ) ,
453+ ..Default :: default ( )
454+ } ,
455+ ] ,
456+ & [ osrel_data, cmdline_data] ,
457+ ) ;
458+
459+ // Test slice-based functions
460+ let osrel_section = get_section ( & uki, ".osrel" ) . unwrap ( ) . unwrap ( ) ;
461+ assert_eq ! ( osrel_section, osrel_data) ;
462+
463+ let cmdline_section = get_section ( & uki, ".cmdline" ) . unwrap ( ) . unwrap ( ) ;
464+ assert_eq ! ( cmdline_section, cmdline_data) ;
465+
466+ let osrel_text = get_text_section ( & uki, ".osrel" ) . unwrap ( ) ;
467+ assert_eq ! ( osrel_text, "PRETTY_NAME='TestOS'\n VERSION_ID=1.0\n " ) ;
468+
469+ let cmdline_text = get_cmdline ( & uki) . unwrap ( ) ;
470+ assert_eq ! ( cmdline_text, "root=/dev/sda1 quiet" ) ;
471+
472+ // Test buffered functions produce same results
473+ let mut cursor = std:: io:: Cursor :: new ( & uki) ;
474+ let osrel_section_buf = get_section_buffered ( & mut cursor, ".osrel" ) . unwrap ( ) ;
475+ assert_eq ! ( osrel_section_buf, osrel_data) ;
476+
477+ cursor. set_position ( 0 ) ;
478+ let cmdline_section_buf = get_section_buffered ( & mut cursor, ".cmdline" ) . unwrap ( ) ;
479+ assert_eq ! ( cmdline_section_buf, cmdline_data) ;
480+
481+ cursor. set_position ( 0 ) ;
482+ let osrel_text_buf = get_text_section_buffered ( & mut cursor, ".osrel" ) . unwrap ( ) ;
483+ assert_eq ! ( osrel_text_buf, "PRETTY_NAME='TestOS'\n VERSION_ID=1.0\n " ) ;
484+
485+ cursor. set_position ( 0 ) ;
486+ let cmdline_text_buf = get_cmdline_buffered ( & mut cursor) . unwrap ( ) ;
487+ assert_eq ! ( cmdline_text_buf, "root=/dev/sda1 quiet" ) ;
488+
489+ // Test missing section
490+ cursor. set_position ( 0 ) ;
491+ let missing_result = get_section_buffered ( & mut cursor, ".missing" ) ;
492+ assert ! ( matches!( missing_result, Err ( UkiError :: MissingSection ( s) ) if s == ".missing" ) ) ;
493+ }
494+
495+ #[ test]
496+ fn test_invalid_utf8 ( ) {
497+ let invalid_utf8 = b"\xff \xfe \xfd " ;
498+ let osrel_offset = data_offset ( 1 ) ;
499+
500+ let uki = peify (
501+ b"" ,
502+ & [ SectionHeader {
503+ name : * b".osrel\0 \0 " ,
504+ virtual_size : U32 :: new ( invalid_utf8. len ( ) as u32 ) ,
505+ pointer_to_raw_data : U32 :: new ( osrel_offset as u32 ) ,
506+ ..Default :: default ( )
507+ } ] ,
508+ & [ invalid_utf8] ,
509+ ) ;
510+
511+ // Test slice-based function
512+ let result = get_text_section ( & uki, ".osrel" ) ;
513+ assert ! ( matches!( result, Err ( UkiError :: UnicodeError ( s) ) if s == ".osrel" ) ) ;
514+
515+ // Test buffered function gives same error
516+ let mut cursor = std:: io:: Cursor :: new ( & uki) ;
517+ let result_buf = get_text_section_buffered ( & mut cursor, ".osrel" ) ;
518+ assert ! ( matches!( result_buf, Err ( UkiError :: UnicodeError ( s) ) if s == ".osrel" ) ) ;
519+ }
416520}
0 commit comments