Tests and fixes for url_decode

This commit is contained in:
2026-06-17 17:34:07 +02:00
parent 434dca7fe7
commit 0208a7549c
2 changed files with 29 additions and 14 deletions

View File

@@ -33,8 +33,8 @@ static inline char to_char_lower(int value) {
static inline int from_char(char c) { static inline int from_char(char c) {
if (c >= '0' && c <= '9') return c - '0'; if (c >= '0' && c <= '9') return c - '0';
if (c >= 'a' && c <= 'z') return c - 'a'; if (c >= 'a' && c <= 'z') return c - 'a' + 10;
if (c >= 'A' && c <= 'Z') return c - 'A'; if (c >= 'A' && c <= 'Z') return c - 'A' + 10;
return 0xFF; return 0xFF;
} }
@@ -78,12 +78,12 @@ void url_decode(Noh_String *string) {
} }
if (c == '%') { if (c == '%') {
int h1 = string->elems[read]; int h1 = from_char(string->elems[read]);
int h2 = string->elems[read + 1]; int h2 = from_char(string->elems[read + 1]);
// If valid hex chars, add it. // If valid hex chars, add it.
if ((h1 | h2) != 0xFF) { if ((h1 | h2) != 0xFF) {
string->elems[write] = (char)((h1 << 4) | h2); string->elems[write++] = (char)((h1 << 4) | h2);
read += 2; read += 2;
continue; continue;
} }
@@ -96,10 +96,10 @@ void url_decode(Noh_String *string) {
} }
// Zero-out the rest. // Zero-out the rest.
string->count = read; for (size_t i = write; i < string->count; i++) {
for (size_t i = read; i < string->count; i++) {
string->elems[i] = 0; string->elems[i] = 0;
} }
string->count = write;
} }
#define build_path(url, first, ...) build_path_((url), (first), __VA_ARGS__, NULL) #define build_path(url, first, ...) build_path_((url), (first), __VA_ARGS__, NULL)

View File

@@ -3,7 +3,7 @@
#include "path.h" #include "path.h"
#include "noh.h" #include "noh.h"
void check_string(size_t *good, size_t *bad, const char* input, const char *expected) { void check_string(void (*fut)(Noh_String *), size_t *good, size_t *bad, const char* input, const char *expected) {
Noh_String test = noh_string_from_cstr(input); Noh_String test = noh_string_from_cstr(input);
Noh_String_View expected_sv = noh_sv_from_cstr(expected); Noh_String_View expected_sv = noh_sv_from_cstr(expected);
@@ -14,14 +14,14 @@ void check_string(size_t *good, size_t *bad, const char* input, const char *expe
opts->write_lf = true; opts->write_lf = true;
opts->write_prefix = false; opts->write_prefix = false;
url_encode(&test); fut(&test);
bool correct = noh_sv_eq(noh_sv_from_string(test), expected_sv); bool correct = noh_sv_eq(noh_sv_from_string(test), expected_sv);
if (!correct) { if (!correct) {
noh_log(NOH_ERROR, " Incorrect."); noh_log(NOH_ERROR, " Incorrect.");
opts->write_prefix = true; opts->write_prefix = true;
noh_log(NOH_ERROR, "Expected: "Nsv_Fmt, Nsv_Arg(expected_sv)); noh_log(NOH_ERROR, "Expected: \""Nsv_Fmt"\"", Nsv_Arg(expected_sv));
noh_log(NOH_ERROR, "Got : "Nsv_Fmt, Nsv_Arg(test)); noh_log(NOH_ERROR, "Got : \""Nsv_Fmt"\"", Nsv_Arg(test));
(*bad)++; (*bad)++;
} else { } else {
noh_log(NOH_INFO, " Ok."); noh_log(NOH_INFO, " Ok.");
@@ -34,14 +34,29 @@ int main() {
size_t good = 0; size_t good = 0;
size_t bad = 0; size_t bad = 0;
check_string(&good, &bad, noh_log(NOH_INFO, "");
noh_log(NOH_INFO, "url_encode:");
check_string(url_encode, &good, &bad,
"abcdefghijklmnopqrstuvwxyz1023456789ABCDEFGHIJKLMNOPQRSTUVWXYZ()*!-._", "abcdefghijklmnopqrstuvwxyz1023456789ABCDEFGHIJKLMNOPQRSTUVWXYZ()*!-._",
"abcdefghijklmnopqrstuvwxyz1023456789ABCDEFGHIJKLMNOPQRSTUVWXYZ()*!-._"); "abcdefghijklmnopqrstuvwxyz1023456789ABCDEFGHIJKLMNOPQRSTUVWXYZ()*!-._");
check_string(&good, &bad, check_string(url_encode, &good, &bad,
"~!@#$%^&*()_+-=", "~!@#$%^&*()_+-=",
"%7e!%40%23%24%25%5e%26*()_%2b-%3d"); "%7e!%40%23%24%25%5e%26*()_%2b-%3d");
noh_log(NOH_INFO, "");
noh_log(NOH_INFO, "url_decode:");
check_string(url_decode, &good, &bad,
"abcdefghijklmnopqrstuvwxyz1023456789ABCDEFGHIJKLMNOPQRSTUVWXYZ()*!-._",
"abcdefghijklmnopqrstuvwxyz1023456789ABCDEFGHIJKLMNOPQRSTUVWXYZ()*!-._");
check_string(url_decode, &good, &bad,
"%7e!%40%23%24%25%5e%26*()_%2b-%3d",
"~!@#$%^&*()_+-=");
noh_log(NOH_INFO, "");
if (bad == 0) { if (bad == 0) {
noh_log(NOH_INFO, "%zu/%zu tests passed.", good, good); noh_log(NOH_INFO, "%zu/%zu tests passed.", good, good);
return 0; return 0;