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