mirror of
https://github.com/waku-org/nwaku.git
synced 2025-01-14 00:46:44 +00:00
tests(common): extend base64 test coverage
This commit is contained in:
parent
4865fd4b7b
commit
4066b7490c
@ -2,6 +2,7 @@
|
||||
{.used.}
|
||||
|
||||
import
|
||||
std/strutils,
|
||||
stew/[results, byteutils],
|
||||
testutils/unittests
|
||||
import
|
||||
@ -9,32 +10,49 @@ import
|
||||
|
||||
|
||||
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)":
|
||||
## Given
|
||||
# From RFC 4648 test vectors: https://www.rfc-editor.org/rfc/rfc4648#page-12
|
||||
let data = "fooba"
|
||||
# Custom test vectors
|
||||
("\x01", Base64String("AQ==")),
|
||||
("\x13", Base64String("Ew==")),
|
||||
("\x01\x02\x03\x04", Base64String("AQIDBA=="))
|
||||
]
|
||||
|
||||
## When
|
||||
let encoded = base64.encode(data)
|
||||
for (plaintext, encoded) in TestData:
|
||||
|
||||
## Then
|
||||
check:
|
||||
encoded == Base64String("Zm9vYmE=")
|
||||
test "encode into base64 (" & escape(plaintext) & " -> \"" & string(encoded) & "\")":
|
||||
## Given
|
||||
let data = plaintext
|
||||
|
||||
## When
|
||||
let encodedData = base64.encode(data)
|
||||
|
||||
## Then
|
||||
check:
|
||||
encodedData == encoded
|
||||
|
||||
|
||||
test "decode from base64 (with padding)":
|
||||
## Given
|
||||
# From RFC 4648 test vectors: https://www.rfc-editor.org/rfc/rfc4648#page-12
|
||||
let data = Base64String("Zm9vYg==")
|
||||
test "decode from base64 (\"" & string(encoded) & "\" -> " & escape(plaintext) & ")":
|
||||
## Given
|
||||
let data = encoded
|
||||
|
||||
## When
|
||||
let decodedRes = base64.decode(data)
|
||||
## When
|
||||
let decodedRes = base64.decode(data)
|
||||
|
||||
## Then
|
||||
check:
|
||||
decodedRes.isOk()
|
||||
## Then
|
||||
check:
|
||||
decodedRes.isOk()
|
||||
|
||||
let decoded = decodedRes.tryGet()
|
||||
check:
|
||||
decoded == toBytes(plaintext)
|
||||
|
||||
let decoded = decodedRes.tryGet()
|
||||
check:
|
||||
decoded == toBytes("foob")
|
||||
|
Loading…
x
Reference in New Issue
Block a user