summaryrefslogtreecommitdiff
path: root/glib/tests/mappedfile.c
blob: 785e7c516dcf9f578e88e49e001d92ab7a2b8607 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
#ifndef GLIB_DISABLE_DEPRECATION_WARNINGS
#define GLIB_DISABLE_DEPRECATION_WARNINGS
#endif

#include <glib.h>
#include <string.h>
#include <glib/gstdio.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <fcntl.h>

#ifdef G_OS_UNIX
#include <unistd.h>
#endif
#ifdef G_OS_WIN32
#include <io.h>
#endif

static void
test_basic (void)
{
  GMappedFile *file;
  GError *error;

  error = NULL;
  file = g_mapped_file_new (g_test_get_filename (G_TEST_DIST, "empty", NULL), FALSE, &error);
  g_assert_no_error (error);

  g_mapped_file_ref (file);
  g_mapped_file_unref (file);

  g_mapped_file_unref (file);
}

static void
test_empty (void)
{
  GMappedFile *file;
  GError *error;

  error = NULL;
  file = g_mapped_file_new (g_test_get_filename (G_TEST_DIST, "empty", NULL), FALSE, &error);
  g_assert_no_error (error);

  g_assert_null (g_mapped_file_get_contents (file));

  g_mapped_file_free (file);
}

#ifdef G_OS_UNIX
static void
test_device (void)
{
  GError *error = NULL;
  GMappedFile *file;

  file = g_mapped_file_new ("/dev/null", FALSE, &error);
  g_assert_true (g_error_matches (error, G_FILE_ERROR, G_FILE_ERROR_INVAL) ||
                 g_error_matches (error, G_FILE_ERROR, G_FILE_ERROR_NODEV) ||
                 g_error_matches (error, G_FILE_ERROR, G_FILE_ERROR_NOMEM));
  g_assert_null (file);
  g_error_free (error);
}
#endif

static void
test_nonexisting (void)
{
  GMappedFile *file;
  GError *error;

  error = NULL;
  file = g_mapped_file_new ("no-such-file", FALSE, &error);
  g_assert_error (error, G_FILE_ERROR, G_FILE_ERROR_NOENT);
  g_clear_error (&error);
  g_assert_null (file);
}

static void
test_writable (void)
{
  GMappedFile *file;
  GError *error = NULL;
  gchar *contents;
  gsize len;
  const gchar *old = "MMMMMMMMMMMMMMMMMMMMMMMMM";
  const gchar *new = "abcdefghijklmnopqrstuvxyz";
  gchar *tmp_copy_path;

  tmp_copy_path = g_build_filename (g_get_tmp_dir (), "glib-test-4096-random-bytes", NULL);

  g_file_get_contents (g_test_get_filename (G_TEST_DIST, "4096-random-bytes", NULL), &contents, &len, &error);
  g_assert_no_error (error);
  g_file_set_contents (tmp_copy_path, contents, len, &error);
  g_assert_no_error (error);
  
  g_free (contents);

  file = g_mapped_file_new (tmp_copy_path, TRUE, &error);
  g_assert_no_error (error);

  contents = g_mapped_file_get_contents (file);
  g_assert_cmpuint (strncmp (contents, old, strlen (old)), ==, 0);

  memcpy (contents, new, strlen (new));
  g_assert_cmpuint (strncmp (contents, new, strlen (new)), ==, 0);

  g_mapped_file_free (file);

  error = NULL;
  file = g_mapped_file_new (tmp_copy_path, FALSE, &error);
  g_assert_no_error (error);

  contents = g_mapped_file_get_contents (file);
  g_assert_cmpuint (strncmp (contents, old, strlen (old)), ==, 0);

  g_mapped_file_free (file);

  g_free (tmp_copy_path);
}

static void
test_writable_fd (void)
{
  GMappedFile *file;
  GError *error = NULL;
  gchar *contents;
  const gchar *old = "MMMMMMMMMMMMMMMMMMMMMMMMM";
  const gchar *new = "abcdefghijklmnopqrstuvxyz";
  gsize len;
  int fd;
  gchar *tmp_copy_path;

  tmp_copy_path = g_build_filename (g_get_tmp_dir (), "glib-test-4096-random-bytes", NULL);

  g_file_get_contents (g_test_get_filename (G_TEST_DIST, "4096-random-bytes", NULL), &contents, &len, &error);
  g_assert_no_error (error);
  g_file_set_contents (tmp_copy_path, contents, len, &error);
  g_assert_no_error (error);
  
  g_free (contents);

  fd = g_open (tmp_copy_path, O_RDWR, 0);
  g_assert_cmpint (fd, !=, -1);
  file = g_mapped_file_new_from_fd (fd, TRUE, &error);
  g_assert_no_error (error);

  contents = g_mapped_file_get_contents (file);
  g_assert_cmpuint (strncmp (contents, old, strlen (old)), ==, 0);

  memcpy (contents, new, strlen (new));
  g_assert_cmpuint (strncmp (contents, new, strlen (new)), ==, 0);

  g_mapped_file_free (file);
  close (fd);

  error = NULL;
  fd = g_open (tmp_copy_path, O_RDWR, 0);
  g_assert_cmpint (fd, !=, -1);
  file = g_mapped_file_new_from_fd (fd, TRUE, &error);
  g_assert_no_error (error);

  contents = g_mapped_file_get_contents (file);
  g_assert_cmpuint (strncmp (contents, old, strlen (old)), ==, 0);

  g_mapped_file_free (file);

  g_free (tmp_copy_path);
}

static void
test_gbytes (void)
{
  GMappedFile *file;
  GBytes *bytes;
  GError *error;

  error = NULL;
  file = g_mapped_file_new (g_test_get_filename (G_TEST_DIST, "empty", NULL), FALSE, &error);
  g_assert_no_error (error);

  bytes = g_mapped_file_get_bytes (file);
  g_mapped_file_unref (file);

  g_assert_cmpint (g_bytes_get_size (bytes), ==, 0);
  g_bytes_unref (bytes);
}

int
main (int argc, char *argv[])
{
  g_test_init (&argc, &argv, NULL);

  g_test_add_func ("/mappedfile/basic", test_basic);
  g_test_add_func ("/mappedfile/empty", test_empty);
#ifdef G_OS_UNIX
  g_test_add_func ("/mappedfile/device", test_device);
#endif
  g_test_add_func ("/mappedfile/nonexisting", test_nonexisting);
  g_test_add_func ("/mappedfile/writable", test_writable);
  g_test_add_func ("/mappedfile/writable_fd", test_writable_fd);
  g_test_add_func ("/mappedfile/gbytes", test_gbytes);

  return g_test_run ();
}