Line data Source code
1 : /*
2 : Unix SMB/CIFS implementation.
3 :
4 : local testing of idtree routines.
5 :
6 : Copyright (C) Andrew Tridgell 2004
7 :
8 : This program is free software; you can redistribute it and/or modify
9 : it under the terms of the GNU General Public License as published by
10 : the Free Software Foundation; either version 3 of the License, or
11 : (at your option) any later version.
12 :
13 : This program is distributed in the hope that it will be useful,
14 : but WITHOUT ANY WARRANTY; without even the implied warranty of
15 : MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 : GNU General Public License for more details.
17 :
18 : You should have received a copy of the GNU General Public License
19 : along with this program. If not, see <http://www.gnu.org/licenses/>.
20 : */
21 :
22 : #include "includes.h"
23 : #include "torture/torture.h"
24 : #include "torture/local/proto.h"
25 : #include "lib/util/idtree.h"
26 :
27 1 : static bool torture_local_idtree_simple(struct torture_context *tctx)
28 : {
29 1 : struct idr_context *idr;
30 1 : int i, ret;
31 1 : int *ids;
32 1 : int *present;
33 1 : extern int torture_numops;
34 1 : int n = torture_numops;
35 1 : TALLOC_CTX *mem_ctx = tctx;
36 :
37 1 : idr = idr_init(mem_ctx);
38 :
39 1 : ids = talloc_zero_array(mem_ctx, int, n);
40 1 : present = talloc_zero_array(mem_ctx, int, n);
41 :
42 12 : for (i=0;i<n;i++) {
43 10 : ids[i] = -1;
44 : }
45 :
46 11 : for (i=0;i<n;i++) {
47 10 : int ii = random() % n;
48 10 : void *p = idr_find(idr, ids[ii]);
49 10 : if (present[ii]) {
50 2 : if (p != &ids[ii]) {
51 0 : torture_fail(tctx, talloc_asprintf(tctx,
52 : "wrong ptr at %d - %p should be %p",
53 : ii, p, &ids[ii]));
54 : }
55 2 : if (random() % 7 == 0) {
56 0 : if (idr_remove(idr, ids[ii]) != 0) {
57 0 : torture_fail(tctx, talloc_asprintf(tctx,
58 : "remove failed at %d (id=%d)",
59 : i, ids[ii]));
60 : }
61 0 : present[ii] = 0;
62 0 : ids[ii] = -1;
63 : }
64 : } else {
65 8 : if (p != NULL) {
66 0 : torture_fail(tctx,
67 : talloc_asprintf(tctx,
68 : "non-present at %d gave %p (would be %d)",
69 : ii, p,
70 : (int)((((char *)p) - (char *)(&ids[0])) / sizeof(int))));
71 : }
72 8 : if (random() % 5) {
73 6 : ids[ii] = idr_get_new(idr, &ids[ii], n);
74 6 : if (ids[ii] < 0) {
75 0 : torture_fail(tctx, talloc_asprintf(tctx,
76 : "alloc failure at %d (ret=%d)",
77 : ii, ids[ii]));
78 : } else {
79 6 : present[ii] = 1;
80 : }
81 : }
82 : }
83 : }
84 :
85 1 : torture_comment(tctx, "done %d random ops\n", i);
86 :
87 12 : for (i=0;i<n;i++) {
88 10 : if (present[i]) {
89 6 : if (idr_remove(idr, ids[i]) != 0) {
90 10 : torture_fail(tctx, talloc_asprintf(tctx,
91 : "delete failed on cleanup at %d (id=%d)",
92 : i, ids[i]));
93 : }
94 : }
95 : }
96 :
97 : /* now test some limits */
98 25001 : for (i=0;i<25000;i++) {
99 25000 : ret = idr_get_new_above(idr, &ids[0], random() % 25000, 0x10000-3);
100 25000 : torture_assert(tctx, ret != -1, "idr_get_new_above failed");
101 : }
102 :
103 1 : ret = idr_get_new_above(idr, &ids[0], 0x10000-2, 0x10000);
104 1 : torture_assert_int_equal(tctx, ret, 0x10000-2, "idr_get_new_above failed");
105 1 : ret = idr_get_new_above(idr, &ids[0], 0x10000-1, 0x10000);
106 1 : torture_assert_int_equal(tctx, ret, 0x10000-1, "idr_get_new_above failed");
107 1 : ret = idr_get_new_above(idr, &ids[0], 0x10000, 0x10000);
108 1 : torture_assert_int_equal(tctx, ret, 0x10000, "idr_get_new_above failed");
109 1 : ret = idr_get_new_above(idr, &ids[0], 0x10000+1, 0x10000);
110 1 : torture_assert_int_equal(tctx, ret, -1, "idr_get_new_above succeeded above limit");
111 1 : ret = idr_get_new_above(idr, &ids[0], 0x10000+2, 0x10000);
112 1 : torture_assert_int_equal(tctx, ret, -1, "idr_get_new_above succeeded above limit");
113 :
114 1 : torture_comment(tctx, "cleaned up\n");
115 1 : return true;
116 : }
117 :
118 2354 : struct torture_suite *torture_local_idtree(TALLOC_CTX *mem_ctx)
119 : {
120 2354 : struct torture_suite *suite = torture_suite_create(mem_ctx, "idtree");
121 2354 : torture_suite_add_simple_test(suite, "idtree", torture_local_idtree_simple);
122 2354 : return suite;
123 : }
|