mirror of
https://github.com/ZoiteChat/zoitechat.git
synced 2026-03-14 01:30:19 +00:00
fishlim: Implement correct handling of long and UTF-8 messages
This commit is contained in:
@@ -230,6 +230,54 @@ void __base64_cbc_len(void) {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Check the calculation of length limit for a plaintext in each encryption mode
|
||||
*/
|
||||
void __max_text_command_len(void) {
|
||||
int max_encoded_len, plaintext_len;
|
||||
enum fish_mode mode;
|
||||
|
||||
for (max_encoded_len = 0; max_encoded_len < 10000; ++max_encoded_len) {
|
||||
for (mode = FISH_ECB_MODE; mode <= FISH_CBC_MODE; ++mode) {
|
||||
plaintext_len = max_text_command_len(max_encoded_len, mode);
|
||||
g_assert_cmpuint(encoded_len(plaintext_len, mode), <= , max_encoded_len);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Check the calculation of length limit for a plaintext in each encryption mode
|
||||
*/
|
||||
void __foreach_utf8_data_chunks(void) {
|
||||
GRand *rand = NULL;
|
||||
GString *chunks = NULL;
|
||||
int tests, max_chunks_len, chunks_len;
|
||||
char ascii_message[1001];
|
||||
char *data_chunk = NULL;
|
||||
|
||||
rand = g_rand_new();
|
||||
|
||||
for (tests = 0; tests < 1000; ++tests) {
|
||||
|
||||
max_chunks_len = g_rand_int_range(rand, 2, 301);
|
||||
random_string(ascii_message, 1000);
|
||||
|
||||
data_chunk = ascii_message;
|
||||
|
||||
chunks = g_string_new(NULL);
|
||||
|
||||
while (foreach_utf8_data_chunks(data_chunk, max_chunks_len, &chunks_len)) {
|
||||
g_string_append(chunks, g_strndup(data_chunk, chunks_len));
|
||||
/* Next chunk */
|
||||
data_chunk += chunks_len;
|
||||
}
|
||||
/* Check data loss */
|
||||
g_assert_cmpstr(chunks->str, == , ascii_message);
|
||||
g_string_free(chunks, TRUE);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
int main(int argc, char *argv[]) {
|
||||
|
||||
g_test_init(&argc, &argv, NULL);
|
||||
@@ -240,6 +288,8 @@ int main(int argc, char *argv[]) {
|
||||
g_test_add_func("/fishlim/__base64_fish_len", __base64_fish_len);
|
||||
g_test_add_func("/fishlim/__base64_ecb_len", __base64_ecb_len);
|
||||
g_test_add_func("/fishlim/__base64_cbc_len", __base64_cbc_len);
|
||||
g_test_add_func("/fishlim/__max_text_command_len", __max_text_command_len);
|
||||
g_test_add_func("/fishlim/__foreach_utf8_data_chunks", __foreach_utf8_data_chunks);
|
||||
|
||||
return g_test_run();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user