Merge branch 'upstream' into tizen
[platform/upstream/gnutls.git] / tests / mpi.c
1 /*
2  * Copyright (C) 2007-2012 Free Software Foundation, Inc.
3  *
4  * Author: Nikos Mavrogiannopoulos, Simon Josefsson
5  *
6  * This file is part of GnuTLS.
7  *
8  * GnuTLS is free software; you can redistribute it and/or modify it
9  * 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  * GnuTLS is distributed in the hope that it will be useful, but
14  * WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with GnuTLS; if not, write to the Free Software Foundation,
20  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
21  */
22
23 #ifdef HAVE_CONFIG_H
24 #include <config.h>
25 #endif
26
27 #include <stdio.h>
28
29 #include "utils.h"
30 #include "../lib/gnutls_int.h"
31 #include "../lib/gnutls_mpi.h"
32 #include "../lib/gnutls_errors.h"
33 #include "../lib/debug.h"
34
35 static void tls_log_func(int level, const char *str)
36 {
37         fprintf(stderr, "|<%d>| %s", level, str);
38 }
39
40 void doit(void)
41 {
42         bigint_t n1, n2, n3, n4;
43         int ret;
44
45         global_init();
46
47         gnutls_global_set_log_function(tls_log_func);
48         if (debug)
49                 gnutls_global_set_log_level(99);
50
51         ret = _gnutls_mpi_init_multi(&n1, &n2, &n3, &n4, NULL);
52         if (ret < 0)
53                 fail("mpi_new failed\n");
54
55         ret = _gnutls_mpi_set_ui(n2, 2);
56         if (ret < 0)
57                 fail("mpi_set_ui failed\n");
58
59         ret = _gnutls_mpi_set_ui(n3, 5);
60         if (ret < 0)
61                 fail("mpi_set_ui failed\n");
62
63         ret = _gnutls_mpi_set_ui(n1, 12498924);
64         if (ret < 0)
65                 fail("mpi_set_ui failed\n");
66
67         ret = _gnutls_mpi_addm(n4, n1, n3, n2);
68         if (ret < 0)
69                 fail("mpi_set_ui failed\n");
70
71         if (_gnutls_mpi_cmp_ui(n4, 0) != 0
72             && _gnutls_mpi_cmp_ui(n4, 1) != 0)
73                 fail("mpi_cmp_ui failed\n");
74
75         _gnutls_mpi_release(&n1);
76         _gnutls_mpi_release(&n2);
77         _gnutls_mpi_release(&n3);
78         _gnutls_mpi_release(&n4);
79
80         gnutls_global_deinit();
81
82         if (debug)
83                 success("mpi ops ok\n");
84 }