Skip to content

Commit

Permalink
Correct failures in base64
Browse files Browse the repository at this point in the history
  • Loading branch information
tgross35 committed Oct 23, 2023
1 parent 185a497 commit 720f936
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 28 deletions.
2 changes: 0 additions & 2 deletions rcrypto-sys/tests/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,6 @@ build: .check-submodules $(build_paths) $(test_exe_files)
# make all test executables
$(dir_testout)test_%.$(target_extension): $(dir_build)unity.o \
$(dir_build)test_framework.o $(dir_build)test_%.o
pwd
ls -l $(dir_sharedlib)
$(link) -o $@ $^ $(ldflags)

# make all test object files
Expand Down
44 changes: 18 additions & 26 deletions rcrypto-sys/tests/b64.c
Original file line number Diff line number Diff line change
@@ -1,49 +1,41 @@
#include "test_framework.h"

#define OUTLEN 100

void b64_runner(RcB64Variant var, char *expected, char *name) {
uchar msg[] = "Hello, world!";
uchar out1[OUTLEN];
uchar out2[OUTLEN];
size_t newlen;

int msglen = strlen((char*)msg);
int explen = strlen(expected);

memset(out1, 0, OUTLEN);
memset(out2, 0, OUTLEN);
const uchar msg[] = "Hello, world! <<?_?>> x";
const int msglen = strlen((char*)msg);
const int explen = strlen(expected);

int outlen = rc_base64_encoded_len(RcB64Original, strlen((char*)msg));
uchar out_enc[explen];
uchar out_dec[msglen];
size_t newlen = 0;

const int outlen = rc_base64_encoded_len(var, strlen((char*)msg));
TEST_ASSERT_EQUAL_INT_MESSAGE(explen, outlen, name);
rc_base64_encode_ct(var, msg, msglen, out1, OUTLEN, &newlen);

rc_base64_encode_ct(var, msg, msglen, out_enc, explen, &newlen);
TEST_ASSERT_EQUAL_INT_MESSAGE(explen, newlen, name);
TEST_ASSERT_EQUAL_STRING_MESSAGE(expected, out1, name);
printf("m %s\n", out1);
TEST_ASSERT_EQUAL_MEMORY_MESSAGE(expected, out_enc, newlen, name);

rc_base64_decode_ct(var, out1, outlen, out2, OUTLEN, &newlen);
newlen = 0;
rc_base64_decode_ct(var, out_enc, outlen, out_dec, msglen, &newlen);
TEST_ASSERT_EQUAL_INT_MESSAGE(msglen, newlen, name);
TEST_ASSERT_EQUAL_STRING_MESSAGE(msg, out2, name);

memset(out2 + newlen, 0, 20);
printf("m %s\n", out2);
TEST_ASSERT_EQUAL_MEMORY_MESSAGE(msg, out_dec, newlen, name);
}

void test_b64_original() {
b64_runner(RcB64Original, "SGVsbG8sIHdvcmxkIQ==", "b64 original");
b64_runner(RcB64Original, "SGVsbG8sIHdvcmxkISA8PD9fPz4+IHg=", "b64 original");
}

void test_b64_original_nopad() {
b64_runner(RcB64OriginalUnpadded, "SGVsbG8sIHdvcmxkIQ", "b64 original nopad");
b64_runner(RcB64OriginalUnpadded, "SGVsbG8sIHdvcmxkISA8PD9fPz4+IHg", "b64 original nopad");
}

void test_b64_url() {
b64_runner(RcB64Url, "SGVsbG8sIHdvcmxkIQ==", "b64 url");
b64_runner(RcB64Url, "SGVsbG8sIHdvcmxkISA8PD9fPz4-IHg=", "b64 url");
}

void test_b64_url_nopad() {
b64_runner(RcB64UrlUnpadded, "SGVsbG8sIHdvcmxkIQ", "b64 url nopad");
b64_runner(RcB64UrlUnpadded, "SGVsbG8sIHdvcmxkISA8PD9fPz4-IHg", "b64 url nopad");
}

int main() {
Expand Down

0 comments on commit 720f936

Please sign in to comment.