Commit 99ff130b authored by Kai Vehmanen's avatar Kai Vehmanen

Updated test coverage of random submodule. Documented the module public API....

Updated test coverage of random submodule. Documented the module public API. Extended the set of generated random strings to match the IETF ICE spec definition of ice-chars. Added NICEAPI_EXPORT attributes.

darcs-hash:20070619080212-77cd4-3c32e1e428cd59ecd0373a7e6f90ac65b6ae28b6.gz
parent 822c9056
......@@ -34,6 +34,9 @@
* not delete the provisions above, a recipient may use your version of this
* file under either the MPL or the LGPL.
*/
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif
#include "random-glib.h"
......@@ -42,6 +45,7 @@ rng_seed (
G_GNUC_UNUSED
NiceRNG *rng, guint32 seed)
{
(void)rng;
g_random_set_seed (seed);
}
......@@ -54,6 +58,8 @@ rng_generate_bytes (
{
guint i;
(void)rng;
for (i = 0; i < len; i++)
buf[i] = g_random_int_range (0, 256);
}
......@@ -65,6 +71,7 @@ rng_generate_int (
guint low,
guint high)
{
(void)rng;
return g_random_int_range (low, high);
}
......@@ -74,7 +81,7 @@ rng_free (NiceRNG *rng)
g_slice_free (NiceRNG, rng);
}
NiceRNG *
NICEAPI_EXPORT NiceRNG *
nice_rng_glib_new (void)
{
NiceRNG *ret;
......@@ -87,7 +94,7 @@ nice_rng_glib_new (void)
return ret;
}
NiceRNG *
NICEAPI_EXPORT NiceRNG *
nice_rng_glib_new_predictable (void)
{
NiceRNG *rng;
......
......@@ -23,6 +23,7 @@
*
* Contributors:
* Dafydd Harries, Collabora Ltd.
* Kai Vehmanen, Nokia
*
* Alternatively, the contents of this file may be used under the terms of the
* the GNU Lesser General Public License Version 2.1 (the "LGPL"), in which
......@@ -34,13 +35,21 @@
* not delete the provisions above, a recipient may use your version of this
* file under either the MPL or the LGPL.
*/
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif
#include <string.h>
#include "random.h"
#include "random-glib.h"
static NiceRNG * (*nice_rng_new_func) (void) = NULL;
NiceRNG *
/**
* Creates a new random number generator instance.
*/
NICEAPI_EXPORT NiceRNG *
nice_rng_new (void)
{
if (nice_rng_new_func == NULL)
......@@ -49,40 +58,74 @@ nice_rng_new (void)
return nice_rng_new_func ();
}
void
/**
* Sets a new generator function.
*/
NICEAPI_EXPORT void
nice_rng_set_new_func (NiceRNG * (*func) (void))
{
nice_rng_new_func = func;
}
void
/**
* Frees the random number generator instance.
*
* @param rng context
*/
NICEAPI_EXPORT void
nice_rng_free (NiceRNG *rng)
{
rng->free (rng);
}
void
/**
* Generates random octets.
*
* @param rng context
* @param len number of octets to product
* @param buf buffer to store the results
*/
NICEAPI_EXPORT void
nice_rng_generate_bytes (NiceRNG *rng, guint len, gchar *buf)
{
rng->generate_bytes (rng, len, buf);
}
guint
/**
* Generates a random unsigned integer.
*
* @param rng context
* @param low closed lower bound
* @param high open upper bound
*/
NICEAPI_EXPORT guint
nice_rng_generate_int (NiceRNG *rng, guint low, guint high)
{
return rng->generate_int (rng, low, high);
}
void
/**
* Generates a stream of octets containing only characters
* with ASCII codecs of 0x41-5A (A-Z), 0x61-7A (a-z),
* 0x30-39 (0-9), 0x2b (+) and 0x2f (/). This matches
* the definition of 'ice-char' in ICE Ispecification,
* section 15.1 (ID-16).
*
* @param rng context
* @param len number of octets to product
* @param buf buffer to store the results
*/
NICEAPI_EXPORT void
nice_rng_generate_bytes_print (NiceRNG *rng, guint len, gchar *buf)
{
guint i;
const gchar *chars =
"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
"abcdefghijklmnopqrstuvwxyz"
"01234567890";
"0123456789"
"+/";
for (i = 0; i < len; i++)
buf[i] = chars[nice_rng_generate_int (rng, 0, 62)];
buf[i] = chars[nice_rng_generate_int (rng, 0, strlen (chars))];
}
......@@ -34,6 +34,9 @@
* not delete the provisions above, a recipient may use your version of this
* file under either the MPL or the LGPL.
*/
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif
#include <string.h>
......@@ -43,19 +46,21 @@ int
main (void)
{
NiceRNG *rng;
gchar buf[9];
const int rngsize = 256;
gchar buf[rngsize];
buf[8] = '\0';
buf[rngsize - 1] = '\0';
nice_rng_set_new_func (nice_rng_glib_new_predictable);
rng = nice_rng_new ();
nice_rng_generate_bytes_print (rng, 8, buf);
//g_debug ("%s", buf);
g_assert (0 == strcmp (buf, "S9PObXR5"));
nice_rng_generate_bytes_print (rng, rngsize - 1, buf);
/* g_debug ("%s", buf); */
g_assert (0 == strcmp (buf, "sv1AD7DnJTVykXGYYM6BmnXuYRlZNIJUzQzF+PvASjYxzdTTOngBJ5/gfK0Xj+Ly3ciAAk1Fmo0RPEpq6f4BBnp5jm3LuSbAOj1M5qULEGEv/0DMk0oOPUj6XPN1VwxFpjAfFeAxykiwdDiqNwnVJ/AKyr6/X7C5i+je7DSujURybOp6BkKWroLCzQg2AmTuqz48oNeY9CDeirNwoITfIaC40Ds9OgEDtL8WN5tL4QYdVuZQ85219Thogk775GV"));
nice_rng_generate_bytes (rng, 8, buf);
g_assert (0 == strcmp (buf, "\x09\xd3\x15\xf2\x24\x57\x46\xd8"));
nice_rng_generate_bytes (rng, 4, buf);
buf[4] = 0;
g_assert (0 == strcmp (buf, "\x1f\x0d\x47\xb8"));
nice_rng_free (rng);
return 0;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment