Skip to content

Commit

Permalink
fix new line difference in Windows
Browse files Browse the repository at this point in the history
Signed-off-by: Valerio Setti <[email protected]>
  • Loading branch information
valeriosetti committed Apr 24, 2023
1 parent f1477da commit 3b608de
Showing 1 changed file with 32 additions and 0 deletions.
32 changes: 32 additions & 0 deletions tests/suites/test_suite_pkwrite.function
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,25 @@ typedef enum {
TEST_DER
} pkwrite_file_format_t;

/* Helper function for removing "\r" chars from a buffer. This i */
static void fix_new_lines(unsigned char *in_str, size_t *len)
{
size_t chars_left;
unsigned int i;

for (i = 0; (i < *len) && (*len > 0); i++) {
if (in_str[i] == '\r') {
if (i < (*len - 1)) {
chars_left = *len - i - 1;
memcpy(&in_str[i], &in_str[i+1], chars_left);
} else {
in_str[i] = '\0';
}
*len = *len - 1;
}
}
}

static void pk_write_check_common(char *key_file, int is_public_key, int is_der)
{
mbedtls_pk_context key;
Expand All @@ -22,6 +41,19 @@ static void pk_write_check_common(char *key_file, int is_public_key, int is_der)
TEST_EQUAL(mbedtls_pk_load_file(key_file, &check_buf, &check_buf_len), 0);
TEST_ASSERT(check_buf_len > 0);

/* Windows' line ending is different from the Linux's one ("\r\n" vs "\n").
* Git treats PEM files as text, so when on Windows, it replaces new lines
* with "\r\n" on checkout.
* Unfortunately mbedtls_pk_load_file() loads files in binary format,
* while mbedtls_pk_write_pubkey_pem() goes through the I/O layer which
* uses "\n" for newlines in both Windows and Linux.
* Here we remove the extra "\r" so that "buf" and "check_buf" can be
* easily compared later. */
if (!is_der) {
fix_new_lines(check_buf, &check_buf_len);
}
TEST_ASSERT(check_buf_len > 0);

ASSERT_ALLOC(buf, check_buf_len);

mbedtls_pk_init(&key);
Expand Down

0 comments on commit 3b608de

Please sign in to comment.