305f75f9c002365a933240914285050aa4994502
[kai/samba.git] / source4 / torture / local / ndr.c
1 /* 
2    Unix SMB/CIFS implementation.
3    test suite for basic ndr functions
4
5    Copyright (C) Jelmer Vernooij 2005
6    
7    This program is free software; you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published by
9    the Free Software Foundation; either version 2 of the License, or
10    (at your option) any later version.
11    
12    This program is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16    
17    You should have received a copy of the GNU General Public License
18    along with this program; if not, write to the Free Software
19    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 */
21
22 #include "includes.h"
23 #include "torture/torture.h"
24 #include "librpc/ndr/libndr.h"
25
26 BOOL test_check_string_terminator(TALLOC_CTX *mem_ctx)
27 {
28         struct ndr_pull *ndr;
29         DATA_BLOB blob;
30
31         /* Simple test */
32         blob = strhex_to_data_blob("0000");
33         
34         ndr = ndr_pull_init_blob(&blob, mem_ctx);
35
36         if (NT_STATUS_IS_ERR(ndr_check_string_terminator(ndr, 1, 2))) {
37                 DEBUG(0, ("simple check_string_terminator test failed\n"));
38                 return False;
39         }
40
41         if (ndr->offset != 0) {
42                 DEBUG(0, ("check_string_terminator did not reset offset\n"));
43                 return False;
44         }
45
46         if (NT_STATUS_IS_OK(ndr_check_string_terminator(ndr, 1, 3))) {
47                 DEBUG(0, ("check_string_terminator checked beyond string boundaries\n"));
48                 return False;
49         }
50
51         if (ndr->offset != 0) {
52                 DEBUG(0, ("check_string_terminator did not reset offset\n"));
53                 return False;
54         }
55
56         talloc_free(ndr);
57
58         blob = strhex_to_data_blob("11220000");
59         ndr = ndr_pull_init_blob(&blob, mem_ctx);
60
61         if (NT_STATUS_IS_ERR(ndr_check_string_terminator(ndr, 4, 1))) {
62                 DEBUG(0, ("check_string_terminator failed to recognize terminator\n"));
63                 return False;
64         }
65
66         if (NT_STATUS_IS_ERR(ndr_check_string_terminator(ndr, 3, 1))) {
67                 DEBUG(0, ("check_string_terminator failed to recognize terminator\n"));
68                 return False;
69         }
70
71         if (NT_STATUS_IS_OK(ndr_check_string_terminator(ndr, 2, 1))) {
72                 DEBUG(0, ("check_string_terminator erroneously reported terminator\n"));
73                 return False;
74         }
75
76         if (ndr->offset != 0) {
77                 DEBUG(0, ("check_string_terminator did not reset offset\n"));
78                 return False;
79         }
80
81         talloc_free(ndr);
82
83         return True;
84 }
85
86 BOOL torture_local_ndr(void)
87 {
88         TALLOC_CTX *mem_ctx;
89         BOOL ret = True;
90
91         mem_ctx = talloc_init("torture_local_ndr");
92
93         ret &= test_check_string_terminator(mem_ctx);
94
95         talloc_free(mem_ctx);
96
97         return ret;
98 }