1/*
2 * Copyright (c) 2013 Kungliga Tekniska Högskolan
3 * (Royal Institute of Technology, Stockholm, Sweden).
4 * All rights reserved.
5 *
6 * Portions Copyright (c) 2013 Apple Inc. All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 *
12 * 1. Redistributions of source code must retain the above copyright
13 *    notice, this list of conditions and the following disclaimer.
14 *
15 * 2. Redistributions in binary form must reproduce the above copyright
16 *    notice, this list of conditions and the following disclaimer in the
17 *    documentation and/or other materials provided with the distribution.
18 *
19 * 3. Neither the name of KTH nor the names of its contributors may be
20 *    used to endorse or promote products derived from this software without
21 *    specific prior written permission.
22 *
23 * THIS SOFTWARE IS PROVIDED BY KTH AND ITS CONTRIBUTORS ``AS IS'' AND ANY
24 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
26 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL KTH OR ITS CONTRIBUTORS BE
27 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
28 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
29 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
30 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
31 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
32 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
33 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */
34
35#include "krb5_locl.h"
36#include <getarg.h>
37
38/*
39 *
40 */
41
42static int version_flag = 0;
43static int help_flag	= 0;
44
45static struct getargs args[] = {
46    {"version",		0,	arg_flag,	&version_flag,
47     "print version", NULL },
48    {"help",		0,	arg_flag,	&help_flag,
49     NULL, NULL }
50};
51
52static void
53usage(int ret)
54{
55    arg_printusage(args,
56		   sizeof(args)/sizeof(*args),
57		   NULL,
58		   "");
59    exit(ret);
60}
61
62#define NUM_ITER 100000
63#define MAX_HOSTS 1000
64
65int
66main(int argc, char **argv)
67{
68    struct _krb5_srv_query_ctx ctx;
69    krb5_context context;
70    krb5_error_code ret;
71    int optidx = 0;
72    size_t n, m;
73
74    setprogname(argv[0]);
75
76    if(getarg(args, sizeof(args) / sizeof(args[0]), argc, argv, &optidx))
77	usage(1);
78
79    if (help_flag)
80	usage (0);
81
82    if(version_flag){
83	print_version(NULL);
84	exit(0);
85    }
86
87    ret = krb5_init_context (&context);
88    if (ret)
89	errx (1, "krb5_init_context failed: %d", ret);
90
91    memset(&ctx, 0, sizeof(ctx));
92
93    ctx.context = context;
94    ctx.domain = rk_UNCONST("domain");
95    ctx.array = calloc(MAX_HOSTS, sizeof(ctx.array[0]));
96    if (ctx.array == NULL)
97	errx(1, "malloc: outo of memory");
98
99#ifdef __APPLE__
100    for (n = 0; n < NUM_ITER; n++) {
101
102	if ((n % (NUM_ITER / 10)) == 0) {
103	    printf("%d ", (int)n);
104	    fflush(stdout);
105	}
106
107	if (n < 10) {
108	    ctx.len = n;
109	} else {
110	    ctx.len = (rk_random() % (MAX_HOSTS - 1)) + 1;
111	}
112
113	for (m = 0; m < ctx.len; m++) {
114	    ctx.array[m].hi = NULL;
115	    ctx.array[m].priority = rk_random_uniform(5);
116	    ctx.array[m].weight = rk_random_uniform(4);
117	}
118
119	_krb5_state_srv_sort(&ctx);
120    }
121#endif
122    printf("\n");
123
124
125    krb5_free_context(context);
126
127    return 0;
128}
129