tests/testthat/test-injection_impossible.R

test_that("code injection fails", {

  # Attempt to Inject if Assertion passes
  expect_error(assert_character("{stop('throw this error')}"), regexp = NA)
  expect_error(assert_character("stop('throw this error')"), regexp = NA)

  expect_message(assert_character("{message('output this message')}"), regexp = NA)
  expect_message(assert_character("message('output this message')"), regexp = NA)

  # Attempt to Inject if Assertion Fails
  expect_error(assert_number("{stop('throw this error')}"), regexp = 'is not a number')
  expect_error(assert_number("stop('throw this error')"), regexp = 'is not a number')

  expect_message(try(silent = TRUE, assert_number("{message('output this message')}")), regexp = NA)
  expect_message(try(silent = TRUE, assert_number("message('output this message')")), regexp = NA)
})

Try the assertions package in your browser

Any scripts or data that you put into this service are public.

assertions documentation built on June 7, 2023, 6:03 p.m.