libcli/smb: add smb_protocol_types_string()
[samba.git] / libcli / smb / test_smb1cli_session.c
1 #include <stdarg.h>
2 #include <stddef.h>
3 #include <setjmp.h>
4 #include <cmocka.h>
5
6 #include "replace.h"
7 #include <talloc.h>
8 #include "libcli/util/ntstatus.h"
9 #include "smb_constants.h"
10 #include "smb_util.h"
11
12 static const uint8_t smb1_session_setup_bytes[] = {
13         0xA1, 0x82, 0x01, 0x02, 0x30, 0x81, 0xFF, 0xA0,
14         0x03, 0x0A, 0x01, 0x01, 0xA1, 0x0C, 0x06, 0x0A,
15         0x2B, 0x06, 0x01, 0x04, 0x01, 0x82, 0x37, 0x02,
16         0x02, 0x0A, 0xA2, 0x81, 0xE9, 0x04, 0x81, 0xE6,
17         0x4E, 0x54, 0x4C, 0x4D, 0x53, 0x53, 0x50, 0x00,
18         0x02, 0x00, 0x00, 0x00, 0x16, 0x00, 0x16, 0x00,
19         0x38, 0x00, 0x00, 0x00, 0x15, 0x82, 0x89, 0x62,
20         0xF6, 0x65, 0xAB, 0x23, 0x47, 0xBC, 0x4D, 0x21,
21         0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
22         0x98, 0x00, 0x98, 0x00, 0x4E, 0x00, 0x00, 0x00,
23         0x06, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0F,
24         0x53, 0x00, 0x41, 0x00, 0x4D, 0x00, 0x42, 0x00,
25         0x41, 0x00, 0x44, 0x00, 0x4F, 0x00, 0x4D, 0x00,
26         0x41, 0x00, 0x49, 0x00, 0x4E, 0x00, 0x02, 0x00,
27         0x16, 0x00, 0x53, 0x00, 0x41, 0x00, 0x4D, 0x00,
28         0x42, 0x00, 0x41, 0x00, 0x44, 0x00, 0x4F, 0x00,
29         0x4D, 0x00, 0x41, 0x00, 0x49, 0x00, 0x4E, 0x00,
30         0x01, 0x00, 0x0E, 0x00, 0x4C, 0x00, 0x4F, 0x00,
31         0x43, 0x00, 0x41, 0x00, 0x4C, 0x00, 0x44, 0x00,
32         0x43, 0x00, 0x04, 0x00, 0x22, 0x00, 0x73, 0x00,
33         0x61, 0x00, 0x6D, 0x00, 0x62, 0x00, 0x61, 0x00,
34         0x2E, 0x00, 0x65, 0x00, 0x78, 0x00, 0x61, 0x00,
35         0x6D, 0x00, 0x70, 0x00, 0x6C, 0x00, 0x65, 0x00,
36         0x2E, 0x00, 0x63, 0x00, 0x6F, 0x00, 0x6D, 0x00,
37         0x03, 0x00, 0x32, 0x00, 0x6C, 0x00, 0x6F, 0x00,
38         0x63, 0x00, 0x61, 0x00, 0x6C, 0x00, 0x64, 0x00,
39         0x63, 0x00, 0x2E, 0x00, 0x73, 0x00, 0x61, 0x00,
40         0x6D, 0x00, 0x62, 0x00, 0x61, 0x00, 0x2E, 0x00,
41         0x65, 0x00, 0x78, 0x00, 0x61, 0x00, 0x6D, 0x00,
42         0x70, 0x00, 0x6C, 0x00, 0x65, 0x00, 0x2E, 0x00,
43         0x63, 0x00, 0x6F, 0x00, 0x6D, 0x00, 0x07, 0x00,
44         0x08, 0x00, 0x0C, 0x40, 0xA3, 0xC3, 0x5B, 0xE0,
45         0xD2, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x55,
46         0x00, 0x6E, 0x00, 0x69, 0x00, 0x78, 0x00, 0x00,
47         0x00, 0x53, 0x00, 0x61, 0x00, 0x6D, 0x00, 0x62,
48         0x00, 0x61, 0x00, 0x20, 0x00, 0x34, 0x00, 0x2E,
49         0x00, 0x37, 0x00, 0x2E, 0x00, 0x30, 0x00, 0x70,
50         0x00, 0x72, 0x00, 0x65, 0x00, 0x31, 0x00, 0x2D,
51         0x00, 0x44, 0x00, 0x45, 0x00, 0x56, 0x00, 0x45,
52         0x00, 0x4C, 0x00, 0x4F, 0x00, 0x50, 0x00, 0x45,
53         0x00, 0x52, 0x00, 0x42, 0x00, 0x55, 0x00, 0x49,
54         0x00, 0x4C, 0x00, 0x44, 0x00, 0x00, 0x00, 0x53,
55         0x00, 0x41, 0x00, 0x4D, 0x00, 0x42, 0x00, 0x41,
56         0x00, 0x44, 0x00, 0x4F, 0x00, 0x4D, 0x00, 0x41,
57         0x00, 0x49, 0x00, 0x4E, 0x00, 0x00, 0x00
58 };
59
60 static void test_smb_bytes_pull_str(void **state)
61 {
62         NTSTATUS status;
63         const uint8_t *bytes = smb1_session_setup_bytes;
64         const size_t num_bytes = sizeof(smb1_session_setup_bytes);
65         const uint8_t *p = NULL;
66         size_t ret = 0;
67         size_t out_security_blob_length = 262;
68         bool use_unicode = true;
69         char *str = NULL;
70
71         p = bytes;
72         p += out_security_blob_length;
73
74         status = smb_bytes_pull_str(NULL, &str, use_unicode,
75                                     bytes, num_bytes,
76                                     p, &ret);
77         assert_true(NT_STATUS_IS_OK(status));
78         assert_string_equal(str, "Unix");
79         assert_int_equal(ret, 0x0b);
80         TALLOC_FREE(str);
81
82         p += ret;
83         status = smb_bytes_pull_str(NULL, &str, use_unicode,
84                                     bytes, num_bytes,
85                                     p, &ret);
86         assert_true(NT_STATUS_IS_OK(status));
87         assert_string_equal(str, "Samba 4.7.0pre1-DEVELOPERBUILD");
88         assert_int_equal(ret, 0x3e);
89         TALLOC_FREE(str);
90
91         p += ret;
92         status = smb_bytes_pull_str(NULL, &str, use_unicode,
93                                     bytes, num_bytes,
94                                     p, &ret);
95         assert_true(NT_STATUS_IS_OK(status));
96         assert_string_equal(str, "SAMBADOMAIN");
97         assert_int_equal(ret, 0x18);
98         TALLOC_FREE(str);
99
100         p += ret;
101         status = smb_bytes_pull_str(NULL, &str, use_unicode,
102                                     bytes, num_bytes,
103                                     p, &ret);
104         assert_true(NT_STATUS_IS_OK(status));
105         assert_string_equal(str, "");
106         assert_int_equal(ret, 0x00);
107         TALLOC_FREE(str);
108 }
109
110 static void test_smb_bytes_pull_str_no_unicode(void **state)
111 {
112         NTSTATUS status;
113         const uint8_t *bytes = smb1_session_setup_bytes;
114         const size_t num_bytes = sizeof(smb1_session_setup_bytes);
115         const uint8_t *p = NULL;
116         size_t ret = 0;
117         size_t out_security_blob_length = 262;
118         bool use_unicode = false;
119         char *str = NULL;
120
121         p = bytes;
122         p += out_security_blob_length;
123
124         status = smb_bytes_pull_str(NULL, &str, use_unicode,
125                                     bytes, num_bytes,
126                                     p, &ret);
127         assert_true(NT_STATUS_IS_OK(status));
128         assert_string_equal(str, "");
129         assert_int_equal(ret, 0x01);
130         TALLOC_FREE(str);
131 }
132
133 static void test_smb_bytes_pull_str_wrong_offset(void **state)
134 {
135         NTSTATUS status;
136         const uint8_t *bytes = smb1_session_setup_bytes;
137         const size_t num_bytes = sizeof(smb1_session_setup_bytes);
138         const uint8_t *p = NULL;
139         size_t ret = 0;
140         size_t out_security_blob_length = 261;
141         bool use_unicode = true;
142         char *str = NULL;
143
144         bytes += 1;
145         p = bytes;
146         p += out_security_blob_length;
147
148         status = smb_bytes_pull_str(NULL, &str, use_unicode,
149                                     bytes, num_bytes,
150                                     p, &ret);
151         assert_true(NT_STATUS_IS_OK(status));
152
153         assert_string_equal(str, "\xE5\x94\x80\xE6\xB8\x80\xE6\xA4\x80\xE7\xA0\x80");
154         assert_int_equal(ret, 0x0a);
155         TALLOC_FREE(str);
156 }
157
158 static void test_smb_bytes_pull_str_invalid_offset(void **state)
159 {
160         NTSTATUS status;
161         const uint8_t *bytes = smb1_session_setup_bytes;
162         const size_t num_bytes = sizeof(smb1_session_setup_bytes);
163         const uint8_t *p = NULL;
164         size_t ret = 0;
165         bool use_unicode = true;
166         char *str = NULL;
167         intptr_t bytes_address = (intptr_t)bytes;
168
169         /* Warning: array subscript is below array bounds */
170         p = (const uint8_t *)(bytes_address - 1);
171         status = smb_bytes_pull_str(NULL, &str, use_unicode,
172                                     bytes, num_bytes,
173                                     p, &ret);
174         assert_int_equal(NT_STATUS_V(status),
175                          NT_STATUS_V(NT_STATUS_INTERNAL_ERROR));
176
177         p = bytes + num_bytes;
178         status = smb_bytes_pull_str(NULL, &str, use_unicode,
179                                     bytes, num_bytes,
180                                     p, &ret);
181         assert_true(NT_STATUS_IS_OK(status));
182         assert_string_equal(str, "");
183         assert_int_equal(ret, 0x00);
184         TALLOC_FREE(str);
185
186         p = bytes + num_bytes - 1;
187         status = smb_bytes_pull_str(NULL, &str, use_unicode,
188                                     bytes, num_bytes,
189                                     p, &ret);
190         assert_true(NT_STATUS_IS_OK(status));
191         assert_string_equal(str, "");
192         assert_int_equal(ret, 0x01);
193         TALLOC_FREE(str);
194
195         /* Warning: array subscript is above array bounds */
196         p = (const uint8_t *)(bytes_address + num_bytes + 1);
197         status = smb_bytes_pull_str(NULL, &str, use_unicode,
198                                     bytes, num_bytes,
199                                     p, &ret);
200         assert_int_equal(NT_STATUS_V(status),
201                          NT_STATUS_V(NT_STATUS_BUFFER_TOO_SMALL));
202 }
203
204 int main(void)
205 {
206         const struct CMUnitTest tests[] = {
207                 cmocka_unit_test(test_smb_bytes_pull_str),
208                 cmocka_unit_test(test_smb_bytes_pull_str_no_unicode),
209                 cmocka_unit_test(test_smb_bytes_pull_str_wrong_offset),
210                 cmocka_unit_test(test_smb_bytes_pull_str_invalid_offset),
211         };
212
213         cmocka_set_message_output(CM_OUTPUT_SUBUNIT);
214         return cmocka_run_group_tests(tests, NULL, NULL);
215 }