2005-05-17 17:50:47 +04:00
#!/usr/bin/perl
# NDR alignment tests
# (C) 2005 Jelmer Vernooij. Published under the GNU GPL
use strict ;
2019-11-30 12:34:54 +03:00
use warnings ;
2005-05-17 17:50:47 +04:00
2005-12-25 06:04:13 +03:00
use Test::More tests = > 5 * 8 ;
use FindBin qw( $RealBin ) ;
use lib "$RealBin" ;
use Util qw( test_samba4_ndr ) ;
2005-05-17 17:50:47 +04:00
2005-12-25 06:04:13 +03:00
test_samba4_ndr ( 'align-uint8-uint16' ,
2005-05-17 17:50:47 +04:00
'
typedef [ public ] struct {
uint8 x ;
uint16 y ;
} bla ;
' ,
'
2018-07-05 16:39:52 +03:00
struct ndr_push * ndr = ndr_push_init_ctx ( NULL ) ;
2005-05-17 17:50:47 +04:00
struct bla r ;
2005-05-17 19:51:35 +04:00
uint8_t expected [] = { 0x0D , 0x00 , 0xef , 0xbe } ;
2005-05-17 17:50:47 +04:00
DATA_BLOB expected_blob = { expected , 4 } ;
DATA_BLOB result_blob ;
r . x = 13 ;
r . y = 0xbeef ;
2007-11-09 21:23:25 +03:00
if ( ! NDR_ERR_CODE_IS_SUCCESS ( ndr_push_bla ( ndr , NDR_SCALARS | NDR_BUFFERS , & r ) ) )
2005-05-17 17:50:47 +04:00
return 1 ;
result_blob = ndr_push_blob ( ndr ) ;
2007-07-03 11:28:46 +04:00
if ( data_blob_cmp ( & result_blob , & expected_blob ) != 0 )
2005-05-17 17:50:47 +04:00
return 2 ;
' ) ;
2005-05-17 19:51:35 +04:00
2005-12-25 06:04:13 +03:00
test_samba4_ndr ( 'align-uint8-uint32' ,
2005-05-17 19:51:35 +04:00
'
typedef [ public ] struct {
uint8 x ;
uint32 y ;
} bla ;
' ,
'
2018-07-05 16:39:52 +03:00
struct ndr_push * ndr = ndr_push_init_ctx ( NULL ) ;
2005-05-17 19:51:35 +04:00
struct bla r ;
uint8_t expected [] = { 0x0D , 0x00 , 0x00 , 0x00 , 0xef , 0xbe , 0xef , 0xbe } ;
DATA_BLOB expected_blob = { expected , 8 } ;
DATA_BLOB result_blob ;
r . x = 13 ;
r . y = 0xbeefbeef ;
2007-11-09 21:23:25 +03:00
if ( ! NDR_ERR_CODE_IS_SUCCESS ( ndr_push_bla ( ndr , NDR_SCALARS | NDR_BUFFERS , & r ) ) )
2005-05-17 19:51:35 +04:00
return 1 ;
result_blob = ndr_push_blob ( ndr ) ;
2007-07-03 11:28:46 +04:00
if ( data_blob_cmp ( & result_blob , & expected_blob ) != 0 )
2005-05-17 19:51:35 +04:00
return 2 ;
' ) ;
2005-12-25 06:04:13 +03:00
test_samba4_ndr ( 'align-uint8-hyper' ,
2005-05-17 19:51:35 +04:00
'
typedef [ public ] struct {
uint8 x ;
hyper y ;
} bla ;
' ,
'
2018-07-05 16:39:52 +03:00
struct ndr_push * ndr = ndr_push_init_ctx ( NULL ) ;
2005-05-17 19:51:35 +04:00
struct bla r ;
uint8_t expected [] = { 0x0D , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 ,
2007-10-31 18:25:44 +03:00
0xef , 0xbe , 0xef , 0xbe , 0xef , 0xbe , 0xef , 0xbe } ;
2005-05-17 19:51:35 +04:00
DATA_BLOB expected_blob = { expected , 16 } ;
DATA_BLOB result_blob ;
r . x = 13 ;
2007-10-31 18:25:44 +03:00
r . y = 0xbeefbeefbeefbeef LLU ;
2005-05-17 19:51:35 +04:00
2007-11-09 21:23:25 +03:00
if ( ! NDR_ERR_CODE_IS_SUCCESS ( ndr_push_bla ( ndr , NDR_SCALARS | NDR_BUFFERS , & r ) ) )
2005-05-17 19:51:35 +04:00
return 1 ;
result_blob = ndr_push_blob ( ndr ) ;
2007-07-03 11:28:46 +04:00
if ( data_blob_cmp ( & result_blob , & expected_blob ) != 0 )
2005-05-17 19:51:35 +04:00
return 2 ;
' ) ;
2005-05-17 21:32:25 +04:00
2005-12-25 06:04:13 +03:00
test_samba4_ndr ( 'noalignflag-uint8-uint16' ,
2005-05-17 21:32:25 +04:00
'
typedef [ public ] struct {
uint8 x ;
uint16 y ;
} bla ;
' ,
'
2018-07-05 16:39:52 +03:00
struct ndr_push * ndr = ndr_push_init_ctx ( NULL ) ;
2005-05-17 21:32:25 +04:00
struct bla r ;
uint8_t expected [] = { 0x0D , 0xef , 0xbe } ;
DATA_BLOB expected_blob = { expected , 3 } ;
DATA_BLOB result_blob ;
ndr - > flags |= LIBNDR_FLAG_NOALIGN ;
r . x = 13 ;
r . y = 0xbeef ;
2007-11-09 21:23:25 +03:00
if ( ! NDR_ERR_CODE_IS_SUCCESS ( ndr_push_bla ( ndr , NDR_SCALARS | NDR_BUFFERS , & r ) ) )
2005-05-17 21:32:25 +04:00
return 1 ;
result_blob = ndr_push_blob ( ndr ) ;
2007-07-03 11:28:46 +04:00
if ( data_blob_cmp ( & result_blob , & expected_blob ) != 0 )
2005-05-17 21:32:25 +04:00
return 2 ;
' ) ;
2005-12-25 06:04:13 +03:00
test_samba4_ndr ( 'align-blob-align2' ,
2005-05-17 21:32:25 +04:00
'
typedef [ public ] struct {
uint8 x ;
[ flag ( LIBNDR_FLAG_ALIGN2 ) ] DATA_BLOB data ;
2006-03-24 14:45:40 +03:00
uint8 y ;
2006-11-23 23:59:09 +03:00
} blie ;
2005-05-17 21:32:25 +04:00
' ,
'
2018-07-05 16:39:52 +03:00
struct ndr_push * ndr = ndr_push_init_ctx ( NULL ) ;
2006-11-23 23:59:09 +03:00
struct blie r ;
2005-05-17 21:32:25 +04:00
uint8_t data [] = { 0x01 , 0x02 } ;
2006-03-24 14:45:40 +03:00
uint8_t expected [] = { 0x0D , 0x00 , 0x0E } ;
DATA_BLOB expected_blob = { expected , 3 } ;
2005-05-17 21:32:25 +04:00
DATA_BLOB result_blob ;
r . x = 13 ;
2006-03-24 14:45:40 +03:00
r . y = 14 ;
2005-05-17 21:32:25 +04:00
r . data . data = data ;
r . data . length = 2 ;
2007-11-09 21:23:25 +03:00
if ( ! NDR_ERR_CODE_IS_SUCCESS ( ndr_push_blie ( ndr , NDR_SCALARS | NDR_BUFFERS , & r ) ) )
2005-05-17 21:32:25 +04:00
return 1 ;
result_blob = ndr_push_blob ( ndr ) ;
2007-07-03 11:28:46 +04:00
if ( data_blob_cmp ( & result_blob , & expected_blob ) != 0 )
2005-05-17 21:32:25 +04:00
return 2 ;
' ) ;