~ubuntu-branches/ubuntu/jaunty/gvfs/jaunty-updates

« back to all changes in this revision

Viewing changes to test/test-query-info-stream.c

  • Committer: Bazaar Package Importer
  • Author(s): Sebastien Bacher
  • Date: 2009-03-02 18:10:40 UTC
  • mfrom: (1.1.37 upstream)
  • Revision ID: james.westby@ubuntu.com-20090302181040-bozliyntjtpd0ozp
Tags: 1.1.7-0ubuntu1
* New upstream version:
  - Fix build on some platforms
  - ftp: Fix short read errors (lp: #208750)
  - gphoto2: Make it work on iphone
  - sftp: Fix symlink creation
  - fuse: Better support of truncation, fixing OOo save (lp: #317587)
  - proxy monitors: Support mount operations, etc
  - gvfs-mount: add --device commandline support
  - sftp: Fix protocol bug that made some servers not work (lp: #286053)
  - general support for query_info over streams, implemented for smb, sftp
  - Initial support for .xdg-volume-info reading (lp: #269159)
  - fix trash crasher (lp: #332554)
  - use ssh config (lp: #264803)
* debian/patches/02_support_xdg_volume_info.patch:
  - the new version fixes this issue
* debian/patches/91_upstream_change_fix_trash_crasher.patch:
  - upstream change to fix a trash crasher (lp: #333791)
* debian/rules:
  - shlibs version update

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* GIO - GLib Input, Output and Streaming Library
 
2
 * 
 
3
 * Copyright (C) 2006-2007 Red Hat, Inc.
 
4
 *
 
5
 * This library is free software; you can redistribute it and/or
 
6
 * modify it under the terms of the GNU Lesser General Public
 
7
 * License as published by the Free Software Foundation; either
 
8
 * version 2 of the License, or (at your option) any later version.
 
9
 *
 
10
 * This library is distributed in the hope that it will be useful,
 
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 
13
 * Lesser General Public License for more details.
 
14
 *
 
15
 * You should have received a copy of the GNU Lesser General
 
16
 * Public License along with this library; if not, write to the
 
17
 * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
 
18
 * Boston, MA 02111-1307, USA.
 
19
 *
 
20
 * Author: Alexander Larsson <alexl@redhat.com>
 
21
 */
 
22
 
 
23
 
 
24
#include <config.h>
 
25
 
 
26
#include <stdio.h>
 
27
#include <unistd.h>
 
28
#include <locale.h>
 
29
#include <errno.h>
 
30
#include <string.h>
 
31
#include <stdlib.h>
 
32
 
 
33
#include <glib.h>
 
34
#include <gio/gio.h>
 
35
 
 
36
/* Fill test data with 0..200, repeadedly.
 
37
 * This is not a power of two to avoid possible
 
38
 * effects with base-2 i/o buffer sizes that could
 
39
 * hide bugs */
 
40
#define DATA_MODULO 200
 
41
 
 
42
static GMainLoop *main_loop;
 
43
 
 
44
static gboolean
 
45
verify_block (guchar *data, guchar *start, gsize size)
 
46
{
 
47
  guchar d;
 
48
  gsize i;
 
49
 
 
50
  d = 0;
 
51
  if (start)
 
52
    d = *start;
 
53
  for (i = 0; i < size; i++)
 
54
    {
 
55
      if (data[i] != d)
 
56
        return FALSE;
 
57
      
 
58
      d++;
 
59
      if (d >= DATA_MODULO)
 
60
        d = 0;
 
61
    }
 
62
 
 
63
  if (start)
 
64
    *start = d;
 
65
  
 
66
  return TRUE;
 
67
}
 
68
 
 
69
static guchar *
 
70
allocate_block (gsize size)
 
71
{
 
72
  guchar *data;
 
73
  gsize i;
 
74
  guchar d;
 
75
 
 
76
  data = g_malloc (size);
 
77
  d = 0;
 
78
  for (i = 0; i < size; i++)
 
79
    {
 
80
      data[i] = d;
 
81
      d++;
 
82
      if (d >= DATA_MODULO)
 
83
        d = 0;
 
84
    }
 
85
  return data;
 
86
}
 
87
 
 
88
static void
 
89
check_query_info_res (GFileInfo *info,
 
90
                      GError *error,
 
91
                      gsize expected_size)
 
92
{
 
93
  goffset file_size;
 
94
 
 
95
  if (info == NULL)
 
96
    {
 
97
      g_print ("error querying info: %s\n", error->message);
 
98
      exit (1);
 
99
    }
 
100
 
 
101
  if (!g_file_info_has_attribute (info, G_FILE_ATTRIBUTE_STANDARD_SIZE))
 
102
    {
 
103
      g_print ("couldn't read size attribute\n");
 
104
      exit (1);
 
105
    }
 
106
  
 
107
  file_size = g_file_info_get_size (info);
 
108
  if (file_size != expected_size)
 
109
    {
 
110
      g_print ("wrong file size\n");
 
111
      exit (1);
 
112
    }
 
113
}
 
114
 
 
115
static void
 
116
check_query_info_out (GFileOutputStream *out, gsize expected_size)
 
117
{
 
118
  GFileInfo *info;
 
119
  GError *error;
 
120
 
 
121
  error = NULL;
 
122
  info = g_file_output_stream_query_info (out, "*", NULL, &error);
 
123
 
 
124
  check_query_info_res (info, error, expected_size);
 
125
}
 
126
 
 
127
static void
 
128
create_file (GFile *file, gsize size)
 
129
{
 
130
  GFileOutputStream *out;
 
131
  guchar *data;
 
132
  gsize written;
 
133
  GError *error;
 
134
 
 
135
  data = allocate_block (size);
 
136
 
 
137
  error = NULL;
 
138
  out = g_file_replace (file, NULL, FALSE, 0, NULL, &error);
 
139
  if (out == NULL)
 
140
    {
 
141
      g_print ("error creating file: %s\n", error->message);
 
142
      exit (1);
 
143
    }
 
144
  
 
145
  check_query_info_out (out, 0);
 
146
  
 
147
  if (!g_output_stream_write_all (G_OUTPUT_STREAM (out),
 
148
                                  data, size, 
 
149
                                  &written,
 
150
                                  NULL, &error))
 
151
    {
 
152
      g_print ("error writing to file: %s\n", error->message);
 
153
      exit (1);
 
154
    }
 
155
 
 
156
  check_query_info_out (out, written);
 
157
  
 
158
  if (written != size)
 
159
    {
 
160
      g_print ("not all data written to file\n");
 
161
      exit (1);
 
162
    }
 
163
 
 
164
  g_output_stream_close (G_OUTPUT_STREAM (out), NULL, NULL);
 
165
  
 
166
  g_free (data);
 
167
}
 
168
 
 
169
static void
 
170
check_query_info (GFileInputStream *in, gsize expected_size)
 
171
{
 
172
  GFileInfo *info;
 
173
  GError *error;
 
174
 
 
175
  error = NULL;
 
176
  info = g_file_input_stream_query_info (in, "*", NULL, &error);
 
177
 
 
178
  check_query_info_res (info, error, expected_size);
 
179
}
 
180
 
 
181
static void
 
182
async_cb (GObject *source_object,
 
183
          GAsyncResult *res,
 
184
          gpointer user_data)
 
185
{
 
186
  GFileInfo *info;
 
187
  GError *error;
 
188
 
 
189
  error = NULL;
 
190
  info =
 
191
    g_file_input_stream_query_info_finish (G_FILE_INPUT_STREAM (source_object),
 
192
                                           res, &error);
 
193
 
 
194
  check_query_info_res (info, error, 100*1000);
 
195
  
 
196
  g_main_loop_quit (main_loop);
 
197
}
 
198
 
 
199
int
 
200
main (int argc, char *argv[])
 
201
{
 
202
  GFile *file;
 
203
  GFileInputStream *in;
 
204
  GError *error;
 
205
  gssize res;
 
206
  gsize read_size;
 
207
  guchar *buffer;
 
208
  guchar start;
 
209
  gboolean do_create_file;
 
210
  
 
211
  g_type_init ();
 
212
 
 
213
  do_create_file = FALSE;
 
214
  
 
215
  if (argc > 1)
 
216
    {
 
217
      if (strcmp(argv[1], "-c") == 0)
 
218
        {
 
219
          do_create_file = TRUE;
 
220
          argc--;
 
221
          argv++;
 
222
        }
 
223
    }
 
224
      
 
225
  if (argc != 2)
 
226
    {
 
227
      g_print ("need file arg");
 
228
      return 1;
 
229
    }
 
230
 
 
231
  file = g_file_new_for_commandline_arg (argv[1]);
 
232
 
 
233
  if (do_create_file)
 
234
    create_file (file, 100*1000);
 
235
 
 
236
  error = NULL;
 
237
  
 
238
  in = g_file_read (file, NULL, &error);
 
239
  if (in == NULL)
 
240
    {
 
241
      g_print ("error reading file: %s\n", error->message);
 
242
      exit (1);
 
243
    }
 
244
 
 
245
  check_query_info (in, 100*1000);
 
246
 
 
247
  buffer = malloc (100*1000);
 
248
 
 
249
  start = 0;
 
250
  read_size = 0;
 
251
  do
 
252
    {
 
253
      res = g_input_stream_read  (G_INPUT_STREAM (in),
 
254
                                  buffer,
 
255
                                  150,
 
256
                                  NULL, &error);
 
257
      if (res == 0)
 
258
        break;
 
259
      
 
260
      if (res < 0)
 
261
        {
 
262
          g_print ("error reading: %s\n", error->message);
 
263
          exit (1);
 
264
        }
 
265
 
 
266
      if (!verify_block (buffer, &start, res))
 
267
        {
 
268
          g_print ("error in block starting at %d\n", (int)read_size);
 
269
          exit (1);
 
270
        }
 
271
 
 
272
      read_size += res;
 
273
 
 
274
      check_query_info (in, 100*1000);
 
275
    }
 
276
  while (1);
 
277
 
 
278
  if (read_size != 100*1000)
 
279
    {
 
280
      g_print ("Didn't read entire file\n");
 
281
      exit (1);
 
282
    }
 
283
 
 
284
  main_loop = g_main_loop_new (NULL, FALSE);
 
285
  
 
286
  g_file_input_stream_query_info_async  (in, "*",
 
287
                                         0, NULL,
 
288
                                         async_cb, NULL);
 
289
  
 
290
  g_main_loop_run (main_loop);
 
291
 
 
292
  g_print ("ALL OK\n");
 
293
  return 0;
 
294
}