summaryrefslogtreecommitdiff
path: root/test/google.c
blob: c920cabae37b37473492f4514c356e948ed16fde (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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
/* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */
/* gvfs - extensions for gio
 *
 * Copyright (C) 2019 Mayank Sharma
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General
 * Public License along with this library; if not, write to the
 * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 * Boston, MA 02110-1301, USA.
 *
 * Author: Mayank Sharma <mayank8019@gmail.com>
 */

#include <gio/gio.h>
#include <glib.h>
#include <locale.h>

#define GOA_API_IS_SUBJECT_TO_CHANGE
#include <gdata/gdata.h>
#include <goa/goa.h>

#define GOOGLE_TEST_DIRECTORY "test-google"

#define OP_COPY_TEST_DIRECTORY "Test-Copy-Dir"
#define OP_MOVE_TEST_DIRECTORY "Test-Move-Dir"

#define TITLE_DUMMY_FILE "Dummy-File"
#define TITLE_COPIED_FILE "Copied-File"
#define TITLE_MOVED_FILE "Moved-File"

typedef struct {
  GDataDocumentsService     *service;
  GDataDocumentsEntry       *test_dir_entry;
  GDataAuthorizationDomain  *domain;
  GMount                    *mount;
  GFile                     *root;                /* GFile corresponding to root-level directory */
  GFile                     *test_dir;            /* GFile corresponding to test_dir_entry */
  GFile                     *test_dummy_file;     /* A GFile inside of test_dir which can be used for
                                                 testing copy/move operations*/
} GVfsGoogleTestData ;

typedef struct
{
  GAsyncResult *res;
  GMainLoop *loop;

} MountData;

/* ---------------------------------------------------------------------------------------------------- */
/* Helper functions begin */
/* ---------------------------------------------------------------------------------------------------- */

static gchar *
get_file_attribute (GFile *file, const char *attribute, GError **error)
{
  g_autoptr(GFileInfo) info = NULL;
  g_autoptr(GError) child_error = NULL;

  g_return_val_if_fail (G_IS_FILE (file), NULL);
  g_return_val_if_fail (error != NULL && *error == NULL, NULL);

  info = g_file_query_info (file,
                            attribute,
                            G_FILE_QUERY_INFO_NONE,
                            NULL,
                            &child_error);
  if (child_error != NULL)
    {
      g_propagate_error (error, g_steal_pointer (&child_error));
      return FALSE;
    }

  return g_file_info_get_attribute_as_string (info, attribute);
}

static gboolean
delete_file_recursively (GFile *file)
{
  gboolean success;
  g_autoptr (GError) error = NULL;

  do
    {
      g_autoptr (GFileEnumerator) enumerator = NULL;

      success = g_file_delete (file, NULL, &error);
      if (success ||
          !g_error_matches (error, G_IO_ERROR, G_IO_ERROR_NOT_EMPTY))
        break;

      g_clear_error (&error);
      enumerator = g_file_enumerate_children (file,
                                              G_FILE_ATTRIBUTE_STANDARD_NAME,
                                              G_FILE_QUERY_INFO_NONE,
                                              NULL, &error);

      if (enumerator)
        {
          GFileInfo *info;

          success = TRUE;
          info = g_file_enumerator_next_file (enumerator,
                                              NULL,
                                              &error);

          while (info != NULL)
            {
              g_autoptr (GFile) child = NULL;

              child = g_file_enumerator_get_child (enumerator, info);
              success = success && delete_file_recursively (child);
              g_object_unref (info);

              info = g_file_enumerator_next_file (enumerator, NULL, &error);
            }
        }

      if (error != NULL)
        success = FALSE;
    }
  while (success);

  return success;
}

static gboolean
delete_and_make_new_directory (GFile *folder, GError **error)
{
  gboolean retval = TRUE;
  g_autoptr(GError) child_error = NULL;

  while (g_file_make_directory (folder, NULL, &child_error) == FALSE)
    {
      gboolean needs_delete = FALSE;

      if (child_error != NULL)
        {
          if (child_error->code != G_IO_ERROR_EXISTS)
            {
              g_propagate_error (error, g_steal_pointer (&child_error));
              return FALSE;
            }
          else
            needs_delete = TRUE;
        }

      child_error = NULL;
      if (needs_delete)
        {
          g_file_delete (folder, NULL, &child_error);
          if (child_error != NULL)
            {
              if (child_error->code != G_IO_ERROR_NOT_EMPTY)
                {
                  retval = FALSE;
                  g_propagate_error (error, g_steal_pointer (&child_error));
                  break;
                }

              retval = delete_file_recursively (folder);
            }
        }
    }

  return retval;
}

static void
volume_mount_cb (GVolume           *volume,
                 GAsyncResult      *res,
                 MountData         *user_data)
{
  MountData *data = user_data;
  data->res = g_object_ref (res);
  g_main_loop_quit (data->loop);
}

static gboolean
volume_mount_sync (GVolume *volume, GMountMountFlags flags, GCancellable *cancellable, GError **error)
{
  g_autoptr (GMainContext) context  = NULL;
  g_autoptr (GMainLoop) loop = NULL;
  g_autoptr(GError) child_error = NULL;
  MountData data;
  gboolean retval = FALSE;

  context = g_main_context_new ();
  loop = g_main_loop_new (context, FALSE);
  g_main_context_push_thread_default (context);
  data.loop = loop;

  g_volume_mount (volume,
                  flags,
                  NULL,
                  cancellable,
                  (GAsyncReadyCallback) volume_mount_cb,
                  (gpointer) &data);

  g_main_loop_run (loop);

  retval = g_volume_mount_finish (volume, data.res, &child_error);

  if (child_error != NULL)
    g_propagate_error (error, g_steal_pointer (&child_error));

  g_object_unref (data.res);
  g_main_context_pop_thread_default (context);
  return retval;
}

static GFile *
create_temporary_duplicate_file (GFile *source_file, GError **error)
{
  static int file_num_counter = 0;
  g_autofree gchar *dest_file_title = NULL;
  g_autofree gchar *source_file_title = NULL;
  g_autofree gchar *parent_path = NULL;
  g_autoptr(GError) child_error = NULL;
  GFile *dest_file = NULL;
  g_autoptr(GFile) dummy_dest_file = NULL;
  g_autoptr(GFile) parent = NULL;
  gchar rand_int_string[G_ASCII_DTOSTR_BUF_SIZE];

  /* Generate a string (here created using the stringified file_num_counter) and use
   * this string as the title of the destination file. */
  g_ascii_dtostr (rand_int_string, G_ASCII_DTOSTR_BUF_SIZE, ++file_num_counter);

  source_file_title = get_file_attribute (source_file, G_FILE_ATTRIBUTE_STANDARD_DISPLAY_NAME, &child_error);
  if (child_error != NULL)
    {
      g_propagate_error (error, g_steal_pointer (&child_error));
      return NULL;
    }

  dest_file_title = g_strconcat (source_file_title, " (", rand_int_string, ")", NULL);

  parent = g_file_get_parent (source_file);
  parent_path = g_file_get_path (parent);
  dummy_dest_file = g_file_new_build_filename (parent_path, dest_file_title, NULL);

  g_file_copy (source_file, dummy_dest_file, G_FILE_COPY_NONE, NULL, NULL, NULL, &child_error);
  if (child_error != NULL)
    {
      g_propagate_error (error, g_steal_pointer (&child_error));
      return NULL;
    }

  dest_file = g_file_get_child_for_display_name (parent, dest_file_title, &child_error);
  if (child_error != NULL)
    {
      g_propagate_error (error, g_steal_pointer (&child_error));
      return NULL;
    }

  return dest_file;
}

/* ---------------------------------------------------------------------------------------------------- */
/* Test init and cleanup functions begin */
/* ---------------------------------------------------------------------------------------------------- */

static inline void
setup_google_mount_and_libgdata_service (GVfsGoogleTestData *self, GError **error)
{
  GList *l;
  GoaObject *test_account_goa_object = NULL;
  g_autofree gchar *mount_email = NULL;
  g_autofree gchar *volume_uuid = NULL;
  g_autolist(GoaObject) accounts = NULL;
  g_autolist(GMount) mounts = NULL;
  g_autoptr(GDataGoaAuthorizer) authorizer = NULL;
  g_autoptr(GError) child_error = NULL;
  g_autoptr(GVolumeMonitor) volume_monitor = NULL;
  g_autoptr(GVolume) volume = NULL;
  g_autoptr(GoaClient) client = NULL;

  /* We firstly check if there exists any mount with mount_name ending in
   * "@gmail.com" */
  volume_monitor = g_volume_monitor_get ();
  mounts = g_volume_monitor_get_mounts (volume_monitor);
  for (l = mounts; l != NULL; l = l->next)
    {
      g_autofree gchar *mount_name = NULL;
      mount_name = g_mount_get_name (G_MOUNT (l->data));
      if (g_str_has_suffix (mount_name, "@gmail.com"))
        {
          mount_email = g_strdup (mount_name);
          self->mount = g_object_ref (G_MOUNT (l->data));
        }
    }

  /* Basic setup pertaining to GOA, and creation of GDataDocumentsService */
  client = goa_client_new_sync (NULL, &child_error);
  if (child_error != NULL)
    {
      g_propagate_error (error, g_steal_pointer (&child_error));
      return;
    }

  accounts = goa_client_get_accounts (client);
  for (l = accounts; l != NULL; l = l->next)
    {
      const gchar *goa_account_email;
      GoaObject *object;
      GoaAccount *account;

      object = GOA_OBJECT (l->data);
      account = goa_object_peek_account (object);

      if (g_strcmp0 (goa_account_get_provider_type (account), "google") == 0)
        {
          if (mount_email != NULL)
            {
              goa_account_email = goa_account_get_identity (account);
              if (g_strcmp0 (goa_account_email, mount_email) == 0)
                {
                  test_account_goa_object = GOA_OBJECT (l->data);
                  break;
                }
            }
          else
            {
              test_account_goa_object = GOA_OBJECT (l->data);
              mount_email = goa_account_dup_identity (account);
              break;
            }
        }
    }

  if (test_account_goa_object == NULL)
    {
      g_error ("No GOA account found with the same email as %s", mount_email);
      return;
    }

  /* We've found an email linked to some Google Account, but the GMount hasn't
   * been mounted yet. So, we manually mount a GMount corresponding to this
   * mount_email. */
  if (self->mount == NULL)
    {
      g_autoptr(GFile) volume_activation_root = NULL;
      volume_uuid = g_strconcat ("google-drive://", mount_email, "/", NULL);
      if ((volume = g_volume_monitor_get_volume_for_uuid (volume_monitor, volume_uuid)) == NULL)
        {
          g_error ("No GVolume found corresponding to the UUID: %s", volume_uuid);
          return;
        }

      volume_mount_sync (volume, G_MOUNT_MOUNT_NONE, NULL, &child_error);
      if (child_error != NULL)
        {
          g_propagate_error (error, g_steal_pointer (&child_error));
          return;
        }

      volume_activation_root = g_volume_get_activation_root (volume);
      self->mount = g_file_find_enclosing_mount (volume_activation_root, NULL, &child_error);
      if (child_error != NULL)
        {
          g_propagate_error (error, g_steal_pointer (&child_error));
          return;
        }
    }

  authorizer = gdata_goa_authorizer_new (test_account_goa_object);
  self->service = gdata_documents_service_new (GDATA_AUTHORIZER (authorizer));
  if (self->service == NULL)
    {
      g_error ("Couldn't initialize libgdata service for email: %s", mount_email);
      return;
    }

  self->domain = gdata_documents_service_get_primary_authorization_domain ();
}

static void
gvfs_google_test_init (GVfsGoogleTestData **_self, GError **error)
{
  gboolean op_done;
  g_autofree gchar *root_path = NULL;
  g_autofree gchar *test_dir_path = NULL;
  g_autofree gchar *test_dir_id = NULL;
  g_autofree gchar *test_dummy_file_id = NULL;
  g_autoptr(GError) child_error = NULL;
  g_autoptr(GFile) root = NULL;
  g_autoptr(GFile) test_dir = NULL;
  g_autoptr(GFile) test_dummy_file = NULL;
  g_autoptr(GFile) copy_test_dir = NULL;
  g_autoptr(GFile) move_test_dir = NULL;
  g_autoptr(GFileOutputStream) file_write_stream = NULL;
  g_autoptr(GDataDocumentsEntry) root_entry = NULL;
  GVfsGoogleTestData *self;

  *_self = g_new0 (GVfsGoogleTestData, 1);
  self = *_self;
  self->mount = NULL;

  setup_google_mount_and_libgdata_service (self, &child_error);
  if (child_error != NULL)
    {
      g_propagate_error (error, g_steal_pointer (&child_error));
      return;
    }

  root_entry = GDATA_DOCUMENTS_ENTRY (gdata_service_query_single_entry (GDATA_SERVICE (self->service),
                                                                        self->domain,
                                                                        "root",
                                                                        NULL,
                                                                        GDATA_TYPE_DOCUMENTS_FOLDER,
                                                                        NULL,
                                                                        &child_error));
  if (child_error != NULL)
    {
      g_propagate_error (error, g_steal_pointer (&child_error));
      return;
    }

  /* The GFile corresponding to the root_entry we fetched above */
  root = g_mount_get_root (self->mount);
  root_path = g_file_get_path (root);
  test_dir = g_file_new_build_filename (root_path, GOOGLE_TEST_DIRECTORY, NULL);

  /* We create a test directory in the root folder (on Google Drive) where all
   * the other test files/folders shall be created */
  g_file_make_directory (test_dir, NULL, &child_error);
  if (child_error != NULL)
    {
      if (child_error->code != G_IO_ERROR_EXISTS)
        {
          g_propagate_error (error, g_steal_pointer (&child_error));
          return;
        }
      else
        child_error = NULL;
    }

  g_object_unref (test_dir);
  test_dir = g_file_get_child_for_display_name (root, GOOGLE_TEST_DIRECTORY, &child_error);
  if (child_error != NULL)
    {
      g_propagate_error (error, g_steal_pointer (&child_error));
      return;
    }

  self->test_dir = g_object_ref (test_dir);

  test_dir_id = get_file_attribute (self->test_dir, G_FILE_ATTRIBUTE_ID_FILE, &child_error);
  if (child_error != NULL)
    {
      g_propagate_error (error, g_steal_pointer (&child_error));
      return;
    }

  self->test_dir_entry = GDATA_DOCUMENTS_ENTRY (gdata_service_query_single_entry (GDATA_SERVICE (self->service),
                                                                                  self->domain,
                                                                                  test_dir_id,
                                                                                  NULL,
                                                                                  GDATA_TYPE_DOCUMENTS_FOLDER,
                                                                                  NULL,
                                                                                  &child_error));
  if (child_error != NULL)
    {
      g_propagate_error (error, g_steal_pointer (&child_error));
      return;
    }

  g_test_message ("Test folder GFile ID: %s", test_dir_id);
  g_test_message ("Test folder Entry ID: %s", gdata_entry_get_id (GDATA_ENTRY (self->test_dir_entry)));

  /* Now we create try to create a dummy file which we'll use for testing
   * copy/move/delete operations.If the file already exists, an error with
   * error code G_IO_ERROR_EXISTS is returned, and we simply use that existing
   * file for testing purposes. */
  test_dir_path = g_file_get_path (self->test_dir);
  test_dummy_file = g_file_new_build_filename (test_dir_path, TITLE_DUMMY_FILE, NULL);

  file_write_stream = g_file_create (test_dummy_file, G_FILE_CREATE_NONE, NULL, &child_error);
  if (child_error != NULL)
    {
      if (child_error->code != G_IO_ERROR_EXISTS)
        {
          g_propagate_error (error, g_steal_pointer (&child_error));
          return;
        }
      else
        {
          /* We simply ignore this because if file already exists, we don't
           * have any issues for initiating the test. */
          child_error = NULL;
        }
    }
  else
    {
      g_output_stream_write (G_OUTPUT_STREAM (file_write_stream), "SomeRandomText", 14, NULL, &child_error);
      if (child_error != NULL)
        {
          g_propagate_error (error, g_steal_pointer (&child_error));
          return;
        }

      g_output_stream_close (G_OUTPUT_STREAM (file_write_stream), NULL, &child_error);
      if (child_error != NULL)
        {
          g_propagate_error (error, g_steal_pointer (&child_error));
          return;
        }
    }

  g_object_unref (test_dummy_file);
  test_dummy_file = g_file_get_child_for_display_name (self->test_dir, TITLE_DUMMY_FILE, &child_error);
  if (child_error != NULL)
    {
      g_propagate_error (error, g_steal_pointer (&child_error));
      return;
    }

  test_dummy_file_id = get_file_attribute (test_dummy_file, G_FILE_ATTRIBUTE_ID_FILE, &child_error);
  if (child_error != NULL)
    {
      g_propagate_error (error, g_steal_pointer (&child_error));
      return;
    }

  g_test_message ("Test dummy GFile ID: %s", test_dummy_file_id);
  self->test_dummy_file = g_object_ref (test_dummy_file);

  copy_test_dir = g_file_new_build_filename (test_dir_path, OP_COPY_TEST_DIRECTORY, NULL);
  op_done = delete_and_make_new_directory (copy_test_dir, &child_error);
  if (child_error != NULL)
    {
      g_propagate_error (error, g_steal_pointer (&child_error));
      return;
    }
  g_assert_true (op_done);

  move_test_dir = g_file_new_build_filename (test_dir_path, OP_MOVE_TEST_DIRECTORY, NULL);
  op_done = delete_and_make_new_directory (move_test_dir, &child_error);
  if (child_error != NULL)
    {
      g_propagate_error (error, g_steal_pointer (&child_error));
      return;
    }
  g_assert_true (op_done);
}

static void
gvfs_google_test_exit_cleanup (GVfsGoogleTestData **_self, GError **error)
{
  GVfsGoogleTestData *self = *_self;

  g_return_if_fail (GDATA_IS_DOCUMENTS_ENTRY (self->test_dir_entry));
  g_return_if_fail (GDATA_IS_DOCUMENTS_SERVICE (self->service));

  g_assert_true (delete_file_recursively (self->test_dir));

  g_object_unref (self->service);
  g_object_unref (self->test_dir_entry);
  g_object_unref (self->test_dir);
  g_object_unref (self->test_dummy_file);
  g_object_unref (self->mount);
  g_free (self);
}

/* ---------------------------------------------------------------------------------------------------- */
/* Actual test functions begin */
/* ---------------------------------------------------------------------------------------------------- */

/* ~~~~~~~~~~~~~~~~~~~~~~~~~~ Make Directory Test Case Functions  ~~~~~~~~~~~~~~~~~~~~~~~~~~ */
static void
gvfs_google_test_make_directory_using_valid_display_name (gconstpointer user_data)
{
  GVfsGoogleTestData *self = (GVfsGoogleTestData *) user_data;
  g_autoptr(GError) error = NULL;
  g_autoptr(GFile) dummy_new_folder = NULL;
  g_autofree gchar *parent_path = NULL;
  const gchar *folder_display_name = "Valid-Display-Name-Dir";

  parent_path = g_file_get_path (self->test_dir);
  dummy_new_folder = g_file_new_build_filename (parent_path, folder_display_name, NULL);

  g_assert_true (delete_and_make_new_directory (dummy_new_folder, &error));
  g_assert_no_error (error);
  g_assert_true (g_file_query_exists (dummy_new_folder, NULL));
}

static void
gvfs_google_test_make_directory_using_valid_id (gconstpointer user_data)
{
  GVfsGoogleTestData *self = (GVfsGoogleTestData *) user_data;
  g_autoptr(GError) error = NULL;
  g_autoptr(GFile) dummy_new_folder = NULL;
  g_autoptr(GFile) actual_new_folder1 = NULL;
  g_autoptr(GFile) actual_new_folder2 = NULL;
  g_autoptr(GFileInfo) info1 = NULL;
  g_autoptr(GFileInfo) info2 = NULL;
  g_autofree gchar *parent_path = NULL;
  const gchar *intended_folder_title = gdata_entry_get_id (GDATA_ENTRY (self->test_dir_entry));
  const gchar *actual_folder_title = gdata_entry_get_title (GDATA_ENTRY (self->test_dir_entry));

  parent_path = g_file_get_path (self->test_dir);
  dummy_new_folder = g_file_new_build_filename (parent_path, intended_folder_title, NULL);

  g_assert_true (delete_and_make_new_directory (dummy_new_folder, &error));
  g_assert_no_error (error);

  error = NULL;
  actual_new_folder1 = g_file_get_child_for_display_name (self->test_dir, intended_folder_title, &error);
  g_assert_nonnull (actual_new_folder1);
  g_assert_no_error (error);

  error = NULL;
  actual_new_folder2 = g_file_get_child_for_display_name (self->test_dir, actual_folder_title, &error);
  g_assert_nonnull (actual_new_folder2);
  g_assert_no_error (error);

  info1 = g_file_query_info (actual_new_folder1, G_FILE_ATTRIBUTE_STANDARD_DISPLAY_NAME, G_FILE_QUERY_INFO_NONE, NULL, &error);
  g_assert_no_error (error);
  g_assert_nonnull (info1);

  info2 = g_file_query_info (actual_new_folder2, G_FILE_ATTRIBUTE_STANDARD_DISPLAY_NAME, G_FILE_QUERY_INFO_NONE, NULL, &error);
  g_assert_no_error (error);
  g_assert_nonnull (info1);

  g_assert_cmpstr (g_file_info_get_display_name (info1), ==, g_file_info_get_display_name (info2));
}

/* ~~~~~~~~~~~~~~~~~~~~~~~~~~ Copy Test Case Functions  ~~~~~~~~~~~~~~~~~~~~~~~~~~ */

static void
gvfs_google_test_copy_file_from_one_parent_to_other_using_same_title (gconstpointer user_data)
{
  GVfsGoogleTestData *self = (GVfsGoogleTestData *) user_data;
  gboolean op_done;
  g_autoptr(GError) error = NULL;
  g_autofree gchar *parent_path = NULL;
  g_autofree gchar *dest_path = NULL;
  g_autofree gchar *copy_test_dir_id = NULL;
  g_autofree gchar *dest_file_actual_display_name = NULL;
  g_autoptr(GFile) copy_test_dir = NULL;
  g_autoptr(GFile) dest_file = NULL;

  parent_path = g_file_get_path (self->test_dir);
  copy_test_dir = g_file_get_child_for_display_name (self->test_dir, OP_COPY_TEST_DIRECTORY, &error);
  g_assert_no_error (error);
  g_assert_nonnull (copy_test_dir);

  copy_test_dir_id = get_file_attribute (copy_test_dir, G_FILE_ATTRIBUTE_ID_FILE, &error);
  g_assert_no_error (error);
  g_assert_nonnull (copy_test_dir_id);

  dest_path = g_file_get_path (copy_test_dir);
  dest_file = g_file_new_build_filename (dest_path, TITLE_COPIED_FILE, NULL);

  op_done = g_file_copy (self->test_dummy_file, dest_file, G_FILE_COPY_NONE, NULL, NULL, NULL, &error);
  g_assert_no_error (error);
  g_assert_true (op_done);

  dest_file_actual_display_name = get_file_attribute (dest_file, G_FILE_ATTRIBUTE_STANDARD_DISPLAY_NAME, &error);
  g_assert_no_error (error);
  g_assert_cmpstr (dest_file_actual_display_name, ==, TITLE_COPIED_FILE);
}

static void
gvfs_google_test_copy_file_from_one_parent_to_other_using_id (gconstpointer user_data)
{
  GVfsGoogleTestData *self = (GVfsGoogleTestData *) user_data;
  gboolean op_done;
  g_autoptr(GError) error = NULL;
  g_autofree gchar *parent_path = NULL;
  g_autofree gchar *dest_path = NULL;
  g_autofree gchar *source_file_id = NULL;
  g_autofree gchar *source_file_title = NULL;
  g_autofree gchar *copy_test_dir_id = NULL;
  g_autofree gchar *dest_file_actual_display_name = NULL;
  g_autoptr(GFile) copy_test_dir = NULL;
  g_autoptr(GFile) dest_file = NULL;

  parent_path = g_file_get_path (self->test_dir);
  copy_test_dir = g_file_get_child_for_display_name (self->test_dir, OP_COPY_TEST_DIRECTORY, &error);
  g_assert_no_error (error);
  g_assert_nonnull (copy_test_dir);

  copy_test_dir_id = get_file_attribute (copy_test_dir, G_FILE_ATTRIBUTE_ID_FILE, &error);
  g_assert_no_error (error);
  g_assert_nonnull (copy_test_dir_id);

  source_file_id = get_file_attribute (self->test_dummy_file, G_FILE_ATTRIBUTE_ID_FILE, &error);
  g_assert_no_error (error);
  g_assert_nonnull (copy_test_dir_id);

  source_file_title = get_file_attribute (self->test_dummy_file, G_FILE_ATTRIBUTE_STANDARD_DISPLAY_NAME, &error);
  g_assert_no_error (error);
  g_assert_nonnull (source_file_title);

  dest_path = g_file_get_path (copy_test_dir);
  dest_file = g_file_new_build_filename (dest_path, source_file_id, NULL);

  op_done = g_file_copy (self->test_dummy_file, dest_file, G_FILE_COPY_NONE, NULL, NULL, NULL, &error);
  g_assert_no_error (error);
  g_assert_true (op_done);

  dest_file_actual_display_name = get_file_attribute (dest_file, G_FILE_ATTRIBUTE_STANDARD_DISPLAY_NAME, &error);
  g_assert_no_error (error);
  g_assert_nonnull (dest_file_actual_display_name);

  g_assert_cmpstr (dest_file_actual_display_name, ==, source_file_title);
}

static void
gvfs_google_test_copy_file_within_same_parent_with_title_change (gconstpointer user_data)
{
  GVfsGoogleTestData *self = (GVfsGoogleTestData *) user_data;
  gboolean op_done;
  g_autoptr(GError) error = NULL;
  g_autofree gchar *parent_path = NULL;
  g_autofree gchar *dest_file_id = NULL;
  g_autofree gchar *dest_file_title = NULL;
  g_autofree gchar *dest_file_actual_display_name = NULL;
  g_autoptr(GFile) source_file = NULL;
  g_autoptr(GFile) dest_file = NULL;

  source_file = g_file_get_child_for_display_name (self->test_dir, TITLE_DUMMY_FILE, &error);
  g_assert_no_error (error);
  g_assert_nonnull (source_file);

  dest_file_title = g_strconcat (TITLE_DUMMY_FILE, " (Copy)", NULL);
  parent_path = g_file_get_path (self->test_dir);
  dest_file = g_file_new_build_filename (parent_path, dest_file_title, NULL);

  op_done = g_file_copy (source_file, dest_file, G_FILE_COPY_NONE, NULL, NULL, NULL, &error);
  g_assert_no_error (error);
  g_assert_true (op_done);

  g_clear_object (&dest_file);
  dest_file = g_file_get_child_for_display_name (self->test_dir, dest_file_title, &error);
  g_assert_no_error (error);
  g_assert_nonnull (dest_file);

  dest_file_id = get_file_attribute (dest_file, G_FILE_ATTRIBUTE_ID_FILE, &error);
  g_assert_no_error (error);
  g_assert_nonnull (dest_file_id);

  dest_file_actual_display_name = get_file_attribute (dest_file, G_FILE_ATTRIBUTE_STANDARD_DISPLAY_NAME, &error);
  g_assert_no_error (error);
  g_assert_cmpstr (dest_file_actual_display_name, ==, dest_file_title);
}

static void
gvfs_google_test_copy_file_within_same_parent_with_same_title (gconstpointer user_data)
{
  GVfsGoogleTestData *self = (GVfsGoogleTestData *) user_data;
  gboolean op_done;
  g_autoptr(GError) error = NULL;
  g_autofree gchar *parent_path = NULL;
  g_autofree gchar *source_file_title = NULL;
  g_autoptr(GFile) source_file = NULL;
  g_autoptr(GFile) dummy_dest_file = NULL;

  source_file = g_file_get_child_for_display_name (self->test_dir, TITLE_DUMMY_FILE, &error);
  g_assert_no_error (error);
  g_assert_nonnull (source_file);

  source_file_title = get_file_attribute (source_file, G_FILE_ATTRIBUTE_STANDARD_DISPLAY_NAME, &error);
  g_assert_no_error (error);
  g_assert_nonnull (source_file_title);

  parent_path = g_file_get_path (self->test_dir);
  dummy_dest_file = g_file_new_build_filename (parent_path, source_file_title, NULL);

  op_done = g_file_copy (source_file, dummy_dest_file, G_FILE_COPY_NONE, NULL, NULL, NULL, &error);
  g_assert_error (error, G_IO_ERROR, G_IO_ERROR_EXISTS);
  g_assert_true (!op_done);
}

static void
gvfs_google_test_copy_file_within_same_parent_try_overwrite_with_id (gconstpointer user_data)
{
  GVfsGoogleTestData *self = (GVfsGoogleTestData *) user_data;
  gboolean op_done;
  g_autoptr(GError) error = NULL;
  g_autofree gchar *parent_path = NULL;
  g_autofree gchar *source_file_id = NULL;
  g_autoptr(GFile) source_file = NULL;
  g_autoptr(GFile) dummy_dest_file = NULL;

  source_file = g_file_get_child_for_display_name (self->test_dir, TITLE_DUMMY_FILE, &error);
  g_assert_no_error (error);
  g_assert_nonnull (source_file);

  source_file_id = get_file_attribute (source_file, G_FILE_ATTRIBUTE_ID_FILE, &error);
  g_assert_no_error (error);
  g_assert_nonnull (source_file_id);

  parent_path = g_file_get_path (self->test_dir);
  dummy_dest_file = g_file_new_build_filename (parent_path, source_file_id, NULL);

  op_done = g_file_copy (source_file, dummy_dest_file, G_FILE_COPY_OVERWRITE, NULL, NULL, NULL, &error);
  g_assert_error (error, G_IO_ERROR, G_IO_ERROR_FAILED);
  g_assert_true (!op_done);
}

static void
gvfs_google_test_copy_file_within_same_parent_with_source_id_as_destination_basename (gconstpointer user_data)
{
  GVfsGoogleTestData *self = (GVfsGoogleTestData *) user_data;
  gboolean op_done;
  g_autoptr(GError) error = NULL;
  g_autofree gchar *parent_path = NULL;
  g_autofree gchar *source_file_id = NULL;
  g_autoptr(GFile) source_file = NULL;
  g_autoptr(GFile) dummy_dest_file = NULL;

  source_file = g_file_get_child_for_display_name (self->test_dir, TITLE_DUMMY_FILE, &error);
  g_assert_no_error (error);
  g_assert_nonnull (source_file);

  source_file_id = get_file_attribute (source_file, G_FILE_ATTRIBUTE_ID_FILE, &error);
  g_assert_no_error (error);
  g_assert_nonnull (source_file_id);

  parent_path = g_file_get_path (self->test_dir);
  dummy_dest_file = g_file_new_build_filename (parent_path, source_file_id, NULL);

  op_done = g_file_copy (source_file, dummy_dest_file, G_FILE_COPY_NONE, NULL, NULL, NULL, &error);
  g_assert_error (error, G_IO_ERROR, G_IO_ERROR_EXISTS);
  g_assert_true (!op_done);
}

static void
gvfs_google_test_copy_file_within_same_parent_try_overwrite_with_same_title (gconstpointer user_data)
{
  GVfsGoogleTestData *self = (GVfsGoogleTestData *) user_data;
  gboolean op_done;
  g_autoptr(GError) error = NULL;
  g_autofree gchar *parent_path = NULL;
  g_autofree gchar *source_file_title = NULL;
  g_autoptr(GFile) source_file = NULL;
  g_autoptr(GFile) dummy_dest_file = NULL;

  source_file = g_file_get_child_for_display_name (self->test_dir, TITLE_DUMMY_FILE, &error);
  g_assert_no_error (error);
  g_assert_nonnull (source_file);

  source_file_title = get_file_attribute (source_file, G_FILE_ATTRIBUTE_STANDARD_DISPLAY_NAME, &error);
  g_assert_no_error (error);
  g_assert_nonnull (source_file_title);

  parent_path = g_file_get_path (self->test_dir);
  dummy_dest_file = g_file_new_build_filename (parent_path, source_file_title, NULL);

  op_done = g_file_copy (source_file, dummy_dest_file, G_FILE_COPY_OVERWRITE, NULL, NULL, NULL, &error);
  g_assert_error (error, G_IO_ERROR, G_IO_ERROR_FAILED);
  g_assert_true (!op_done);
}

static void
gvfs_google_test_copy_file_from_one_parent_to_other_with_volatile_entry_collision_only (gconstpointer user_data)
{
  GVfsGoogleTestData *self = (GVfsGoogleTestData *) user_data;
  gboolean op_done;
  gchar rand_int_string[G_ASCII_DTOSTR_BUF_SIZE];
  g_autoptr(GError) error = NULL;
  g_autofree gchar *parent_path = NULL;
  g_autoptr(GFile) source_file = NULL;
  g_autoptr(GFile) copy_test_dir = NULL;
  g_autoptr(GFile) dummy_dest_file = NULL;

  copy_test_dir = g_file_get_child_for_display_name (self->test_dir, OP_COPY_TEST_DIRECTORY, &error);
  g_assert_no_error (error);
  g_assert_nonnull (copy_test_dir);

  source_file = g_file_get_child_for_display_name (self->test_dir, TITLE_DUMMY_FILE, &error);
  g_assert_no_error (error);
  g_assert_nonnull (source_file);

  /* Generate any random string (here created using a random integer) and use
   * this random string as the title of the destination file. We do this so
   * that we can cause a collision just on the volatile entry and not on the
   * title. */
  g_ascii_dtostr (rand_int_string, G_ASCII_DTOSTR_BUF_SIZE, rand ());

  parent_path = g_file_get_path (copy_test_dir);
  dummy_dest_file = g_file_new_build_filename (parent_path, rand_int_string, NULL);

  op_done = g_file_copy (source_file, dummy_dest_file, G_FILE_COPY_OVERWRITE, NULL, NULL, NULL, &error);
  g_assert_no_error (error);
  g_assert_true (op_done);
}

/* ~~~~~~~~~~~~~~~~~~~~~~~~~~ Move Test Case Functions  ~~~~~~~~~~~~~~~~~~~~~~~~~~ */

static void
gvfs_google_test_move_file_within_same_parent_without_title_change (gconstpointer user_data)
{
  GVfsGoogleTestData *self = (GVfsGoogleTestData *) user_data;
  gboolean op_done;
  g_autoptr(GError) error = NULL;
  g_autofree gchar *parent_path = NULL;
  g_autofree gchar *dest_file_title = NULL;
  g_autoptr(GFile) source_file = NULL;
  g_autoptr(GFile) dest_file = NULL;

  /* We need some file which we can move. So, we use test_dummy_file to create
   * a copy of that file and move its copy around using its ID. */
  source_file = create_temporary_duplicate_file (self->test_dummy_file, &error);
  g_assert_no_error (error);
  g_assert_nonnull (source_file);

  dest_file_title = get_file_attribute (source_file, G_FILE_ATTRIBUTE_STANDARD_DISPLAY_NAME, &error);
  parent_path = g_file_get_path (self->test_dir);
  dest_file = g_file_new_build_filename (parent_path, dest_file_title, NULL);

  op_done = g_file_move (source_file, dest_file, G_FILE_COPY_NONE, NULL, NULL, NULL, &error);
  g_assert_error (error, G_IO_ERROR, G_IO_ERROR_EXISTS);
  g_assert_false (op_done);
}

static void
gvfs_google_test_move_file_within_same_parent_with_title_change (gconstpointer user_data)
{
  GVfsGoogleTestData *self = (GVfsGoogleTestData *) user_data;
  gboolean op_done;
  g_autoptr(GError) error = NULL;
  g_autofree gchar *parent_path = NULL;
  g_autofree gchar *dest_file_title = NULL;
  g_autofree gchar *dest_file_actual_display_name = NULL;
  g_autoptr(GFile) source_file = NULL;
  g_autoptr(GFile) dest_file = NULL;

  /* We need some file which we can move. So, we use test_dummy_file to create
   * a copy of that file and move its copy around using its ID. */
  source_file = create_temporary_duplicate_file (self->test_dummy_file, &error);
  g_assert_no_error (error);
  g_assert_nonnull (source_file);

  dest_file_title = g_strconcat (TITLE_DUMMY_FILE, "_test_move_file_within_same_parent_with_title_change", NULL);
  parent_path = g_file_get_path (self->test_dir);
  dest_file = g_file_new_build_filename (parent_path, dest_file_title, NULL);

  op_done = g_file_move (source_file, dest_file, G_FILE_COPY_NONE, NULL, NULL, NULL, &error);
  g_assert_no_error (error);
  g_assert_true (op_done);

  g_clear_object (&dest_file);
  dest_file = g_file_get_child_for_display_name (self->test_dir, dest_file_title, &error);
  g_assert_no_error (error);
  g_assert_nonnull (dest_file);

  dest_file_actual_display_name = get_file_attribute (dest_file, G_FILE_ATTRIBUTE_STANDARD_DISPLAY_NAME, &error);
  g_assert_no_error (error);
  g_assert_cmpstr (dest_file_actual_display_name, ==, dest_file_title);
}

static void
gvfs_google_test_move_file_from_one_parent_to_other_without_backup (gconstpointer user_data)
{
  GVfsGoogleTestData *self = (GVfsGoogleTestData *) user_data;
  gboolean op_done;
  g_autoptr(GError) error = NULL;
  g_autofree gchar *dest_path = NULL;
  g_autofree gchar *source_file_title = NULL;
  g_autofree gchar *dest_file_actual_display_name = NULL;
  g_autoptr(GFile) move_test_dir = NULL;
  g_autoptr(GFile) source_file = NULL;
  g_autoptr(GFile) dest_file = NULL;

  move_test_dir = g_file_get_child_for_display_name (self->test_dir, OP_MOVE_TEST_DIRECTORY, &error);
  g_assert_no_error (error);
  g_assert_nonnull (move_test_dir);

  dest_path = g_file_get_path (move_test_dir);
  dest_file = g_file_new_build_filename (dest_path, TITLE_MOVED_FILE, NULL);

  /* We need some file which we can move. So, we use test_dummy_file to create
   * a copy of that file and move its copy around using its ID. */
  source_file = create_temporary_duplicate_file (self->test_dummy_file, &error);
  g_assert_no_error (error);
  g_assert_nonnull (source_file);

  source_file_title = get_file_attribute (source_file, G_FILE_ATTRIBUTE_STANDARD_DISPLAY_NAME, &error);
  g_assert_no_error (error);
  g_assert_nonnull (source_file_title);

  dest_path = g_file_get_path (move_test_dir);
  dest_file = g_file_new_build_filename (dest_path, source_file_title, NULL);

  op_done = g_file_move (source_file, dest_file, G_FILE_COPY_NONE, NULL, NULL, NULL, &error);
  g_assert_no_error (error);
  g_assert_true (op_done);

  dest_file_actual_display_name = get_file_attribute (dest_file, G_FILE_ATTRIBUTE_STANDARD_DISPLAY_NAME, &error);
  g_assert_cmpstr (dest_file_actual_display_name, ==, source_file_title);
}

static void
gvfs_google_test_move_file_from_one_parent_to_other_with_backup (gconstpointer user_data)
{
  GVfsGoogleTestData *self = (GVfsGoogleTestData *) user_data;
  gboolean op_done;
  g_autoptr(GError) error = NULL;
  g_autofree gchar *parent_path = NULL;
  g_autofree gchar *dest_path = NULL;
  g_autofree gchar *source_file_title = NULL;
  g_autofree gchar *dest_file_actual_display_name = NULL;
  g_autoptr(GFile) move_test_dir = NULL;
  g_autoptr(GFile) source_file = NULL;
  g_autoptr(GFile) dest_file = NULL;

  parent_path = g_file_get_path (self->test_dir);
  move_test_dir = g_file_get_child_for_display_name (self->test_dir, OP_MOVE_TEST_DIRECTORY, &error);
  g_assert_no_error (error);
  g_assert_nonnull (move_test_dir);

  /* We need some file which we can move. So, we use test_dummy_file to create
   * a copy of that file and move its copy around using its ID. */
  source_file = create_temporary_duplicate_file (self->test_dummy_file, &error);
  g_assert_no_error (error);
  g_assert_nonnull (source_file);

  source_file_title = get_file_attribute (source_file, G_FILE_ATTRIBUTE_STANDARD_DISPLAY_NAME, &error);
  g_assert_no_error (error);
  g_assert_nonnull (source_file_title);

  dest_path = g_file_get_path (move_test_dir);
  dest_file = g_file_new_build_filename (dest_path, source_file_title, NULL);

  op_done = g_file_move (source_file, dest_file, G_FILE_COPY_BACKUP, NULL, NULL, NULL, &error);
  g_assert_no_error (error);
  g_assert_true (op_done);

  dest_file_actual_display_name = get_file_attribute (dest_file, G_FILE_ATTRIBUTE_STANDARD_DISPLAY_NAME, &error);
  g_assert_cmpstr (dest_file_actual_display_name, ==, source_file_title);
}

static void
gvfs_google_test_move_file_from_one_parent_to_other_using_same_title (gconstpointer user_data)
{
  GVfsGoogleTestData *self = (GVfsGoogleTestData *) user_data;
  gboolean op_done;
  g_autoptr(GError) error = NULL;
  g_autofree gchar *parent_path = NULL;
  g_autofree gchar *dest_path = NULL;
  g_autofree gchar *source_file_title = NULL;
  g_autofree gchar *dest_file_actual_display_name = NULL;
  g_autoptr(GFile) move_test_dir = NULL;
  g_autoptr(GFile) source_file = NULL;
  g_autoptr(GFile) dest_file = NULL;

  parent_path = g_file_get_path (self->test_dir);
  move_test_dir = g_file_get_child_for_display_name (self->test_dir, OP_MOVE_TEST_DIRECTORY, &error);
  g_assert_no_error (error);
  g_assert_nonnull (move_test_dir);

  /* We need some file which we can move. So, we use test_dummy_file to create
   * a copy of that file and move its copy around using its ID. */
  source_file = create_temporary_duplicate_file (self->test_dummy_file, &error);
  g_assert_no_error (error);
  g_assert_nonnull (source_file);

  source_file_title = get_file_attribute (source_file, G_FILE_ATTRIBUTE_STANDARD_DISPLAY_NAME, &error);
  g_assert_no_error (error);
  g_assert_nonnull (source_file_title);

  dest_path = g_file_get_path (move_test_dir);
  dest_file = g_file_new_build_filename (dest_path, source_file_title, NULL);

  op_done = g_file_move (source_file, dest_file, G_FILE_COPY_NONE, NULL, NULL, NULL, &error);
  g_assert_no_error (error);
  g_assert_true (op_done);

  dest_file_actual_display_name = get_file_attribute (dest_file, G_FILE_ATTRIBUTE_STANDARD_DISPLAY_NAME, &error);
  g_assert_cmpstr (dest_file_actual_display_name, ==, source_file_title);
}

static void
gvfs_google_test_move_file_from_one_parent_to_other_using_new_title (gconstpointer user_data)
{
  GVfsGoogleTestData *self = (GVfsGoogleTestData *) user_data;
  gboolean op_done;
  g_autoptr(GError) error = NULL;
  g_autofree gchar *parent_path = NULL;
  g_autofree gchar *dest_path = NULL;
  g_autofree gchar *source_file_title = NULL;
  g_autofree gchar *new_file_title = NULL;
  g_autofree gchar *dest_file_actual_display_name = NULL;
  g_autoptr(GFile) move_test_dir = NULL;
  g_autoptr(GFile) source_file = NULL;
  g_autoptr(GFile) dest_file = NULL;

  parent_path = g_file_get_path (self->test_dir);
  move_test_dir = g_file_get_child_for_display_name (self->test_dir, OP_MOVE_TEST_DIRECTORY, &error);
  g_assert_no_error (error);
  g_assert_nonnull (move_test_dir);

  /* We need some file which we can move. So, we use test_dummy_file to create
   * a copy of that file and move its copy around using its ID. */
  source_file = create_temporary_duplicate_file (self->test_dummy_file, &error);
  g_assert_no_error (error);
  g_assert_nonnull (source_file);

  source_file_title = get_file_attribute (source_file, G_FILE_ATTRIBUTE_STANDARD_DISPLAY_NAME, &error);
  g_assert_no_error (error);
  g_assert_nonnull (source_file_title);

  new_file_title = g_strconcat (source_file_title, " (NewTitle)", NULL);
  dest_path = g_file_get_path (move_test_dir);
  dest_file = g_file_new_build_filename (dest_path, new_file_title, NULL);

  op_done = g_file_move (source_file, dest_file, G_FILE_COPY_NONE, NULL, NULL, NULL, &error);
  g_assert_no_error (error);
  g_assert_true (op_done);

  dest_file_actual_display_name = get_file_attribute (dest_file, G_FILE_ATTRIBUTE_STANDARD_DISPLAY_NAME, &error);
  g_assert_cmpstr (dest_file_actual_display_name, ==, new_file_title);
}

static void
gvfs_google_test_move_file_from_one_parent_to_other_using_id (gconstpointer user_data)
{
  GVfsGoogleTestData *self = (GVfsGoogleTestData *) user_data;
  gboolean op_done;
  g_autoptr(GError) error = NULL;
  g_autofree gchar *test_dir_path = NULL;
  g_autofree gchar *dest_path = NULL;
  g_autofree gchar *source_file_id = NULL;
  g_autofree gchar *source_file_title = NULL;
  g_autofree gchar *dest_file_actual_display_name = NULL;
  g_autoptr(GFile) move_test_dir = NULL;
  g_autoptr(GFile) source_file = NULL;
  g_autoptr(GFile) dest_file = NULL;

  /* We need some file which we can move. So, we use test_dummy_file to create
   * a copy of that file and move its copy around using its ID.   */
  source_file = create_temporary_duplicate_file (self->test_dummy_file, &error);
  g_assert_no_error (error);
  g_assert_nonnull (source_file);

  test_dir_path = g_file_get_path (self->test_dir);
  move_test_dir = g_file_get_child_for_display_name (self->test_dir, OP_MOVE_TEST_DIRECTORY, &error);
  g_assert_no_error (error);
  g_assert_nonnull (move_test_dir);

  source_file_title = get_file_attribute (source_file, G_FILE_ATTRIBUTE_STANDARD_DISPLAY_NAME, &error);
  g_assert_no_error (error);
  g_assert_nonnull (source_file_title);

  source_file_id = get_file_attribute (source_file, G_FILE_ATTRIBUTE_ID_FILE, &error);
  g_assert_no_error (error);
  g_assert_nonnull (source_file_id);

  dest_path = g_file_get_path (move_test_dir);
  dest_file = g_file_new_build_filename (dest_path, source_file_id, NULL);

  op_done = g_file_move (source_file, dest_file, G_FILE_COPY_NONE, NULL, NULL, NULL, &error);
  g_assert_no_error (error);
  g_assert_true (op_done);

  dest_file_actual_display_name = get_file_attribute (dest_file, G_FILE_ATTRIBUTE_STANDARD_DISPLAY_NAME, &error);
  g_assert_no_error (error);
  g_assert_nonnull (dest_file_actual_display_name);

  g_assert_cmpstr (dest_file_actual_display_name, ==, source_file_title);
}

#ifdef HAVE_GDATA_DOCUMENTS_QUERY_SET_ORDER_BY
static void
gvfs_google_test_move_file_from_one_parent_to_other_with_both_title_and_volatile_entry_collision (gconstpointer user_data)
{
  GVfsGoogleTestData *self = (GVfsGoogleTestData *) user_data;
  gboolean op_done;
  g_autoptr(GError) error = NULL;
  g_autofree gchar *test_dir_path = NULL;
  g_autofree gchar *parent_path = NULL;
  g_autofree gchar *source_file_id = NULL;
  g_autoptr(GFile) move_test_dir = NULL;
  g_autoptr(GFile) source_file = NULL;
  g_autoptr(GFile) dest_file = NULL;

  /* We need some file which we can firstly copy. So, we use test_dummy_file to create
   * a copy of that file and move its copy around using its ID.  */
  source_file = create_temporary_duplicate_file (self->test_dummy_file, &error);
  g_assert_no_error (error);
  g_assert_nonnull (source_file);

  source_file_id = get_file_attribute (source_file, G_FILE_ATTRIBUTE_ID_FILE, &error);
  g_assert_no_error (error);
  g_assert_nonnull (source_file_id);

  test_dir_path = g_file_get_path (self->test_dir);
  move_test_dir = g_file_get_child_for_display_name (self->test_dir, OP_MOVE_TEST_DIRECTORY, &error);
  g_assert_no_error (error);
  g_assert_nonnull (move_test_dir);

  parent_path = g_file_get_path (move_test_dir);
  dest_file = g_file_new_build_filename (parent_path, source_file_id, NULL);

  /* The copy operation, i.e. `gio copy id1 ./id2/` (which is equivalent to
   * `gio copy id1 ./id2/id1`)  */
  op_done = g_file_copy (source_file, dest_file, G_FILE_COPY_NONE, NULL, NULL, NULL, &error);
  g_assert_no_error (error);
  g_assert_true (op_done);

  /* The move operation, i.e. `gio move id1 ./id2/` (which is equivalent to
   * `gio move id1 ./id2/id1`) */
  op_done = g_file_move (source_file, dest_file, G_FILE_COPY_NONE, NULL, NULL, NULL, &error);
  g_assert_no_error (error);
  g_assert_true (op_done);

  /* TODO: Assert here that both the files are present */
}
#endif

/* ~~~~~~~~~~~~~~~~~~~~~~~~~~ Delete Test Case Functions  ~~~~~~~~~~~~~~~~~~~~~~~~~~ */

static void
gvfs_google_test_recursive_delete_test_dir_folder (gconstpointer user_data)
{
  GVfsGoogleTestData *self = (GVfsGoogleTestData *) user_data;
  gboolean op_done;
  g_autoptr(GError) error = NULL;

  op_done = delete_and_make_new_directory (self->test_dir, &error);
  g_assert_no_error (error);
  g_assert_true (op_done);
}

/* ~~~~~~~~~~~~~~~~~~~~~~~~~~ Main Function  ~~~~~~~~~~~~~~~~~~~~~~~~~~ */

gint
main (gint argc, gchar *argv [])
{
  gint retval = EXIT_SUCCESS;
  GVfsGoogleTestData *self;
  g_autoptr(GError) error = NULL;

  setlocale (LC_ALL, "");

  g_test_init (&argc, &argv, NULL);

  gvfs_google_test_init (&self, &error);
  if (error != NULL)
    {
      g_warning ("Error (init): %s", error->message);
      return 1;
    }

  /* ~~~~~~~~~~~~~~~~~~~~~~~~~~ Make Dir test cases  ~~~~~~~~~~~~~~~~~~~~~~~~~~ */

  /* Test Scenario: We try to create a folder with its title set to the string
   * "valid_display_name_directory".
   *
   * Expected Behaviour: The newly created folder has the title
   * "valid_display_name_directory".
   *
   * Actual Behaviour is same as expected behaviour. */
  g_test_add_data_func ("/make_directory/using_valid_display_name",
                        (gconstpointer) self,
                        gvfs_google_test_make_directory_using_valid_display_name);

  /* Test Scenario: We try to create a folder having the title of the folder set
   * to some other file/folder's ID. So, we try to create a new folder with
   * self->test_dir_entry's ID.
   *
   * Expected Behaviour: The newly created folder has the same title as
   * self->test_dir_entry's ID.
   *
   * Actual Behaviour: The newly created folder gets its title set to
   * self->test_dir_entry's title and *NOT* its ID. The reason is well documented
   * in the make_directory function in gvfsbackendgoogle.c */
  g_test_add_data_func ("/make_directory/using_valid_id_of_other_directory",
                        (gconstpointer) self,
                        gvfs_google_test_make_directory_using_valid_id);

  /* ~~~~~~~~~~~~~~~~~~~~~~~~~~ Copy test cases  ~~~~~~~~~~~~~~~~~~~~~~~~~~ */

  /* The below test case is equivalent to the following commandline operations:
   * (Assume that file with ID `id1` has the title $T)
   *
   * `gio copy id1 ./"$T (copy)"`           (where id2 is the ID of a folder)
   *
   * This operation mimics what nautilus does when we do a Ctrl+C & Ctrl+V
   * operation on a file inside same parent directory. It simply appends a
   * " (copy)" to the name of the file. */
  g_test_add_data_func ("/copy_file/within_same_parent_with_title_change",
                        (gconstpointer) self,
                        gvfs_google_test_copy_file_within_same_parent_with_title_change);

  /* We try to copy a file within the same parent without changing the title, i.e. this
   * operation corresponds to `gio copy -i ./file1_title ./file1_title` but without overwrite.
   *
   * This operation should supposedly fail with error code `G_IO_ERROR_EXISTS`
   * and string "Target file already exists". */
  g_test_add_data_func ("/copy_file/within_same_parent_with_same_title",
                        (gconstpointer) self,
                        gvfs_google_test_copy_file_within_same_parent_with_same_title);

  /* We try to copy a file within the same parent and keeping the title of file
   * to be same as the ID of source file, i.e. this operation corresponds to
   * `gio copy -i ./id1 ./id1` but without overwrite.
   *
   * This operation should supposedly fail with error code `G_IO_ERROR_EXISTS`
   * and string "Target file already exists". */
  g_test_add_data_func ("/copy_file/within_same_parent_with_source_id_as_destination_basename",
                        (gconstpointer) self,
                        gvfs_google_test_copy_file_within_same_parent_with_source_id_as_destination_basename);

  /* The below test case is equivalent to the following commandline operations:
   *
   * `gio copy ./id1 ./$Title$` where $Title$ is the title of file with ID `id1`.
   *
   * This operation should supposedly fail when done over the commandline gio
   * tool with error code `G_IO_ERROR_FAILED` and string "Operation Not Supported".
   * TODO: Fix the below test-case to use actual overwrite. */
  g_test_add_data_func ("/copy_file/within_same_parent_try_overwrite_with_same_title",
                        (gconstpointer) self,
                        gvfs_google_test_copy_file_within_same_parent_try_overwrite_with_same_title);

  /* The below test case is equivalent to the following commandline operations:
   *
   * `gio copy id1 ./id1`
   *
   * This operation should supposedly fail when done over the commandline gio
   * tool with error code `G_IO_ERROR_FAILED` and string "Operation Not Supported".
   * TODO: Fix the below test-case to use actual overwrite. */
  g_test_add_data_func ("/copy_file/within_same_parent_try_overwrite_with_id",
                        (gconstpointer) self,
                        gvfs_google_test_copy_file_within_same_parent_try_overwrite_with_id);

  /* The below test case is equivalent to the following commandline operations:
   * `gio copy id1 id2/$Title$`  (where id2 is the ID of a folder, and $Title$
   * is the title of file with ID `id1`) */
  g_test_add_data_func ("/copy_file/from_one_parent_to_other_using_same_title",
                        (gconstpointer) self,
                        gvfs_google_test_copy_file_from_one_parent_to_other_using_same_title);

  /* The below test cases causes just a collision on the volatile entry. This
   * is because the previously executed test copies a file into the OP_COPY_TEST_DIRECTORY.
   * So, one volatile entry was already there because of that file, and we
   * change the title here to only cause a collision on the volatile entry (and
   * not the title too).
   *
   * It basically mimics the following case:
   * `gio copy id1 id2/some_title`
   * `gio copy id1 id2/some_random_title` */
  g_test_add_data_func ("/copy_file/from_one_parent_to_other_with_volatile_entry_collision_only",
                        (gconstpointer) self,
                        gvfs_google_test_copy_file_from_one_parent_to_other_with_volatile_entry_collision_only);

  /* The below test case uses the same function as the test case with
   * identifier "/copy_file/from_one_parent_to_other" because copying
   * file from source to destination is same as producing both kinds of
   * collisions at once.
   *
   * It basically mimics the following case:
   * `gio copy id1 id2/some_title`
   * `gio copy id1 id2/some_title`
   *
   * TODO: Re-enable the below test-case once you have orderBy="modifiedDate" */
#ifdef HAVE_GDATA_DOCUMENTS_QUERY_SET_ORDER_BY
  g_test_add_data_func ("/copy_file/from_one_parent_to_other_with_both_title_and_volatile_entry_collision",
                        (gconstpointer) self,
                        gvfs_google_test_copy_file_from_one_parent_to_other_using_same_title);
#endif

  /* The below test case is equivalent to the following commandline operations:
   * `gio copy id1 id2/id1`  (where id2 is the ID of a folder) */
  g_test_add_data_func ("/copy_file/from_one_parent_to_other_using_id",
                        (gconstpointer) self,
                        gvfs_google_test_copy_file_from_one_parent_to_other_using_id);

  /* ~~~~~~~~~~~~~~~~~~~~~~~~~~ Move test cases  ~~~~~~~~~~~~~~~~~~~~~~~~~~ */

  /* The below test case is equivalent to the following commandline operations:
   * (Assume that file with ID `id1` has the title "title1")
   *
   * `gio move ./title1 ./title1`
   *
   * This operation will fail with an error */
  g_test_add_data_func ("/move_file/within_same_parent_without_title_change",
                        (gconstpointer) self,
                        gvfs_google_test_move_file_within_same_parent_without_title_change);

  /* The below test case is equivalent to the following commandline operations:
   * (Assume that file with ID `id1` has the title ${TITLE})
   *
   * `gio move ./${TITLE} ./SomeOtherTitle`
   *
   * This is just a normal rename operation in POSIX world, and the backend allows this. */
  g_test_add_data_func ("/move_file/within_same_parent_with_title_change",
                        (gconstpointer) self,
                        gvfs_google_test_move_file_within_same_parent_with_title_change);

  /* The below test case is equivalent to the following commandline operations:
   * `gio move ./id1 ./id2/$Title$` (where `id2` is the ID of a folder, and
   * $Title$ is the title of file with ID `id1`).
   *
   * This is simplest case for cross directory file moving. */
  g_test_add_data_func ("/move_file/from_one_parent_to_other_without_backup",
                        (gconstpointer) self,
                        gvfs_google_test_move_file_from_one_parent_to_other_without_backup);

  /* The below test case is equivalent to the following commandline operations
   * (with the G_FILE_COPY_BACKUP flag supplied to g_file_move function):
   * `gio move ./id1 id2/$Title$`  (where `id2` is the ID of a folder, and
   * $Title$ is the title of file with ID `id1`)
   *
   * Although the g_vfs_backend_google_move () will fail with G_IO_ERROR_NOT_SUPPORTED,
   * gio will use the fallback (copy + create + read + write + delete) to
   * support a backup operation. */
  g_test_add_data_func ("/move_file/from_one_parent_to_other_with_backup",
                        (gconstpointer) self,
                        gvfs_google_test_move_file_from_one_parent_to_other_with_backup);

  /* The below test case is equivalent to the following commandline operations:
   * `gio move ./id1 id2/$Title$`  (where `id2` is the ID of a folder, and
   * $Title$ is the title of file with ID `id1`) */
  g_test_add_data_func ("/move_file/from_one_parent_to_other_using_same_title",
                        (gconstpointer) self,
                        gvfs_google_test_move_file_from_one_parent_to_other_using_same_title);

  /* The below test case is equivalent to the following commandline operations:
   * `gio move ./id1 id2/SomeNewTitle`  (where `id2` is the ID of a folder, and
   * $Title$ is the title of file with ID `id1`) */
  g_test_add_data_func ("/move_file/from_one_parent_to_other_using_new_title",
                        (gconstpointer) self,
                        gvfs_google_test_move_file_from_one_parent_to_other_using_new_title);

  /* The below test case is equivalent to the following commandline operations:
   * `gio move ./id1 id2/id1`  (where `id2` is the ID of a folder) */
  g_test_add_data_func ("/move_file/from_one_parent_to_other_using_id",
                        (gconstpointer) self,
                        gvfs_google_test_move_file_from_one_parent_to_other_using_id);

  /* The below test case is equivalent to the following commandline operations:
   *
   * 1. `gio copy ./id1 ./id2/`                   ("id2" is the real ID of a folder)
   * 2. `gio move ./id1 ./id2/`
   *
   * In this, the copy operation will create a volatile entry i.e. (id1, id2) -> Entry tuple.
   * Later, during the move operation, this volatile entry will collide with
   * the real ID "id1". We support this operation and the below test should pass.
   * TODO: Re-enable the below test-case once you have orderBy="modifiedDate" */
#ifdef HAVE_GDATA_DOCUMENTS_QUERY_SET_ORDER_BY
  g_test_add_data_func ("/move_file/from_one_parent_to_other_with_both_title_and_volatile_entry_collision",
                        (gconstpointer) self,
                        gvfs_google_test_move_file_from_one_parent_to_other_with_both_title_and_volatile_entry_collision);
#endif

  /* ~~~~~~~~~~~~~~~~~~~~~~~~~~ Delete test cases  ~~~~~~~~~~~~~~~~~~~~~~~~~~ */

  /* We simply delete everything in the below case. If the backend crashes at
   * this point, there is some issue with cache. Moreover, delete is the case
   * which can be used to ensure the sanity of the cache. If delete wrongly
   * removes an entry (or wrongly decreases the ref_count), there's a very high
   * chance of getting a segfault sometime later.
   *
   * Also, beyond the execution of below test case, the GFile corresponding to
   * self->test_dummy_file won't be valid since that file will be deleted. As a result,
   * any operation done using test_dummy_file will result into an error. The memory
   * corresponding to the test_dummy_file GFile will be freed in the cleanup
   * function. */
  g_test_add_data_func ("/recursive_delete/test_dir_folder",
                        (gconstpointer) self,
                        gvfs_google_test_recursive_delete_test_dir_folder);

  retval = g_test_run ();

  gvfs_google_test_exit_cleanup (&self, &error);
  if (error != NULL)
    {
      g_warning ("Error (cleanup): %s", error->message);
      retval = EXIT_FAILURE;
    }

  return retval;
}