2005-12-25 06:04:13 +03:00
# Some simple utility functions for pidl tests
2006-03-24 04:03:02 +03:00
# Copyright (C) 2005-2006 Jelmer Vernooij
2005-12-25 06:04:13 +03:00
# Published under the GNU General Public License
package Util ;
require Exporter ;
@ ISA = qw( Exporter ) ;
2007-01-05 17:55:26 +03:00
@ EXPORT = qw( test_samba4_ndr test_warnings test_errors ) ;
2005-12-25 06:04:13 +03:00
use strict ;
2007-01-05 17:55:26 +03:00
use FindBin qw( $RealBin ) ;
use lib "$RealBin/../lib" ;
2009-06-19 18:17:08 +04:00
use Parse::Pidl::Samba4 qw( is_intree ) ;
2007-01-05 17:55:26 +03:00
use Parse::Pidl ;
my $ warnings = "" ;
2007-01-05 18:03:21 +03:00
undef & Parse::Pidl:: warning ;
* Parse::Pidl:: warning = sub {
2007-01-05 17:55:26 +03:00
my ( $ e , $ l ) = @ _ ;
2007-01-09 09:02:41 +03:00
if ( defined ( $ e ) ) {
$ warnings . = "$e->{FILE}:$e->{LINE}: $l\n" ;
} else {
$ warnings . = "$l\n" ;
}
2007-01-05 17:55:26 +03:00
} ;
my $ errors = "" ;
2007-01-05 18:03:21 +03:00
undef & Parse::Pidl:: error ;
* Parse::Pidl:: error = sub {
2007-01-05 17:55:26 +03:00
my ( $ e , $ l ) = @ _ ;
2007-01-09 09:02:41 +03:00
if ( defined ( $ e ) ) {
$ errors . = "$e->{FILE}:$e->{LINE}: $l\n" ;
} else {
$ errors . = "$l\n" ;
}
2007-01-05 17:55:26 +03:00
} ;
2005-12-25 06:04:13 +03:00
use Test::More ;
use Parse::Pidl::IDL ;
use Parse::Pidl::NDR ;
use Parse::Pidl::Samba4::NDR::Parser ;
use Parse::Pidl::Samba4::Header ;
# Generate a Samba4 parser for an IDL fragment and run it with a specified
# piece of code to check whether the parser works as expected
2006-03-24 04:03:02 +03:00
sub test_samba4_ndr
2005-12-25 06:04:13 +03:00
{
2006-03-24 04:03:02 +03:00
my ( $ name , $ idl , $ c , $ extra ) = @ _ ;
2007-10-31 18:29:32 +03:00
$ extra = "" unless defined ( $ extra ) ;
2005-12-25 06:04:13 +03:00
my $ pidl = Parse::Pidl::IDL:: parse_string ( "interface echo { $idl }; " , "<$name>" ) ;
2006-01-08 23:20:18 +03:00
ok ( defined ( $ pidl ) , "($name) parse idl" ) ;
2007-08-16 17:41:48 +04:00
2005-12-25 06:04:13 +03:00
my $ pndr = Parse::Pidl::NDR:: Parse ( $ pidl ) ;
ok ( defined ( $ pndr ) , "($name) generate NDR tree" ) ;
2007-08-16 17:41:48 +04:00
my $ header = Parse::Pidl::Samba4::Header:: Parse ( $ pndr ) ;
ok ( defined ( $ header ) , "($name) generate generic header" ) ;
2007-04-22 19:59:34 +04:00
my $ generator = new Parse::Pidl::Samba4::NDR:: Parser ( ) ;
my ( $ ndrheader , $ ndrparser ) = $ generator - > Parse ( $ pndr , undef , undef ) ;
2005-12-25 06:04:13 +03:00
ok ( defined ( $ ndrparser ) , "($name) generate NDR parser" ) ;
ok ( defined ( $ ndrheader ) , "($name) generate NDR header" ) ;
SKIP: {
2009-06-19 18:17:08 +04:00
my $ flags ;
if ( system ( "pkg-config --exists ndr" ) == 0 and ! is_intree ( ) ) {
$ flags = `pkg-config --libs --cflags ndr` ;
} else {
skip "no samba environment available, skipping compilation" , 3 ;
}
2005-12-25 06:04:13 +03:00
2007-10-31 18:29:32 +03:00
my $ main = "
#include <stdint.h>
#include <stdlib.h>
#include <stdio.h>
#include <stdbool.h>
#include <stdarg.h>
#include <util/data_blob.h>
/* header start */
$ header
/* header end */
/* ndrheader start */
$ ndrheader
/* ndrheader end */
/* extra start */
$ extra
/* extra end */
/* ndrparser start */
$ ndrparser
/* ndrparser end */
/* main start */
int main ( int argc , const char ** argv )
{
TALLOC_CTX * mem_ctx = talloc_init ( NULL ) ;
$ c
talloc_free ( mem_ctx ) ;
2006-01-08 23:20:18 +03:00
2007-10-31 18:29:32 +03:00
return 0 ;
}
/* main end */
\ n " ;
my $ main_debug = "# " . join ( "\n# " , split ( "\n" , $ main ) ) ;
my $ test_data_prefix = $ ENV { TEST_DATA_PREFIX } ;
2006-01-08 23:20:18 +03:00
my $ outfile ;
if ( defined ( $ test_data_prefix ) ) {
2014-11-21 18:52:32 +03:00
$ outfile = "$test_data_prefix/test-$name" ;
2006-01-08 23:20:18 +03:00
} else {
2006-12-21 05:48:46 +03:00
$ outfile = "./test-$name" ;
2006-01-08 23:20:18 +03:00
}
2005-12-25 06:04:13 +03:00
2007-01-23 13:08:08 +03:00
my $ cflags = $ ENV { CFLAGS } ;
unless ( defined ( $ cflags ) ) {
$ cflags = "" ;
}
my $ ldflags = $ ENV { LDFLAGS } ;
unless ( defined ( $ ldflags ) ) {
$ ldflags = "" ;
}
my $ cc = $ ENV { CC } ;
unless ( defined ( $ cc ) ) {
$ cc = "cc" ;
}
my $ cmd = "$cc $cflags -x c - -o $outfile $flags $ldflags" ;
$ cmd =~ s/\n//g ;
open CC , "|$cmd" ;
2007-10-31 18:29:32 +03:00
print CC $ main ;
2005-12-25 06:04:13 +03:00
close CC ;
ok ( - f $ outfile , "($name) compile" ) ;
2006-12-21 05:48:46 +03:00
my $ ret = system ( $ outfile , ( ) ) >> 8 ;
2007-10-31 18:29:32 +03:00
print "# code:\n#\n$main_debug\n" if ( $ ret != 0 ) ;
2007-05-22 16:45:58 +04:00
print "# cmd: $cmd\n" if ( $ ret != 0 ) ;
2006-03-24 00:47:16 +03:00
print "# return code: $ret\n" if ( $ ret != 0 ) ;
2005-12-25 06:04:13 +03:00
2006-03-24 00:47:16 +03:00
ok ( $ ret == 0 , "($name) run" ) ;
2005-12-25 06:04:13 +03:00
ok ( unlink ( $ outfile ) , "($name) remove" ) ;
}
}
2007-01-05 17:55:26 +03:00
sub test_warnings ($$)
{
my ( $ exp , $ code ) = @ _ ;
$ warnings = "" ;
$ code - > ( ) ;
is ( $ warnings , $ exp ) ;
}
sub test_errors ($$)
{
my ( $ exp , $ code ) = @ _ ;
$ errors = "" ;
$ code - > ( ) ;
is ( $ errors , $ exp ) ;
}
2005-12-25 06:04:13 +03:00
1 ;