r12775: free elements early...
[samba.git] / source4 / pidl / tests / Util.pm
1 # Some simple utility functions for pidl tests
2 # Copyright (C) 2005 Jelmer Vernooij
3 # Published under the GNU General Public License
4
5 package Util;
6
7 require Exporter;
8 @ISA = qw(Exporter);
9 @EXPORT_OK = qw(test_samba4_ndr);
10
11 use strict;
12
13 use Test::More;
14 use Parse::Pidl::IDL;
15 use Parse::Pidl::NDR;
16 use Parse::Pidl::Samba4::NDR::Parser;
17 use Parse::Pidl::Samba4::Header;
18
19 my $sanecc = 0;
20
21 # Generate a Samba4 parser for an IDL fragment and run it with a specified 
22 # piece of code to check whether the parser works as expected
23 sub test_samba4_ndr($$$)
24 {
25         my ($name,$idl,$c) = @_;
26         my $pidl = Parse::Pidl::IDL::parse_string("interface echo { $idl }; ", "<$name>");
27         
28         ok (defined($pidl), "($name) parse idl");
29         my $header = Parse::Pidl::Samba4::Header::Parse($pidl);
30         ok(defined($header), "($name) generate generic header");
31         my $pndr = Parse::Pidl::NDR::Parse($pidl);
32         ok(defined($pndr), "($name) generate NDR tree");
33         my ($ndrheader,$ndrparser) = Parse::Pidl::Samba4::NDR::Parser::Parse($pndr, "foo");
34         ok(defined($ndrparser), "($name) generate NDR parser");
35         ok(defined($ndrheader), "($name) generate NDR header");
36
37 SKIP: {
38
39         my $insamba = -f "include/includes.h";
40         my $link = $insamba && 0; # FIXME
41
42         skip "no samba environment available, skipping compilation", 3 
43                 if not $insamba;
44
45         skip "no sane C compiler, skipping compilation", 3
46                 if not $sanecc;
47
48         my $outfile = "test-$name";
49
50         #my $cflags = $ENV{CFLAGS};
51         my $cflags = "-Iinclude -Ilib -I.";
52
53         if ($insamba and $link) {
54                 open CC, "|cc -x c -o $outfile $cflags -";
55         } elsif ($insamba) {
56                         open CC, "|cc -x c -c -o $outfile $cflags -";
57         }
58         print CC "#include \"includes.h\"\n";
59         print CC $header;
60         print CC $ndrheader;
61         print CC $ndrparser;
62         print CC "int main(int argc, const char **argv)
63 {
64         TALLOC_CTX *mem_ctx = talloc_init(NULL);
65         
66         $c
67  
68         talloc_free(mem_ctx);
69         
70         return 0; }\n";
71         close CC;
72
73         ok(-f $outfile, "($name) compile");
74
75         unless ($link) {
76                 skip "no shared libraries of Samba available yet, can't run test", 2;
77                 unlink($outfile);
78         }
79
80         ok(system($outfile), "($name) run");
81
82         ok(unlink($outfile), "($name) remove");
83
84         }
85 }
86
87 my $outfile = "test"; # FIXME: Somewhat more unique name
88
89 # Test whether CC is sane. The real 'fix' here would be using the 
90 # Samba build system, but unfortunately, we have no way of hooking into that 
91 # yet so we're running CC directly for now
92 $sanecc = 1 if system('echo "main() {}"'." | cc -I. -x c -c - -o $outfile") == 0;
93
94 1;