Switch all open() calls to use g_open()
[platform/upstream/glib.git] / glib / tests / mappedfile.c
1 #define GLIB_DISABLE_DEPRECATION_WARNINGS
2
3 #include <config.h>
4 #include <glib.h>
5 #include <string.h>
6 #ifdef HAVE_UNISTD_H
7 #include <unistd.h>
8 #endif
9 #include <glib/gstdio.h>
10 #include <sys/stat.h>
11 #include <sys/types.h>
12 #include <fcntl.h>
13
14 static void
15 test_basic (void)
16 {
17   GMappedFile *file;
18   GError *error;
19
20   error = NULL;
21   file = g_mapped_file_new (SRCDIR "/empty", FALSE, &error);
22   g_assert_no_error (error);
23
24   g_mapped_file_ref (file);
25   g_mapped_file_unref (file);
26
27   g_mapped_file_unref (file);
28 }
29
30 static void
31 test_empty (void)
32 {
33   GMappedFile *file;
34   GError *error;
35
36   error = NULL;
37   file = g_mapped_file_new (SRCDIR "/empty", FALSE, &error);
38   g_assert_no_error (error);
39
40   g_assert (g_mapped_file_get_contents (file) == NULL);
41
42   g_mapped_file_free (file);
43 }
44
45 static void
46 test_device (void)
47 {
48   GError *error = NULL;
49   GMappedFile *file;
50
51   file = g_mapped_file_new ("/dev/null", FALSE, &error);
52   g_assert_error (error, G_FILE_ERROR, G_FILE_ERROR_INVAL);
53   g_assert (file == NULL);
54   g_error_free (error);
55 }
56
57 static void
58 test_nonexisting (void)
59 {
60   GMappedFile *file;
61   GError *error;
62
63   error = NULL;
64   file = g_mapped_file_new ("no-such-file", FALSE, &error);
65   g_assert_error (error, G_FILE_ERROR, G_FILE_ERROR_NOENT);
66   g_clear_error (&error);
67   g_assert (file == NULL);
68 }
69
70 static void
71 test_writable (void)
72 {
73   GMappedFile *file;
74   GError *error;
75   gchar *contents;
76   const gchar *old = "MMMMMMMMMMMMMMMMMMMMMMMMM";
77   const gchar *new = "abcdefghijklmnopqrstuvxyz";
78
79   if (access (SRCDIR "/4096-random-bytes", W_OK) != 0)
80     {
81       g_test_message ("Skipping writable mapping test");
82       return;
83     }
84
85   error = NULL;
86   file = g_mapped_file_new (SRCDIR "/4096-random-bytes", TRUE, &error);
87   g_assert_no_error (error);
88
89   contents = g_mapped_file_get_contents (file);
90   g_assert (strncmp (contents, old, strlen (old)) == 0);
91
92   memcpy (contents, new, strlen (new));
93   g_assert (strncmp (contents, new, strlen (new)) == 0);
94
95   g_mapped_file_free (file);
96
97   error = NULL;
98   file = g_mapped_file_new (SRCDIR "/4096-random-bytes", TRUE, &error);
99   g_assert_no_error (error);
100
101   contents = g_mapped_file_get_contents (file);
102   g_assert (strncmp (contents, old, strlen (old)) == 0);
103
104   g_mapped_file_free (file);
105 }
106
107 static void
108 test_writable_fd (void)
109 {
110   GMappedFile *file;
111   GError *error;
112   gchar *contents;
113   const gchar *old = "MMMMMMMMMMMMMMMMMMMMMMMMM";
114   const gchar *new = "abcdefghijklmnopqrstuvxyz";
115   int fd;
116
117   if (access (SRCDIR "/4096-random-bytes", W_OK) != 0)
118     {
119       g_test_message ("Skipping writable mapping test");
120       return;
121     }
122
123   error = NULL;
124   fd = g_open (SRCDIR "/4096-random-bytes", O_RDWR, 0);
125   g_assert (fd != -1);
126   file = g_mapped_file_new_from_fd (fd, TRUE, &error);
127   g_assert_no_error (error);
128
129   contents = g_mapped_file_get_contents (file);
130   g_assert (strncmp (contents, old, strlen (old)) == 0);
131
132   memcpy (contents, new, strlen (new));
133   g_assert (strncmp (contents, new, strlen (new)) == 0);
134
135   g_mapped_file_free (file);
136   close (fd);
137
138   error = NULL;
139   fd = g_open (SRCDIR "/4096-random-bytes", O_RDWR, 0);
140   g_assert (fd != -1);
141   file = g_mapped_file_new_from_fd (fd, TRUE, &error);
142   g_assert_no_error (error);
143
144   contents = g_mapped_file_get_contents (file);
145   g_assert (strncmp (contents, old, strlen (old)) == 0);
146
147   g_mapped_file_free (file);
148
149 }
150
151 static void
152 test_gbytes (void)
153 {
154   GMappedFile *file;
155   GBytes *bytes;
156   GError *error;
157
158   error = NULL;
159   file = g_mapped_file_new (SRCDIR "/empty", FALSE, &error);
160   g_assert_no_error (error);
161
162   bytes = g_mapped_file_get_bytes (file);
163   g_mapped_file_unref (file);
164
165   g_assert_cmpint (g_bytes_get_size (bytes), ==, 0);
166   g_bytes_unref (bytes);
167 }
168
169 int
170 main (int argc, char *argv[])
171 {
172   g_test_init (&argc, &argv, NULL);
173
174   g_test_add_func ("/mappedfile/basic", test_basic);
175   g_test_add_func ("/mappedfile/empty", test_empty);
176   g_test_add_func ("/mappedfile/device", test_device);
177   g_test_add_func ("/mappedfile/nonexisting", test_nonexisting);
178   g_test_add_func ("/mappedfile/writable", test_writable);
179   g_test_add_func ("/mappedfile/writable_fd", test_writable_fd);
180   g_test_add_func ("/mappedfile/gbytes", test_gbytes);
181
182   return g_test_run ();
183 }