Skip to content

Commit

Permalink
suppressable warning for base64dec()
Browse files Browse the repository at this point in the history
  • Loading branch information
shikokuchuo committed Sep 30, 2024
1 parent a31fb1f commit 8fc80e3
Show file tree
Hide file tree
Showing 5 changed files with 11 additions and 6 deletions.
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# secretbase 1.0.2.9000 (development)

* `base64dec()` now emits a suppressable warning when failing to convert back to a character string.

# secretbase 1.0.2

* Improves hash performance in most situations, especially for large files, by optimizing buffer sizes.
Expand Down
3 changes: 2 additions & 1 deletion R/base.R
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,8 @@ base64enc <- function(x, convert = TRUE) .Call(secretbase_base64enc, x, convert)
#' unserialize back to the original object.
#'
#' @return A character string, raw vector, or other object depending on the
#' value of \sQuote{convert}.
#' value of \sQuote{convert}. If conversion to a character string fails,
#' a raw vector will be returned instead (along with a warning).
#'
#' @details The value of \sQuote{convert} should be set to TRUE, FALSE or NA to
#' be the reverse of the 3 encoding operations (for strings, raw vectors and
Expand Down
3 changes: 2 additions & 1 deletion man/base64dec.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion src/base.c
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,7 @@ static SEXP rawToChar(const unsigned char *buf, const size_t sz) {
int i, j;
for (i = 0, j = -1; i < sz; i++) if (buf[i]) j = i; else break;
if (sz - i > 1) {
REprintf("data could not be converted to a character string\n");
Rf_warningcall_immediate(R_NilValue, "data could not be converted to a character string");
out = Rf_allocVector(RAWSXP, sz);
memcpy(SB_DATAPTR(out), buf, sz);
return out;
Expand Down
7 changes: 4 additions & 3 deletions tests/tests.R
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
# minitest - a minimal testing framework ---------------------------------------
# minitest - a minimal testing framework v0.0.1 --------------------------------
test_library <- function(package) library(package = package, character.only = TRUE)
test_type <- function(type, x) invisible(typeof(x) == type || {stop("object of type '", typeof(x), "' was returned instead of '", type, "'")})
test_equal <- function(a, b) invisible(a == b || {print(a); print(b); stop("the above expressions were not equal")})
test_error <- function(x, containing = "") inherits(x <- tryCatch(x, error = identity), "error") && grepl(containing, x[["message"]], fixed = TRUE) || stop("expected error message containing '", containing, "' was not generated")
test_error <- function(x, containing = "") invisible(inherits(x <- tryCatch(x, error = identity), "error") && grepl(containing, x[["message"]], fixed = TRUE) || stop("expected error message containing '", containing, "' was not generated"))
# ------------------------------------------------------------------------------

test_library("secretbase")
# Known SHA hashes from NIST:
test_equal(sha3("", 224), "6b4e03423667dbb73b6e15454f0eb1abd4597f9a1b078e3f5b5a6bc7")
Expand Down Expand Up @@ -126,7 +127,7 @@ test_type("character", base64enc(c("secret", "base")))
test_type("raw", base64enc(data.frame(), convert = FALSE))
test_type("raw", base64dec(base64enc(as.raw(c(1L, 2L)), convert = FALSE), convert = FALSE))
test_type("integer", base64dec(base64enc(c(1L, 2L)), convert = NA))
test_type("raw", base64dec(base64enc(data.frame())))
test_type("raw", suppressWarnings(base64dec(base64enc(data.frame()))))
test_error(base64enc("", convert = 0), "'convert' must be a logical value")
test_error(base64dec("", convert = 1L), "'convert' must be a logical value")
test_error(base64dec("__"), "input is not valid base64")
Expand Down

0 comments on commit 8fc80e3

Please sign in to comment.