From 835a8a1d33a08431da3e8487af459fd4a9dc93c0 Mon Sep 17 00:00:00 2001 From: asi345 Date: Thu, 8 Feb 2024 10:58:09 +0100 Subject: [PATCH] simulator: make simulator the server side in socket connection In the first implementation of socket connection in simulator, it was acting as a client in the communication, trying to connect to a specific network host and port. It was done like this because `send_message.py` running in host machine was having problems connecting to a simulator But actually, now that we do not need to connect BitBox02 device physically while running the simulator, we also do not need to run `send_message.py` in the host machine. Also ideally simulator should be a server hosting the firmware to the clients. Signed-off-by: asi345 --- py/send_message.py | 16 ++++----- test/simulator/CMakeLists.txt | 8 +---- test/simulator/framework/eh_personality.c | 15 ++------ .../framework/includes/mock_qtouch.h | 6 ---- .../framework/includes/test_random.h | 25 ------------- test/simulator/framework/mock_memory.c | 35 +------------------ test/simulator/simulator.c | 34 ++++++++++-------- test/unit-test/CMakeLists.txt | 1 - 8 files changed, 30 insertions(+), 110 deletions(-) delete mode 100644 test/simulator/framework/includes/test_random.h diff --git a/py/send_message.py b/py/send_message.py index 47fa429ce9..b7a9c8d49d 100755 --- a/py/send_message.py +++ b/py/send_message.py @@ -1573,28 +1573,24 @@ class Simulator(PhysicalLayer): def __init__(self) -> None: self.client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) port = 15423 - self.client_socket.bind(("", port)) - self.client_socket.listen(50) - print(f"Waiting for connection on port {port}") - self.connection, addr = self.client_socket.accept() - print(f"Connected to {addr}") + self.client_socket.connect(("127.0.0.1", port)) + if debug: + print("Connected to the simulator") def write(self, data: bytes) -> None: - self.connection.send(data[1:]) + self.client_socket.send(data[1:]) if debug: print(f"Written to the simulator:\n{data.hex()[2:]}") def read(self, size: int, timeout_ms: int) -> bytes: - res = self.connection.recv(64) + res = self.client_socket.recv(64) if debug: print(f"Read from the simulator:\n{res.hex()}") return res def __del__(self) -> None: print("Simulator quit") - if self.connection: - self.connection.shutdown(socket.SHUT_RDWR) - self.connection.close() + self.client_socket.close() simulator = Simulator() diff --git a/test/simulator/CMakeLists.txt b/test/simulator/CMakeLists.txt index 9d9e60c28b..d245e70425 100644 --- a/test/simulator/CMakeLists.txt +++ b/test/simulator/CMakeLists.txt @@ -1,4 +1,4 @@ -# Copyright 2018 Shift Cryptosecurity AG +# Copyright 2024 Shift Cryptosecurity AG # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. @@ -31,7 +31,6 @@ set(IGNORE_SOURCES "src/memory/smarteeprom.c" "src/memory.mpu.c" ) -#f9b3ceaa91e # Exclude some files which depends on the hardware. foreach(SOURCEFILE ${IGNORE_SOURCES}) @@ -169,11 +168,6 @@ if(SANTIZE_UNDEFINED) target_compile_options(bitbox_objects-simulator PUBLIC "-fsanitize=undefined") target_compile_options(bitbox-simulator PUBLIC "-fsanitize=undefined") endif() -if(COVERAGE) - target_link_libraries(bitbox-simulator PUBLIC "--coverage") - target_compile_options(bitbox_objects-simulator PUBLIC "--coverage") - target_compile_options(bitbox-simulator PUBLIC "--coverage") -endif() #----------------------------------------------------------------------------- # Simulator diff --git a/test/simulator/framework/eh_personality.c b/test/simulator/framework/eh_personality.c index f48a526b09..04b45c9abf 100644 --- a/test/simulator/framework/eh_personality.c +++ b/test/simulator/framework/eh_personality.c @@ -12,17 +12,8 @@ // See the License for the specific language governing permissions and // limitations under the License. -// Needed to link the C unit test executables in /test/unit-test, which link to bitbox_merged. -// `rust_eh_personality` is provided by Rust when building the firmware or running Rust unit tests -// -// See -// https://doc.rust-lang.org/unstable-book/language-features/lang-items.html#writing-an-executable-without-stdlib. -// -// One could get rid of this and also considerably shrink the binary size by compiling core instead -// of using pre-built binaries. See a proof of concept implementation here: -// https://github.com/digitalbitbox/bitbox02-firmware/tree/build-std-PoC. We decided against doing -// this for now as the feature seems immature and because of the warnings against using it in -// production: -// https://github.com/rust-lang/wg-cargo-std-aware/tree/81765f0eb744b9c47840c16f43a32c9f61fd7f0c#mvp-implementation +// Needed to link the simulator executable in /test/simulator, which link to +// bitbox_merged-simulator. `rust_eh_personality` is provided by Rust when building the firmware or +// running Rust unit tests. void rust_eh_personality(void); void rust_eh_personality(void) {} diff --git a/test/simulator/framework/includes/mock_qtouch.h b/test/simulator/framework/includes/mock_qtouch.h index eff3c105e0..e012a2e6a2 100644 --- a/test/simulator/framework/includes/mock_qtouch.h +++ b/test/simulator/framework/includes/mock_qtouch.h @@ -17,12 +17,6 @@ #include -void __wrap_qtouch_process(void); - -uint8_t __wrap_qtouch_get_scroller_is_active(uint16_t sensor_node); - -uint16_t __wrap_qtouch_get_scroller_position(uint16_t sensor_node); - void qtouch_process(void); uint8_t qtouch_is_scroller_active(uint16_t sensor_node); diff --git a/test/simulator/framework/includes/test_random.h b/test/simulator/framework/includes/test_random.h deleted file mode 100644 index f158a536af..0000000000 --- a/test/simulator/framework/includes/test_random.h +++ /dev/null @@ -1,25 +0,0 @@ -// Copyright 2019 Shift Cryptosecurity AG -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -#ifndef _TEST_RANDOM_H -#define _TEST_RANDOM_H - -int __wrap_rand(void); -int __wrap_wally_sha256( - const unsigned char* bytes, - size_t bytes_len, - unsigned char* bytes_out, - size_t len); - -#endif diff --git a/test/simulator/framework/mock_memory.c b/test/simulator/framework/mock_memory.c index 5ccc2771af..63e14bc4c0 100644 --- a/test/simulator/framework/mock_memory.c +++ b/test/simulator/framework/mock_memory.c @@ -71,37 +71,4 @@ void memory_read_chunk_mock(uint32_t chunk_num, uint8_t* chunk_out) void memory_read_shared_bootdata_mock(uint8_t* chunk_out) { memcpy(chunk_out, _memory_shared_data, FLASH_SHARED_DATA_LEN); -} - -static uint8_t _encrypted_seed_and_hmac[96]; -static uint8_t _encrypted_seed_and_hmac_len; - -bool __wrap_memory_set_encrypted_seed_and_hmac(uint8_t* encrypted_seed_and_hmac, uint8_t len) -{ - memcpy(_encrypted_seed_and_hmac, encrypted_seed_and_hmac, len); - _encrypted_seed_and_hmac_len = len; - return true; -} - -bool __wrap_memory_get_encrypted_seed_and_hmac( - uint8_t* encrypted_seed_and_hmac_out, - uint8_t* len_out) -{ - *len_out = _encrypted_seed_and_hmac_len; - memcpy(encrypted_seed_and_hmac_out, _encrypted_seed_and_hmac, *len_out); - return true; -} - -static uint8_t _salt_root[32] = { - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, - 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, -}; -void mock_memory_set_salt_root(const uint8_t* salt_root) -{ - memcpy(_salt_root, salt_root, 32); -} -bool __wrap_memory_get_salt_root(uint8_t* salt_root_out) -{ - memcpy(salt_root_out, _salt_root, 32); - return true; -} +} \ No newline at end of file diff --git a/test/simulator/simulator.c b/test/simulator/simulator.c index 0ea3c1c74e..368c6d4b82 100644 --- a/test/simulator/simulator.c +++ b/test/simulator/simulator.c @@ -35,11 +35,11 @@ #define BUFFER_SIZE 1024 int data_len; -int sockfd; +int commfd; int get_usb_message_socket(uint8_t* input) { - return read(sockfd, input, USB_HID_REPORT_OUT_SIZE); + return read(commfd, input, USB_HID_REPORT_OUT_SIZE); } void send_usb_message_socket(void) @@ -48,7 +48,7 @@ void send_usb_message_socket(void) const uint8_t* data = queue_pull(q); if (data != NULL) { data_len = 256 * (int)data[5] + (int)data[6]; - if (!write(sockfd, data, USB_HID_REPORT_OUT_SIZE)) { + if (!write(commfd, data, USB_HID_REPORT_OUT_SIZE)) { perror("ERROR, could not write to socket"); exit(1); } @@ -67,27 +67,31 @@ int main(void) { // Establish socket connection with client int portno = 15423; - sockfd = socket(AF_INET, SOCK_STREAM, 0); + int sockfd = socket(AF_INET, SOCK_STREAM, 0); if (sockfd < 0) { perror("ERROR opening socket"); return 1; } - struct hostent* server = gethostbyname("host.docker.internal"); - if (server == NULL) { - fprintf(stderr, "ERROR, no such host\n"); - return 1; - } struct sockaddr_in serv_addr; - memset((char*)&serv_addr, 0, sizeof(serv_addr)); serv_addr.sin_family = AF_INET; - memcpy((char*)&serv_addr.sin_addr.s_addr, (char*)server->h_addr_list[0], server->h_length); + serv_addr.sin_addr.s_addr = INADDR_ANY; serv_addr.sin_port = htons(portno); - if (connect(sockfd, (struct sockaddr*)&serv_addr, sizeof(serv_addr)) < 0) { - perror("ERROR, could not connect to client"); + int serv_addr_len = sizeof(serv_addr); + if (bind(sockfd, (struct sockaddr*)&serv_addr, serv_addr_len) < 0) { + perror("ERROR binding socket"); + return 1; + } + if (listen(sockfd, 50) < 0) { + perror("ERROR listening on socket"); + return 1; + } + if ((commfd = accept(sockfd, (struct sockaddr*)&serv_addr, (socklen_t*)&serv_addr_len)) < 0) { + perror("accept"); return 1; } + printf("Socket connection setup success\n"); - // BitBox02 simulation initializaition + // BitBox02 simulation initialization usb_processing_init(); usb_processing_set_send(usb_processing_hww(), send_usb_message_socket); printf("USB setup success\n"); @@ -137,6 +141,6 @@ int main(void) temp_len -= (USB_HID_REPORT_OUT_SIZE - 5); } } - close(sockfd); + close(commfd); return 0; } \ No newline at end of file diff --git a/test/unit-test/CMakeLists.txt b/test/unit-test/CMakeLists.txt index 6d6e2598ba..2c698bc8ba 100644 --- a/test/unit-test/CMakeLists.txt +++ b/test/unit-test/CMakeLists.txt @@ -31,7 +31,6 @@ set(IGNORE_SOURCES "src/memory/smarteeprom.c" "src/memory.mpu.c" ) -#f9b3ceaa91e # Exclude some files which depends on the hardware. foreach(SOURCEFILE ${IGNORE_SOURCES})