File: srm_testJiri.c

package info (click to toggle)
lfc 1.7.4.7-1
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 13,728 kB
  • ctags: 10,780
  • sloc: ansic: 146,136; sh: 13,198; perl: 11,142; python: 5,529; cpp: 5,113; sql: 1,790; makefile: 892; fortran: 113
file content (1494 lines) | stat: -rw-r--r-- 40,663 bytes parent folder | download | duplicates (8)
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
1487
1488
1489
1490
1491
1492
1493
1494
/*
 * SRM test suite, written by Jiri Kosina <kosina@fzu.cz>, based on work done 
 * by Jean-Phillipe Baud <Jean-Philippe.Baud@cern.ch>
 * Migrated to gsoap_2.6 by GG
 *
 * TODO (among others):
 * - let user choose between rfio, gsiftp & dcap
 *
 */

// $Id: srm_testJiri.c,v 1.8 2005/11/14 11:30:21 grodid Exp $

#define VERSION "0.3.4_multiple_gsoap_2.6"

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <signal.h>
#include <setjmp.h>

#include <sys/types.h>
#include <sys/wait.h>
#include <sys/stat.h>

#include "srmv1H.h"
#include "ISRM.nsmap"

#define DEFPOLLINT 10
#define SRM_EP_PATH "/srm/managerv1"

#define PINNED 0
#define UNPINNED 1

#define METHOD_RFIO 1
#define METHOD_GSIFTP 2
#define METHOD_DCAP 3

#ifdef GFAL_SECURE
#include "cgsi_plugin.h"
#endif

#ifdef DMALLOC
#include "dmalloc.h"
#endif

/* debugging output */
#define DEBUG
#ifdef DEBUG
#define PRINTF(format, args...) printf (format, ## args); fflush(stdout)
#else
#define PRINTF(format, args...)
#endif 

/* taken from gfal_api.h */
struct srm_filestatus {
        char    *surl;
        char    *turl;
        int     fileid;
        int     status;
};

/* forward declaration */
struct ns1__FileMetaData **request_get_file_metadata(char **argv, int i);

char testfilename1[1024], testfilename2[1024]; /* ugly */
char current_command[1024]; /* ugly, should be replaced by union */
struct soap soap;

volatile int segfault = 0;
sigjmp_buf env;
int saved_sigs;

int flags;
int reqid;
int nbproto;
int somebug = 0; /* this is global variable which indicates if there was
		    any problem during the whoel run of test suite - in other
		    words, if everything passed or not */
int transfer_method = 0;
int SURL_was_not_set = 0;

/* SIGSEGV handler used to detect SRM server returning junk */
void segv(int dummy)
{
	segfault = 1;
	siglongjmp(env, 1);	
}

/* check if returned metadata about file corresponds to local file */
int check_metadata_consistency (struct ns1__FileMetaData **p, char *fname, int pin)
{
	struct stat buf;
	int ret;

	printf("stating %s\n", fname);
	ret = stat(fname, &buf);
	if (ret == -1){
		PRINTF("[!] \tstrange, local stat() failed\n");
		perror("stat");
		somebug = 1;
		return 1;
		
	}

	if (buf.st_size != (*p)->size){
		PRINTF("[!]\tsize of localfile (%d) differs from what SRM reports (%d), FAILED\n",
				buf.st_size, (*p)->size);
		somebug = 1;
	}
	else
		PRINTF("[+]\tsizes of local file and SRM reported size match\n");
	
	if (buf.st_mode != (*p)->permMode){
		PRINTF("[?]\tmode of localfile (%o) differs from what SRM reports (%o)\n", 
				buf.st_mode, (*p)->permMode);
	}
	else
		PRINTF("[+]\tmode of local file and SRM reported size match (%o)\n", 
				buf.st_mode);

	if (pin == PINNED && (*p)->isPinned == true_){
		PRINTF("[+]\tfile is correctly pinned\n");
	} else if (pin == PINNED && (*p)->isPinned == false_){
		PRINTF("[?]\tfile is not pinned, but should be\n");
/*		somebug = 1;*/
	}

	if (pin == UNPINNED && (*p)->isPinned == false_) {
		PRINTF("[+]\tfile is correctly unpinned\n");
	} else if (pin == UNPINNED && (*p)->isPinned == true_) {
		PRINTF("[?]\tfile is pinned, but should be unpinned\n");
/*		somebug = 1;*/
	}

	return 0;
}


/* this routine checks if all fields of requestFileStatus are set 
 * with compilance of "standard"
 *
 * More checks could be added (StartTime > SubmitTime, etc)
 *
 * Maybe dumping the whole structure at error would be nice
 */
int check_request_file_status_consistency(struct ns1__RequestStatus *p, 
		char *invoking_method, char **argv)
{
/* FIXME */
#define CHKPRINTF PRINTF
	
  //OLDBADstruct ns1__RequestFileStatus *status;
  struct ns1__RequestFileStatus **status;
	int i;

	/* FIXME FIXME FIXME DEBUG */
	
	if (!p) {
		CHKPRINTF("[!]\t\trequest status is NULL, FAILED\n");
		somebug = 1;
	}
	else CHKPRINTF("[+]\t\trequest status check not-NULL, passed\n");
	
	if (strcasecmp(p->type, invoking_method) != 0){
		CHKPRINTF("[!]\t\trequest %s and returned type %s do not match, FAILED\n",
				invoking_method, p->type);
		somebug = 1;
	}
	else CHKPRINTF("[+]\t\trequest and returned type match, passed\n");
	
	if (!p->fileStatuses){
		CHKPRINTF("[!]\t\tGot no filestatuses, FAILED\n");
		errno = EPROTONOSUPPORT;
		perror ("put");
		soap_end (&soap);
		somebug = 1;
		return -1;
	}


	if (p->fileStatuses->__size != 2){
		CHKPRINTF("[!]\t\trequest status contains %d file(s), WRONG", 
			p->fileStatuses->__size);
		somebug = 1;
	} else {
		CHKPRINTF("[+]\t\trequest status contains %d file(s), passed\n",
			p->fileStatuses->__size);
	}

	
	status = p->fileStatuses->__ptr;
	for (i=0; i < p->fileStatuses->__size; i++){

		signal(SIGSEGV, segv);
		if (sigsetjmp(env, saved_sigs) != 0){
			CHKPRINTF("\t\tFATAL: SRM server returned junk SURL in second fileStatus.\n");
			somebug = 1;
			exit (1);				
		}

		//OLDBADif (!status[i].SURL) {
		if (!status[i]->SURL) {
			CHKPRINTF("[!]\t\t%d: surl not set\n", i);
			somebug = 1;
		} 
		else 
			if (!strcasecmp(status[i]->SURL, argv[i+2])){ 
					CHKPRINTF("[+]\t\t%d: surl (%s) check passed\n", i, status[i]->SURL);
			}
			else {
				CHKPRINTF("[!]\t\t%d: surl (%s, %s) check mismatch! FAILED\n", i, status[i]->SURL, argv[i+2]);
				somebug = 1;
			}	

		signal(SIGSEGV, SIG_DFL);
		
		if (status[i]->size <  0) {
			CHKPRINTF("[!]\t\t%dsize < 0, this shouldn't happen, FAILED\n", i); 
			somebug = 1;
		} 
		else CHKPRINTF("[+]\t\t%d: size (%d) check passed\n", i, status[i]->size);

		if (!status[i]->state || (strcasecmp(status[i]->state, "Pending") && 
				strcasecmp(status[i]->state, "Running") &&
				strcasecmp(status[i]->state, "Ready") &&
				strcasecmp(status[i]->state, "Failed") &&
				strcasecmp(status[i]->state, "Done"))){
			CHKPRINTF("[!]\t\t%d: state (%s) check FAILED\n", i, status[i]->state);
			somebug = 1;
		}
		else CHKPRINTF("[+]\t\t%d: state (%s) check passed\n", i, status[i]->state);

		/*
		if (status[i]->fileId < 0){
			CHKPRINTF("[!]\t\t%d: fileId < 0, this shouldn't happen, check FAILED\n", i);
			somebug = 1;
		}
		else CHKPRINTF("[+]\t\t%d: fileId (%d) check passed\n", i, status[i]->fileId);
		*/
		CHKPRINTF("[+]\t\t%d: fileId (%d) check disabled\n", i, status[i]->fileId);

		if ((!strcasecmp(status[i]->state, "Ready") || 
			!strcasecmp(status[i]->state, "Done")) && 
			!status[i]->TURL){
			CHKPRINTF("[!]\t\t%d: TURL shouldn't be NULL at this moment, FAILED\n", i);
			somebug = 1;
		}

	}
	return 0;
}

/* just ping the SRM server */
int request_ping(char **argv)
{
	struct ns5__pingResponse out;
	struct ArrayOfstring surlarray;	
	char *srm_endpoint, *sfn;
	struct ns1__FileMetaData *p;
	
        if (parsesurl (argv[2], &srm_endpoint, &sfn) < 0) {
                perror ("parsesurl");
		somebug = 1;
		return 0;
        }

	surlarray.__ptr = &argv[2]; 
        surlarray.__size = 1;	
        //surlarray.__offset = 0;

        if (soap_call_ns5__ping (&soap, srm_endpoint, "Ping", &out)) {
                soap_print_fault (&soap, stderr);
	        soap_print_fault_location (&soap, stderr);
		somebug = 1;
	}
        if (out._Result){
                PRINTF ("[+]\tSRM server is alive\n");
	}
        else {
                PRINTF ("[!]\tSRM server is not responding\n");
		somebug = 1;
		return 1;
	}
	return 0;
}

/* make the file pinned (in use) on SRM server */
int request_pin(char **argv)
{
	struct ns5__pinResponse out;
	struct ArrayOfstring surlarray;	
	char *srm_endpoint, *sfn;
	struct ns1__FileMetaData *p;
	
	// Missing same cross-check for argv[3] ?
        if (parsesurl (argv[2], &srm_endpoint, &sfn) < 0) {
                perror ("parsesurl");
		somebug = 1;
		return 1;
        }

	surlarray.__ptr = (char **)argv+2; 
        surlarray.__size = 2;	
        //surlarray.__offset = 0;	

        if (soap_call_ns5__pin (&soap, srm_endpoint, "Pin", &surlarray, &out)) {
                soap_print_fault (&soap, stderr);
	        soap_print_fault_location (&soap, stderr);
		somebug = 1;
	}
	/* here the out._Result->fileStatuses == NULL :/ */	
/*	if (out._Result->fileStatuses == NULL){
		printf("[!]\tfileStatuses pointer is bogus after Pin command. reporting as a bug (is this OK?)\n");
		somebug = 1;
	}
*/
	return 0;
}

/* advisoryDelete both files on the SRM server */

int request_advisoryDelete (char **argv, int i)
{
	int flags;
	struct ns5__advisoryDeleteResponse out;
	char *sfn;
	struct soap soap;
	char *srm_endpoint;
	struct ArrayOfstring surlarray;

	if (parsesurl (argv[2+i], &srm_endpoint, &sfn) < 0) {
		perror ("parsesurl");
		exit (1);
	}

	soap_init (&soap);
#ifdef GFAL_SECURE
	flags = CGSI_OPT_DISABLE_NAME_CHECK;
	soap_register_plugin_arg (&soap, client_cgsi_plugin, &flags);
#endif

	/* issue "advisoryDelete" request */

	surlarray.__ptr = &argv[2+i];
	surlarray.__size = 2;
	surlarray.__size = 1;  //  1st try ...

	if (soap_call_ns5__advisoryDelete (&soap, srm_endpoint,
	    "advisoryDelete", &surlarray, &out)) {
		soap_print_fault (&soap, stderr);
		soap_end (&soap);
		return (1);
	}
	soap_end (&soap);
	return (0);
}


/* make the file unpin (not in use) on SRM server */
int request_unpin(char **argv, int reqid)
{
	struct ns5__unPinResponse out;
	struct ArrayOfstring surlarray;	
	char *srm_endpoint, *sfn;
	struct ns1__FileMetaData *p;
	
	// Missing same cross-check for argv[3] ?
        if (parsesurl (argv[2], &srm_endpoint, &sfn) < 0) {
                perror ("parsesurl");
		somebug = 1;
		return 0;
        }

	//Jiri's Bug ? 	surlarray.__ptr = (char **)argv; 
	surlarray.__ptr = (char **)argv+2; 
        surlarray.__size = 2;	
        //surlarray.__offset = 0;	

        if (soap_call_ns5__unPin (&soap, srm_endpoint, "UnPin", &surlarray, reqid, 
				&out)) {
                soap_print_fault (&soap, stderr);
	        soap_print_fault_location (&soap, stderr);
		somebug = 1;
	}
	/* here ... se above */
}

/* I made the following macros and not functions because of
 * argument madness :)
 */

/* initialize structures for getFileMetaData and send to SRM server */
#define init_get_file_metadata_request() \
        surlarray.__ptr = (char **)argv+2; \
        surlarray.__size = 2;	\
	\
        if (soap_call_ns5__getFileMetaData (&soap, srm_endpoint,	\
            "getFileMetaData", &surlarray, &out)) {	\
                soap_print_fault (&soap, stderr);	\
		somebug = 1; \
        }	


/* initialize structures for GET request and send to SRM server */
#define init_get_request() \
	while (*protocols[nbproto]) nbproto++;	\
	\
	surlarray = (struct ArrayOfstring *) malloc(sizeof(struct ArrayOfstring));\
	surlarray->__ptr = (char **)argv+2;	\
	\
	surlarray->__size = 2;		\
	\
	protoarray.__ptr = protocols;	\
	protoarray.__size = nbproto;	\
	\
	if (soap_call_ns5__get (&soap, srm_endpoint, "Get", surlarray,	\
	    &protoarray, &out)) {	\
		soap_print_fault (&soap, stderr);	\
		soap_print_fault_location (&soap, stderr);	\
		somebug = 1; \
		return -1;	\
	}	\
	\
	reqstatp = out._Result;	\
	/* FIXME will be duplicit after writing checks */\
	if (reqstatp->fileStatuses == NULL) {	\
		errno = EPROTONOSUPPORT;	\
		perror ("get");	\
		somebug = 1;	\
		return -1;	\
	}	\
	reqid = reqstatp->requestId;	

/* initialize structures for PUT request and send to SRM server */
#define init_put_request() \
	while (*protocols[nbproto]) nbproto++; \
	\
	surlarray = (struct ArrayOfstring *) malloc(sizeof(struct ArrayOfstring));\
	\
	srcarray = (struct ArrayOfstring *) malloc(sizeof(struct ArrayOfstring));\
	\
	sizearray = (struct ArrayOflong *) malloc(sizeof(struct ArrayOflong));\
	\
	permarray = (struct ArrayOfboolean *) malloc(sizeof(struct ArrayOfboolean));\
	\
	srcarray->__ptr = (char **)argv+2;	\
	srcarray->__size = 2;		\
	\
	surlarray->__size = 2;		\
	surlarray->__ptr = (char **)argv+2;	\
	\
        /* dummy */  \
        size_tmp_arr[0] = 1;	\
        size_tmp_arr[1] = 1;	\
	sizearray->__size = 2;		\
	sizearray->__ptr = &size_tmp_arr[0];		\
	   \
        /* dummy */ 	\
        perm_tmp_arr[0] = 1;	\
        perm_tmp_arr[1] = 1;	\
	permarray->__size = 2;		\
	permarray->__ptr = &perm_tmp_arr[0];	\
	\
	protoarray.__ptr = protocols;	\
	protoarray.__size = nbproto;	\
	\
		    	    \
	/* send PUT command to SRM */	\
	PRINTF("[+]\tSending Put to SRM server\n");	\
	if (soap_call_ns5__put (&soap, srm_endpoint, "Put", srcarray,	\
	    surlarray, sizearray, permarray, &protoarray, &out)) {	\
		soap_print_fault (&soap, stderr);	\
		soap_print_fault_location (&soap, stderr);	\
		somebug = 1; \
		return 1;	\
		}	\
	\
	reqstatp = out._Result;	\
        if (reqstatp->errorMessage)	\
	                printf ("errorMessage: %s\n", reqstatp->errorMessage);	\
	\
	fflush (stdout);\
	fflush (stderr);\
	/* FIXME will be duplicit after writing checks */\
	if (reqstatp->fileStatuses == NULL) {	\
		errno = EPROTONOSUPPORT;	\
		perror ("put");	\
		somebug = 1; \
		return 1;	\
	}	\
	reqid = reqstatp->requestId;

/* this macro loops, polling SRM server, while the status of the
 * request is "pending". 
 * TODO add some timeouting, this could loop forever
 */
//                !strcasecmp(reqstatp->state, "active") || 
#if OLD
#define loop_while_pending() 	\
        PRINTF ("[GG0]\tsoap_call_ns5__put returned reqstatp->state: %s\n", reqstatp->state); \
        PRINTF ("[GG0]\tsoap_call_ns5__put returned reqstatp->fileStatuses->__ptr->state: %s\n", \
                 reqstatp->fileStatuses->__ptr->state); \
        while ((strcmp(reqstatp->state, "pending") == 0 || 	\
                strcmp(reqstatp->state, "Pending") == 0) ||	\
               (strcmp(reqstatp->fileStatuses->__ptr->state, "pending") == 0	\
                || strcmp(reqstatp->fileStatuses->__ptr->state,	\
                          "Pending") == 0)) {	\
                sleep((r++ == 0) ? 1 : (reqstatp->retryDeltaTime > 0) ?	\
                      reqstatp->retryDeltaTime : DEFPOLLINT);	\
                if (soap_call_ns5__getRequestStatus(&soap, srm_endpoint,	\
                                                    "getRequestStatus",	\
                                                    reqid, &outq)) {	\
                        soap_print_fault(&soap, stderr);	\
                        soap_end(&soap);	\
                        exit(1);	\
                }	\
                reqstatp = outq._Result;	\
        PRINTF ("[GGw]\tsoap_call_ns5__put returned reqstatp->state: %s\n", reqstatp->state); \
        PRINTF ("[GGw]\tsoap_call_ns5__put returned reqstatp->fileStatuses->__ptr->state: %s\n", \
                 reqstatp->fileStatuses->__ptr->state); \
        }	\
        if (reqstatp->errorMessage)	\
                printf("errorMessage: %s\n", reqstatp->errorMessage);	\
        if (strcmp(reqstatp->fileStatuses->__ptr->state, "ready") &&	\
            strcmp(reqstatp->fileStatuses->__ptr->state, "Ready")) {	\
                printf("state = %s\n",	\
                       reqstatp->fileStatuses->__ptr->state);	\
		somebug = 1; \
		return 1; \
        }	\
	\

#endif
//        while ( ! strcasecmp(reqstatp->state, "pending") ) {	

#if GG 
                ( strcasecmp(reqstatp->state, "Done") && \
                  strcasecmp(reqstatp->state, "Failed") ) ) {	\

#endif

#define loop_while_pending() 	\
        PRINTF ("[GG0]\tsoap_call_ns5__put returned reqstatp->state: %s\n", reqstatp->state); \
        while ( ! reqstatp->state || \
\
                ( strcasecmp(reqstatp->state, "pending" ) == 0 ) && \
		  (strcasecmp(reqstatp->fileStatuses->__ptr[0]->state, "pending") == 0	\
                || strcasecmp(reqstatp->fileStatuses->__ptr[1]->state, "pending") == 0 ) ) {	\
\
                sleep((r++ == 0) ? 1 : (reqstatp->retryDeltaTime > 0) ?	\
                      reqstatp->retryDeltaTime : DEFPOLLINT);	\
                if (soap_call_ns5__getRequestStatus(&soap, srm_endpoint,	\
                                                    "getRequestStatus",	\
                                                    reqid, &outq)) {	\
                        soap_print_fault(&soap, stderr);	\
                        soap_end(&soap);	\
                        exit(1);	\
                }	\
                reqstatp = outq._Result;	\
        PRINTF ("[GGw]\tsoap_call_ns5__put returned reqstatp->state: %s\n", reqstatp->state); \
        }	\
        if (reqstatp->errorMessage)	\
                printf("errorMessage: %s\n", reqstatp->errorMessage);	\
        if ( ( strcasecmp(reqstatp->fileStatuses->__ptr[0]->state, "ready") ||	\
               strcasecmp(reqstatp->fileStatuses->__ptr[1]->state, "ready") ) ) {	\
                printf("[?]\tstates = %s %s\n",	\
                       reqstatp->fileStatuses->__ptr[0]->state,	\
                       reqstatp->fileStatuses->__ptr[1]->state);	\
		somebug = 1; \
		return 1; \
        }	\
	\
        /* now the request status is ready, let's proceed */	\
        n = reqstatp->fileStatuses->__size;	\
        filestatuses = malloc (n * sizeof(struct srm_filestatus));	\
        //f = reqstatp->fileStatuses->__ptr;	\
        f = (*reqstatp->fileStatuses->__ptr);	\
        fs = filestatuses;	\
        for (i = 0; i < n; i++) {	\
                if ((f+i)->SURL && (fs->surl = strdup ((f+i)->SURL)) == NULL)	\
                        somebug = 1;	\
                if ((f+i)->state) {	\
                        if (strcmp ((f+i)->state, "Pending") == 0 ||	\
                            strcmp ((f+i)->state, "pending") == 0)	\
                                fs->status = 0;	\
                        else if (strcmp ((f+i)->state, "Failed") == 0 ||	\
                            strcmp ((f+i)->state, "failed") == 0)	\
                                fs->status = -1;	\
                        else	\
                                fs->status = 1;	\
                }	\
                fs->fileid = (f+i)->fileId;	\
                if ((f+i)->TURL && (fs->turl = strdup ((f+i)->TURL)) == NULL)	\
                        somebug = 1;	\
                fs++;	\
        }	\
	\
	PRINTF("[+]\tCalling check_request_file_status_consistency()\n");	\
	check_request_file_status_consistency(reqstatp, current_command, argv);

/* macros used to set file status */ /* GG on SRM server ? */
#define set_file_status(status)	\
	PRINTF("[+]\t\tCalled set_file_status() with %s (fileId %d)\n", status, p->__ptr[i]->fileId); \
	if (soap_call_ns5__setFileStatus (&soap, srm_endpoint, "setFileStatus",	\
				reqid, p->__ptr[i]->fileId, \
				status, &outs)){	\
		soap_print_fault (&soap, stderr);	\
		somebug = 1;	\
		return 1;	\
	\
	}\
	\
	memset (&outq, 0, sizeof(struct ns5__getRequestStatusResponse));\
        if (soap_call_ns5__getRequestStatus (&soap, srm_endpoint, \
                            "getRequestStatus", reqid, &outq)) { \
		soap_print_fault (&soap, stderr); \
		somebug = 1; \
                return 1; \
	} \
        reqstatp = outq._Result; \
	PRINTF("[+]\tCalling check_request_file_status_consistency()\n");\
        check_request_file_status_consistency(reqstatp, current_command, argv);\
	if (strcasecmp(reqstatp->fileStatuses->__ptr[i]->state, status)){\
		PRINTF("[!]\t\tReturned state %s differs from %s (which was set)\n", \
			reqstatp->fileStatuses->__ptr[i]->state, status);\
		somebug = 1;\
	}

int parsesurl (const char *surl, char **endpoint, char **sfn)
{
	int len;
	int lenp;
	char *p;
	static char srm_ep[1024];

	if (strncmp (surl, "srm://", 6)) {
		errno = EINVAL;
		return (-1);
	}
	if (p = strstr (surl + 6, "?SFN=")) {
		*sfn = p + 5;
	} else if (p = strchr (surl + 6, '/')) {
		*sfn = p;
	} else {
		errno = EINVAL;
		somebug = 1;
		return (-1);
	}
#ifdef GFAL_SECURE
	strcpy (srm_ep, "https://");
	lenp = 8;
#else
	strcpy (srm_ep, "http://");
	lenp = 7;
#endif
	len = p - surl - 6;
	if (lenp + len >= sizeof(srm_ep)) {
		errno = EINVAL;
		return (-1);
	}
	strncpy (srm_ep + lenp, surl + 6, len);
	*(srm_ep + lenp + len) = '\0';
	if (strchr (srm_ep + lenp, '/') == NULL) {
		if (strlen (SRM_EP_PATH) + lenp + len >= sizeof(srm_ep)) {
			errno = EINVAL;
			somebug = 1;
			return (-1);
		}
		strcat (srm_ep, SRM_EP_PATH);
	}
	
	*endpoint = srm_ep;
	return (0);
}
int gsiftp_copy_out(char *src, char *dest)
{
	char command[1024];
	int ret;
	
	//sprintf(command, "./my_gsiftp_out.sh %s %s\n", src,
	sprintf(command, "my_gsiftp_out.sh %s %s\n", src,
		dest);
	PRINTF("%s\n", command);
	ret = system(command);
	ret = WEXITSTATUS(ret);
	if (ret != 0){
		PRINTF("[!]\tgsiftp copy failed!!");
		somebug = 1;
		return 1;
	}
	return 0;
}

int gsiftp_copy_in(char *src, char *dest)
{
	char command[1024];
	int ret;
	
	//sprintf(command, "./my_gsiftp_in.sh %s %s\n", src,
	sprintf(command, "my_gsiftp_in.sh %s %s\n", src,
		dest);
	PRINTF("%s\n", command);
	ret = system(command);
	ret = WEXITSTATUS(ret);
	if (ret != 0){
		PRINTF("[!]\tgsiftp copy failed!!");
		somebug = 1;
		return 1;
	}
	return 0;
}

int rfio_copy_out(char *src, char *dest)
{
        char command[512];
        int ret;

        //sprintf(command, "./my_rfcp_out.sh %s %s\n", src,
        sprintf(command, "my_rfcp_out.sh %s %s\n", src,
                dest);
        PRINTF("%s\n", command);
	ret = system(command);
	ret = WEXITSTATUS(ret);
        if (ret != 0){
                PRINTF("[!]\trfio copy failed!!");
                somebug = 1;
                return 1;
        }
        return 0;
}

int rfio_copy_in(char *src, char *dest)
{
        char command[512];
        int ret;

        //sprintf(command, "./my_rfcp_in.sh %s %s\n", src,
        sprintf(command, "my_rfcp_in.sh %s %s\n", src,
                dest);
        PRINTF("%s\n", command);
	ret = system(command);
	ret = WEXITSTATUS(ret);
        if (ret != 0){
                PRINTF("[!]\trfio copy failed!!");
                somebug = 1;
                return 1;
        }
        return 0;
}

int dcap_copy_out(char *src, char *dest)
{
        char command[512];
        int ret;

        //sprintf(command, "./my_dccp_out.sh %s %s\n", src,
        sprintf(command, "my_dccp_out.sh %s %s\n", src,
                dest);
        PRINTF("%s\n", command);
	ret = system(command);
	ret = WEXITSTATUS(ret);
        if (ret != 0){
                PRINTF("[!]\tdcap copy failed!!");
                somebug = 1;
                return 1;
        }
        return 0;
}

int dcap_copy_in(char *src, char *dest)
{
        char command[512];
        int ret;

        //sprintf(command, "./my_dccp_in.sh %s %s\n", src,
        sprintf(command, "my_dccp_in.sh %s %s\n", src,
                dest);
        PRINTF("%s\n", command);
	ret = system(command);
	ret = WEXITSTATUS(ret);
        if (ret != 0){
                PRINTF("[!]\tdcap copy failed!!");
                somebug = 1;
                return 1;
        }
        return 0;
}


/* this handles communication with SRM server in case of 
 * getFileMetaData request
 *
 * the second parameter indicates which file (in case of multiple files
 * per request) is caller interested into
 */
 /* returns pointer to received structure, caller should check
 * the values against uploaded file
 */
struct ns1__FileMetaData **request_get_file_metadata(char **argv, int i)
{
	/* we need the structure static, because caller wants to access it */
        static struct ns5__getFileMetaDataResponse out;
	struct ArrayOfstring surlarray;
	char *srm_endpoint, *sfn;
	struct ns1__FileMetaData **p;
	
	// Missing same cross-check for argv[3] ?
        if (parsesurl (argv[2], &srm_endpoint, &sfn) < 0) {
                perror ("parsesurl");
		somebug = 1;
		return NULL;
        }
	
	init_get_file_metadata_request();

	p = out._Result->__ptr;
	
	/* FIXME specification doesn't say which fields are mandatory and
	 * which are not :/
	 */
	PRINTF("\t======== metadata dump begin =========\n");
//	for (i=0; i < out._Result->__size; i++){
		if (p[i]->SURL){
			PRINTF("\tSURL: %s\n", p[i]->SURL);
			if (strcasecmp(p[i]->SURL, argv[i+2])){
				PRINTF("\t[!] returned SURL and requested SURL don't match!\n");
				somebug = 1;
			}
		}
		else {
			PRINTF("\t[?] SURL was not set\n");
			SURL_was_not_set++;
		}

		if (p[i]->permMode >= 0){
			PRINTF("\t[+] permMode: %o\n", p[i]->permMode);
		}
		else
			PRINTF("\t[?] permMode was not set\n");

		if (p[i]->checksumType){
			PRINTF("\t[+] checksumType: %s\n", p[i]->checksumType);
		}
		else
			PRINTF("\t[?] checksumType was not set\n");
		
		if (p[i]->owner){
			PRINTF ("\t[+] owner = %s\n", p[i]->owner);
		}
		else
			PRINTF ("\t[?] owner was not set\n");	

		if (p[i]->group) {
			printf ("\t[+] group = %s\n", p[i]->group);
		}
		else
			PRINTF ("\t[?] group was not set\n");

		if (p[i]->size >= 0) {
			PRINTF ("\t[+] size = %lld\n", p[i]->size);
		}
		else
			PRINTF ("\t[?] size was negative\n");
		if (p[i]->isPinned == true_) {
			PRINTF("\t[+] the file is pinned\n");
		}
		else
			PRINTF("\t[+] the file is not pinned\n");
		if (p[i]->isPermanent == true_) {
			PRINTF("\t[+] the file is permanent\n");
		}
		else
			PRINTF("\t[+] the file is not permanent\n");
		if (p[i]->isCached == true_) {
			PRINTF("\t[+] the file is cached\n");
		}
		else
			PRINTF("\t[+] the file is not cached\n");

//	}
	PRINTF("\t======== metadata dump end =========\n");			
	return p+i;
}




/* this handles communication with SRM server in case of PUT request
 * and also copies file to TURL received from server
 * 
 */
int request_push(char **argv)
{
	int flags;
	int nbproto = 0;
	int ret;
	struct ns5__setFileStatusResponse outs;
	struct ns5__putResponse out;
	struct ns5__getRequestStatusResponse outq;
	char command[1024]; /* ugly */
	char rfcp_file[1024]; /* ugly */
	char *rfcp_file_p;
/*
	static char *protocols[] = {
#if GFAL_ENABLE_RFIO
		"rfio",
#endif
#if GFAL_ENABLE_DCAP
		"dcap",
#endif
#if GFAL_ENABLE_GSIFTP
		"gsiftp"
#endif
		""
	};
*/
	char *protocols[2];
	int r = 0, i;
	struct ns1__RequestStatus *reqstatp;
	struct ArrayOfRequestFileStatus *p;
	char *sfn;
	struct ArrayOflong *sizearray;
	struct ArrayOfstring *srcarray;
	struct ArrayOfstring *surlarray;
	struct ArrayOfboolean *permarray;	
	struct ArrayOfstring protoarray;
	char ** ptr;
	char *srm_endpoint;
        //struct ns1__RequestFileStatus **f;
        struct ns1__RequestFileStatus *f;
        struct srm_filestatus *filestatuses;
        struct srm_filestatus *fs;
	int n;      
        enum xsd__boolean perm_tmp_arr[2];
        LONG64 size_tmp_arr[2];
        //enum xsd__boolean size_tmp_arr[2];

	strcpy(current_command, "put");
	if (transfer_method == METHOD_RFIO)
		protocols[0] = "rfio";
	if (transfer_method == METHOD_GSIFTP)
		protocols[0] = "gsiftp";
	if (transfer_method == METHOD_DCAP)
		protocols[0] = "gsidcap";

	protocols[1] = "";
	if (parsesurl (argv[2], &srm_endpoint, &sfn) < 0) {
		perror ("parsesurl");
		somebug = 1;
		return 1;
	}
	if (parsesurl (argv[3], &srm_endpoint, &sfn) < 0) {
		perror ("parsesurl");
		somebug = 1;
		return 1;
	}

	//PRINTF ("[GG0]\tsoap_call_ns5__put returned reqstatp->state: %s\n", reqstatp->state);
	init_put_request();

	PRINTF ("[+]\tsoap_call_ns5__put returned reqid: %d %x\n", reqid, reqid);
	PRINTF ("[GG1]\tsoap_call_ns5__put returned reqstatp->state: %s\n", reqstatp->state);

	/* loop while the file is pending */

	loop_while_pending();
	PRINTF ("[GG2]\tsoap_call_ns5__put returned reqstatp->state: %s\n", reqstatp->state);
	
	/* let's roll */
	p = reqstatp->fileStatuses;
	for (i = 0; i< p->__size; i++){
		PRINTF ("[+]\tfileid %d, request state %s\n[+]\tTURL = %s\n", 
			p->__ptr[i]->fileId,
			p->__ptr[i]->state,
			p->__ptr[i]->TURL);

		/* update the status to running */
		PRINTF ("[+]\tUpdating status to Running\n");
		set_file_status("Running");
	
	
		if (i == 0){
			if (transfer_method == METHOD_GSIFTP) {
				PRINTF("[+]\tPerforming gsiftp copy from %s to %s\n", 
					testfilename1, p->__ptr[i]->TURL);
					ret = gsiftp_copy_out(testfilename1, p->__ptr[i]->TURL);
			}
			if (transfer_method == METHOD_RFIO) {
				PRINTF("[+]\tPerforming rfio copy from %s to %s\n", 
					testfilename1, p->__ptr[i]->TURL);
					ret = rfio_copy_out(testfilename1, p->__ptr[i]->TURL);
			}
			if (transfer_method == METHOD_DCAP) {
				PRINTF("[+]\tPerforming dcap copy from %s to %s\n", 
					testfilename1, p->__ptr[i]->TURL);
					ret = dcap_copy_out(testfilename1, p->__ptr[i]->TURL);
			}

		}
		else {
			if (transfer_method == METHOD_GSIFTP) {
				PRINTF("[+]\tPerforming gsiftp copy from %s to %s\n",
					testfilename2, p->__ptr[i]->TURL);
				ret = gsiftp_copy_out(testfilename2, p->__ptr[i]->TURL);
			}
			if (transfer_method == METHOD_RFIO) {
				PRINTF("[+]\tPerforming rfio copy from %s to %s\n",
					testfilename2, p->__ptr[i]->TURL);
					ret = rfio_copy_out(testfilename2, p->__ptr[i]->TURL);
			}
			if (transfer_method == METHOD_DCAP) {
				PRINTF("[+]\tPerforming dcap copy from %s to %s\n", 
					testfilename2, p->__ptr[i]->TURL);
					ret = dcap_copy_out(testfilename2, p->__ptr[i]->TURL);
			}

		}
		
		if (ret != 0){
			PRINTF("[+]\tUpdating status to Failed\n");
			set_file_status("Failed");
			somebug = 1;
			ret = 1;
		}
		else {
			PRINTF("[+]\tFile copied successfully to %s\n", 
					p->__ptr[i]->TURL);
			PRINTF("[+]\tUpdating status to Done ...\n");
			set_file_status("Done");
		}
	}
	fflush(stdout);
	
	return 0;
}



int request_pull(char **argv)
{
	char command[512]; /* ugly */
	int ret, i;
	struct ns5__getResponse out;
	struct ns5__getRequestStatusResponse outq;
	struct ns5__setFileStatusResponse outs;
	struct ArrayOfRequestFileStatus *p;
        //struct ns1__RequestFileStatus **f;
        struct ns1__RequestFileStatus *f;
        struct srm_filestatus *filestatuses;
        struct srm_filestatus *fs;
/*	static char *protocols[] = {
#if GFAL_ENABLE_RFIO
		"rfio",
#endif
#if GFAL_ENABLE_DCAP
		"dcap",
#endif
#if GFAL_ENABLE_GSIFTP
		"gsiftp",
#endif
		""
	};
*/
	char *protocols[2];
	int r = 0;
	int n;
	struct ns1__RequestStatus *reqstatp;
	char *sfn;
	char *srm_endpoint;
	struct ArrayOfstring *surlarray;
	struct ArrayOfstring protoarray;

	strcpy(current_command, "get");	

	if (transfer_method == METHOD_RFIO)
		protocols[0] = "rfio";
	if (transfer_method == METHOD_GSIFTP)
		protocols[0] = "gsiftp";
	if (transfer_method == METHOD_DCAP)
		protocols[0] = "gsidcap";

	protocols[1] = "";

	if (parsesurl (argv[2], &srm_endpoint, &sfn) < 0) {
		perror ("parsesurl");
		somebug = 1;
		return 1;
	}
	if (parsesurl (argv[3], &srm_endpoint, &sfn) < 0) {
		perror ("parsesurl");
		somebug = 1;
		return 1;
	}

	init_get_request();

	PRINTF ("[+]\tsoap_call_ns5__get returned reqid: %x\n", reqid);
	PRINTF ("[GG1]\tsoap_call_ns5__get returned reqstatp->state: %s\n", reqstatp->state);

	/* now wait for change from pending state */

	loop_while_pending();
	PRINTF ("[GG2]\tsoap_call_ns5__get returned reqstatp->state: %s\n", reqstatp->state);

	/* let's roll */
	p = reqstatp->fileStatuses;
	for (i = 0; i< p->__size; i++){
		PRINTF ("[+]\tfileid %d, request state %s\n[+]\tTURL = %s\n", 
			p->__ptr[i]->fileId,
			p->__ptr[i]->state,
			p->__ptr[i]->TURL);

		/* update the status to running */
		PRINTF ("[+]\tUpdating status to Running\n");
		set_file_status("Running");
	

		if (i == 0){
			if (transfer_method == METHOD_GSIFTP) {
				PRINTF("[+]\tPerforming gsiftp copy from %s to %s\n", 
					p->__ptr[i]->TURL, testfilename1);
					ret = gsiftp_copy_in(p->__ptr[i]->TURL, testfilename1);
			}
			if (transfer_method == METHOD_RFIO) {
				PRINTF("[+]\tPerforming rfio copy from %s to %s\n", 
					p->__ptr[i]->TURL, testfilename1);
					ret = rfio_copy_in(p->__ptr[i]->TURL, testfilename1);
			}
			if (transfer_method == METHOD_DCAP) {
				PRINTF("[+]\tPerforming dcap copy from %s to %s\n", 
					p->__ptr[i]->TURL, testfilename1);
					ret = dcap_copy_in(p->__ptr[i]->TURL, testfilename1);
			}

		}
		else {
			if (transfer_method == METHOD_GSIFTP) {
				PRINTF("[+]\tPerforming gsiftp copy from %s to %s\n",
					p->__ptr[i]->TURL, testfilename2);
				ret = gsiftp_copy_in(p->__ptr[i]->TURL, testfilename2);

			}
			if (transfer_method == METHOD_RFIO) {
				PRINTF("[+]\tPerforming rfio copy from %s to %s\n",
					p->__ptr[i]->TURL, testfilename2);
					ret = rfio_copy_in(p->__ptr[i]->TURL, testfilename2);
			}
			if (transfer_method == METHOD_DCAP) {
				PRINTF("[+]\tPerforming dcap copy from %s to %s\n", 
					p->__ptr[i]->TURL, testfilename2);
					ret = dcap_copy_in(p->__ptr[i]->TURL, testfilename2);
			}

		}
		
		
		
		if (ret != 0){
			PRINTF("[+]\tUpdating status to Failed\n");
			set_file_status("Failed");
			somebug = 1;
			ret = 1;
		}
		else {
			PRINTF("[+]\tFile copied successfully\n");
			PRINTF("[+]\tUpdating status to Done ...\n");
			set_file_status("Done");
		}
	}

	
	fflush(stdout);
	return 0;
	
}


int main(int argc, char **argv)
{

	char command[1024];
	struct ns1__FileMetaData **metadata;
	int ret = 0;
		
#ifdef DEBUG
	printf("[+] srm_testsuite %s starting, compiled in verbose mode\n", VERSION);
#else
	printf("[+] srm_testsuite %s starting, compiled in quiet mode\n", VERSION);
#endif
	fflush(stdout);
	
	if (argc != 4) {
		fprintf (stderr, "[!] usage: %s [rfio|gsiftp|dcap] SURL SURL\n", argv[0]);
		return 1;
	}

	if ((strcmp(argv[1],"rfio") != 0) && (strcmp(argv[1], "gsiftp") != 0) && (strcmp(argv[1], "dcap") != 0)) {
		fprintf (stderr, "[!] usage: %s [rfio|gsiftp|dcap] SURL SURL\n", argv[0]);
		return 1;
	}

	if (!strcmp(argv[1], "rfio")) 
		transfer_method = METHOD_RFIO;
	if (!strcmp(argv[1], "gsiftp"))
		transfer_method = METHOD_GSIFTP;
	if (!strcmp(argv[1], "dcap"))
		transfer_method = METHOD_DCAP;
	
	soap_init(&soap);
#ifdef GFAL_SECURE
	flags = CGSI_OPT_DISABLE_NAME_CHECK;
	soap_register_plugin_arg (&soap, client_cgsi_plugin, &flags);
#endif

	/* first, ping the server 
	 * FIXME this is not in specification (?)
	 */

	PRINTF("[+] Calling request_ping()\n");
	ret = request_ping(argv);
	if (ret == 1){
		PRINTF("[+] request_ping() failed, no use to try more\n");
		somebug = 1;
		goto out;
	}
	
	/* ugly */
	strcpy(testfilename1,"TESTFILE");
	strcpy(testfilename2,"TESTFILE2");

/* 
 * 1) create file (done by wrapper)
 * 2) upload
 * 3) get it back to file.x
 * 4) modify it and cp to file.x.y
 * 5) overwrite through SRM on server
 * 6) get it back as file.x.y.y
 */

	/* the file management GET/PUT tests */
	
	/* pin and upload the file */
	/* Jiri's version, does not work
	PRINTF("[+] Calling request_pin()\n");
	ret = request_pin(argv);
	if (ret == 1){
		PRINTF("[!] request_pin() failed, but this is not too critical, going on\n");
		
	}
	*/
	

	PRINTF("[+] Calling request_push()\n");
	ret = request_push(argv);
	if (ret == 1) {
		PRINTF("[!] request_push() failed, no use to try more\n");
		somebug = 1;
		goto out;
	}

	/* moved by GG, but not successfully */
#ifdef RPIN	
	PRINTF("[+] Calling request_pin()\n");
	ret = request_pin(argv);
	if (ret == 1){
		PRINTF("[!] request_pin() failed, but this is not too critical, going on\n");
		
	}
#endif
	

	/* now check through getFileMetaData if it is OK */ 
	PRINTF("[+] Calling request_getFileMetaData(0)\n");
	metadata = request_get_file_metadata(argv, 0);
	if (metadata) {
		check_metadata_consistency(metadata, testfilename1, PINNED);
	}
	else {
		PRINTF("[!] request_getFileMetaData(0) failed\n");
		somebug = 1;
	}

	PRINTF("[+] Calling request_getFileMetaData(1)\n");
	metadata = request_get_file_metadata(argv, 1);
	if (metadata) {
		check_metadata_consistency(metadata, testfilename2, PINNED);
	}
	else {
		PRINTF("[!] request_getFileMetaData(1) failed\n");
		somebug = 1;
	}
	
	/* get it back to .x */
	strcat(testfilename1, ".x");
	strcat(testfilename2, ".x");
	PRINTF("[+] Calling request_pull()\n");
	ret = request_pull(argv);
	if (ret == 1){
		PRINTF("[!] request_pull() failed, no use to try more\n");
		somebug = 1;
		goto out;
	}
	
	/* 2nd try by GG */
#ifdef RPIN	
	PRINTF("[+] Calling GG request_pin() for both files.x, after request_pull\n");
	ret = request_pin(argv);
	if (ret == 1){
		PRINTF("[!] request_pin() failed, but this is not too critical, going on\n");
		
	}
#endif

	/* now check through getFileMetaData if it is OK */ 
	PRINTF("[+] Calling request_getFileMetaData()\n");
	metadata = request_get_file_metadata(argv, 0);
	if (metadata) {
		check_metadata_consistency(metadata, testfilename1, PINNED);
	}
	else {
		PRINTF("[!] request_getFileMetaData() failed\n");
		somebug = 1;
	}

	PRINTF("[+] Calling request_getFileMetaData()\n");
	metadata = request_get_file_metadata(argv, 1);
	if (metadata) {
		check_metadata_consistency(metadata, testfilename2, PINNED);
	}
	else {
		PRINTF("[!] request_getFileMetaData() failed\n");
		somebug = 1;
	}

	sprintf(command, "cp -p %s %s.y", testfilename1, testfilename1);
	system(command);
	sprintf(command, "cp -p %s %s.y", testfilename2, testfilename2);
	system(command);
	strcat(testfilename1, ".y");
	strcat(testfilename2, ".y");
	sprintf(command, "echo 'ADDED' >> %s", testfilename1);
	/* FIXME this can also fail */
	system(command);
	sprintf(command, "echo 'ADDED' >> %s", testfilename2);
	/* FIXME this can also fail */
	system(command);

	/* now the file will be uploaded back. We'll modify the SURL in
	 * last byte, since SRM implementation doesn't allow overwriting
	 * (while castor SRM handles it, dCache SRM does not)
	 * This is nasty, but works, as far as we have control over the
	 * SURL format.
	 */
	argv[2][strlen(argv[2])-1] = 'X';
	argv[3][strlen(argv[3])-1] = 'X';
	PRINTF("[+] Calling request_push()\n");
	ret = request_push(argv);
	if (ret == 1){
		PRINTF("[!] request_push() failed, no use to try more\n");
		somebug = 1;
		goto out;
	}

	/* again check through getFileMetaData if it is OK */ 
	PRINTF("[+] Calling request_getFileMetaData()\n");
	metadata = request_get_file_metadata(argv, 0);
	if (metadata) {
		check_metadata_consistency(metadata, testfilename1, PINNED);
	}
	else {
		PRINTF("[!] request_getFileMetaData() failed\n");
		somebug = 1;
	}
	metadata = request_get_file_metadata(argv, 1);
	if (metadata) {
		check_metadata_consistency(metadata, testfilename2, PINNED);
	}
	else {
		PRINTF("[!] request_getFileMetaData() failed\n");
		somebug = 1;
	}

	
	/* get it back to .x.y.y and unpin it */
	strcat(testfilename1, ".y");
	strcat(testfilename2, ".y");
	PRINTF("[+] Calling request_pull()\n");
	ret = request_pull(argv);
	if (ret == 1){
		PRINTF("[!] request_pull() failed, no use to try more\n");
		somebug = 1;
		goto out;
	}

#ifdef RPIN	
	PRINTF("[+] Calling request_unpin()\n");
	ret = request_unpin(argv, reqid);
	if (ret == 1){
		PRINTF("[!] request_unpin() failed, but this is not too critical, going on\n");
		
	}
#endif

	
	/* now check through getFileMetaData if it is OK
	 * especially if the file is unpinned */ 

	PRINTF("[+] Calling request_getFileMetaData()\n");
	metadata = request_get_file_metadata(argv, 0);
	if (metadata) {
		check_metadata_consistency(metadata, testfilename1, UNPINNED);
	}
	else {
		PRINTF("[!] request_getFileMetaData() failed\n");
		somebug = 1;
	}
	metadata = request_get_file_metadata(argv, 1);
	if (metadata) {
		check_metadata_consistency(metadata, testfilename2, UNPINNED);
	}
	else {
		PRINTF("[!] request_getFileMetaData() failed\n");
		somebug = 1;
	}

	/* request_advisoryDelete Test */

	ret = request_advisoryDelete(argv, 0);
	if (ret == 1){
		PRINTF("[!] request_advisoryDelete(0) failed, but this is not too critical, going on\n");
		
	}

	/*
	ret = request_advisoryDelete(argv, 1);
	if (ret == 1){
		PRINTF("[!] request_advisoryDelete(1) failed, but this is not too critical, going on\n");
		
	}
	*/
	
	SURL_was_not_set = 0;
	PRINTF("[+] Calling request_getFileMetaData() after request_advisoryDelete for %s \n", argv[2]);
	metadata = request_get_file_metadata(argv, 0);
	/*
	if (metadata) {
		check_metadata_consistency(metadata, testfilename1, UNPINNED);
	}
	else {
		PRINTF("[!] request_getFileMetaData() failed\n");
		somebug = 1;
	}
	*/
	PRINTF("[+] Calling request_getFileMetaData() after request_advisoryDelete for %s \n", argv[3]);
	metadata = request_get_file_metadata(argv, 1);
	/*
	if (metadata) {
		check_metadata_consistency(metadata, testfilename2, UNPINNED);
	}
	else {
		PRINTF("[!] request_getFileMetaData() failed\n");
		somebug = 1;
	}
	*/
	if ( SURL_was_not_set != 1 )
	  {
	    PRINTF("[!] request_advisoryDelete() failed: %d\n", SURL_was_not_set);
	    somebug = 1;
	  }
	  

	/* now it is up to the wrapping script above us to test
	 * if original and .x are the same, and .x and .x.y.y differ
	 */

	/* the remaining methods are advisoryDelete and mkPermanent. As
	 * both of these methods are not required to be supported by SRM,
	 * it's not worth testing them, because noone can know if it failed
	 * because 'not supported' or because 'bug'. 
	 */

	soap_end(&soap);
	/* this is the only exit point. we return the global flag
	 * to wrapping scipt, to indicate him if everything went flawlessly
	 * or not
	 */
	printf("[%s] srm_testsuite finished, bug(s) %sencountered\n", 
			somebug == 1 ? "!" : "+",
			somebug == 1 ? "" : "not ");
out:
	return somebug;
}