2010-12-06 20:03:22 +03:00
/*
2014-03-17 13:38:38 +04:00
* Copyright ( C ) 2011 , 2014 Red Hat , Inc .
2010-12-06 20:03:22 +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.1 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
2012-09-21 02:30:55 +04:00
* License along with this library . If not , see
2012-07-21 14:06:23 +04:00
* < http : //www.gnu.org/licenses/>.
2010-12-06 20:03:22 +03:00
*/
# include <config.h>
# include <signal.h>
# include "testutils.h"
2012-12-13 22:21:53 +04:00
# include "virerror.h"
2012-12-12 22:06:53 +04:00
# include "viralloc.h"
2012-12-12 21:59:27 +04:00
# include "virlog.h"
2013-05-03 16:52:21 +04:00
# include "virstring.h"
2010-12-06 20:03:22 +03:00
# include "rpc/virnetmessage.h"
# define VIR_FROM_THIS VIR_FROM_RPC
2014-02-28 16:16:17 +04:00
VIR_LOG_INIT ( " tests.netmessagetest " ) ;
2010-12-06 20:03:22 +03:00
static int testMessageHeaderEncode ( const void * args ATTRIBUTE_UNUSED )
{
2012-04-26 19:21:24 +04:00
virNetMessagePtr msg = virNetMessageNew ( true ) ;
2010-12-06 20:03:22 +03:00
static const char expect [ ] = {
0x00 , 0x00 , 0x00 , 0x1c , /* Length */
0x11 , 0x22 , 0x33 , 0x44 , /* Program */
0x00 , 0x00 , 0x00 , 0x01 , /* Version */
0x00 , 0x00 , 0x06 , 0x66 , /* Procedure */
0x00 , 0x00 , 0x00 , 0x00 , /* Type */
0x00 , 0x00 , 0x00 , 0x99 , /* Serial */
0x00 , 0x00 , 0x00 , 0x00 , /* Status */
} ;
2012-04-26 19:21:24 +04:00
/* According to doc to virNetMessageEncodeHeader(&msg):
* msg - > buffer will be this long */
2013-05-07 15:22:00 +04:00
unsigned long msg_buf_size = VIR_NET_MESSAGE_INITIAL + VIR_NET_MESSAGE_LEN_MAX ;
2012-04-26 19:21:24 +04:00
int ret = - 1 ;
2010-12-06 20:03:22 +03:00
2013-07-04 14:20:21 +04:00
if ( ! msg )
2010-12-06 20:03:22 +03:00
return - 1 ;
2012-04-26 19:21:24 +04:00
msg - > header . prog = 0x11223344 ;
msg - > header . vers = 0x01 ;
msg - > header . proc = 0x666 ;
msg - > header . type = VIR_NET_CALL ;
msg - > header . serial = 0x99 ;
msg - > header . status = VIR_NET_OK ;
if ( virNetMessageEncodeHeader ( msg ) < 0 )
goto cleanup ;
2010-12-06 20:03:22 +03:00
2012-04-26 19:21:24 +04:00
if ( ARRAY_CARDINALITY ( expect ) ! = msg - > bufferOffset ) {
2010-12-06 20:03:22 +03:00
VIR_DEBUG ( " Expect message offset %zu got %zu " ,
2012-04-26 19:21:24 +04:00
sizeof ( expect ) , msg - > bufferOffset ) ;
goto cleanup ;
2010-12-06 20:03:22 +03:00
}
2012-04-26 19:21:24 +04:00
if ( msg - > bufferLength ! = msg_buf_size ) {
2012-06-08 23:24:13 +04:00
VIR_DEBUG ( " Expect message offset %lu got %zu " ,
2012-04-26 19:21:24 +04:00
msg_buf_size , msg - > bufferLength ) ;
goto cleanup ;
2010-12-06 20:03:22 +03:00
}
2012-04-26 19:21:24 +04:00
if ( memcmp ( expect , msg - > buffer , sizeof ( expect ) ) ! = 0 ) {
2016-05-26 18:02:04 +03:00
virTestDifferenceBin ( stderr , expect , msg - > buffer , sizeof ( expect ) ) ;
2012-04-26 19:21:24 +04:00
goto cleanup ;
2010-12-06 20:03:22 +03:00
}
2012-04-26 19:21:24 +04:00
ret = 0 ;
2014-03-25 10:53:44 +04:00
cleanup :
2012-04-26 19:21:24 +04:00
virNetMessageFree ( msg ) ;
return ret ;
2010-12-06 20:03:22 +03:00
}
static int testMessageHeaderDecode ( const void * args ATTRIBUTE_UNUSED )
{
2012-04-26 19:21:24 +04:00
virNetMessagePtr msg = virNetMessageNew ( true ) ;
static char input_buf [ ] = {
0x00 , 0x00 , 0x00 , 0x1c , /* Length */
0x11 , 0x22 , 0x33 , 0x44 , /* Program */
0x00 , 0x00 , 0x00 , 0x01 , /* Version */
0x00 , 0x00 , 0x06 , 0x66 , /* Procedure */
0x00 , 0x00 , 0x00 , 0x01 , /* Type */
0x00 , 0x00 , 0x00 , 0x99 , /* Serial */
0x00 , 0x00 , 0x00 , 0x01 , /* Status */
2010-12-06 20:03:22 +03:00
} ;
2012-04-26 19:21:24 +04:00
int ret = - 1 ;
2013-07-04 14:20:21 +04:00
if ( ! msg )
2012-04-26 19:21:24 +04:00
return - 1 ;
msg - > bufferLength = 4 ;
2013-07-04 14:20:21 +04:00
if ( VIR_ALLOC_N ( msg - > buffer , msg - > bufferLength ) < 0 )
2012-04-26 19:21:24 +04:00
goto cleanup ;
memcpy ( msg - > buffer , input_buf , msg - > bufferLength ) ;
2010-12-06 20:03:22 +03:00
2012-04-26 19:21:24 +04:00
msg - > header . prog = 0x11223344 ;
msg - > header . vers = 0x01 ;
msg - > header . proc = 0x666 ;
msg - > header . type = VIR_NET_CALL ;
msg - > header . serial = 0x99 ;
msg - > header . status = VIR_NET_OK ;
2010-12-06 20:03:22 +03:00
2012-04-26 19:21:24 +04:00
if ( virNetMessageDecodeLength ( msg ) < 0 ) {
2010-12-06 20:03:22 +03:00
VIR_DEBUG ( " Failed to decode message header " ) ;
2012-04-26 19:21:24 +04:00
goto cleanup ;
2010-12-06 20:03:22 +03:00
}
2012-04-26 19:21:24 +04:00
if ( msg - > bufferOffset ! = 0x4 ) {
2010-12-06 20:03:22 +03:00
VIR_DEBUG ( " Expecting offset %zu got %zu " ,
2012-04-26 19:21:24 +04:00
( size_t ) 4 , msg - > bufferOffset ) ;
goto cleanup ;
2010-12-06 20:03:22 +03:00
}
2012-04-26 19:21:24 +04:00
if ( msg - > bufferLength ! = 0x1c ) {
2010-12-06 20:03:22 +03:00
VIR_DEBUG ( " Expecting length %zu got %zu " ,
2012-04-26 19:21:24 +04:00
( size_t ) 0x1c , msg - > bufferLength ) ;
goto cleanup ;
2010-12-06 20:03:22 +03:00
}
2012-04-26 19:21:24 +04:00
memcpy ( msg - > buffer , input_buf , msg - > bufferLength ) ;
if ( virNetMessageDecodeHeader ( msg ) < 0 ) {
2010-12-06 20:03:22 +03:00
VIR_DEBUG ( " Failed to decode message header " ) ;
2012-04-26 19:21:24 +04:00
goto cleanup ;
2010-12-06 20:03:22 +03:00
}
2012-04-26 19:21:24 +04:00
if ( msg - > bufferOffset ! = msg - > bufferLength ) {
2010-12-06 20:03:22 +03:00
VIR_DEBUG ( " Expect message offset %zu got %zu " ,
2012-04-26 19:21:24 +04:00
msg - > bufferOffset , msg - > bufferLength ) ;
goto cleanup ;
2010-12-06 20:03:22 +03:00
}
2012-04-26 19:21:24 +04:00
if ( msg - > header . prog ! = 0x11223344 ) {
2010-12-06 20:03:22 +03:00
VIR_DEBUG ( " Expect prog %d got %d " ,
2012-04-26 19:21:24 +04:00
0x11223344 , msg - > header . prog ) ;
goto cleanup ;
2010-12-06 20:03:22 +03:00
}
2012-04-26 19:21:24 +04:00
if ( msg - > header . vers ! = 0x1 ) {
2010-12-06 20:03:22 +03:00
VIR_DEBUG ( " Expect vers %d got %d " ,
2012-04-26 19:21:24 +04:00
0x11223344 , msg - > header . vers ) ;
goto cleanup ;
2010-12-06 20:03:22 +03:00
}
2012-04-26 19:21:24 +04:00
if ( msg - > header . proc ! = 0x666 ) {
2010-12-06 20:03:22 +03:00
VIR_DEBUG ( " Expect proc %d got %d " ,
2012-04-26 19:21:24 +04:00
0x666 , msg - > header . proc ) ;
goto cleanup ;
2010-12-06 20:03:22 +03:00
}
2012-04-26 19:21:24 +04:00
if ( msg - > header . type ! = VIR_NET_REPLY ) {
2010-12-06 20:03:22 +03:00
VIR_DEBUG ( " Expect type %d got %d " ,
2012-04-26 19:21:24 +04:00
VIR_NET_REPLY , msg - > header . type ) ;
goto cleanup ;
2010-12-06 20:03:22 +03:00
}
2012-04-26 19:21:24 +04:00
if ( msg - > header . serial ! = 0x99 ) {
2010-12-06 20:03:22 +03:00
VIR_DEBUG ( " Expect serial %d got %d " ,
2012-04-26 19:21:24 +04:00
0x99 , msg - > header . serial ) ;
goto cleanup ;
2010-12-06 20:03:22 +03:00
}
2012-04-26 19:21:24 +04:00
if ( msg - > header . status ! = VIR_NET_ERROR ) {
2010-12-06 20:03:22 +03:00
VIR_DEBUG ( " Expect status %d got %d " ,
2012-04-26 19:21:24 +04:00
VIR_NET_ERROR , msg - > header . status ) ;
goto cleanup ;
2010-12-06 20:03:22 +03:00
}
2012-04-26 19:21:24 +04:00
ret = 0 ;
2014-03-25 10:53:44 +04:00
cleanup :
2012-04-26 19:21:24 +04:00
virNetMessageFree ( msg ) ;
return ret ;
2010-12-06 20:03:22 +03:00
}
static int testMessagePayloadEncode ( const void * args ATTRIBUTE_UNUSED )
{
virNetMessageError err ;
2012-04-26 19:21:24 +04:00
virNetMessagePtr msg = virNetMessageNew ( true ) ;
2011-06-29 06:47:54 +04:00
int ret = - 1 ;
2010-12-06 20:03:22 +03:00
static const char expect [ ] = {
0x00 , 0x00 , 0x00 , 0x74 , /* Length */
0x11 , 0x22 , 0x33 , 0x44 , /* Program */
0x00 , 0x00 , 0x00 , 0x01 , /* Version */
0x00 , 0x00 , 0x06 , 0x66 , /* Procedure */
0x00 , 0x00 , 0x00 , 0x02 , /* Type */
0x00 , 0x00 , 0x00 , 0x99 , /* Serial */
0x00 , 0x00 , 0x00 , 0x01 , /* Status */
0x00 , 0x00 , 0x00 , 0x01 , /* Error code */
0x00 , 0x00 , 0x00 , 0x07 , /* Error domain */
0x00 , 0x00 , 0x00 , 0x01 , /* Error message pointer */
0x00 , 0x00 , 0x00 , 0x0b , /* Error message length */
' H ' , ' e ' , ' l ' , ' l ' , /* Error message string */
' o ' , ' ' , ' W ' , ' o ' ,
' r ' , ' l ' , ' d ' , ' \0 ' ,
0x00 , 0x00 , 0x00 , 0x02 , /* Error level */
0x00 , 0x00 , 0x00 , 0x00 , /* Error domain pointer */
0x00 , 0x00 , 0x00 , 0x01 , /* Error str1 pointer */
0x00 , 0x00 , 0x00 , 0x03 , /* Error str1 length */
' O ' , ' n ' , ' e ' , ' \0 ' , /* Error str1 message */
0x00 , 0x00 , 0x00 , 0x01 , /* Error str2 pointer */
0x00 , 0x00 , 0x00 , 0x03 , /* Error str2 length */
' T ' , ' w ' , ' o ' , ' \0 ' , /* Error str2 message */
0x00 , 0x00 , 0x00 , 0x01 , /* Error str3 pointer */
0x00 , 0x00 , 0x00 , 0x05 , /* Error str3 length */
' T ' , ' h ' , ' r ' , ' e ' , /* Error str3 message */
' e ' , ' \0 ' , ' \0 ' , ' \0 ' ,
0x00 , 0x00 , 0x00 , 0x01 , /* Error int1 */
0x00 , 0x00 , 0x00 , 0x02 , /* Error int2 */
0x00 , 0x00 , 0x00 , 0x00 , /* Error network pointer */
} ;
2012-04-26 19:21:24 +04:00
2013-07-04 14:20:21 +04:00
if ( ! msg )
2012-04-26 19:21:24 +04:00
return - 1 ;
2010-12-06 20:03:22 +03:00
memset ( & err , 0 , sizeof ( err ) ) ;
err . code = VIR_ERR_INTERNAL_ERROR ;
err . domain = VIR_FROM_RPC ;
2011-06-29 06:47:54 +04:00
err . level = VIR_ERR_ERROR ;
2013-05-03 16:52:21 +04:00
if ( VIR_ALLOC ( err . message ) < 0 | |
VIR_STRDUP ( * err . message , " Hello World " ) < 0 | |
VIR_ALLOC ( err . str1 ) < 0 | |
VIR_STRDUP ( * err . str1 , " One " ) < 0 | |
VIR_ALLOC ( err . str2 ) < 0 | |
VIR_STRDUP ( * err . str2 , " Two " ) < 0 | |
VIR_ALLOC ( err . str3 ) < 0 | |
VIR_STRDUP ( * err . str3 , " Three " ) < 0 )
2011-06-29 06:47:54 +04:00
goto cleanup ;
2010-12-06 20:03:22 +03:00
err . int1 = 1 ;
err . int2 = 2 ;
2012-04-26 19:21:24 +04:00
msg - > header . prog = 0x11223344 ;
msg - > header . vers = 0x01 ;
msg - > header . proc = 0x666 ;
msg - > header . type = VIR_NET_MESSAGE ;
msg - > header . serial = 0x99 ;
msg - > header . status = VIR_NET_ERROR ;
2010-12-06 20:03:22 +03:00
2012-04-26 19:21:24 +04:00
if ( virNetMessageEncodeHeader ( msg ) < 0 )
2011-06-29 06:47:54 +04:00
goto cleanup ;
2010-12-06 20:03:22 +03:00
2012-04-26 19:21:24 +04:00
if ( virNetMessageEncodePayload ( msg , ( xdrproc_t ) xdr_virNetMessageError , & err ) < 0 )
2011-06-29 06:47:54 +04:00
goto cleanup ;
2010-12-06 20:03:22 +03:00
2012-04-26 19:21:24 +04:00
if ( ARRAY_CARDINALITY ( expect ) ! = msg - > bufferLength ) {
2010-12-06 20:03:22 +03:00
VIR_DEBUG ( " Expect message length %zu got %zu " ,
2012-04-26 19:21:24 +04:00
sizeof ( expect ) , msg - > bufferLength ) ;
2011-06-29 06:47:54 +04:00
goto cleanup ;
2010-12-06 20:03:22 +03:00
}
2012-04-26 19:21:24 +04:00
if ( msg - > bufferOffset ! = 0 ) {
2010-12-06 20:03:22 +03:00
VIR_DEBUG ( " Expect message offset 0 got %zu " ,
2012-04-26 19:21:24 +04:00
msg - > bufferOffset ) ;
2011-06-29 06:47:54 +04:00
goto cleanup ;
2010-12-06 20:03:22 +03:00
}
2012-04-26 19:21:24 +04:00
if ( memcmp ( expect , msg - > buffer , sizeof ( expect ) ) ! = 0 ) {
2016-05-26 18:02:04 +03:00
virTestDifferenceBin ( stderr , expect , msg - > buffer , sizeof ( expect ) ) ;
2011-06-29 06:47:54 +04:00
goto cleanup ;
2010-12-06 20:03:22 +03:00
}
2011-06-29 06:47:54 +04:00
ret = 0 ;
2014-03-25 10:53:44 +04:00
cleanup :
2011-06-29 06:47:54 +04:00
if ( err . message )
VIR_FREE ( * err . message ) ;
if ( err . str1 )
VIR_FREE ( * err . str1 ) ;
if ( err . str2 )
VIR_FREE ( * err . str2 ) ;
if ( err . str3 )
VIR_FREE ( * err . str3 ) ;
VIR_FREE ( err . message ) ;
VIR_FREE ( err . str1 ) ;
VIR_FREE ( err . str2 ) ;
VIR_FREE ( err . str3 ) ;
2012-04-26 19:21:24 +04:00
virNetMessageFree ( msg ) ;
2011-06-29 06:47:54 +04:00
return ret ;
2010-12-06 20:03:22 +03:00
}
static int testMessagePayloadDecode ( const void * args ATTRIBUTE_UNUSED )
{
virNetMessageError err ;
2012-04-26 19:21:24 +04:00
virNetMessagePtr msg = virNetMessageNew ( true ) ;
static char input_buffer [ ] = {
0x00 , 0x00 , 0x00 , 0x74 , /* Length */
0x11 , 0x22 , 0x33 , 0x44 , /* Program */
0x00 , 0x00 , 0x00 , 0x01 , /* Version */
0x00 , 0x00 , 0x06 , 0x66 , /* Procedure */
0x00 , 0x00 , 0x00 , 0x02 , /* Type */
0x00 , 0x00 , 0x00 , 0x99 , /* Serial */
0x00 , 0x00 , 0x00 , 0x01 , /* Status */
0x00 , 0x00 , 0x00 , 0x01 , /* Error code */
0x00 , 0x00 , 0x00 , 0x07 , /* Error domain */
0x00 , 0x00 , 0x00 , 0x01 , /* Error message pointer */
0x00 , 0x00 , 0x00 , 0x0b , /* Error message length */
' H ' , ' e ' , ' l ' , ' l ' , /* Error message string */
' o ' , ' ' , ' W ' , ' o ' ,
' r ' , ' l ' , ' d ' , ' \0 ' ,
0x00 , 0x00 , 0x00 , 0x02 , /* Error level */
0x00 , 0x00 , 0x00 , 0x00 , /* Error domain pointer */
0x00 , 0x00 , 0x00 , 0x01 , /* Error str1 pointer */
0x00 , 0x00 , 0x00 , 0x03 , /* Error str1 length */
' O ' , ' n ' , ' e ' , ' \0 ' , /* Error str1 message */
0x00 , 0x00 , 0x00 , 0x01 , /* Error str2 pointer */
0x00 , 0x00 , 0x00 , 0x03 , /* Error str2 length */
' T ' , ' w ' , ' o ' , ' \0 ' , /* Error str2 message */
0x00 , 0x00 , 0x00 , 0x01 , /* Error str3 pointer */
0x00 , 0x00 , 0x00 , 0x05 , /* Error str3 length */
' T ' , ' h ' , ' r ' , ' e ' , /* Error str3 message */
' e ' , ' \0 ' , ' \0 ' , ' \0 ' ,
0x00 , 0x00 , 0x00 , 0x01 , /* Error int1 */
0x00 , 0x00 , 0x00 , 0x02 , /* Error int2 */
0x00 , 0x00 , 0x00 , 0x00 , /* Error network pointer */
2010-12-06 20:03:22 +03:00
} ;
2012-04-26 19:21:24 +04:00
int ret = - 1 ;
2013-09-25 18:37:10 +04:00
memset ( & err , 0 , sizeof ( err ) ) ;
2013-09-25 18:36:39 +04:00
if ( ! msg )
return - 1 ;
2012-04-26 19:21:24 +04:00
msg - > bufferLength = 4 ;
2013-07-04 14:20:21 +04:00
if ( VIR_ALLOC_N ( msg - > buffer , msg - > bufferLength ) < 0 )
2012-04-26 19:21:24 +04:00
goto cleanup ;
memcpy ( msg - > buffer , input_buffer , msg - > bufferLength ) ;
2010-12-06 20:03:22 +03:00
2012-04-26 19:21:24 +04:00
if ( virNetMessageDecodeLength ( msg ) < 0 ) {
2010-12-06 20:03:22 +03:00
VIR_DEBUG ( " Failed to decode message header " ) ;
2012-04-26 19:21:24 +04:00
goto cleanup ;
2010-12-06 20:03:22 +03:00
}
2012-04-26 19:21:24 +04:00
if ( msg - > bufferOffset ! = 0x4 ) {
2010-12-06 20:03:22 +03:00
VIR_DEBUG ( " Expecting offset %zu got %zu " ,
2012-04-26 19:21:24 +04:00
( size_t ) 4 , msg - > bufferOffset ) ;
goto cleanup ;
2010-12-06 20:03:22 +03:00
}
2012-04-26 19:21:24 +04:00
if ( msg - > bufferLength ! = 0x74 ) {
2010-12-06 20:03:22 +03:00
VIR_DEBUG ( " Expecting length %zu got %zu " ,
2012-04-26 19:21:24 +04:00
( size_t ) 0x74 , msg - > bufferLength ) ;
goto cleanup ;
2010-12-06 20:03:22 +03:00
}
2012-04-26 19:21:24 +04:00
memcpy ( msg - > buffer , input_buffer , msg - > bufferLength ) ;
if ( virNetMessageDecodeHeader ( msg ) < 0 ) {
2010-12-06 20:03:22 +03:00
VIR_DEBUG ( " Failed to decode message header " ) ;
2012-04-26 19:21:24 +04:00
goto cleanup ;
2010-12-06 20:03:22 +03:00
}
2012-04-26 19:21:24 +04:00
if ( msg - > bufferOffset ! = 28 ) {
2010-12-06 20:03:22 +03:00
VIR_DEBUG ( " Expect message offset %zu got %zu " ,
2012-04-26 19:21:24 +04:00
msg - > bufferOffset , ( size_t ) 28 ) ;
goto cleanup ;
2010-12-06 20:03:22 +03:00
}
2012-04-26 19:21:24 +04:00
if ( msg - > bufferLength ! = 0x74 ) {
2010-12-06 20:03:22 +03:00
VIR_DEBUG ( " Expecting length %zu got %zu " ,
2012-04-26 19:21:24 +04:00
( size_t ) 0x1c , msg - > bufferLength ) ;
goto cleanup ;
2010-12-06 20:03:22 +03:00
}
2012-04-26 19:21:24 +04:00
if ( virNetMessageDecodePayload ( msg , ( xdrproc_t ) xdr_virNetMessageError , & err ) < 0 ) {
2010-12-06 20:03:22 +03:00
VIR_DEBUG ( " Failed to decode message payload " ) ;
2012-04-26 19:21:24 +04:00
goto cleanup ;
2010-12-06 20:03:22 +03:00
}
if ( err . code ! = VIR_ERR_INTERNAL_ERROR ) {
VIR_DEBUG ( " Expect code %d got %d " ,
VIR_ERR_INTERNAL_ERROR , err . code ) ;
2012-04-26 19:21:24 +04:00
goto cleanup ;
2010-12-06 20:03:22 +03:00
}
if ( err . domain ! = VIR_FROM_RPC ) {
VIR_DEBUG ( " Expect domain %d got %d " ,
VIR_ERR_RPC , err . domain ) ;
2012-04-26 19:21:24 +04:00
goto cleanup ;
2010-12-06 20:03:22 +03:00
}
if ( err . message = = NULL | |
STRNEQ ( * err . message , " Hello World " ) ) {
VIR_DEBUG ( " Expect str1 'Hello World' got %s " ,
err . message ? * err . message : " (null) " ) ;
2012-04-26 19:21:24 +04:00
goto cleanup ;
2010-12-06 20:03:22 +03:00
}
if ( err . dom ! = NULL ) {
VIR_DEBUG ( " Expect NULL dom " ) ;
2012-04-26 19:21:24 +04:00
goto cleanup ;
2010-12-06 20:03:22 +03:00
}
if ( err . level ! = VIR_ERR_ERROR ) {
VIR_DEBUG ( " Expect leve %d got %d " ,
VIR_ERR_ERROR , err . level ) ;
2012-04-26 19:21:24 +04:00
goto cleanup ;
2010-12-06 20:03:22 +03:00
}
if ( err . str1 = = NULL | |
STRNEQ ( * err . str1 , " One " ) ) {
VIR_DEBUG ( " Expect str1 'One' got %s " ,
err . str1 ? * err . str1 : " (null) " ) ;
2012-04-26 19:21:24 +04:00
goto cleanup ;
2010-12-06 20:03:22 +03:00
}
if ( err . str2 = = NULL | |
STRNEQ ( * err . str2 , " Two " ) ) {
VIR_DEBUG ( " Expect str3 'Two' got %s " ,
err . str2 ? * err . str2 : " (null) " ) ;
2012-04-26 19:21:24 +04:00
goto cleanup ;
2010-12-06 20:03:22 +03:00
}
if ( err . str3 = = NULL | |
STRNEQ ( * err . str3 , " Three " ) ) {
VIR_DEBUG ( " Expect str3 'Three' got %s " ,
err . str3 ? * err . str3 : " (null) " ) ;
2012-04-26 19:21:24 +04:00
goto cleanup ;
2010-12-06 20:03:22 +03:00
}
if ( err . int1 ! = 1 ) {
VIR_DEBUG ( " Expect int1 1 got %d " ,
err . int1 ) ;
2012-04-26 19:21:24 +04:00
goto cleanup ;
2010-12-06 20:03:22 +03:00
}
if ( err . int2 ! = 2 ) {
VIR_DEBUG ( " Expect int2 2 got %d " ,
err . int2 ) ;
2012-04-26 19:21:24 +04:00
goto cleanup ;
2010-12-06 20:03:22 +03:00
}
if ( err . net ! = NULL ) {
VIR_DEBUG ( " Expect NULL network " ) ;
2012-04-26 19:21:24 +04:00
goto cleanup ;
2010-12-06 20:03:22 +03:00
}
2012-04-26 19:21:24 +04:00
ret = 0 ;
2014-03-25 10:53:44 +04:00
cleanup :
2010-12-06 20:03:22 +03:00
xdr_free ( ( xdrproc_t ) xdr_virNetMessageError , ( void * ) & err ) ;
2012-04-26 19:21:24 +04:00
virNetMessageFree ( msg ) ;
return ret ;
2010-12-06 20:03:22 +03:00
}
static int testMessagePayloadStreamEncode ( const void * args ATTRIBUTE_UNUSED )
{
char stream [ ] = " The quick brown fox jumps over the lazy dog " ;
2012-04-26 19:21:24 +04:00
virNetMessagePtr msg = virNetMessageNew ( true ) ;
2010-12-06 20:03:22 +03:00
static const char expect [ ] = {
0x00 , 0x00 , 0x00 , 0x47 , /* Length */
0x11 , 0x22 , 0x33 , 0x44 , /* Program */
0x00 , 0x00 , 0x00 , 0x01 , /* Version */
0x00 , 0x00 , 0x06 , 0x66 , /* Procedure */
0x00 , 0x00 , 0x00 , 0x03 , /* Type */
0x00 , 0x00 , 0x00 , 0x99 , /* Serial */
0x00 , 0x00 , 0x00 , 0x02 , /* Status */
' T ' , ' h ' , ' e ' , ' ' ,
' q ' , ' u ' , ' i ' , ' c ' ,
' k ' , ' ' , ' b ' , ' r ' ,
' o ' , ' w ' , ' n ' , ' ' ,
' f ' , ' o ' , ' x ' , ' ' ,
' j ' , ' u ' , ' m ' , ' p ' ,
' s ' , ' ' , ' o ' , ' v ' ,
' e ' , ' r ' , ' ' , ' t ' ,
' h ' , ' e ' , ' ' , ' l ' ,
' a ' , ' z ' , ' y ' , ' ' ,
' d ' , ' o ' , ' g ' ,
} ;
2012-04-26 19:21:24 +04:00
int ret = - 1 ;
2010-12-06 20:03:22 +03:00
2013-09-25 18:36:39 +04:00
if ( ! msg )
return - 1 ;
2012-04-26 19:21:24 +04:00
msg - > header . prog = 0x11223344 ;
msg - > header . vers = 0x01 ;
msg - > header . proc = 0x666 ;
msg - > header . type = VIR_NET_STREAM ;
msg - > header . serial = 0x99 ;
msg - > header . status = VIR_NET_CONTINUE ;
2010-12-06 20:03:22 +03:00
2012-04-26 19:21:24 +04:00
if ( virNetMessageEncodeHeader ( msg ) < 0 )
goto cleanup ;
2010-12-06 20:03:22 +03:00
2012-04-26 19:21:24 +04:00
if ( virNetMessageEncodePayloadRaw ( msg , stream , strlen ( stream ) ) < 0 )
goto cleanup ;
2010-12-06 20:03:22 +03:00
2012-04-26 19:21:24 +04:00
if ( ARRAY_CARDINALITY ( expect ) ! = msg - > bufferLength ) {
2010-12-06 20:03:22 +03:00
VIR_DEBUG ( " Expect message length %zu got %zu " ,
2012-04-26 19:21:24 +04:00
sizeof ( expect ) , msg - > bufferLength ) ;
goto cleanup ;
2010-12-06 20:03:22 +03:00
}
2012-04-26 19:21:24 +04:00
if ( msg - > bufferOffset ! = 0 ) {
2010-12-06 20:03:22 +03:00
VIR_DEBUG ( " Expect message offset 0 got %zu " ,
2012-04-26 19:21:24 +04:00
msg - > bufferOffset ) ;
goto cleanup ;
2010-12-06 20:03:22 +03:00
}
2012-04-26 19:21:24 +04:00
if ( memcmp ( expect , msg - > buffer , sizeof ( expect ) ) ! = 0 ) {
2016-05-26 18:02:04 +03:00
virTestDifferenceBin ( stderr , expect , msg - > buffer , sizeof ( expect ) ) ;
2012-04-26 19:21:24 +04:00
goto cleanup ;
2010-12-06 20:03:22 +03:00
}
2012-04-26 19:21:24 +04:00
ret = 0 ;
2014-03-25 10:53:44 +04:00
cleanup :
2012-04-26 19:21:24 +04:00
virNetMessageFree ( msg ) ;
return ret ;
2010-12-06 20:03:22 +03:00
}
static int
mymain ( void )
{
int ret = 0 ;
signal ( SIGPIPE , SIG_IGN ) ;
2016-05-26 18:01:50 +03:00
if ( virTestRun ( " Message Header Encode " , testMessageHeaderEncode , NULL ) < 0 )
2010-12-06 20:03:22 +03:00
ret = - 1 ;
2016-05-26 18:01:50 +03:00
if ( virTestRun ( " Message Header Decode " , testMessageHeaderDecode , NULL ) < 0 )
2010-12-06 20:03:22 +03:00
ret = - 1 ;
2016-05-26 18:01:50 +03:00
if ( virTestRun ( " Message Payload Encode " , testMessagePayloadEncode , NULL ) < 0 )
2010-12-06 20:03:22 +03:00
ret = - 1 ;
2016-05-26 18:01:50 +03:00
if ( virTestRun ( " Message Payload Decode " , testMessagePayloadDecode , NULL ) < 0 )
2010-12-06 20:03:22 +03:00
ret = - 1 ;
2016-05-26 18:01:50 +03:00
if ( virTestRun ( " Message Payload Stream Encode " , testMessagePayloadStreamEncode , NULL ) < 0 )
2010-12-06 20:03:22 +03:00
ret = - 1 ;
2014-03-17 13:38:38 +04:00
return ret = = 0 ? EXIT_SUCCESS : EXIT_FAILURE ;
2010-12-06 20:03:22 +03:00
}
2017-03-29 17:45:42 +03:00
VIR_TEST_MAIN ( mymain )