@@ -236,6 +236,18 @@ describe("search methods", async () => {
236236 expect ( foundEntry ) . toBe ( false ) ;
237237 }
238238 } ) ;
239+
240+ it ( "throws an error if the search value is longer than the current value" , async ( ) => {
241+ const { manifest } = await makeManifestOfLength ( 10 ) ;
242+ expect ( ( ) => {
243+ binarySearch (
244+ new Uint8Array ( manifest , HEADER_SIZE ) ,
245+ new Uint8Array ( PATH_HASH_SIZE + 1 ) . fill ( 127 )
246+ ) ;
247+ } ) . toThrowErrorMatchingInlineSnapshot (
248+ `[TypeError: Search value and current value are of different lengths]`
249+ ) ;
250+ } ) ;
239251 } ) ;
240252
241253 describe ( "interpolation search" , ( ) => {
@@ -339,6 +351,57 @@ describe("search methods", async () => {
339351 }
340352 } ) ;
341353
354+ it ( "throws an error if the search value is longer than the current value" , async ( ) => {
355+ const { manifest } = await makeManifestOfLength ( 10 ) ;
356+ expect ( ( ) => {
357+ interpolationSearch (
358+ new Uint8Array ( manifest , HEADER_SIZE ) ,
359+ new Uint8Array ( PATH_HASH_SIZE + 1 ) . fill ( 127 )
360+ ) ;
361+ } ) . toThrowErrorMatchingInlineSnapshot (
362+ `[TypeError: Search value and current value are of different lengths]`
363+ ) ;
364+ } ) ;
365+
366+ it ( "throws an error if the search space is out of order" , async ( ) => {
367+ const smallEntry = {
368+ path : "/small" ,
369+ pathHashBytes : new Uint8Array ( PATH_HASH_SIZE ) ,
370+ contentHash : "00000000000000000000000000000000" ,
371+ } ;
372+ const largeEntry = {
373+ path : "/large" ,
374+ pathHashBytes : new Uint8Array ( PATH_HASH_SIZE ) . fill ( 255 ) ,
375+ contentHash : "ffffffffffffffffffffffffffffffff" ,
376+ } ;
377+ const entries = [ smallEntry , largeEntry ] ;
378+ entries . sort ( ( a , b ) => 0 - compare ( a . pathHashBytes , b . pathHashBytes ) ) ; // inverted order!
379+
380+ const assetManifestBytes = new Uint8Array (
381+ HEADER_SIZE + entries . length * ENTRY_SIZE
382+ ) ;
383+
384+ for ( const [ i , { pathHashBytes, contentHash } ] of entries . entries ( ) ) {
385+ const contentHashBytes = hexToBytes ( contentHash ) ;
386+ const entryOffset = HEADER_SIZE + i * ENTRY_SIZE ;
387+
388+ assetManifestBytes . set ( pathHashBytes , entryOffset + PATH_HASH_OFFSET ) ;
389+ assetManifestBytes . set (
390+ contentHashBytes ,
391+ entryOffset + CONTENT_HASH_OFFSET
392+ ) ;
393+ }
394+
395+ expect ( ( ) => {
396+ interpolationSearch (
397+ new Uint8Array ( assetManifestBytes . buffer , HEADER_SIZE ) ,
398+ smallEntry . pathHashBytes
399+ ) ;
400+ } ) . toThrowErrorMatchingInlineSnapshot (
401+ `[TypeError: Search space is unordered]`
402+ ) ;
403+ } ) ;
404+
342405 it ( "doesn't throw 'RangeError: Division by zero' with extreme manifests" , async ( ) => {
343406 const smallEntry = {
344407 path : "/small" ,
0 commit comments