tests(common): extend base64 test coverage

This commit is contained in:
Lorenzo Delgado 2023-03-01 10:28:55 +01:00 committed by GitHub
parent 4865fd4b7b
commit 4066b7490c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -2,6 +2,7 @@
{.used.} {.used.}
import import
std/strutils,
stew/[results, byteutils], stew/[results, byteutils],
testutils/unittests testutils/unittests
import import
@ -9,32 +10,49 @@ import
suite "Waku Common - stew base64 wrapper": suite "Waku Common - stew base64 wrapper":
const TestData = @[
# Test vectors from RFC 4648
# See: https://datatracker.ietf.org/doc/html/rfc4648#section-10
("", Base64String("")),
("f", Base64String("Zg==")),
("fo", Base64String("Zm8=")),
("foo", Base64String("Zm9v")),
("foob", Base64String("Zm9vYg==")),
("fooba", Base64String("Zm9vYmE=")),
("foobar", Base64String("Zm9vYmFy")),
test "encode into base64 (with padding)": # Custom test vectors
## Given ("\x01", Base64String("AQ==")),
# From RFC 4648 test vectors: https://www.rfc-editor.org/rfc/rfc4648#page-12 ("\x13", Base64String("Ew==")),
let data = "fooba" ("\x01\x02\x03\x04", Base64String("AQIDBA=="))
]
## When for (plaintext, encoded) in TestData:
let encoded = base64.encode(data)
## Then test "encode into base64 (" & escape(plaintext) & " -> \"" & string(encoded) & "\")":
check: ## Given
encoded == Base64String("Zm9vYmE=") let data = plaintext
## When
let encodedData = base64.encode(data)
## Then
check:
encodedData == encoded
test "decode from base64 (with padding)": test "decode from base64 (\"" & string(encoded) & "\" -> " & escape(plaintext) & ")":
## Given ## Given
# From RFC 4648 test vectors: https://www.rfc-editor.org/rfc/rfc4648#page-12 let data = encoded
let data = Base64String("Zm9vYg==")
## When ## When
let decodedRes = base64.decode(data) let decodedRes = base64.decode(data)
## Then ## Then
check: check:
decodedRes.isOk() decodedRes.isOk()
let decoded = decodedRes.tryGet()
check:
decoded == toBytes(plaintext)
let decoded = decodedRes.tryGet()
check:
decoded == toBytes("foob")