From 483701ad02666feaf4c98ca7695db8fc5f066a8c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bruno=20Gon=C3=A7alves?= Date: Sun, 15 Oct 2023 23:22:40 +0200 Subject: [PATCH] Add new tests --- tests/testthat/test-envelope_read.R | 12 ++++++++++++ tests/testthat/test-envelope_write.R | 28 ++++++++++++++++++++++++++++ 2 files changed, 40 insertions(+) create mode 100644 tests/testthat/test-envelope_read.R create mode 100644 tests/testthat/test-envelope_write.R diff --git a/tests/testthat/test-envelope_read.R b/tests/testthat/test-envelope_read.R new file mode 100644 index 0000000..4fb226e --- /dev/null +++ b/tests/testthat/test-envelope_read.R @@ -0,0 +1,12 @@ +test_that("Envelope reading works", { + + key <- keygen_kyber() + secret_message <- "Hello world!!" + env <- envelope_create(secret_message, key$public) + fn <- write_envelope(env) + + retrieved_envelope <- read_envelope(fn) + expect_identical(env, retrieved_envelope) + + expect_error(read_envelope("/not_a_valid/file"), "Invalid 'file_name'") +}) diff --git a/tests/testthat/test-envelope_write.R b/tests/testthat/test-envelope_write.R new file mode 100644 index 0000000..cb785d8 --- /dev/null +++ b/tests/testthat/test-envelope_write.R @@ -0,0 +1,28 @@ +test_that("Envelope writing works", { + + key <- keygen_kyber() + secret_message <- "Hello world!!" + env <- envelope_create(secret_message, key$public) + dest <- tempfile() + + fn <- write_envelope(env, dest) + expect_equal(fn, dest) + expect_true(file.exists(fn)) + + fn <- write_envelope(env) + expect_true(file.exists(fn)) + + expect_error(write_envelope("envelope"), + "'envelope' parameter does not have the expected class") + expect_error(write_envelope(key), + "'envelope' parameter does not have the expected class") + + if (Sys.info()[1] == "Linux") { + dest <- "/home/file.env" # Shouldn't be able to write into root's home dir + } else if ((Sys.info()[1] == "Windows")) { + dest <- "c:/Windows/system32/file.env" # Shouldn't be able to write into system dir + } else { + skip("Unknown OS") + } + expect_error(write_envelope(env, dest), "Permission denied") +})