From 4a1c0dfb0b03ea0b494bcb7c47a476ed348a0613 Mon Sep 17 00:00:00 2001
From: Mattias Andrée <maandree@operamail.com>
Date: Wed, 5 Nov 2014 16:19:56 +0100
Subject: test state + m doc fix + m fix
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

Signed-off-by: Mattias Andrée <maandree@operamail.com>
---
 src/libkeccak/state.c |  2 +-
 src/test/test.c       | 65 +++++++++++++++++++++++++++++++++++++++++++++++----
 2 files changed, 62 insertions(+), 5 deletions(-)

(limited to 'src')

diff --git a/src/libkeccak/state.c b/src/libkeccak/state.c
index a4178be..18ec2d9 100644
--- a/src/libkeccak/state.c
+++ b/src/libkeccak/state.c
@@ -67,7 +67,7 @@ void libkeccak_state_wipe(volatile libkeccak_state_t* restrict state)
   M = state->M;
   for (i = 0; i < 25; i++)
     S[i] = 0;
-  for (i = 0; i < state->mlen; i++)
+  for (i = 0; i < state->mptr; i++)
     M[i] = 0;
 }
 
diff --git a/src/test/test.c b/src/test/test.c
index b384595..23bd8be 100644
--- a/src/test/test.c
+++ b/src/test/test.c
@@ -23,7 +23,7 @@
 
 
 /**
- * Test functions in <libkeccak.h>
+ * Test functions in <libkeccak/hex.h>
  * 
  * @return  Zero on success, -1 on error
  */
@@ -67,14 +67,68 @@ static int test_hex(void)
 }
 
 
+/**
+ * Test functions in <libkeccak/state.h>
+ * 
+ * @param   spec  The specifications for the state
+ * @return        Zero on success, -1 on error
+ */
+static int test_state(libkeccak_spec_t* restrict spec)
+{
+  libkeccak_state_t* restrict state;
+  libkeccak_state_t* restrict state2;
+  size_t marshal_size, marshalled_size, i, n;
+  char* restrict marshalled_data;
+  
+  if (state = libkeccak_state_create(spec), state == NULL)
+    return perror("libkeccak_state_initialise"), -1;
+  
+  n = state->mlen / 2;
+  for (i = 0; i < n; i++)
+    state->M[state->mptr++] = (char)(i & 255);
+  
+  if (state2 = libkeccak_state_duplicate(state), state2 == NULL)
+    return perror("libkeccak_state_duplicate"), -1;
+  
+  if (state->M[state->mptr - 1] != state2->M[state2->mptr - 1])
+    return printf("Inconsistency found between original state and duplicate state.\n"), -1;
+  
+  marshal_size = libkeccak_state_marshal_size(state2);
+  if (marshalled_data = malloc(marshal_size), marshalled_data == NULL)
+    return perror("malloc"), -1;
+  
+  marshalled_size = libkeccak_state_marshal(state2, marshalled_data);
+  if (marshalled_size != marshal_size)
+    return printf("libkeccak_state_marshal returned an unexpected value.\n"), -1;
+  
+  libkeccak_state_free(state);
+  
+  if (state = malloc(sizeof(libkeccak_state_t)), state == NULL)
+    return perror("malloc"), -1;
+  marshalled_size = libkeccak_state_unmarshal(state, marshalled_data);
+  if (marshalled_size == 0)
+    return perror("libkeccak_state_unmarshal"), -1;
+  if (marshalled_size != marshal_size)
+    return printf("libkeccak_state_unmarshal returned an unexpected value.\n"), -1;
+  
+  if (libkeccak_state_unmarshal_skip(marshalled_data) != marshal_size)
+    return printf("libkeccak_state_unmarshal_skip returned an unexpected value.\n"), -1;
+  
+  if (state->M[state->mptr - 1] != state2->M[state2->mptr - 1])
+    return printf("Inconsistency found between original state and unmarshalled state.\n"), -1;
+
+  free(marshalled_data);
+  libkeccak_state_free(state);
+  libkeccak_state_free(state2);
+  return 0;
+}
+
+
 int main(void)
 {
   libkeccak_generalised_spec_t gspec;
   libkeccak_spec_t spec;
   
-  if (test_hex())
-    return 1;
-  
   libkeccak_generalised_spec_initialise(&gspec);
   if (libkeccak_degeneralise_spec(&gspec, &spec))
     return printf("libkeccak_degeneralise_spec failed with all members at automatic.\n"), 1;
@@ -92,6 +146,9 @@ int main(void)
   if (gspec.output != 512)                                 return printf("Incorrect information\n"), 1;
   printf("\n");
   
+  if (test_hex())         return 1;
+  if (test_state(&spec))  return 1;
+  
   return 0;
 }
 
-- 
cgit v1.2.3-70-g09d2