nexmon – Blame information for rev 1
?pathlinks?
Rev | Author | Line No. | Line |
---|---|---|---|
1 | office | 1 | #define GLIB_DISABLE_DEPRECATION_WARNINGS |
2 | |||
3 | #include <glib.h> |
||
4 | #include <string.h> |
||
5 | #include <glib/gstdio.h> |
||
6 | #include <sys/stat.h> |
||
7 | #include <sys/types.h> |
||
8 | #include <fcntl.h> |
||
9 | |||
10 | #ifdef G_OS_UNIX |
||
11 | #include <unistd.h> |
||
12 | #endif |
||
13 | #ifdef G_OS_WIN32 |
||
14 | #include <io.h> |
||
15 | #endif |
||
16 | |||
17 | static void |
||
18 | test_basic (void) |
||
19 | { |
||
20 | GMappedFile *file; |
||
21 | GError *error; |
||
22 | |||
23 | error = NULL; |
||
24 | file = g_mapped_file_new (g_test_get_filename (G_TEST_DIST, "empty", NULL), FALSE, &error); |
||
25 | g_assert_no_error (error); |
||
26 | |||
27 | g_mapped_file_ref (file); |
||
28 | g_mapped_file_unref (file); |
||
29 | |||
30 | g_mapped_file_unref (file); |
||
31 | } |
||
32 | |||
33 | static void |
||
34 | test_empty (void) |
||
35 | { |
||
36 | GMappedFile *file; |
||
37 | GError *error; |
||
38 | |||
39 | error = NULL; |
||
40 | file = g_mapped_file_new (g_test_get_filename (G_TEST_DIST, "empty", NULL), FALSE, &error); |
||
41 | g_assert_no_error (error); |
||
42 | |||
43 | g_assert (g_mapped_file_get_contents (file) == NULL); |
||
44 | |||
45 | g_mapped_file_free (file); |
||
46 | } |
||
47 | |||
48 | #ifdef G_OS_UNIX |
||
49 | static void |
||
50 | test_device (void) |
||
51 | { |
||
52 | GError *error = NULL; |
||
53 | GMappedFile *file; |
||
54 | |||
55 | file = g_mapped_file_new ("/dev/null", FALSE, &error); |
||
56 | g_assert (g_error_matches (error, G_FILE_ERROR, G_FILE_ERROR_INVAL) || |
||
57 | g_error_matches (error, G_FILE_ERROR, G_FILE_ERROR_NODEV) || |
||
58 | g_error_matches (error, G_FILE_ERROR, G_FILE_ERROR_NOMEM)); |
||
59 | g_assert (file == NULL); |
||
60 | g_error_free (error); |
||
61 | } |
||
62 | #endif |
||
63 | |||
64 | static void |
||
65 | test_nonexisting (void) |
||
66 | { |
||
67 | GMappedFile *file; |
||
68 | GError *error; |
||
69 | |||
70 | error = NULL; |
||
71 | file = g_mapped_file_new ("no-such-file", FALSE, &error); |
||
72 | g_assert_error (error, G_FILE_ERROR, G_FILE_ERROR_NOENT); |
||
73 | g_clear_error (&error); |
||
74 | g_assert (file == NULL); |
||
75 | } |
||
76 | |||
77 | static void |
||
78 | test_writable (void) |
||
79 | { |
||
80 | GMappedFile *file; |
||
81 | GError *error = NULL; |
||
82 | gchar *contents; |
||
83 | gsize len; |
||
84 | const gchar *old = "MMMMMMMMMMMMMMMMMMMMMMMMM"; |
||
85 | const gchar *new = "abcdefghijklmnopqrstuvxyz"; |
||
86 | gchar *tmp_copy_path; |
||
87 | |||
88 | tmp_copy_path = g_build_filename (g_get_user_runtime_dir (), "glib-test-4096-random-bytes", NULL); |
||
89 | |||
90 | g_file_get_contents (g_test_get_filename (G_TEST_DIST, "4096-random-bytes", NULL), &contents, &len, &error); |
||
91 | g_assert_no_error (error); |
||
92 | g_file_set_contents (tmp_copy_path, contents, len, &error); |
||
93 | g_assert_no_error (error); |
||
94 | |||
95 | g_free (contents); |
||
96 | |||
97 | file = g_mapped_file_new (tmp_copy_path, 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 | memcpy (contents, new, strlen (new)); |
||
104 | g_assert (strncmp (contents, new, strlen (new)) == 0); |
||
105 | |||
106 | g_mapped_file_free (file); |
||
107 | |||
108 | error = NULL; |
||
109 | file = g_mapped_file_new (tmp_copy_path, FALSE, &error); |
||
110 | g_assert_no_error (error); |
||
111 | |||
112 | contents = g_mapped_file_get_contents (file); |
||
113 | g_assert (strncmp (contents, old, strlen (old)) == 0); |
||
114 | |||
115 | g_mapped_file_free (file); |
||
116 | |||
117 | g_free (tmp_copy_path); |
||
118 | } |
||
119 | |||
120 | static void |
||
121 | test_writable_fd (void) |
||
122 | { |
||
123 | GMappedFile *file; |
||
124 | GError *error = NULL; |
||
125 | gchar *contents; |
||
126 | const gchar *old = "MMMMMMMMMMMMMMMMMMMMMMMMM"; |
||
127 | const gchar *new = "abcdefghijklmnopqrstuvxyz"; |
||
128 | gsize len; |
||
129 | int fd; |
||
130 | gchar *tmp_copy_path; |
||
131 | |||
132 | tmp_copy_path = g_build_filename (g_get_user_runtime_dir (), "glib-test-4096-random-bytes", NULL); |
||
133 | |||
134 | g_file_get_contents (g_test_get_filename (G_TEST_DIST, "4096-random-bytes", NULL), &contents, &len, &error); |
||
135 | g_assert_no_error (error); |
||
136 | g_file_set_contents (tmp_copy_path, contents, len, &error); |
||
137 | g_assert_no_error (error); |
||
138 | |||
139 | g_free (contents); |
||
140 | |||
141 | fd = g_open (tmp_copy_path, O_RDWR, 0); |
||
142 | g_assert (fd != -1); |
||
143 | file = g_mapped_file_new_from_fd (fd, TRUE, &error); |
||
144 | g_assert_no_error (error); |
||
145 | |||
146 | contents = g_mapped_file_get_contents (file); |
||
147 | g_assert (strncmp (contents, old, strlen (old)) == 0); |
||
148 | |||
149 | memcpy (contents, new, strlen (new)); |
||
150 | g_assert (strncmp (contents, new, strlen (new)) == 0); |
||
151 | |||
152 | g_mapped_file_free (file); |
||
153 | close (fd); |
||
154 | |||
155 | error = NULL; |
||
156 | fd = g_open (tmp_copy_path, O_RDWR, 0); |
||
157 | g_assert (fd != -1); |
||
158 | file = g_mapped_file_new_from_fd (fd, TRUE, &error); |
||
159 | g_assert_no_error (error); |
||
160 | |||
161 | contents = g_mapped_file_get_contents (file); |
||
162 | g_assert (strncmp (contents, old, strlen (old)) == 0); |
||
163 | |||
164 | g_mapped_file_free (file); |
||
165 | |||
166 | g_free (tmp_copy_path); |
||
167 | } |
||
168 | |||
169 | static void |
||
170 | test_gbytes (void) |
||
171 | { |
||
172 | GMappedFile *file; |
||
173 | GBytes *bytes; |
||
174 | GError *error; |
||
175 | |||
176 | error = NULL; |
||
177 | file = g_mapped_file_new (g_test_get_filename (G_TEST_DIST, "empty", NULL), FALSE, &error); |
||
178 | g_assert_no_error (error); |
||
179 | |||
180 | bytes = g_mapped_file_get_bytes (file); |
||
181 | g_mapped_file_unref (file); |
||
182 | |||
183 | g_assert_cmpint (g_bytes_get_size (bytes), ==, 0); |
||
184 | g_bytes_unref (bytes); |
||
185 | } |
||
186 | |||
187 | int |
||
188 | main (int argc, char *argv[]) |
||
189 | { |
||
190 | g_test_init (&argc, &argv, NULL); |
||
191 | |||
192 | g_test_add_func ("/mappedfile/basic", test_basic); |
||
193 | g_test_add_func ("/mappedfile/empty", test_empty); |
||
194 | #ifdef G_OS_UNIX |
||
195 | g_test_add_func ("/mappedfile/device", test_device); |
||
196 | #endif |
||
197 | g_test_add_func ("/mappedfile/nonexisting", test_nonexisting); |
||
198 | g_test_add_func ("/mappedfile/writable", test_writable); |
||
199 | g_test_add_func ("/mappedfile/writable_fd", test_writable_fd); |
||
200 | g_test_add_func ("/mappedfile/gbytes", test_gbytes); |
||
201 | |||
202 | return g_test_run (); |
||
203 | } |