e00dd01c8e26a63119828116f1fe453094855123
[bbaumbach/samba-autobuild/.git] / pidl / tests / ndr_string.pl
1 #!/usr/bin/perl
2 # String tests for pidl
3 # (C) 2005 Jelmer Vernooij <jelmer@samba.org>
4 # Published under the GNU General Public License
5 use strict;
6
7 use Test::More tests => 6 * 8;
8 use FindBin qw($RealBin);
9 use lib "$RealBin";
10 use Util qw(test_samba4_ndr);
11
12 test_samba4_ndr("string-pull-empty", 
13 ' [public] void TestString([in,flag(STR_ASCII|LIBNDR_FLAG_STR_SIZE4)] string data);',
14 '
15         uint8_t data[] = { 0x00, 0x00, 0x00, 0x00 };
16         DATA_BLOB b = { data, 4 };
17         struct ndr_pull *ndr = ndr_pull_init_blob(&b, NULL, 
18                 smb_iconv_convenience_reinit(NULL, "ASCII", "UTF8", true, NULL));
19         struct TestString r;
20         r.in.data = NULL;
21
22         if (!NDR_ERR_CODE_IS_SUCCESS(ndr_pull_TestString(ndr, NDR_IN, &r)))
23                 return 1;
24         
25         if (r.in.data == NULL)
26                 return 2;
27
28         if (r.in.data[0] != 0)
29                 return 3;
30 ');
31
32 test_samba4_ndr("string-ascii-pull", 
33 '
34         [public] void TestString([in,flag(STR_ASCII|LIBNDR_FLAG_STR_SIZE4)] string data);
35 ',
36 '
37         uint8_t data[] = { 0x03, 0x00, 0x00, 0x00, 
38                                            \'f\', \'o\', \'o\', 0 };
39         DATA_BLOB b = { data, 8 };
40         struct ndr_pull *ndr = ndr_pull_init_blob(&b, NULL,
41                 smb_iconv_convenience_reinit(NULL, "ASCII", "UTF8", true, NULL));
42         struct TestString r;
43         r.in.data = NULL;
44
45         if (!NDR_ERR_CODE_IS_SUCCESS(ndr_pull_TestString(ndr, NDR_IN, &r)))
46                 return 1;
47         
48         if (r.in.data == NULL)
49                 return 2;
50
51         if (strncmp(r.in.data, "foo", 3) != 0)
52                 return 3;
53
54         if (r.in.data[4] != 0)
55                 return 4;
56 ');
57
58 test_samba4_ndr("string-wchar-fixed-array-01",
59 '
60         typedef struct {
61                 uint32 l1;
62                 [string,charset(UTF16)] uint16 str[6];
63                 uint32 l2;
64         } TestStringStruct;
65
66         [public] void TestString([in,ref] TestStringStruct *str);
67 ',
68 '
69         uint8_t data[] = { 0x01,  0x00, 0x00,  0x00,
70                            0x00,  0x00, 0x00,  0x00,
71                            0x04,  0x00, 0x00,  0x00,
72                            \'f\', 0x00, \'o\', 0x00,
73                            \'o\', 0x00, 0x00,  0x00,
74                            0x02,  0x00, 0x00,  0x00
75         };
76         DATA_BLOB b = { data, sizeof(data) };
77         struct ndr_pull *ndr = ndr_pull_init_blob(&b, NULL,
78                 smb_iconv_convenience_reinit(NULL, "ASCII", "UTF8", true, NULL));
79         struct TestString r;
80         struct TestStringStruct str;
81         r.in.str = &str;
82
83         if (!NDR_ERR_CODE_IS_SUCCESS(ndr_pull_TestString(ndr, NDR_IN, &r)))
84                 return 1;
85
86         if (r.in.str == NULL)
87                 return 2;
88
89         if (r.in.str->l1 != 0x00000001)
90                 return 3;
91
92         if (strncmp(str.str, "foo", 3) != 0)
93                 return 4;
94
95         if (r.in.str->str[4] != 0)
96                 return 5;
97
98         if (r.in.str->l2 != 0x00000002)
99                 return 6;
100 ');
101
102 test_samba4_ndr("string-wchar-fixed-array-02",
103 '
104         typedef struct {
105                 uint32 l1;
106                 [string,charset(UTF16)] uint16 str[6];
107                 uint32 l2;
108         } TestStringStruct;
109
110         [public] void TestString([in,ref] TestStringStruct *str);
111 ',
112 '
113         uint8_t data[] = { 0x01,  0x00, 0x00,  0x00,
114                            0x00,  0x00, 0x00,  0x00,
115                            0x06,  0x00, 0x00,  0x00,
116                            \'f\', 0x00, \'o\', 0x00,
117                            \'o\', 0x00, \'b\', 0x00,
118                            \'a\', 0x00, \'r\', 0x00,
119                            0x00,  0x00, 0x00,  0x00,
120                            0x02,  0x00, 0x00,  0x00
121         };
122         DATA_BLOB b = { data, sizeof(data) };
123         struct ndr_pull *ndr = ndr_pull_init_blob(&b, NULL,
124                 smb_iconv_convenience_reinit(NULL, "ASCII", "UTF8", true, NULL));
125         struct TestString r;
126         struct TestStringStruct str;
127         r.in.str = &str;
128
129         /* the string terminator is wrong */
130         if (NDR_ERR_CODE_IS_SUCCESS(ndr_pull_TestString(ndr, NDR_IN, &r)))
131                 return 1;
132 ');
133
134 test_samba4_ndr("string-wchar-fixed-array-03",
135 '
136         typedef struct {
137                 uint32 l1;
138                 [string,charset(UTF16)] uint16 str[6];
139                 uint32 l2;
140         } TestStringStruct;
141
142         [public] void TestString([in,ref] TestStringStruct *str);
143 ',
144 '
145         uint8_t data[] = { 0x01,  0x00, 0x00,  0x00,
146                            0x00,  0x00, 0x00,  0x00,
147                            0x07,  0x00, 0x00,  0x00,
148                            \'f\', 0x00, \'o\', 0x00,
149                            \'o\', 0x00, \'b\', 0x00,
150                            \'a\', 0x00, \'r\', 0x00,
151                            0x00,  0x00, 0x00,  0x00,
152                            0x02,  0x00, 0x00,  0x00
153         };
154         DATA_BLOB b = { data, sizeof(data) };
155         struct ndr_pull *ndr = ndr_pull_init_blob(&b, NULL,
156                 smb_iconv_convenience_reinit(NULL, "ASCII", "UTF8", true, NULL));
157         struct TestString r;
158         struct TestStringStruct str;
159         r.in.str = &str;
160
161         /* the length 0x07 is to large */
162         if (NDR_ERR_CODE_IS_SUCCESS(ndr_pull_TestString(ndr, NDR_IN, &r)))
163                 return 1;
164 ');
165
166 SKIP: {
167         skip "doesn't seem to work yet", 8;
168
169 test_samba4_ndr("string-out", 
170 '
171         [public] void TestString([out,string,charset(UNIX)] uint8 **data);
172 ',
173 '
174         uint8_t data[] = { 0x03, 0x00, 0x00, 0x00, 
175                                            \'f\', \'o\', \'o\', 0 };
176         DATA_BLOB b = { data, 8 };
177         struct ndr_pull *ndr = ndr_pull_init_blob(&b, NULL,
178                 smb_iconv_convenience_reinit(NULL, "ASCII", "UTF8", true, NULL));
179         struct TestString r;
180         char *str = NULL;
181         r.out.data = &str;
182
183         if (!NDR_ERR_CODE_IS_SUCCESS(ndr_pull_TestString(ndr, NDR_IN, &r)))
184                 return 1;
185
186         if (r.out.data == NULL)
187                 return 2;
188
189         if (*r.out.data == NULL)
190                 return 3;
191
192         if (strncmp(r.out.data, "foo", 3) != 0)
193                 return 4;
194
195         if (r.out.data[4] != 0)
196                 return 5;
197 ');
198 }