File: prng.c

package info (click to toggle)
libigloo 0.9.4-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,968 kB
  • sloc: ansic: 5,476; sh: 4,996; makefile: 62
file content (77 lines) | stat: -rw-r--r-- 2,204 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
/* Copyright (C) 2021       Philipp "ph3-der-loewe" Schafft <lion@lion.leolix.org>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library 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
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
 * Boston, MA  02110-1301, USA.
 */

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include <igloo/tap.h>
#include <igloo/igloo.h>
#include <igloo/error.h>
#include <igloo/prng.h>


static igloo_ro_t g_instance;

static void dump(void)
{
    char *buf;

    if (igloo_ro_stringify(g_instance, &buf, igloo_RO_SY_DEFAULT) != igloo_ERROR_NONE)
        return;

    igloo_tap_diagnostic(buf);
    free(buf);
}

int main (void) {
    ssize_t res;
    size_t iter;
    unsigned char buf[16];

    igloo_tap_init();

    igloo_tap_test_success("igloo_initialize", igloo_initialize(&g_instance));
    if (igloo_ro_is_null(g_instance)) {
        igloo_tap_bail_out("Can not get an instance");
        return 1;
    }

    dump();
    igloo_tap_test_success("igloo_prng_auto_reseed", igloo_prng_auto_reseed(g_instance, igloo_PRNG_FLAG_NONE));

    dump();
    memset(buf, 0, sizeof(buf));
    igloo_tap_test_success("igloo_prng_write", igloo_prng_write(g_instance, buf, sizeof(buf), sizeof(buf)*8, igloo_PRNG_FLAG_NONE));

    for (iter = 0; iter < 16; iter++) {
        dump();
        res = igloo_prng_read(g_instance, buf, sizeof(buf), igloo_PRNG_FLAG_NONE);
        igloo_tap_test("igloo_prng_read", res == (ssize_t)sizeof(buf));
    }

    igloo_tap_test_success("unref instance", igloo_ro_unref(&g_instance));

    igloo_tap_fin();

    return 0;
}