|
1 /* GLIB - Library of useful routines for C programming |
|
2 * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald |
|
3 * |
|
4 * This library is free software; you can redistribute it and/or |
|
5 * modify it under the terms of the GNU Lesser General Public |
|
6 * License as published by the Free Software Foundation; either |
|
7 * version 2 of the License, or (at your option) any later version. |
|
8 * |
|
9 * This library is distributed in the hope that it will be useful, |
|
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
|
12 * Lesser General Public License for more details. |
|
13 * |
|
14 * You should have received a copy of the GNU Lesser General Public |
|
15 * License along with this library; if not, write to the |
|
16 * Free Software Foundation, Inc., 59 Temple Place - Suite 330, |
|
17 * Boston, MA 02111-1307, USA. |
|
18 */ |
|
19 |
|
20 /* |
|
21 * Modified by the GLib Team and others 1997-2000. See the AUTHORS |
|
22 * file for a list of people on the GLib Team. See the ChangeLog |
|
23 * files for a list of changes. These files are distributed with |
|
24 * GLib at ftp://ftp.gtk.org/pub/gtk/. |
|
25 */ |
|
26 |
|
27 #include "config.h" |
|
28 |
|
29 #undef G_DISABLE_ASSERT |
|
30 #undef G_LOG_DOMAIN |
|
31 |
|
32 #ifdef GLIB_COMPILATION |
|
33 #undef GLIB_COMPILATION |
|
34 #endif |
|
35 |
|
36 #include <string.h> |
|
37 |
|
38 #include <glib.h> |
|
39 |
|
40 #include <gstdio.h> |
|
41 |
|
42 #ifdef HAVE_UNISTD_H |
|
43 #include <unistd.h> |
|
44 #endif |
|
45 |
|
46 #ifdef G_OS_WIN32 |
|
47 #include <io.h> /* For read(), write() etc */ |
|
48 #endif |
|
49 |
|
50 #ifdef SYMBIAN |
|
51 #include "mrt2_glib2_test.h" |
|
52 #endif /*SYMBIAN*/ |
|
53 |
|
54 |
|
55 static void |
|
56 test_mkstemp (void) |
|
57 { |
|
58 char template[32]; |
|
59 int fd; |
|
60 int i; |
|
61 const char hello[] = "Hello, World"; |
|
62 const int hellolen = sizeof (hello) - 1; |
|
63 char chars[62]; |
|
64 |
|
65 strcpy (template, "foobar"); |
|
66 fd = g_mkstemp (template); |
|
67 if (fd != -1) |
|
68 g_warning ("g_mkstemp works even if template doesn't end in XXXXXX"); |
|
69 close (fd); |
|
70 |
|
71 strcpy (template, "fooXXXXXX"); |
|
72 fd = g_mkstemp (template); |
|
73 g_assert (fd != -1 && "g_mkstemp didn't work for template fooXXXXXX"); |
|
74 i = write (fd, hello, hellolen); |
|
75 g_assert (i != -1 && "write() failed"); |
|
76 g_assert (i == hellolen && "write() has written too few bytes"); |
|
77 |
|
78 lseek (fd, 0, 0); |
|
79 i = read (fd, chars, sizeof (chars)); |
|
80 g_assert (i != -1 && "read() failed: %s"); |
|
81 g_assert (i == hellolen && "read() has got wrong number of bytes"); |
|
82 |
|
83 chars[i] = 0; |
|
84 g_assert (strcmp (chars, hello) == 0 && "read() didn't get same string back"); |
|
85 |
|
86 close (fd); |
|
87 remove (template); |
|
88 } |
|
89 |
|
90 static void |
|
91 test_readlink (void) |
|
92 { |
|
93 #ifdef HAVE_SYMLINK |
|
94 FILE *file; |
|
95 int result; |
|
96 char *filename = "file-test-data"; |
|
97 char *link1 = "file-test-link1"; |
|
98 char *link2 = "file-test-link2"; |
|
99 char *link3 = "file-test-link3"; |
|
100 char *data; |
|
101 GError *error; |
|
102 |
|
103 file = fopen (filename, "w"); |
|
104 g_assert (file != NULL && "fopen() failed"); |
|
105 fclose (file); |
|
106 |
|
107 result = symlink (filename, link1); |
|
108 g_assert (result == 0 && "symlink() failed"); |
|
109 result = symlink (filename, link2); |
|
110 g_assert (result == 0 && "symlink() failed"); |
|
111 |
|
112 error = NULL; |
|
113 data = g_file_read_link (link1, &error); |
|
114 g_assert (data != NULL && "couldn't read link1"); |
|
115 g_assert (strcmp (data, filename) == 0 && "link1 contains wrong data"); |
|
116 g_free (data); |
|
117 error = NULL; |
|
118 data = g_file_read_link (link2, &error); |
|
119 g_assert (data != NULL && "couldn't read link2"); |
|
120 g_assert (strcmp (data, filename) == 0 && "link2 contains wrong data"); |
|
121 g_free (data); |
|
122 |
|
123 error = NULL; |
|
124 data = g_file_read_link (link3, &error); |
|
125 g_assert (data == NULL && "could read link3"); |
|
126 g_assert (error != NULL && "error not set"); |
|
127 |
|
128 error = NULL; |
|
129 data = g_file_read_link (filename, &error); |
|
130 g_assert (data == NULL && "could read regular file as link"); |
|
131 g_assert (error != NULL && "error not set"); |
|
132 |
|
133 remove (filename); |
|
134 remove (link1); |
|
135 remove (link2); |
|
136 #endif |
|
137 } |
|
138 |
|
139 static void |
|
140 test_get_contents (void) |
|
141 { |
|
142 const gchar *text = "abcdefghijklmnopqrstuvwxyz"; |
|
143 const gchar *filename = "file-test-get-contents"; |
|
144 gchar *contents; |
|
145 gsize len; |
|
146 FILE *f; |
|
147 GError *error = NULL; |
|
148 |
|
149 f = g_fopen (filename, "w"); |
|
150 if( f == NULL) |
|
151 { |
|
152 g_print("file-test.c : g_fopen Failed !!"); |
|
153 return; |
|
154 } |
|
155 fwrite (text, 1, strlen (text), f); |
|
156 fclose (f); |
|
157 |
|
158 g_assert (g_file_test (filename, G_FILE_TEST_IS_REGULAR)); |
|
159 |
|
160 if (! g_file_get_contents (filename, &contents, &len, &error)) |
|
161 g_error ("g_file_get_contents() failed: %s", error->message); |
|
162 |
|
163 g_assert (strcmp (text, contents) == 0 && "content mismatch"); |
|
164 |
|
165 g_free (contents); |
|
166 } |
|
167 |
|
168 int |
|
169 main (int argc, char *argv[]) |
|
170 { |
|
171 #ifdef SYMBIAN |
|
172 g_log_set_handler (NULL, G_LOG_FLAG_FATAL| G_LOG_FLAG_RECURSION | G_LOG_LEVEL_CRITICAL | G_LOG_LEVEL_WARNING | G_LOG_LEVEL_MESSAGE | G_LOG_LEVEL_INFO | G_LOG_LEVEL_DEBUG, &mrtLogHandler, NULL); |
|
173 g_set_print_handler(mrtPrintHandler); |
|
174 #endif /*SYMBIAN*/ |
|
175 |
|
176 |
|
177 test_mkstemp (); |
|
178 test_readlink (); |
|
179 test_get_contents (); |
|
180 |
|
181 #if SYMBIAN |
|
182 testResultXml("file-test"); |
|
183 #endif /* EMULATOR */ |
|
184 |
|
185 return 0; |
|
186 } |