Skip to content

Commit

Permalink
Added unit tests for seed module (#209)
Browse files Browse the repository at this point in the history
  • Loading branch information
italo-sampaio authored Oct 15, 2024
1 parent ecb7756 commit bc1a6e5
Show file tree
Hide file tree
Showing 5 changed files with 492 additions and 5 deletions.
16 changes: 16 additions & 0 deletions firmware/src/hal/sgx/test/mock/mock_secret_store.c
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,22 @@ uint8_t mock_sest_read(char *key, uint8_t *dest, size_t dest_length) {
return 0;
}

bool mock_sest_remove(char *key) {
for (size_t i = 0; i < g_mock_secret_store.num_registers; i++) {
if (strcmp(g_mock_secret_store.registers[i].key, key) == 0) {
free(g_mock_secret_store.registers[i].key);
free(g_mock_secret_store.registers[i].secret);
for (size_t j = i; j < g_mock_secret_store.num_registers - 1; j++) {
g_mock_secret_store.registers[j] =
g_mock_secret_store.registers[j + 1];
}
g_mock_secret_store.num_registers--;
return true;
}
}
return false;
}

void mock_sest_init() {
memset(&g_mock_secret_store, 0, sizeof(g_mock_secret_store));
}
Expand Down
9 changes: 9 additions & 0 deletions firmware/src/hal/sgx/test/mock/mock_secret_store.h
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,15 @@ bool mock_sest_write(char *key, uint8_t *secret, size_t secret_length);
*/
uint8_t mock_sest_read(char *key, uint8_t *dest, size_t dest_length);

/**
* @brief Mock implementation of sest_remove
*
* @param key the key for the secret
*
* @returns whether the secret was successfully removed
*/
bool mock_sest_remove(char *key);

/**
* @brief Resets the mock secret store to its initial state
*/
Expand Down
9 changes: 4 additions & 5 deletions firmware/src/hal/sgx/test/run-all.sh
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
#!/bin/bash
BASEDIR=$(dirname $0)
TESTDIRS="nvmem secret_store"
ROOTDIR=$(dirname $0)/../../../../..
TESTDIR=$(realpath $(dirname $0) --relative-to $ROOTDIR)
TESTDIRS="nvmem secret_store seed"
TESTDIRS=${1:-"$TESTDIRS"}

for d in $TESTDIRS; do
echo "******************************"
echo "Testing $d..."
echo "******************************"
cd "$BASEDIR/$d"
make clean test || exit $?
cd - > /dev/null
$ROOTDIR/docker/mware/do-notty-nousb /hsm2/$TESTDIR/$d "make clean test" || exit $?
done
39 changes: 39 additions & 0 deletions firmware/src/hal/sgx/test/seed/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# The MIT License (MIT)
#
# Copyright (c) 2021 RSK Labs Ltd
#
# Permission is hereby granted, free of charge, to any person obtaining a copy of
# this software and associated documentation files (the "Software"), to deal in
# the Software without restriction, including without limitation the rights to
# use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
# of the Software, and to permit persons to whom the Software is furnished to do
# so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.

include ../common/common.mk

PROG = test.out
OBJS = seed.o test_seed.o mock_secret_store.o log.o
LIBS = -lsecp256k1

all: $(PROG)

$(PROG): $(OBJS)
$(CC) $(COVFLAGS) -o $@ $^ $(LIBS)

.PHONY: clean test
clean:
rm -f $(PROG) *.o $(COVFILES)

test: all
./$(PROG)
Loading

0 comments on commit bc1a6e5

Please sign in to comment.