· 6 years ago · Nov 11, 2019, 04:50 PM
1*&---------------------------------------------------------------------*
2*& Report Z_FTP_CONNECTION_TEST
3*&
4*&---------------------------------------------------------------------*
5*& Little check to see if FTP Server is handling unicode commands correctly.
6*&
7*&---------------------------------------------------------------------*
8
9REPORT z_ftp_connection_test.
10DATA: hdl TYPE i,
11 key TYPE i VALUE 26101957,
12 slen TYPE i,
13 result TYPE TABLE OF text255 WITH HEADER LINE,
14 pwd_scr TYPE zftppass.
15
16DATA: gv_subrc TYPE sy-subrc,
17 gv_folder type char100,
18 lv_command TYPE char120,
19 lv_command_len TYPE i,
20 lv_command_hex TYPE char120.
21
22SELECTION-SCREEN BEGIN OF BLOCK login WITH FRAME TITLE text-100.
23PARAMETERS: user TYPE zftpuser LOWER CASE,
24 pwd TYPE zftppass LOWER CASE,
25 host TYPE zfserver_zs LOWER CASE.
26SELECTION-SCREEN SKIP 1.
27PARAMETERS: dest LIKE rfcdes-rfcdest.
28SELECTION-SCREEN SKIP 1.
29SELECTION-SCREEN END OF BLOCK login.
30
31
32CALL FUNCTION 'SAPGUI_PROGRESS_INDICATOR'
33 EXPORTING
34 text = 'Running FTP test'.
35
36"++++++++++++++++++++++++++++++++++++++ STEP 1
37FORMAT RESET.
38WRITE: / '---------------'.
39WRITE: / 'STEP 1'.
40WRITE: / 'This step just connects to the FTP Server'.
41lv_command = 'FTP_CONNECT'.
42WRITE: / 'COMMAND: ', lv_command.
43REFRESH result.
44
45slen = strlen( pwd ).
46CALL FUNCTION 'HTTP_SCRAMBLE'
47 EXPORTING
48 source = pwd
49 sourcelen = slen
50 key = key
51 IMPORTING
52 destination = pwd_scr.
53
54CALL FUNCTION 'FTP_CONNECT'
55 EXPORTING
56 user = user
57 password = pwd_scr
58 host = host
59 rfc_destination = dest
60
61 IMPORTING
62 handle = hdl
63 EXCEPTIONS
64 not_connected = 99.
65
66IF sy-subrc = 99.
67 FORMAT COLOR COL_NEGATIVE.
68 WRITE / 'Cannot connect'.
69 PERFORM close.
70 EXIT.
71ELSE.
72 FORMAT COLOR COL_POSITIVE.
73 WRITE / 'Looks like we are connected'.
74ENDIF.
75
76
77"++++++++++++++++++++++++++++++++++++++ STEP 2
78" Commands
79FORMAT RESET.
80WRITE: / '---------------'.
81WRITE: / 'STEP 2'.
82WRITE: / 'This step sends a valid FTP request'.
83lv_command = 'cd /test'.
84WRITE: / 'COMMAND: ', lv_command.
85REFRESH result.
86
87"Verify test environment exists
88CALL FUNCTION 'FTP_COMMAND'
89 EXPORTING
90 handle = hdl
91 command = lv_command
92 TABLES
93 data = result
94 EXCEPTIONS
95 OTHERS = 1.
96WRITE: / 'Detail:'.
97LOOP AT result.
98 WRITE: / result.
99ENDLOOP.
100FIND '550' IN TABLE result.
101IF sy-subrc = 0.
102 FORMAT COLOR COL_NEGATIVE.
103 WRITE / 'Folder does not exists. need to check'.
104 PERFORM close.
105 exit.
106ELSE.
107 FIND '250' IN TABLE result.
108 IF sy-subrc = 0.
109 FORMAT COLOR COL_POSITIVE.
110 WRITE / 'Test environment ok'.
111 ELSE.
112 FORMAT COLOR COL_NEGATIVE.
113 WRITE / 'Unexpected result. Closing'.
114 PERFORM close.
115 exit.
116 ENDIF.
117ENDIF.
118
119
120
121"++++++++++++++++++++++++++++++++++++++ STEP 3
122" Commands
123FORMAT RESET.
124WRITE: / '---------------'.
125WRITE: / 'STEP 3'.
126WRITE: / 'This step sends a valid FTP request'.
127
128lv_command = 'set passive on'.
129WRITE: / 'COMMAND: ', lv_command.
130REFRESH result.
131
132CALL FUNCTION 'FTP_COMMAND'
133 EXPORTING
134 handle = hdl
135 command = lv_command
136 TABLES
137 data = result
138 EXCEPTIONS
139 OTHERS = 1.
140gv_subrc = sy-subrc.
141
142WRITE: / 'Detail:'.
143LOOP AT result.
144 WRITE: / result.
145ENDLOOP.
146
147IF gv_subrc = 0.
148 FORMAT COLOR COL_POSITIVE.
149 WRITE / 'FTP responded normally'.
150ELSE.
151 FORMAT COLOR COL_NEGATIVE.
152 WRITE / 'FTP not responding'.
153 "EXIT.
154ENDIF.
155
156"++++++++++++++++++++++++++++++++++++++ STEP 4
157" Create Chinese folder
158
159FORMAT RESET.
160WRITE: / '---------------'.
161WRITE: / 'STEP 4'.
162WRITE: / 'This step wants to create a folder with chinese symbols'.
163
164lv_command = 'mkd 汉语'.
165WRITE: / 'COMMAND: ', lv_command.
166REFRESH result.
167
168CALL FUNCTION 'FTP_COMMAND'
169 EXPORTING
170 handle = hdl
171 command = lv_command
172 TABLES
173 data = result
174 EXCEPTIONS
175 OTHERS = 1.
176gv_subrc = sy-subrc.
177
178WRITE: / 'Detail:'.
179LOOP AT result.
180 WRITE: / result.
181ENDLOOP.
182
183FIND '257' IN TABLE result.
184IF sy-subrc = 0.
185 FORMAT COLOR COL_POSITIVE.
186 WRITE / 'Folder 汉语 might have been created.. need to check'.
187ELSE.
188 FIND '550' IN TABLE result.
189 IF sy-subrc = 0.
190 FORMAT COLOR COL_NEGATIVE.
191 WRITE / 'Folder 汉语 already exist. Please delete first'.
192 PERFORM close.
193 EXIT.
194 ELSE.
195 FORMAT COLOR COL_NEGATIVE.
196 WRITE / 'Folder 汉语 could not be created, try system call'.
197 ENDIF.
198ENDIF.
199
200"++++++++++++++++++++++++++++++++++++++ STEP 5
201" Read wrong Chinese folder
202FORMAT RESET.
203WRITE: / '---------------'.
204WRITE: / 'STEP 5'.
205WRITE: / 'This step reads a DIFFERENT folder, must not work to be successfull (negativ test)'.
206
207lv_command = 'cd 语汉'.
208WRITE: / 'COMMAND: ', lv_command.
209REFRESH result.
210
211
212CALL FUNCTION 'FTP_COMMAND'
213 EXPORTING
214 handle = hdl
215 command = lv_command
216 TABLES
217 data = result
218 EXCEPTIONS
219 OTHERS = 1.
220
221WRITE: / 'Detail:'.
222LOOP AT result.
223 WRITE / result.
224ENDLOOP.
225
226
227FIND '550' IN TABLE result. " .. is not a directory
228IF sy-subrc = 0.
229 FORMAT COLOR COL_POSITIVE.
230 WRITE / 'Folder could not be read, looks good'.
231ELSE.
232 FORMAT COLOR COL_NEGATIVE.
233 WRITE / 'Folder could be read anyway? WRONG!'.
234ENDIF.
235
236
237"Back to root
238CALL FUNCTION 'FTP_COMMAND'
239 EXPORTING
240 handle = hdl
241 command = 'cd /test'
242 TABLES
243 data = result
244 EXCEPTIONS
245 OTHERS = 1.
246
247"++++++++++++++++++++++++++++++++++++++ STEP 6
248" Read correct Chinese folder
249FORMAT RESET.
250WRITE: / '---------------'.
251WRITE: / 'STEP 6'.
252WRITE: / 'Doublecheck, now we read the folder we just created (positiv test)'.
253
254lv_command = 'cd 汉语'.
255WRITE: / 'COMMAND: ', lv_command.
256REFRESH result.
257
258CALL FUNCTION 'FTP_COMMAND'
259 EXPORTING
260 handle = hdl
261 command = lv_command
262 TABLES
263 data = result
264 EXCEPTIONS
265 OTHERS = 1.
266gv_subrc = sy-subrc.
267
268WRITE: / 'Detail:'.
269LOOP AT result.
270 WRITE / result.
271ENDLOOP.
272
273FIND '250' IN TABLE result.
274IF gv_subrc = 0.
275 FORMAT COLOR COL_POSITIVE.
276 WRITE / 'Correct Chinese folder could be read'.
277ELSE.
278 FORMAT COLOR COL_NEGATIVE.
279 WRITE / 'Folder could not be read'.
280 " EXIT.
281ENDIF.
282
283
284
285"Back to test
286CALL FUNCTION 'FTP_COMMAND'
287 EXPORTING
288 handle = hdl
289 command = 'cd /test'
290 TABLES
291 data = result
292 EXCEPTIONS
293 OTHERS = 1.
294
295
296"++++++++++++++++++++++++++++++++++++++ STEP 7
297" Read the folder name
298FORMAT RESET.
299WRITE: / '---------------'.
300WRITE: / 'STEP 7'.
301WRITE: / 'Now we try to read the folders name (so we can cd into it)'.
302
303lv_command = 'ls'.
304WRITE: / 'COMMAND: ', lv_command.
305REFRESH result.
306
307CALL FUNCTION 'FTP_COMMAND'
308 EXPORTING
309 handle = hdl
310 command = lv_command
311 TABLES
312 data = result
313 EXCEPTIONS
314 OTHERS = 1.
315gv_subrc = sy-subrc.
316
317WRITE: / 'Detail:'.
318LOOP AT result.
319 FIND REGEX '(d[rwx-]{9})' IN result.
320 IF sy-subrc = 0.
321 FORMAT COLOR COL_POSITIVE.
322 WRITE: / result(70), '<-- this must be a folder'..
323 gv_folder = result+49(100).
324 ELSE.
325 FORMAT COLOR OFF.
326 WRITE / result.
327 ENDIF.
328ENDLOOP.
329
330"++++++++++++++++++++++++++++++++++++++ STEP 8
331"Navigating into the found folder:
332FORMAT RESET.
333WRITE: / '---------------'.
334WRITE: / 'STEP 8'.
335WRITE: / 'Now lets navigate into that folder'.
336
337lv_command = 'cd /test/' && gv_folder.
338WRITE: / 'COMMAND: ', lv_command.
339REFRESH result.
340
341CALL FUNCTION 'FTP_COMMAND'
342 EXPORTING
343 handle = hdl
344 command = lv_command
345 TABLES
346 data = result
347 EXCEPTIONS
348 OTHERS = 1.
349WRITE: / 'Detail:'.
350LOOP AT result.
351 write / result.
352endloop.
353
354FIND '250' IN TABLE result.
355IF sy-subrc = 0.
356 FORMAT COLOR COL_POSITIVE.
357 WRITE / 'Directory Change successful'.
358ELSE.
359 FIND '550' IN TABLE result.
360 IF sy-subrc = 0.
361 FORMAT COLOR COL_NEGATIVE.
362 WRITE / 'Error entering the folder.'.
363 ELSE.
364 FORMAT COLOR COL_NEGATIVE.
365 WRITE / 'Unexpected error'.
366 ENDIF.
367 PERFORM close.
368 exit.
369ENDIF.
370
371
372PERFORM close.
373
374
375
376
377*&---------------------------------------------------------------------*
378*& Form close
379*&---------------------------------------------------------------------*
380* text
381*----------------------------------------------------------------------*
382FORM close.
383
384 "++++++++++++++++++++++++++++++++++++++ STEP 9 Cleanup
385FORMAT RESET.
386WRITE: / '---------------'.
387WRITE: / 'STEP 9'.
388WRITE: / 'Cleanup'.
389
390lv_command = 'cd /test'.
391WRITE: / 'COMMAND: ', lv_command.
392REFRESH result.
393
394"Back to root
395CALL FUNCTION 'FTP_COMMAND'
396 EXPORTING
397 handle = hdl
398 command = lv_command
399 TABLES
400 data = result
401 EXCEPTIONS
402 OTHERS = 1.
403
404 lv_command = 'rmd 汉语'.
405 FORMAT RESET.
406 WRITE: / 'COMMAND: ', lv_command.
407 REFRESH result.
408
409 CALL FUNCTION 'FTP_COMMAND'
410 EXPORTING
411 handle = hdl
412 command = lv_command
413 TABLES
414 data = result
415 EXCEPTIONS
416 OTHERS = 1.
417 gv_subrc = sy-subrc.
418
419 WRITE: / 'Detail:'.
420 LOOP AT result.
421 WRITE: / result.
422 ENDLOOP.
423
424
425 IF gv_subrc = 0.
426 FORMAT COLOR COL_POSITIVE.
427 WRITE / 'FTP responded normally'.
428 ELSE.
429 FORMAT COLOR COL_NEGATIVE.
430 WRITE / 'FTP not responding'.
431 ENDIF.
432
433
434 CALL FUNCTION 'FTP_DISCONNECT'
435 EXPORTING
436 handle = hdl.
437
438 CALL FUNCTION 'RFC_CONNECTION_CLOSE'
439 EXPORTING
440 destination = dest
441 EXCEPTIONS
442 OTHERS = 99.
443
444
445 IF sy-subrc = 99.
446 FORMAT COLOR COL_NEGATIVE.
447 WRITE / 'Connection could not be closed'.
448ELSE.
449 FORMAT COLOR COL_POSITIVE.
450 WRITE / 'Connection closed'.
451ENDIF.
452ENDFORM. "close