s4:torture: Adapt KDC canon test to Heimdal upstream changes
[samba.git] / source4 / heimdal / lib / gssapi / krb5 / test_sequence.c
1 /*
2  * Copyright (c) 2003 Kungliga Tekniska Högskolan
3  * (Royal Institute of Technology, Stockholm, Sweden).
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  *
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  *
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  *
17  * 3. Neither the name of the Institute nor the names of its contributors
18  *    may be used to endorse or promote products derived from this software
19  *    without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  */
33
34 #include "gsskrb5_locl.h"
35
36 /* correct ordering */
37 OM_uint32 pattern1[] = {
38     0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13
39 };
40
41 /* gap 10 */
42 OM_uint32 pattern2[] = {
43     0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 11, 12, 13
44 };
45
46 /* dup 9 */
47 OM_uint32 pattern3[] = {
48     0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 9, 10, 11, 12, 13
49 };
50
51 /* gaps */
52 OM_uint32 pattern4[] = {
53     0, 1, 2, 3, 4, 5, 6, 7, 8, 10, 12, 13, 14, 15, 16, 18, 100
54 };
55
56 /* 11 before 10 */
57 OM_uint32 pattern5[] = {
58     0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 11, 10, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21
59 };
60
61 /* long */
62 OM_uint32 pattern6[] = {
63     0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
64     10, 11, 12, 13, 14, 15, 16, 17, 18, 19,
65     20, 21, 22, 23, 24, 25, 26, 27, 28, 29,
66     30, 31, 32, 33, 34, 35, 36, 37, 38, 39,
67     40, 41, 42, 43, 44, 45, 46, 47, 48, 49,
68     50, 51, 52, 53, 54, 55, 56, 57, 58, 59
69 };
70
71 /* don't start at 0 */
72 OM_uint32 pattern7[] = {
73     11, 12, 13
74 };
75
76 /* wrap around */
77 OM_uint32 pattern8[] = {
78     4294967293U, 4294967294U, 4294967295U, 0, 1, 2
79 };
80
81 static int
82 test_seq(int t, OM_uint32 flags, OM_uint32 start_seq,
83          OM_uint32 *pattern, int pattern_len, OM_uint32 expected_error)
84 {
85     struct gss_msg_order *o;
86     OM_uint32 maj_stat, min_stat;
87     krb5_storage *sp;
88     int i;
89
90     maj_stat = _gssapi_msg_order_create(&min_stat, &o, flags,
91                                         start_seq, 20, 0);
92     if (maj_stat)
93         errx(1, "create: %d %d", maj_stat, min_stat);
94
95     sp = krb5_storage_emem();
96     if (sp == NULL)
97         errx(1, "krb5_storage_from_emem");
98
99     _gssapi_msg_order_export(sp, o);
100
101     for (i = 0; i < pattern_len; i++) {
102         maj_stat = _gssapi_msg_order_check(o, pattern[i]);
103         if (maj_stat)
104             break;
105     }
106     if (maj_stat != expected_error) {
107         printf("test pattern %d failed with %d (should have been %d)\n",
108                t, maj_stat, expected_error);
109         krb5_storage_free(sp);
110         _gssapi_msg_order_destroy(&o);
111         return 1;
112     }
113
114
115     _gssapi_msg_order_destroy(&o);
116
117     /* try again, now with export/imported blob */
118     krb5_storage_seek(sp, 0, SEEK_SET);
119
120     maj_stat = _gssapi_msg_order_import(&min_stat, sp, &o);
121     if (maj_stat)
122         errx(1, "import: %d %d", maj_stat, min_stat);
123
124     for (i = 0; i < pattern_len; i++) {
125         maj_stat = _gssapi_msg_order_check(o, pattern[i]);
126         if (maj_stat)
127             break;
128     }
129     if (maj_stat != expected_error) {
130         printf("import/export test pattern %d failed "
131                "with %d (should have been %d)\n",
132                t, maj_stat, expected_error);
133         _gssapi_msg_order_destroy(&o);
134         krb5_storage_free(sp);
135         return 1;
136     }
137
138     _gssapi_msg_order_destroy(&o);
139     krb5_storage_free(sp);
140
141     return 0;
142 }
143
144 struct {
145     OM_uint32 flags;
146     OM_uint32 *pattern;
147     int pattern_len;
148     OM_uint32 error_code;
149     OM_uint32 start_seq;
150 } pl[] = {
151     {
152         GSS_C_REPLAY_FLAG|GSS_C_SEQUENCE_FLAG,
153         pattern1,
154         sizeof(pattern1)/sizeof(pattern1[0]),
155         0
156     },
157     {
158         GSS_C_REPLAY_FLAG|GSS_C_SEQUENCE_FLAG,
159         pattern2,
160         sizeof(pattern2)/sizeof(pattern2[0]),
161         GSS_S_GAP_TOKEN
162     },
163     {
164         GSS_C_REPLAY_FLAG|GSS_C_SEQUENCE_FLAG,
165         pattern3,
166         sizeof(pattern3)/sizeof(pattern3[0]),
167         GSS_S_DUPLICATE_TOKEN
168     },
169     {
170         GSS_C_REPLAY_FLAG|GSS_C_SEQUENCE_FLAG,
171         pattern4,
172         sizeof(pattern4)/sizeof(pattern4[0]),
173         GSS_S_GAP_TOKEN
174     },
175     {
176         GSS_C_REPLAY_FLAG|GSS_C_SEQUENCE_FLAG,
177         pattern5,
178         sizeof(pattern5)/sizeof(pattern5[0]),
179         GSS_S_GAP_TOKEN
180     },
181     {
182         GSS_C_REPLAY_FLAG|GSS_C_SEQUENCE_FLAG,
183         pattern6,
184         sizeof(pattern6)/sizeof(pattern6[0]),
185         GSS_S_COMPLETE
186     },
187     {
188         GSS_C_REPLAY_FLAG|GSS_C_SEQUENCE_FLAG,
189         pattern7,
190         sizeof(pattern7)/sizeof(pattern7[0]),
191         GSS_S_GAP_TOKEN
192     },
193     {
194         GSS_C_REPLAY_FLAG|GSS_C_SEQUENCE_FLAG,
195         pattern8,
196         sizeof(pattern8)/sizeof(pattern8[0]),
197         GSS_S_COMPLETE,
198         4294967293U
199     },
200     {
201         0,
202         pattern1,
203         sizeof(pattern1)/sizeof(pattern1[0]),
204         GSS_S_COMPLETE
205     },
206     {
207         0,
208         pattern2,
209         sizeof(pattern2)/sizeof(pattern2[0]),
210         GSS_S_COMPLETE
211     },
212     {
213         0,
214         pattern3,
215         sizeof(pattern3)/sizeof(pattern3[0]),
216         GSS_S_COMPLETE
217     },
218     {
219         0,
220         pattern4,
221         sizeof(pattern4)/sizeof(pattern4[0]),
222         GSS_S_COMPLETE
223     },
224     {
225         0,
226         pattern5,
227         sizeof(pattern5)/sizeof(pattern5[0]),
228         GSS_S_COMPLETE
229     },
230     {
231         0,
232         pattern6,
233         sizeof(pattern6)/sizeof(pattern6[0]),
234         GSS_S_COMPLETE
235     },
236     {
237         0,
238         pattern7,
239         sizeof(pattern7)/sizeof(pattern7[0]),
240         GSS_S_COMPLETE
241     },
242     {
243         0,
244         pattern8,
245         sizeof(pattern8)/sizeof(pattern8[0]),
246         GSS_S_COMPLETE,
247         4294967293U
248
249     },
250     {
251         GSS_C_REPLAY_FLAG,
252         pattern1,
253         sizeof(pattern1)/sizeof(pattern1[0]),
254         GSS_S_COMPLETE
255     },
256     {
257         GSS_C_REPLAY_FLAG,
258         pattern2,
259         sizeof(pattern2)/sizeof(pattern2[0]),
260         GSS_S_COMPLETE
261     },
262     {
263         GSS_C_REPLAY_FLAG,
264         pattern3,
265         sizeof(pattern3)/sizeof(pattern3[0]),
266         GSS_S_DUPLICATE_TOKEN
267     },
268     {
269         GSS_C_REPLAY_FLAG,
270         pattern4,
271         sizeof(pattern4)/sizeof(pattern4[0]),
272         GSS_S_COMPLETE
273     },
274     {
275         GSS_C_REPLAY_FLAG,
276         pattern5,
277         sizeof(pattern5)/sizeof(pattern5[0]),
278         0
279     },
280     {
281         GSS_C_REPLAY_FLAG,
282         pattern6,
283         sizeof(pattern6)/sizeof(pattern6[0]),
284         GSS_S_COMPLETE
285     },
286     {
287         GSS_C_REPLAY_FLAG,
288         pattern7,
289         sizeof(pattern7)/sizeof(pattern7[0]),
290         GSS_S_COMPLETE
291     },
292     {
293         GSS_C_SEQUENCE_FLAG,
294         pattern8,
295         sizeof(pattern8)/sizeof(pattern8[0]),
296         GSS_S_COMPLETE,
297         4294967293U
298     },
299     {
300         GSS_C_SEQUENCE_FLAG,
301         pattern1,
302         sizeof(pattern1)/sizeof(pattern1[0]),
303         0
304     },
305     {
306         GSS_C_SEQUENCE_FLAG,
307         pattern2,
308         sizeof(pattern2)/sizeof(pattern2[0]),
309         GSS_S_GAP_TOKEN
310     },
311     {
312         GSS_C_SEQUENCE_FLAG,
313         pattern3,
314         sizeof(pattern3)/sizeof(pattern3[0]),
315         GSS_S_DUPLICATE_TOKEN
316     },
317     {
318         GSS_C_SEQUENCE_FLAG,
319         pattern4,
320         sizeof(pattern4)/sizeof(pattern4[0]),
321         GSS_S_GAP_TOKEN
322     },
323     {
324         GSS_C_SEQUENCE_FLAG,
325         pattern5,
326         sizeof(pattern5)/sizeof(pattern5[0]),
327         GSS_S_GAP_TOKEN
328     },
329     {
330         GSS_C_SEQUENCE_FLAG,
331         pattern6,
332         sizeof(pattern6)/sizeof(pattern6[0]),
333         GSS_S_COMPLETE
334     },
335     {
336         GSS_C_SEQUENCE_FLAG,
337         pattern7,
338         sizeof(pattern7)/sizeof(pattern7[0]),
339         GSS_S_GAP_TOKEN
340     },
341     {
342         GSS_C_REPLAY_FLAG,
343         pattern8,
344         sizeof(pattern8)/sizeof(pattern8[0]),
345         GSS_S_COMPLETE,
346         4294967293U
347     }
348 };
349
350 int
351 main(int argc, char **argv)
352 {
353     int i, failed = 0;
354
355     for (i = 0; i < sizeof(pl)/sizeof(pl[0]); i++) {
356         if (test_seq(i,
357                      pl[i].flags,
358                      pl[i].start_seq,
359                      pl[i].pattern,
360                      pl[i].pattern_len,
361                      pl[i].error_code))
362             failed++;
363     }
364     if (failed)
365         printf("FAILED %d tests\n", failed);
366     return failed != 0;
367 }