File: init_bignum_test.c

package info (click to toggle)
sscg 3.0.8-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 456 kB
  • sloc: ansic: 4,836; sh: 103; makefile: 11
file content (255 lines) | stat: -rw-r--r-- 7,188 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
/* SPDX-License-Identifier: GPL-3.0-or-later WITH cryptsetup-OpenSSL-exception */
/*
    This file is part of sscg.

    sscg is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    sscg is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with sscg.  If not, see <http://www.gnu.org/licenses/>.

    In addition, as a special exception, the copyright holders give
    permission to link the code of portions of this program with the
    OpenSSL library under certain conditions as described in each
    individual source file, and distribute linked combinations
    including the two.
    You must obey the GNU General Public License in all respects
    for all of the code used other than OpenSSL.  If you modify
    file(s) with this exception, you may extend this exception to your
    version of the file(s), but you are not obligated to do so.  If you
    do not wish to do so, delete this exception statement from your
    version.  If you delete this exception statement from all source
    files in the program, then also delete it here.

    Copyright 2017-2025 by Stephen Gallagher <sgallagh@redhat.com>
*/

#include <errno.h>
#include <stdio.h>
#include <limits.h>

#include "include/bignum.h"

int
main (int argc, char **argv)
{
  int ret;
  unsigned long val;
  struct sscg_bignum *bn;
  size_t initial_blocks, final_blocks;

  /* Enable talloc leak reporting for memory leak detection */
  talloc_enable_leak_report_full ();

  TALLOC_CTX *tmp_ctx = talloc_new (NULL);
  if (!tmp_ctx)
    {
      return ENOMEM;
    }

  /* Record initial memory state for leak detection */
  initial_blocks = talloc_total_blocks (NULL);

  printf ("=== SSCG Init Bignum Test with Talloc Leak Detection ===\n");
  printf ("Initial talloc blocks: %zu\n", initial_blocks);

  /* Test 1: Initialize bignum with zero */
  printf ("Testing sscg_init_bignum with zero. ");

  ret = sscg_init_bignum (tmp_ctx, 0, &bn);
  if (ret != EOK)
    {
      printf ("FAILED.\n");
      goto done;
    }
  printf ("SUCCESS.\n");

  printf ("Verifying bignum initialized to zero. ");
  val = BN_get_word (bn->bn);
  if (val != 0)
    {
      /* Bignum should always be initialized to 0 */
      printf ("FAILED.\n");
      ret = EINVAL;
      goto done;
    }
  printf ("SUCCESS.\n");

  /* Test 2: Initialize bignum with a non-zero value */
  printf ("Testing sscg_init_bignum with value 12345. ");

  ret = sscg_init_bignum (tmp_ctx, 12345, &bn);
  if (ret != EOK)
    {
      printf ("FAILED.\n");
      goto done;
    }
  printf ("SUCCESS.\n");

  printf ("Verifying bignum initialized to 12345. ");
  val = BN_get_word (bn->bn);
  if (val != 12345)
    {
      printf ("FAILED. Expected 12345, got %lu.\n", val);
      ret = EINVAL;
      goto done;
    }
  printf ("SUCCESS.\n");

  /* Test 3: Initialize bignum with maximum unsigned long value */
  printf ("Testing sscg_init_bignum with ULONG_MAX. ");

  ret = sscg_init_bignum (tmp_ctx, ULONG_MAX, &bn);
  if (ret != EOK)
    {
      printf ("FAILED.\n");
      goto done;
    }
  printf ("SUCCESS.\n");

  printf ("Verifying bignum initialized to ULONG_MAX. ");
  val = BN_get_word (bn->bn);
  if (val != ULONG_MAX && val != 0xffffffffL)
    {
      /* BN_get_word returns 0xffffffffL if the value is too large to fit */
      printf (
        "FAILED. Expected %lu or 0xffffffffL, got %lu.\n", ULONG_MAX, val);
      ret = EINVAL;
      goto done;
    }
  printf ("SUCCESS.\n");

  /* Test 4: Verify memory management by creating and destroying multiple bignums */
  printf ("Testing memory management with multiple bignums. ");

  struct sscg_bignum *bn1, *bn2, *bn3;

  ret = sscg_init_bignum (tmp_ctx, 100, &bn1);
  if (ret != EOK)
    goto memory_test_failed;

  ret = sscg_init_bignum (tmp_ctx, 200, &bn2);
  if (ret != EOK)
    goto memory_test_failed;

  ret = sscg_init_bignum (tmp_ctx, 300, &bn3);
  if (ret != EOK)
    goto memory_test_failed;

  /* Verify values are correct */
  if (BN_get_word (bn1->bn) != 100 || BN_get_word (bn2->bn) != 200 ||
      BN_get_word (bn3->bn) != 300)
    {
      goto memory_test_failed;
    }

  printf ("SUCCESS.\n");

  /* Test 5: Test with different value ranges */
  printf ("Testing various value ranges. ");

  struct sscg_bignum *bn_small, *bn_medium, *bn_large;

  ret = sscg_init_bignum (tmp_ctx, 1, &bn_small);
  if (ret != EOK)
    goto range_test_failed;

  ret = sscg_init_bignum (tmp_ctx, 65536, &bn_medium);
  if (ret != EOK)
    goto range_test_failed;

  ret = sscg_init_bignum (tmp_ctx, 4294967295UL, &bn_large);
  if (ret != EOK)
    goto range_test_failed;

  /* Verify values are correct */
  if (BN_get_word (bn_small->bn) != 1 ||
      BN_get_word (bn_medium->bn) != 65536 ||
      (BN_get_word (bn_large->bn) != 4294967295UL &&
       BN_get_word (bn_large->bn) != 0xffffffffL))
    {
      goto range_test_failed;
    }

  printf ("SUCCESS.\n");

  /* Test 6: Test talloc leak detection by creating isolated context */
  printf ("Testing talloc leak detection with isolated context. ");

  TALLOC_CTX *leak_test_ctx = talloc_new (NULL);
  if (!leak_test_ctx)
    {
      printf ("FAILED. Could not create leak test context.\n");
      ret = ENOMEM;
      goto done;
    }

  struct sscg_bignum *leak_test_bn;
  ret = sscg_init_bignum (leak_test_ctx, 999, &leak_test_bn);
  if (ret != EOK)
    {
      printf ("FAILED. Could not create bignum in leak test context.\n");
      talloc_free (leak_test_ctx);
      goto done;
    }

  /* Verify the value and then properly clean up */
  if (BN_get_word (leak_test_bn->bn) != 999)
    {
      printf ("FAILED. Leak test bignum has wrong value.\n");
      talloc_free (leak_test_ctx);
      ret = EINVAL;
      goto done;
    }

  /* Clean up the isolated context - this should free all associated memory */
  talloc_free (leak_test_ctx);

  printf ("SUCCESS.\n");

  ret = EOK;
  goto done;

memory_test_failed:
  printf ("FAILED.\n");
  ret = EINVAL;
  goto done;

range_test_failed:
  printf ("FAILED.\n");
  ret = EINVAL;

done:
  /* Clean up main test context */
  talloc_free (tmp_ctx);

  /* Check for memory leaks */
  final_blocks = talloc_total_blocks (NULL);
  printf ("\n=== Talloc Leak Detection Report ===\n");
  printf ("Initial talloc blocks: %zu\n", initial_blocks);
  printf ("Final talloc blocks: %zu\n", final_blocks);

  if (final_blocks > initial_blocks)
    {
      printf ("FAILED: Memory leak detected! %zu blocks leaked.\n",
              final_blocks - initial_blocks);
      printf ("Detailed leak report:\n");
      talloc_report_full (NULL, stderr);
      /* Fail the test when leaks are detected */
      ret = ENOMEM;
    }
  else
    {
      printf ("SUCCESS: No memory leaks detected.\n");
    }

  printf ("=== Test Complete ===\n");
  return ret;
}