aboutsummaryrefslogtreecommitdiffstats
path: root/libtests/qutil.cc
diff options
context:
space:
mode:
authorJay Berkenbilt <ejb@ql.org>2019-08-31 17:00:53 +0200
committerJay Berkenbilt <ejb@ql.org>2019-08-31 21:51:04 +0200
commit4fa7b1eb606ecd749409a907ea7a47b39d48cb7b (patch)
treeb3d6eaa0e8b62e7edc0e39cfa0d56320f8daa4b7 /libtests/qutil.cc
parentcd2bd66781b13c7afef8c0111008860e6cb94ad7 (diff)
downloadqpdf-4fa7b1eb606ecd749409a907ea7a47b39d48cb7b.tar.zst
Add remove_file and rename_file to QUtil
Diffstat (limited to 'libtests/qutil.cc')
-rw-r--r--libtests/qutil.cc53
1 files changed, 53 insertions, 0 deletions
diff --git a/libtests/qutil.cc b/libtests/qutil.cc
index ed605a1d..3c87cd31 100644
--- a/libtests/qutil.cc
+++ b/libtests/qutil.cc
@@ -457,6 +457,57 @@ void hex_encode_decode_test()
std::cout << "end hex encode/decode\n";
}
+static void assert_no_file(char const* filename)
+{
+ try
+ {
+ fclose(QUtil::safe_fopen(filename, "r"));
+ assert(false);
+ }
+ catch (QPDFSystemError&)
+ {
+ }
+}
+
+void rename_delete_test()
+{
+ PointerHolder<char> buf;
+ size_t size = 0;
+
+ try
+ {
+ QUtil::remove_file("old\xcf\x80");
+ }
+ catch (QPDFSystemError&)
+ {
+ }
+ assert_no_file("old\xcf\x80");
+ std::cout << "create file" << std::endl;;
+ FILE* f1 = QUtil::safe_fopen("old\xcf\x80", "w");
+ fprintf(f1, "one");
+ fclose(f1);
+ QUtil::read_file_into_memory("old\xcf\x80", buf, size);
+ assert(memcmp(buf.getPointer(), "one", 3) == 0);
+ std::cout << "rename file" << std::endl;;
+ QUtil::rename_file("old\xcf\x80", "old\xcf\x80.~tmp");
+ QUtil::read_file_into_memory("old\xcf\x80.~tmp", buf, size);
+ assert(memcmp(buf.getPointer(), "one", 3) == 0);
+ assert_no_file("old\xcf\x80");
+ std::cout << "create file" << std::endl;;
+ f1 = QUtil::safe_fopen("old\xcf\x80", "w");
+ fprintf(f1, "two");
+ fclose(f1);
+ std::cout << "rename over existing" << std::endl;;
+ QUtil::rename_file("old\xcf\x80", "old\xcf\x80.~tmp");
+ QUtil::read_file_into_memory("old\xcf\x80.~tmp", buf, size);
+ assert(memcmp(buf.getPointer(), "two", 3) == 0);
+ assert_no_file("old\xcf\x80");
+ std::cout << "delete file" << std::endl;;
+ QUtil::remove_file("old\xcf\x80.~tmp");
+ assert_no_file("old\xcf\x80");
+ assert_no_file("old\xcf\x80.~tmp");
+}
+
int main(int argc, char* argv[])
{
try
@@ -485,6 +536,8 @@ int main(int argc, char* argv[])
read_from_file_test();
std::cout << "---- hex encode/decode" << std::endl;
hex_encode_decode_test();
+ std::cout << "---- rename/delete" << std::endl;
+ rename_delete_test();
}
catch (std::exception& e)
{