r19339: Merge my 4.0-unittest branch. This adds an API for more fine-grained
[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 static bool test_check_string_terminator(struct torture_context *tctx)
27 {
28         struct ndr_pull *ndr;
29         DATA_BLOB blob;
30         TALLOC_CTX *mem_ctx = tctx;
31
32         /* Simple test */
33         blob = strhex_to_data_blob("0000");
34         
35         ndr = ndr_pull_init_blob(&blob, mem_ctx);
36
37         torture_assert_ntstatus_ok(tctx, ndr_check_string_terminator(ndr, 1, 2),
38                                                            "simple check_string_terminator test failed");
39
40         torture_assert(tctx, ndr->offset == 0,
41                 "check_string_terminator did not reset offset");
42
43         if (NT_STATUS_IS_OK(ndr_check_string_terminator(ndr, 1, 3))) {
44                 torture_fail(tctx, "check_string_terminator checked beyond string boundaries");
45         }
46
47         torture_assert(tctx, ndr->offset == 0, 
48                 "check_string_terminator did not reset offset");
49
50         talloc_free(ndr);
51
52         blob = strhex_to_data_blob("11220000");
53         ndr = ndr_pull_init_blob(&blob, mem_ctx);
54
55         torture_assert_ntstatus_ok(tctx, 
56                 ndr_check_string_terminator(ndr, 4, 1),
57                 "check_string_terminator failed to recognize terminator");
58
59         torture_assert_ntstatus_ok(tctx, 
60                 ndr_check_string_terminator(ndr, 3, 1),
61                 "check_string_terminator failed to recognize terminator");
62
63         if (NT_STATUS_IS_OK(ndr_check_string_terminator(ndr, 2, 1))) {
64                 torture_fail(tctx, 
65                                          "check_string_terminator erroneously reported terminator");
66         }
67
68         torture_assert(tctx, ndr->offset == 0,
69                 "check_string_terminator did not reset offset");
70         return true;
71 }
72
73 struct torture_suite *torture_local_ndr(TALLOC_CTX *mem_ctx)
74 {
75         struct torture_suite *suite = torture_suite_create(mem_ctx, "NDR");
76
77         torture_suite_add_simple_test(suite, "string terminator", 
78                                                                    test_check_string_terminator);
79
80         return suite;
81 }