bool
p11_save_write_and_finish (p11_save_file *file,
const void *data,
- size_t length)
+ ssize_t length)
{
bool ret;
bool
p11_save_write (p11_save_file *file,
const void *data,
- size_t length)
+ ssize_t length)
{
const unsigned char *buf = data;
ssize_t written = 0;
if (!file)
return false;
+ /* Automatically calculate length */
+ if (length < 0) {
+ if (!data)
+ return true;
+ length = strlen (data);
+ }
+
while (written < length) {
res = write (file->fd, buf + written, length - written);
if (res <= 0) {
teardown (tc);
}
+static void
+test_file_auto_empty (CuTest *tc)
+{
+ p11_save_file *file;
+ char *filename;
+ bool ret;
+
+ setup (tc);
+
+ if (asprintf (&filename, "%s/%s", test.directory, "extract-file") < 0)
+ CuFail (tc, "asprintf() failed");
+
+ file = p11_save_open_file (filename, 0);
+ CuAssertPtrNotNull (tc, file);
+
+ ret = p11_save_write_and_finish (file, NULL, -1);
+ CuAssertIntEquals (tc, true, ret);
+ free (filename);
+
+ test_check_file (tc, test.directory, "extract-file", SRCDIR "/files/empty-file");
+
+ teardown (tc);
+}
+
+static void
+test_file_auto_length (CuTest *tc)
+{
+ p11_save_file *file;
+ char *filename;
+ bool ret;
+
+ setup (tc);
+
+ if (asprintf (&filename, "%s/%s", test.directory, "extract-file") < 0)
+ CuFail (tc, "asprintf() failed");
+
+ file = p11_save_open_file (filename, 0);
+ CuAssertPtrNotNull (tc, file);
+
+ ret = p11_save_write_and_finish (file, "The simple string is hairy", -1);
+ CuAssertIntEquals (tc, true, ret);
+ free (filename);
+
+ test_check_file (tc, test.directory, "extract-file", SRCDIR "/files/simple-string");
+
+ teardown (tc);
+}
+
static void
test_write_with_null (CuTest *tc)
{
SUITE_ADD_TEST (suite, test_file_exists);
SUITE_ADD_TEST (suite, test_file_bad_directory);
SUITE_ADD_TEST (suite, test_file_overwrite);
+ SUITE_ADD_TEST (suite, test_file_auto_empty);
+ SUITE_ADD_TEST (suite, test_file_auto_length);
SUITE_ADD_TEST (suite, test_write_with_null);
SUITE_ADD_TEST (suite, test_write_and_finish_with_null);
SUITE_ADD_TEST (suite, test_file_abort);