summaryrefslogtreecommitdiff
path: root/random
diff options
context:
space:
mode:
authorKai Vehmanen <first.surname@nokia.com>2007-06-19 08:02:00 +0000
committerKai Vehmanen <first.surname@nokia.com>2007-06-19 08:02:00 +0000
commit99ff130b9bc44c75a30ee60078d1548d61f99270 (patch)
tree38027d1ca1bb1fd176cc4f4bdf71438098c6062a /random
parent822c9056909d4de84413010544090cc22793fac8 (diff)
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.
Diffstat (limited to 'random')
-rw-r--r--random/random-glib.c11
-rw-r--r--random/random.c59
-rw-r--r--random/test.c19
3 files changed, 72 insertions, 17 deletions
diff --git a/random/random-glib.c b/random/random-glib.c
index 0e7fb9a..2b3875d 100644
--- a/random/random-glib.c
+++ b/random/random-glib.c
@@ -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;
diff --git a/random/random.c b/random/random.c
index 1299e9b..edf6bcf 100644
--- a/random/random.c
+++ b/random/random.c
@@ -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))];
}
diff --git a/random/test.c b/random/test.c
index 2ba6eb5..0082b3b 100644
--- a/random/test.c
+++ b/random/test.c
@@ -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;