Skip the writable test if the file is not writable
[platform/upstream/glib.git] / glib / tests / mappedfile.c
1 #include <config.h>
2 #include <glib.h>
3 #include <string.h>
4 #ifdef HAVE_UNISTD_H
5 #include <unistd.h>
6 #endif
7
8 static void
9 test_empty (void)
10 {
11   GMappedFile *file;
12   GError *error;
13
14   error = NULL;
15   file = g_mapped_file_new (SRCDIR "/empty", FALSE, &error);
16   g_assert_no_error (error);
17
18   g_assert (g_mapped_file_get_contents (file) == NULL);
19
20   g_mapped_file_free (file);
21 }
22
23 static void
24 test_nonexisting (void)
25 {
26   GMappedFile *file;
27   GError *error;
28
29   error = NULL;
30   file = g_mapped_file_new ("no-such-file", FALSE, &error);
31   g_assert_error (error, G_FILE_ERROR, G_FILE_ERROR_NOENT);
32   g_clear_error (&error);
33   g_assert (file == NULL);
34 }
35
36 static void
37 test_writable (void)
38 {
39   GMappedFile *file;
40   GError *error;
41   gchar *contents;
42   const gchar *old = "MMMMMMMMMMMMMMMMMMMMMMMMM";
43   const gchar *new = "abcdefghijklmnopqrstuvxyz";
44
45   if (access (SRCDIR "/4096-random-bytes", W_OK) != 0)
46     {
47       g_test_message ("Skipping writable mapping test");
48       return;
49     }
50
51   error = NULL;
52   file = g_mapped_file_new (SRCDIR "/4096-random-bytes", TRUE, &error);
53   g_assert_no_error (error);
54
55   contents = g_mapped_file_get_contents (file);
56   g_assert (strncmp (contents, old, strlen (old)) == 0);
57
58   memcpy (contents, new, strlen (new));
59   g_assert (strncmp (contents, new, strlen (new)) == 0);
60
61   g_mapped_file_free (file);
62
63   error = NULL;
64   file = g_mapped_file_new (SRCDIR "/4096-random-bytes", TRUE, &error);
65   g_assert_no_error (error);
66
67   contents = g_mapped_file_get_contents (file);
68   g_assert (strncmp (contents, old, strlen (old)) == 0);
69
70   g_mapped_file_free (file);
71 }
72
73 int
74 main (int argc, char *argv[])
75 {
76   g_test_init (&argc, &argv, NULL);
77
78   g_test_add_func ("/mappedfile/empty", test_empty);
79   g_test_add_func ("/mappedfile/nonexisting", test_nonexisting);
80   g_test_add_func ("/mappedfile/writable", test_writable);
81
82   return g_test_run ();
83 }