Skip to content

Commit

Permalink
dded unit tests for communication and onboard modules
Browse files Browse the repository at this point in the history
  • Loading branch information
italo-sampaio committed Nov 17, 2022
1 parent 5d0fa7e commit 623074f
Show file tree
Hide file tree
Showing 9 changed files with 532 additions and 8 deletions.
50 changes: 50 additions & 0 deletions ledger/src/ui/test/communication/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
# 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.

SRCDIR = ../../src
MOCKDIR = ../mock
CFLAGS = -I $(SRCDIR) -I $(MOCKDIR) -I ./

PROG = test.out
OBJS = os.o communication.o test_communication.o

all: $(PROG)

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

test_communication.o: test_communication.c
$(CC) $(CFLAGS) -c -o $@ $^

communication.o: $(SRCDIR)/communication.c
$(CC) $(CFLAGS) -c -o $@ $^

os.o: $(MOCKDIR)/os.c
$(CC) $(CFLAGS) -c -o $@ $^

.PHONY: clean test

clean:
rm -f $(PROG) ./*.o

test: all
./$(PROG)
61 changes: 61 additions & 0 deletions ledger/src/ui/test/communication/test_communication.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/**
* 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 <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <string.h>

#include "apdu.h"
#include "defs.h"
#include "communication.h"
#include "os.h"

void test_echo() {
printf("Test echo...\n");
unsigned int rx = 4;
assert(4 == echo(rx));
}

void test_get_mode() {
printf("Test get mode...\n");
assert(2 == get_mode());
assert(RSK_MODE_BOOTLOADER == APDU_AT(1));
}

void test_get_retries() {
printf("Test get retries...\n");
reset_mock_func_call_list();
assert(3 == get_retries());
assert(0 == APDU_AT(2));
assert(get_mock_func_call(0) == MOCK_FUNC_OS_GLOBAL_PIN_RETRIES);
assert(get_mock_func_call_count() == 1);
}

int main() {
test_echo();
test_get_mode();
test_get_retries();
}
1 change: 1 addition & 0 deletions ledger/src/ui/test/mock/bolos_ux_onboarding_seed_bip39.h
31 changes: 31 additions & 0 deletions ledger/src/ui/test/mock/cx.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/**
* 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 "cx.h"

unsigned char mock_random_buffer[] = "random-buffer\0";

unsigned char *cx_rng(unsigned char *buffer, unsigned int len) {
return mock_random_buffer;
}
28 changes: 28 additions & 0 deletions ledger/src/ui/test/mock/cx.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/**
* 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.
*/

/**
* generate a random buffer
*/
unsigned char *cx_rng(unsigned char *buffer, unsigned int len);
94 changes: 86 additions & 8 deletions ledger/src/ui/test/mock/os.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,38 +25,116 @@
#include "os.h"
#include "string.h"

static mock_func_call_t mock_func_call_list[128];
static size_t mock_func_call_count = 0;

/**
* Mocks pin currently loaded to device
*/
unsigned char current_pin[10];

/**
* APDU buffer
* Helper functions to handle call list
*/
unsigned char G_io_apdu_buffer[IO_APDU_BUFFER_SIZE];
void reset_mock_func_call_list() {
explicit_bzero(mock_func_call_list, sizeof(mock_func_call_list));
mock_func_call_count = 0;
}

unsigned int os_global_pin_check(unsigned char *pin_buffer,
unsigned char pin_length) {
return !strncmp(
(const char *)pin_buffer, (const char *)current_pin, pin_length);
void add_mock_func_call(mock_func_call_t func) {
mock_func_call_list[mock_func_call_count++] = func;
}

mock_func_call_t get_mock_func_call(int order) {
return mock_func_call_list[order];
}

int get_mock_func_call_count() {
return mock_func_call_count;
}

/**
* APDU buffer
*/
unsigned char G_io_apdu_buffer[IO_APDU_BUFFER_SIZE];

void explicit_bzero(void *s, size_t len) {
memset(s, '\0', len);
/* Compiler barrier. */
asm volatile("" ::: "memory");
}

unsigned int os_global_pin_check(unsigned char *pin_buffer,
unsigned char pin_length) {
add_mock_func_call(MOCK_FUNC_OS_GLOBAL_PIN_CHECK);
return !strncmp(
(const char *)pin_buffer, (const char *)current_pin, pin_length);
}

void os_perso_set_pin(unsigned int identity,
unsigned char *pin,
unsigned int length) {
// Do nothing
add_mock_func_call(MOCK_FUNC_OS_PERSO_SET_PIN);
strncpy((char *)current_pin, (char *)pin, length);
}

void os_global_pin_invalidate(void) {
// Do nothing
add_mock_func_call(MOCK_FUNC_OS_GLOBAL_PIN_INVALIDATE);
}

void os_memset(void *dst, unsigned char c, unsigned int length) {
memset(dst, c, length);
}

void mock_set_pin(unsigned char *pin, size_t n) {
memcpy(current_pin, pin, n);
}

void nvm_write(void *dst_adr, void *src_adr, unsigned int src_len) {
add_mock_func_call(MOCK_FUNC_NVM_WRITE);
if (src_adr == NULL) {
// Treat as memory reset
memset(dst_adr, 0, src_len);
} else {
// Treat as normal copy
memmove(dst_adr, src_adr, src_len);
}
}

void os_perso_wipe() {
add_mock_func_call(MOCK_FUNC_OS_PERSO_WIPE);
}

void os_perso_derive_and_set_seed(unsigned char identity,
const char *prefix,
unsigned int prefix_length,
const char *passphrase,
unsigned int passphrase_length,
const char *words,
unsigned int words_length) {
add_mock_func_call(MOCK_FUNC_OS_PERSO_DERIVE_AND_SET_SEED);
}

void os_perso_finalize(void) {
add_mock_func_call(MOCK_FUNC_OS_PERSO_FINALIZE);
}

unsigned int os_perso_isonboarded(void) {
add_mock_func_call(MOCK_FUNC_OS_PERSO_ISONBOARDED);
return 1;
}

unsigned int os_global_pin_retries(void) {
add_mock_func_call(MOCK_FUNC_OS_GLOBAL_PIN_RETRIES);
return 0;
}

unsigned int bolos_ux_mnemonic_from_data(unsigned char *in,
unsigned int inLength,
unsigned char *out,
unsigned int outLength) {
add_mock_func_call(MOCK_FUNC_BOLOS_UX_MNEMONIC_FROM_DATA);
const char mnemonic[] = "the-mnemonics";
strcpy((char *)out, mnemonic);
return strlen(mnemonic);
}
38 changes: 38 additions & 0 deletions ledger/src/ui/test/mock/os.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,19 +23,44 @@
*/

#include <stddef.h>
#include <stdint.h>
#include "cx.h"

/**
* Utility macros
*/
#define UNUSED(x) (void)x
#define THROW(e) return e
#define PIC(x) (x)

typedef enum {
MOCK_FUNC_OS_GLOBAL_PIN_CHECK,
MOCK_FUNC_OS_PERSO_SET_PIN,
MOCK_FUNC_OS_GLOBAL_PIN_INVALIDATE,
MOCK_FUNC_OS_MEMSET,
MOCK_FUNC_NVM_WRITE,
MOCK_FUNC_OS_PERSO_WIPE,
MOCK_FUNC_OS_PERSO_DERIVE_AND_SET_SEED,
MOCK_FUNC_OS_PERSO_FINALIZE,
MOCK_FUNC_OS_PERSO_ISONBOARDED,
MOCK_FUNC_BOLOS_UX_MNEMONIC_FROM_DATA,
MOCK_FUNC_OS_GLOBAL_PIN_RETRIES,
} mock_func_call_t;

/**
* Mock APDU buffer
*/
#define IO_APDU_BUFFER_SIZE 85
extern unsigned char G_io_apdu_buffer[IO_APDU_BUFFER_SIZE];

/**
* Helper functions to handle call list
*/
void reset_mock_func_call_list();
void add_mock_func_call(mock_func_call_t func);
mock_func_call_t get_mock_func_call(int order);
int get_mock_func_call_count();

/**
* Mock calls for os API
*/
Expand All @@ -45,6 +70,19 @@ void os_perso_set_pin(unsigned int identity,
unsigned char *pin,
unsigned int length);
void os_global_pin_invalidate(void);
void os_memset(void *dst, unsigned char c, unsigned int length);
void nvm_write(void *dst_adr, void *src_adr, unsigned int src_len);
void os_perso_wipe();
void os_perso_derive_and_set_seed(unsigned char identity,
const char *prefix,
unsigned int prefix_length,
const char *passphrase,
unsigned int passphrase_length,
const char *words,
unsigned int words_length);
void os_perso_finalize(void);
unsigned int os_perso_isonboarded(void);
unsigned int os_global_pin_retries(void);

/**
* Other mocks
Expand Down
Loading

0 comments on commit 623074f

Please sign in to comment.