2017-09-21 22:26:06 +03:00
/*
2015-02-23 12:05:19 +03:00
* Copyright ( C ) 2015 Red Hat , Inc .
*
2018-01-30 22:26:26 +03:00
* SPDX - License - Identifier : LGPL - 2.0 +
*
2015-02-23 12:05:19 +03:00
* This library is free software ; you can redistribute it and / or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation ; either
* version 2 of the License , or ( at your option ) any later version .
*
* This library is distributed in the hope that it will be useful ,
* but WITHOUT ANY WARRANTY ; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE . See the GNU
* Lesser General Public License for more details .
*
* You should have received a copy of the GNU Lesser General Public
2021-12-07 04:20:55 +03:00
* License along with this library . If not , see < https : //www.gnu.org/licenses/>.
2015-02-23 12:05:19 +03:00
*/
# include "config.h"
# include "bsdiff/bsdiff.h"
# include "bsdiff/bspatch.h"
2023-05-01 21:24:29 +03:00
# include "libglnx.h"
# include <gio/gio.h>
2015-02-23 12:05:19 +03:00
# include <glib.h>
# include <stdlib.h>
# include <string.h>
static int
2023-05-01 21:24:29 +03:00
bzpatch_read ( const struct bspatch_stream * stream , void * buffer , int length )
2015-02-23 12:05:19 +03:00
{
GInputStream * in = stream - > opaque ;
2023-05-01 21:24:29 +03:00
if ( length & & ! g_input_stream_read ( in , buffer , length , NULL , NULL ) )
2015-02-23 12:05:19 +03:00
return - 1 ;
return 0 ;
}
static int
2023-05-01 21:24:29 +03:00
bzdiff_write ( struct bsdiff_stream * stream , const void * buffer , int size )
2015-02-23 12:05:19 +03:00
{
GOutputStream * out = stream - > opaque ;
2023-05-01 21:24:29 +03:00
if ( ! g_output_stream_write ( out , buffer , size , NULL , NULL ) )
2015-02-23 12:05:19 +03:00
return - 1 ;
return 0 ;
}
static void
test_bsdiff ( void )
{
# define OLD_SIZE 512
2023-05-01 21:24:29 +03:00
# define NEW_SIZE (512 + 24)
2015-02-23 12:05:19 +03:00
struct bsdiff_stream bsdiff_stream ;
struct bspatch_stream bspatch_stream ;
int i ;
2015-05-05 00:58:26 +03:00
g_autofree guint8 * old = g_new ( guint8 , OLD_SIZE ) ;
g_autofree guint8 * new = g_new ( guint8 , NEW_SIZE ) ;
g_autofree guint8 * new_generated = g_new0 ( guint8 , NEW_SIZE ) ;
2023-05-01 21:24:29 +03:00
g_autoptr ( GOutputStream ) out = g_memory_output_stream_new_resizable ( ) ;
g_autoptr ( GInputStream ) in = NULL ;
2015-02-23 12:05:19 +03:00
new [ 0 ] = ' A ' ;
for ( i = 0 ; i < OLD_SIZE ; i + + )
{
old [ i ] = i ;
new [ i + 1 ] = old [ i ] ;
}
for ( i = OLD_SIZE + 1 ; i < NEW_SIZE ; i + + )
new [ i ] = i ;
bsdiff_stream . malloc = malloc ;
bsdiff_stream . free = free ;
bsdiff_stream . write = bzdiff_write ;
bsdiff_stream . opaque = out ;
g_assert_cmpint ( bsdiff ( old , OLD_SIZE , new , NEW_SIZE , & bsdiff_stream ) , = = , 0 ) ;
2017-07-31 16:27:56 +03:00
g_assert ( g_output_stream_close ( out , NULL , NULL ) ) ;
2015-02-23 12:05:19 +03:00
/* Now generate NEW_GENERATED from OLD and OUT. */
2023-05-01 21:24:29 +03:00
{
g_autoptr ( GBytes ) bytes = g_memory_output_stream_steal_as_bytes ( G_MEMORY_OUTPUT_STREAM ( out ) ) ;
2016-12-02 21:45:04 +03:00
in = g_memory_input_stream_new_from_bytes ( bytes ) ;
}
2015-02-23 12:05:19 +03:00
bspatch_stream . read = bzpatch_read ;
bspatch_stream . opaque = in ;
g_assert_cmpint ( bspatch ( old , OLD_SIZE , new_generated , NEW_SIZE , & bspatch_stream ) , = = , 0 ) ;
g_assert_cmpint ( memcmp ( new , new_generated , NEW_SIZE ) , = = , 0 ) ;
}
2023-05-01 21:24:29 +03:00
int
main ( int argc , char * * argv )
2015-02-23 12:05:19 +03:00
{
g_test_init ( & argc , & argv , NULL ) ;
g_test_add_func ( " /bsdiff " , test_bsdiff ) ;
2023-05-01 21:24:29 +03:00
return g_test_run ( ) ;
2015-02-23 12:05:19 +03:00
}