base64: use url encoding with updated api

Ensure that the URL alphabet for base64 is used with the urlencode
functions (urlencode, urlencode_buffer, urlencode_temp and
urlencode_new) are used. Add a new test.
This commit is contained in:
Koni Marti
2024-11-24 23:25:55 +01:00
committed by Christoffer Lerno
parent 8d03aafe72
commit 3f7f7a0aa7
2 changed files with 51 additions and 24 deletions

View File

@@ -31,7 +31,7 @@ fn void encode()
char[64] buf;
b.encode(tc.in, buf[:n])!;
assert(buf[:n] == tc.out);
assert(base64::encode_temp(tc.in) == tc.out);
assert(base64::encode_temp(tc.in)! == tc.out);
};
}
}
@@ -105,3 +105,34 @@ fn void decode_nopadding()
assert(buf[:nn] == tc.out);
}
}
fn void! urlencode() {
TestCase[] tcases = {
{ x"14fb9c03d97e", "FPucA9l-"},
};
@pool()
{
usz n;
char[] got;
char[64] buf;
foreach (t : tcases)
{
Base64Encoder enc;
enc.init(base64::URL_ALPHABET)!;
n = enc.encode(t.in, buf[..])!;
assert (buf[:n] == t.out, "got: %s, want: %s", (String)buf[:n], (String)t.out);
got = base64::urlencode_temp(t.in)!;
assert (got == t.out, "got: %s, want: %s", got, (String)t.out);
Base64Decoder dec;
dec.init(base64::URL_ALPHABET)!;
n = dec.decode(t.out, buf[..])!;
assert (buf[:n] == t.in, "got: %s, want: %s", (String)buf[:n], (String)t.in);
got = base64::urldecode_temp(t.out)!;
assert (got == t.in, "got: %s, want: %s", got, (String)t.in);
}
};
}