GMappedFile: Add API to create from an existing file descriptor
[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 #include <sys/stat.h>
8 #include <sys/types.h>
9 #include <fcntl.h>
10
11 static void
12 test_basic (void)
13 {
14   GMappedFile *file;
15   GError *error;
16
17   error = NULL;
18   file = g_mapped_file_new (SRCDIR "/empty", FALSE, &error);
19   g_assert_no_error (error);
20
21   g_mapped_file_ref (file);
22   g_mapped_file_unref (file);
23
24   g_mapped_file_unref (file);
25 }
26
27 static void
28 test_empty (void)
29 {
30   GMappedFile *file;
31   GError *error;
32
33   error = NULL;
34   file = g_mapped_file_new (SRCDIR "/empty", FALSE, &error);
35   g_assert_no_error (error);
36
37   g_assert (g_mapped_file_get_contents (file) == NULL);
38
39   g_mapped_file_free (file);
40 }
41
42 static void
43 test_device (void)
44 {
45   GError *error = NULL;
46   GMappedFile *file;
47
48   file = g_mapped_file_new ("/dev/null", FALSE, &error);
49   g_assert_error (error, G_FILE_ERROR, G_FILE_ERROR_INVAL);
50   g_assert (file == NULL);
51   g_error_free (error);
52 }
53
54 static void
55 test_nonexisting (void)
56 {
57   GMappedFile *file;
58   GError *error;
59
60   error = NULL;
61   file = g_mapped_file_new ("no-such-file", FALSE, &error);
62   g_assert_error (error, G_FILE_ERROR, G_FILE_ERROR_NOENT);
63   g_clear_error (&error);
64   g_assert (file == NULL);
65 }
66
67 static void
68 test_writable (void)
69 {
70   GMappedFile *file;
71   GError *error;
72   gchar *contents;
73   const gchar *old = "MMMMMMMMMMMMMMMMMMMMMMMMM";
74   const gchar *new = "abcdefghijklmnopqrstuvxyz";
75
76   if (access (SRCDIR "/4096-random-bytes", W_OK) != 0)
77     {
78       g_test_message ("Skipping writable mapping test");
79       return;
80     }
81
82   error = NULL;
83   file = g_mapped_file_new (SRCDIR "/4096-random-bytes", TRUE, &error);
84   g_assert_no_error (error);
85
86   contents = g_mapped_file_get_contents (file);
87   g_assert (strncmp (contents, old, strlen (old)) == 0);
88
89   memcpy (contents, new, strlen (new));
90   g_assert (strncmp (contents, new, strlen (new)) == 0);
91
92   g_mapped_file_free (file);
93
94   error = NULL;
95   file = g_mapped_file_new (SRCDIR "/4096-random-bytes", TRUE, &error);
96   g_assert_no_error (error);
97
98   contents = g_mapped_file_get_contents (file);
99   g_assert (strncmp (contents, old, strlen (old)) == 0);
100
101   g_mapped_file_free (file);
102 }
103
104 static void
105 test_writable_fd (void)
106 {
107   GMappedFile *file;
108   GError *error;
109   gchar *contents;
110   const gchar *old = "MMMMMMMMMMMMMMMMMMMMMMMMM";
111   const gchar *new = "abcdefghijklmnopqrstuvxyz";
112   int fd;
113
114   if (access (SRCDIR "/4096-random-bytes", W_OK) != 0)
115     {
116       g_test_message ("Skipping writable mapping test");
117       return;
118     }
119
120   error = NULL;
121   fd = open (SRCDIR "/4096-random-bytes", O_RDWR, 0);
122   g_assert (fd != -1);
123   file = g_mapped_file_new_from_fd (fd, TRUE, &error);
124   g_assert_no_error (error);
125
126   contents = g_mapped_file_get_contents (file);
127   g_assert (strncmp (contents, old, strlen (old)) == 0);
128
129   memcpy (contents, new, strlen (new));
130   g_assert (strncmp (contents, new, strlen (new)) == 0);
131
132   g_mapped_file_free (file);
133   close (fd);
134
135   error = NULL;
136   fd = open (SRCDIR "/4096-random-bytes", O_RDWR, 0);
137   g_assert (fd != -1);
138   file = g_mapped_file_new_from_fd (fd, TRUE, &error);
139   g_assert_no_error (error);
140
141   contents = g_mapped_file_get_contents (file);
142   g_assert (strncmp (contents, old, strlen (old)) == 0);
143
144   g_mapped_file_free (file);
145
146 }
147
148 int
149 main (int argc, char *argv[])
150 {
151   g_test_init (&argc, &argv, NULL);
152
153   g_test_add_func ("/mappedfile/basic", test_basic);
154   g_test_add_func ("/mappedfile/empty", test_empty);
155   g_test_add_func ("/mappedfile/device", test_device);
156   g_test_add_func ("/mappedfile/nonexisting", test_nonexisting);
157   g_test_add_func ("/mappedfile/writable", test_writable);
158   g_test_add_func ("/mappedfile/writable_fd", test_writable_fd);
159
160   return g_test_run ();
161 }