SHARE
TWEET

Z_FTP_CONNECTION_TEST

a guest Nov 11th, 2019 182 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  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.  
  9. REPORT z_ftp_connection_test.
  10. DATA: 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.  
  16. DATA: 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.  
  22. SELECTION-SCREEN BEGIN OF BLOCK login WITH FRAME TITLE text-100.
  23. PARAMETERS: user TYPE zftpuser LOWER CASE,
  24.             pwd TYPE zftppass LOWER CASE,
  25.             host TYPE zfserver_zs LOWER CASE.
  26. SELECTION-SCREEN SKIP 1.
  27. PARAMETERS: dest LIKE rfcdes-rfcdest.
  28. SELECTION-SCREEN SKIP 1.
  29. SELECTION-SCREEN END OF BLOCK login.
  30.  
  31.  
  32. CALL FUNCTION 'SAPGUI_PROGRESS_INDICATOR'
  33.   EXPORTING
  34.     text = 'Running FTP test'.
  35.  
  36. "++++++++++++++++++++++++++++++++++++++ STEP 1
  37. FORMAT RESET.
  38. WRITE: / '---------------'.
  39. WRITE: / 'STEP 1'.
  40. WRITE: / 'This step just connects to the FTP Server'.
  41. lv_command = 'FTP_CONNECT'.
  42. WRITE:  / 'COMMAND: ', lv_command.
  43. REFRESH result.
  44.  
  45. slen = strlen( pwd ).
  46. CALL FUNCTION 'HTTP_SCRAMBLE'
  47.   EXPORTING
  48.     source      = pwd
  49.     sourcelen   = slen
  50.     key         = key
  51.   IMPORTING
  52.     destination = pwd_scr.
  53.  
  54. CALL 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.  
  66. IF sy-subrc = 99.
  67.   FORMAT COLOR COL_NEGATIVE.
  68.   WRITE /  'Cannot connect'.
  69.   PERFORM close.
  70.   EXIT.
  71. ELSE.
  72.   FORMAT COLOR COL_POSITIVE.
  73.   WRITE / 'Looks like we are connected'.
  74. ENDIF.
  75.  
  76.  
  77. "++++++++++++++++++++++++++++++++++++++ STEP 2
  78. " Commands
  79. FORMAT RESET.
  80. WRITE: / '---------------'.
  81. WRITE: / 'STEP 2'.
  82. WRITE: / 'This step sends a valid FTP request'.
  83. lv_command =  'cd /test'.
  84. WRITE:  / 'COMMAND: ', lv_command.
  85. REFRESH result.
  86.  
  87. "Verify test environment exists
  88. CALL FUNCTION 'FTP_COMMAND'
  89.   EXPORTING
  90.     handle  = hdl
  91.     command = lv_command
  92.   TABLES
  93.     data    = result
  94.   EXCEPTIONS
  95.     OTHERS  = 1.
  96. WRITE: / 'Detail:'.
  97. LOOP AT result.
  98.   WRITE: / result.
  99. ENDLOOP.
  100. FIND '550' IN TABLE result.
  101. IF sy-subrc = 0.
  102.   FORMAT COLOR COL_NEGATIVE.
  103.   WRITE /  'Folder does not exists. need to check'.
  104.   PERFORM close.
  105.   exit.
  106. ELSE.
  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.
  117. ENDIF.
  118.  
  119.  
  120.  
  121. "++++++++++++++++++++++++++++++++++++++ STEP 3
  122. " Commands
  123. FORMAT RESET.
  124. WRITE: / '---------------'.
  125. WRITE: / 'STEP 3'.
  126. WRITE: / 'This step sends a valid FTP request'.
  127.  
  128. lv_command =  'set passive on'.
  129. WRITE:  / 'COMMAND: ', lv_command.
  130. REFRESH result.
  131.  
  132. CALL FUNCTION 'FTP_COMMAND'
  133.   EXPORTING
  134.     handle  = hdl
  135.     command = lv_command
  136.   TABLES
  137.     data    = result
  138.   EXCEPTIONS
  139.     OTHERS  = 1.
  140. gv_subrc = sy-subrc.
  141.  
  142. WRITE: / 'Detail:'.
  143. LOOP AT result.
  144.   WRITE: / result.
  145. ENDLOOP.
  146.  
  147. IF gv_subrc  = 0.
  148.   FORMAT COLOR COL_POSITIVE.
  149.   WRITE / 'FTP responded normally'.
  150. ELSE.
  151.   FORMAT COLOR COL_NEGATIVE.
  152.   WRITE /  'FTP not responding'.
  153.   "EXIT.
  154. ENDIF.
  155.  
  156. "++++++++++++++++++++++++++++++++++++++ STEP 4
  157. " Create Chinese folder
  158.  
  159. FORMAT RESET.
  160. WRITE: / '---------------'.
  161. WRITE: / 'STEP 4'.
  162. WRITE: / 'This step wants to create a folder with chinese symbols'.
  163.  
  164. lv_command =  'mkd 汉语'.
  165. WRITE:  / 'COMMAND: ', lv_command.
  166. REFRESH result.
  167.  
  168. CALL FUNCTION 'FTP_COMMAND'
  169.   EXPORTING
  170.     handle  = hdl
  171.     command = lv_command
  172.   TABLES
  173.     data    = result
  174.   EXCEPTIONS
  175.     OTHERS  = 1.
  176. gv_subrc = sy-subrc.
  177.  
  178. WRITE: / 'Detail:'.
  179. LOOP AT result.
  180.   WRITE: / result.
  181. ENDLOOP.
  182.  
  183. FIND '257' IN TABLE result.
  184. IF sy-subrc = 0.
  185.   FORMAT COLOR COL_POSITIVE.
  186.   WRITE /  'Folder  汉语 might have been created.. need to check'.
  187. ELSE.
  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.
  198. ENDIF.
  199.  
  200. "++++++++++++++++++++++++++++++++++++++ STEP 5
  201. " Read wrong Chinese folder
  202. FORMAT RESET.
  203. WRITE: / '---------------'.
  204. WRITE: / 'STEP 5'.
  205. WRITE: / 'This step reads a DIFFERENT folder, must not work to be successfull (negativ test)'.
  206.  
  207. lv_command =  'cd 语汉'.
  208. WRITE:  / 'COMMAND: ', lv_command.
  209. REFRESH result.
  210.  
  211.  
  212. CALL FUNCTION 'FTP_COMMAND'
  213.   EXPORTING
  214.     handle  = hdl
  215.     command = lv_command
  216.   TABLES
  217.     data    = result
  218.   EXCEPTIONS
  219.     OTHERS  = 1.
  220.  
  221. WRITE: / 'Detail:'.
  222. LOOP AT result.
  223.   WRITE / result.
  224. ENDLOOP.
  225.  
  226.  
  227. FIND '550' IN TABLE result. " .. is not a directory
  228. IF sy-subrc = 0.
  229.   FORMAT COLOR COL_POSITIVE.
  230.   WRITE / 'Folder could not be read, looks good'.
  231. ELSE.
  232.   FORMAT COLOR COL_NEGATIVE.
  233.   WRITE /  'Folder could be read anyway? WRONG!'.
  234. ENDIF.
  235.  
  236.  
  237. "Back to root
  238. CALL 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
  249. FORMAT RESET.
  250. WRITE: / '---------------'.
  251. WRITE: / 'STEP 6'.
  252. WRITE: / 'Doublecheck, now we read the folder we just created (positiv test)'.
  253.  
  254. lv_command =  'cd 汉语'.
  255. WRITE:  / 'COMMAND: ', lv_command.
  256. REFRESH result.
  257.  
  258. CALL FUNCTION 'FTP_COMMAND'
  259.   EXPORTING
  260.     handle  = hdl
  261.     command = lv_command
  262.   TABLES
  263.     data    = result
  264.   EXCEPTIONS
  265.     OTHERS  = 1.
  266. gv_subrc = sy-subrc.
  267.  
  268. WRITE: / 'Detail:'.
  269. LOOP AT result.
  270.   WRITE / result.
  271. ENDLOOP.
  272.  
  273. FIND '250' IN TABLE result.
  274. IF gv_subrc = 0.
  275.   FORMAT COLOR COL_POSITIVE.
  276.   WRITE /  'Correct Chinese folder could be read'.
  277. ELSE.
  278.   FORMAT COLOR COL_NEGATIVE.
  279.   WRITE / 'Folder could not be read'.
  280.   " EXIT.
  281. ENDIF.
  282.  
  283.  
  284.  
  285. "Back to test
  286. CALL 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
  298. FORMAT RESET.
  299. WRITE: / '---------------'.
  300. WRITE: / 'STEP 7'.
  301. WRITE: / 'Now we try to read the folders name (so we can cd into it)'.
  302.  
  303. lv_command =  'ls'.
  304. WRITE:  / 'COMMAND: ', lv_command.
  305. REFRESH result.
  306.  
  307. CALL FUNCTION 'FTP_COMMAND'
  308.   EXPORTING
  309.     handle  = hdl
  310.     command = lv_command
  311.   TABLES
  312.     data    = result
  313.   EXCEPTIONS
  314.     OTHERS  = 1.
  315. gv_subrc = sy-subrc.
  316.  
  317. WRITE: / 'Detail:'.
  318. LOOP 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.
  328. ENDLOOP.
  329.  
  330. "++++++++++++++++++++++++++++++++++++++ STEP 8
  331. "Navigating into the found folder:
  332. FORMAT RESET.
  333. WRITE: / '---------------'.
  334. WRITE: / 'STEP 8'.
  335. WRITE: / 'Now lets navigate into that folder'.
  336.  
  337. lv_command =  'cd /test/' && gv_folder.
  338. WRITE:  / 'COMMAND: ', lv_command.
  339. REFRESH result.
  340.  
  341. CALL FUNCTION 'FTP_COMMAND'
  342.   EXPORTING
  343.     handle  = hdl
  344.     command = lv_command
  345.   TABLES
  346.     data    = result
  347.   EXCEPTIONS
  348.     OTHERS  = 1.
  349. WRITE: / 'Detail:'.
  350. LOOP AT result.
  351.   write / result.
  352. endloop.
  353.  
  354. FIND '250' IN TABLE result.
  355. IF sy-subrc = 0.
  356.   FORMAT COLOR COL_POSITIVE.
  357.   WRITE /  'Directory Change successful'.
  358. ELSE.
  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.
  369. ENDIF.
  370.  
  371.  
  372. PERFORM close.
  373.  
  374.  
  375.  
  376.  
  377. *&---------------------------------------------------------------------*
  378. *&      Form  close
  379. *&---------------------------------------------------------------------*
  380. *       text
  381. *----------------------------------------------------------------------*
  382. FORM close.
  383.  
  384.   "++++++++++++++++++++++++++++++++++++++ STEP 9 Cleanup
  385. FORMAT RESET.
  386. WRITE: / '---------------'.
  387. WRITE: / 'STEP 9'.
  388. WRITE: / 'Cleanup'.
  389.  
  390. lv_command =  'cd /test'.
  391. WRITE:  / 'COMMAND: ', lv_command.
  392. REFRESH result.
  393.  
  394. "Back to root
  395. CALL 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'.
  448. ELSE.
  449.   FORMAT COLOR COL_POSITIVE.
  450.   WRITE / 'Connection closed'.
  451. ENDIF.
  452. ENDFORM.                    "close
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Top