tests: Add some C tests for object writing

Prep for https://github.com/ostreedev/ostree/pull/881

Closes: #884
Approved by: jlebon
This commit is contained in:
Colin Walters 2017-05-24 16:43:39 -04:00 committed by Atomic Bot
parent c6960e63b2
commit 822ade62c6

View File

@ -170,6 +170,54 @@ test_raw_file_to_archive_z2_stream (gconstpointer data)
g_assert_cmpint (checks, >, 0);
}
static gboolean hi_content_stream_new (GInputStream **out_stream,
guint64 *out_length,
GError **error)
{
static const char hi[] = "hi";
g_autoptr(GMemoryInputStream) hi_memstream = (GMemoryInputStream*)g_memory_input_stream_new_from_data (hi, sizeof(hi)-1, NULL);
g_autoptr(GFileInfo) finfo = g_file_info_new ();
g_file_info_set_attribute_uint32 (finfo, "standard::type", G_FILE_TYPE_REGULAR);
g_file_info_set_attribute_boolean (finfo, "standard::is-symlink", FALSE);
g_file_info_set_attribute_uint32 (finfo, "unix::uid", 0);
g_file_info_set_attribute_uint32 (finfo, "unix::gid", 0);
g_file_info_set_attribute_uint32 (finfo, "unix::mode", S_IFREG|0644);
return ostree_raw_file_to_content_stream ((GInputStream*)hi_memstream, finfo, NULL, out_stream, out_length, NULL, error);
}
static void
test_object_writes (gconstpointer data)
{
OstreeRepo *repo = OSTREE_REPO (data);
g_autoptr(GError) error = NULL;
static const char hi_sha256[] = "2301b5923720c3edc1f0467addb5c287fd5559e3e0cd1396e7f1edb6b01be9f0";
/* Successful content write */
{ g_autoptr(GInputStream) hi_memstream = NULL;
guint64 len;
hi_content_stream_new (&hi_memstream, &len, &error);
g_assert_no_error (error);
g_autofree guchar *csum = NULL;
(void)ostree_repo_write_content (repo, hi_sha256, hi_memstream, len, &csum,
NULL, &error);
g_assert_no_error (error);
}
/* Invalid content write */
{ g_autoptr(GInputStream) hi_memstream = NULL;
guint64 len;
hi_content_stream_new (&hi_memstream, &len, &error);
g_assert_no_error (error);
g_autofree guchar *csum = NULL;
static const char invalid_hi_sha256[] = "cafebabecafebabecafebabecafebabecafebabecafebabecafebabecafebabe";
g_assert (!ostree_repo_write_content (repo, invalid_hi_sha256, hi_memstream, len, &csum,
NULL, &error));
g_assert (error);
g_assert (strstr (error->message, "Corrupted file object"));
}
}
int main (int argc, char **argv)
{
g_autoptr(GError) error = NULL;
@ -183,6 +231,7 @@ int main (int argc, char **argv)
g_test_add_data_func ("/repo-not-system", repo, test_repo_is_not_system);
g_test_add_data_func ("/raw-file-to-archive-z2-stream", repo, test_raw_file_to_archive_z2_stream);
g_test_add_data_func ("/objectwrites", repo, test_object_writes);
return g_test_run();
out: