DEFINE CLASS wwSftpTests as FxuTestCase OF FxuTestCase.prg

*!*	NOTE this test uses a local Rebex sftp server from
*!*	http://labs.rebex.net/tiny-sftp-server

*** SETUP AND CONFIGURATION *** 

************************************************************************
*  Setup
****************************************
***  Function:
***    Assume:
***      Pass:
***    Return:
************************************************************************
FUNCTION Setup()
	*** Make sure libraries are loaded
	DO wconnect
    DO wwSftp
ENDFUNC
*   Setup

************************************************************************
*  TearDown
****************************************
***  Function:
***    Assume:
***      Pass:
***    Return:
************************************************************************
FUNCTION TearDown()

ENDFUNC
*   TearDown


************************************************************************
*  IsNumber
****************************************
***  Function:
***    Assume:
***      Pass:
***    Return:
************************************************************************
FUNCTION FormDate()

PRIVATE Request
Request = CREATEOBJECT("wwRequest")

Request.cFormVars = "&date1=2015-10-01"
ltDate = Request.FormDate("date1")

this.MessageOut( TRANSFORM(ltDate) )
THIS.AssertTrue( VARTYPE(ltDate) = "T" )
this.AssertTrue( TTOD(ltDate) = {2015-10-01} )


Request.cFormVars = "&date1=2015-10-01T16:01:21Z"
ltDate = Request.FormDate("date1")

this.MessageOut( TRANSFORM(ltDate) )
THIS.AssertTrue( VARTYPE(ltDate) = "T" )
this.AssertTrue( ltDate = {2015-10-01 16:01:21} )

ENDFUNC
*   FormDate

************************************************************************
*  DownloadFileSimple
****************************************
***  Function:
***    Assume:
***      Pass:
***    Return:
************************************************************************
FUNCTION DownloadFileSimple()

loFtp = CREATEOBJECT("wwSftp")

lcHost = "127.0.0.1"
lnPort = 23
lcUsername = "tester"
lcPassword = "password"

loFtp.nFtpPort = lnPort

lcOutputFile = ".\tests\sailbig.jpg"
DELETE FILE lcOutputFile

lnResult = loFtp.FtpGetFile(lcHost,"sailbig.jpg",lcOutputFile,1,lcUsername,lcPassword)

this.AssertTrue(lnResult == 0,loFtp.cErrorMsg)
this.AssertTrue(FILE(lcOutputFile),"Failed: File wasn't created")
this.AssertTrue(FileSIZE(lcOutputFile) > 0,"Failed: File Size is " + TRANSFORM(FSIZE(lcOutputFile)))

ENDFUNC
*   DownloadFileSimple


************************************************************************
*  DownloadFileWithSpacesSimple
****************************************
***  Function:
***    Assume:
***      Pass:
***    Return:
************************************************************************
FUNCTION DownloadFileWithSpacesSimple()

loFtp = CREATEOBJECT("wwSftp")

lcHost = "127.0.0.1"
lnPort = 23
lcUsername = "tester"
lcPassword = "password"

loFtp.nFtpPort = lnPort

lcOutputFile = ".\tests\sailbig.jpg"
DELETE FILE lcOutputFile

lnResult = loFtp.FtpGetFile(lcHost,"Sail Big.jpg",lcOutputFile,1,lcUsername,lcPassword)

this.AssertTrue(lnResult == 0,loFtp.cErrorMsg)
this.AssertTrue(FILE(lcOutputFile),"Failed: File wasn't created")
this.AssertTrue(FileSIZE(lcOutputFile) > 0,"Failed: File Size is " + TRANSFORM(FileSIZE(lcOutputFile)))

ENDFUNC
*   DownloadFileWithSpacesSimple



************************************************************************
*  UploadFileSimple
****************************************
***  Function:
***    Assume:
***      Pass:
***    Return:
************************************************************************
FUNCTION UploadFileSimple()


loFtp = CREATEOBJECT("wwSftp")
loFtp.nFtpPort = 23

lcHost = "127.0.0.1"
lcUsername = "tester"
lcPassword = "password"


lcSourceFile = ".\tests\sailbig.jpg"
lcTargetFile = "Sailbig.jpg"
lnResult = loFtp.FtpSendFile(lcHost,lcSourceFile,lcTargetFile,lcUsername,lcPassword)

this.AssertTrue(lnResult == 0,loFtp.cErrorMsg)

ENDFUNC
*   UploadFileSimple


************************************************************************
*  DownloadFile
****************************************
***  Function:
***    Assume:
***      Pass:
***    Return:
************************************************************************
FUNCTION DownloadFile()

loFtp = CREATEOBJECT("wwSftp")

lcHost = "127.0.0.1"
lnPort = 23
lcUsername = "tester"
lcPassword = "password"

loFtp.cFtpServer = lcHost
loFtp.nFtpPort = lnPort
loFtp.cUsername = lcUsername
loFtp.cPassword = lcPassword

BINDEVENT(loFtp,"OnFtpBufferUpdate",this,"BufferUpdate")

lcOutputFile = ".\tests\sailbig_downloaded.jpg"
DELETE FILE lcOutputFile

loFtp.FtpConnect()

lnResult = loFtp.FtpGetFileEx("sailbig.jpg",lcOutputFile)

loFtp.FtpClose()


this.AssertTrue(lnResult == 0,loFtp.cErrorMsg)
this.AssertTrue(FILE(lcOutputFile))

ENDFUNC
*   DownloadFile


FUNCTION BufferUpdate(lnbytesdownloaded,lnbufferreads,lccurrentchunk, lnTotalBytes, loFtp)

WAIT WINDOW NOWAIT TRANSFORM(lnBytesDownloaded) + " of " + TRANSFORM(lnTotalBytes)

ENDFUNC

************************************************************************
*  UploadFileSimple
****************************************
***  Function:
***    Assume:
***      Pass:
***    Return:
************************************************************************
FUNCTION UploadFile()

loFtp = CREATEOBJECT("wwSftp")

loFtp.cFtpServer ="127.0.0.1"
loFtp.nFtpPort = 23
loFtp.cUsername = "tester"
loFtp.cPassword = "password"


DELETE FILE "SENDFILETEST.ZZZ"

IF NOT FILE("SENDFILETEST.ZZZ")
  lcBlob = ""
  FOR i = 1 TO 255 
    lcBlob = lcBlob + CHR(i)
  ENDFOR 
  lcBigBlob = REPLICATE(lcBlob,256)
  FOR i = 1 TO 101
    STRTOFILE(lcBigBlob,"SENDFILETEST.ZZZ",1)
  ENDFOR
ENDIF

lcSourceFile =  FULLPATH(".\SENDFILETEST.ZZZ")   && "C:\installs\Distribution CD\Demos\wconnect.exe"
lcTargetFile = "SendFileTest_Fox_Simple.zzz"

BINDEVENT(loFtp,"OnFtpBufferUpdate",this,"BufferUpdate")

loFtp.FtpConnect()

lnResult = loFtp.FtpSendFileEx(lcSourceFile,lcTargetFile)

loFtp.FtpClose()

this.AssertTrue(lnResult == 0,loFtp.cErrorMsg)

ENDFUNC
*   UploadFile


************************************************************************
*  UploadBigFile
****************************************
***  Function:
***    Assume:
***      Pass:
***    Return:
************************************************************************
FUNCTION UploadBigFileSendFileEx()

loFtp = CREATEOBJECT("wwSftp")

loFtp.cFtpServer ="127.0.0.1"
loFtp.nFtpPort = 23
loFtp.cUsername = "tester"
loFtp.cPassword = "password"

DELETE FILE "SENDFILETEST.ZZZ"

IF NOT FILE("SENDFILETEST.ZZZ")
  lcBlob = ""
  FOR i = 1 TO 255 
    lcBlob = lcBlob + CHR(i)
  ENDFOR 
  lcBigBlob = REPLICATE(lcBlob,256)
  FOR i = 1 TO 201
    STRTOFILE(lcBigBlob,"SENDFILETEST.ZZZ",1)
  ENDFOR
ENDIF

lcSourceFile =  FULLPATH(".\SENDFILETEST.ZZZ")   && "C:\installs\Distribution CD\Demos\wconnect.exe"
lcTargetFile = "SendFileTest_Fox.zzz"

*BINDEVENT(loFtp,"OnFtpBufferUpdate",this,"BufferUpdate")

loFtp.FtpConnect()

lnResult = loFtp.FtpSendFileEx(lcSourceFile,lcTargetFile)

loFtp.FtpClose()

this.AssertTrue(lnResult == 0,loFtp.cErrorMsg)

ENDFUNC
*   UploadFile

************************************************************************
*  UploadFileSimple
****************************************
***  Function:
***    Assume:
***      Pass:
***    Return:
************************************************************************
FUNCTION UploadFileWithPrivateKey()

loFtp = CREATEOBJECT("wwSftp")

loFtp.cFtpServer ="127.0.0.1"
loFtp.nFtpPort = 23
loFtp.cUsername = "tester"
loFtp.cPassword = "password"
loFtp.cPrivateKeyFile = "c:\utl\Rebex_PrivateKey_OpenSSH.ppk"
*loFtp.cPrivateKeyFilePassword = "my-super-secure-password"


lcSourceFile = ".\Tests\SailBig.jpg"
lcTargetFile = "Sailbig.jpg"
lcTargetFile = STRTRAN(lcTargetFile,".jpg","_" + TRANSFORM(SECONDS())+ ".jpg")

BINDEVENT(loFtp,"OnFtpBufferUpdate",this,"BufferUpdate")

loFtp.FtpConnect()

lnResult = loFtp.FtpSendFileEx(lcSourceFile,lcTargetFile)

loFtp.FtpClose()

this.AssertTrue(lnResult == 0,loFtp.cErrorMsg)

ENDFUNC
*   UploadFileWithPrivateKeyFile

************************************************************************
*  UploadFileSimple
****************************************
***  Function:
***    Assume:
***      Pass:
***    Return:
************************************************************************
FUNCTION UploadFileWithPrivateKeyAndPassword()

loFtp = CREATEOBJECT("wwSftp")

loFtp.cFtpServer ="127.0.0.1"
loFtp.nFtpPort = 23
loFtp.cUsername = "tester"
*loFtp.cPassword = "password"
loFtp.cPrivateKeyFile = "c:\utl\server-private-key-rsa_OPENSSL.ppk"
loFtp.cPrivateKeyFilePassword = "my-super-secure-password"


lcSourceFile = ".\Tests\SailBig.jpg"
lcTargetFile = "Sailbig.jpg"
lcTargetFile = STRTRAN(lcTargetFile,".jpg","_" + TRANSFORM(SECONDS())+ ".jpg")

BINDEVENT(loFtp,"OnFtpBufferUpdate",this,"BufferUpdate")

loFtp.FtpConnect()

lnResult = loFtp.FtpSendFileEx(lcSourceFile,lcTargetFile)

loFtp.FtpClose()

this.AssertTrue(lnResult == 0,loFtp.cErrorMsg)

ENDFUNC
*   UploadFileWithPrivateKeyFile


************************************************************************
*  UploadBigFile
****************************************
***  Function:
***    Assume:
***      Pass:
***    Return:
************************************************************************
FUNCTION UploadBigFileSendFile()

loFtp = CREATEOBJECT("wwSftp")

loFtp.cFtpServer ="127.0.0.1"
loFtp.nFtpPort = 23
loFtp.cUsername = "tester"
loFtp.cPassword = "password"

DELETE FILE "SENDFILETEST.ZZZ"

IF NOT FILE("SENDFILETEST.ZZZ")
  lcBlob = ""
  FOR i = 0 TO 255 
    lcBlob = lcBlob + CHR(i)
  ENDFOR 
  lcBigBlob = REPLICATE(lcBlob,256)
  FOR i = 1 TO 500
    STRTOFILE(lcBigBlob,"SENDFILETEST.ZZZ",1)
  ENDFOR
ENDIF

lcSourceFile =  FULLPATH(".\SENDFILETEST.ZZZ")   && "C:\installs\Distribution CD\Demos\wconnect.exe"
THIS.AssertTrue(FILE(lcSourceFile),"Source File " + lcSourceFile + " doesn't exist.")
lcTargetFile = "./SubFolder/SendFileTest.zzz"

BINDEVENT(loFtp,"OnFtpBufferUpdate",this,"BufferUpdate")

lnResult = loFtp.FtpSendFile(loFtp.cFtpServer,lcSourceFile,lcTargetFile,loFtp.cUsername, loFtp.cPassword)

this.AssertTrue(lnResult == 0,loFtp.cErrorMsg)

ENDFUNC
*   UploadFile

************************************************************************
*  FtpListFiles
****************************************
***  Function:
***    Assume:
***      Pass:
***    Return:
************************************************************************
FUNCTION FtpListFiles()

loFtp = CREATEOBJECT("wwFtp")
loFtp.cFtpServer = "west-wind.com"
loFtp.nFtpPort = 23
loFtp.cUsername = "tester"
loFtp.cPassword = "password"

loFtp.FtpConnect()
loFiles = loFtp.FtpListFiles("*.*")
loFtp.FtpClose()

this.AssertTrue(!ISNULL(loFiles),loFtp.cErrorMsg)

FOR EACH loFile IN loFiles FOXOBJECT
  ? loFile.Name + " - " + TRANSFORM(loFile.Length) + " - " + TRANSFORM(loFile.FileAttribute) + " -  " + TRANSFORM(loFile.LastWriteTime)
ENDFOR

ENDFUNC
*   ListFilesFtp

************************************************************************
*  aSFtpFiles
****************************************
***  Function:
***    Assume:
***      Pass:
***    Return:
************************************************************************
FUNCTION aSFtpFiles()

loFtp = CREATEOBJECT("wwSFtp")

lcHost = "127.0.0.1"
lnPort = 23
lcUsername = "tester"
lcPassword = "password"


loFtp.cFtpServer = lcHost
loFtp.nFtpPort = lnPort
loFtp.cUsername = lcUsername
loFtp.cPassword = lcPassword
loFtp.nFtpPort = lnPort

loFtp.FtpConnect()


DIMENSION laFiles[1,4]
lnFiles = loFtp.aFtpDir(@laFiles,"/")
loFtp.FtpClose()

this.AssertTrue(lnFiles > 0,loFtp.cErrorMsg)

FOR lnX = 1 TO lnFiles
  ? laFiles[lnX,1] + " - " + TRANSFORM(laFiles[lnX,2]) + " - " + TRANSFORM(laFiles[lnX,3]) + " -  " + TRANSFORM(laFiles[lnX,4])
ENDFOR

ENDFUNC
*   aFtpFiles



************************************************************************
*  SFtpListFiles
****************************************
***  Function:
***    Assume:
***      Pass:
***    Return:
************************************************************************
FUNCTION SftpListFiles()

loFtp = this.StartSftpConnection()

loFiles = loFtp.FtpListFiles("/SubFolder")
loFtp.FtpClose()

this.AssertTrue(!ISNULL(loFiles),loFtp.cErrorMsg)

FOR EACH loFile IN loFiles FOXOBJECT
  ? loFile.Name + " - " + TRANSFORM(loFile.Length) + " - " + TRANSFORM(loFile.FileAttribute) + " -  " + TRANSFORM(loFile.LastWriteTime)
ENDFOR

ENDFUNC
*   ListFilesFtp


************************************************************************
*  ChangeRenameAndList
****************************************
***  Function:
***    Assume:
***      Pass:
***    Return:
************************************************************************
FUNCTION ChangeFolderUploadRenameAndList()


CLEAR

*** Connect()
loFtp = THIS.StartSftpConnection()


this.AssertTrue(loFtp.FtpSetDirectory("subfolder"),loFtp.cErrorMsg)

llResult = loFtp.FtpSendFileEx(".\tests\sailbig.jpg","sailbig.jpg")
? "Uploaded: " + TRANSFORM(llResult)

THIS.AssertTrue(llResult == 0,loFtp.cErrorMsg)


loFiles = loFtp.FtpListFiles("")
? "List: "  + TRANSFORM(!ISNULL(loFiles))

this.AssertTrue(!ISNULL(loFiles),loFtp.cErrorMsg)

? loFiles.Count

FOR EACH loFile IN loFiles FOXOBJECT
  ? loFile.Name + " - " + TRANSFORM(loFile.Length) + " - " + TRANSFORM(loFile.FileAttribute) + " -  " + TRANSFORM(loFile.LastWriteTime)
  IF ATC("sailbig",loFile.Name) >0
     lcNewname = STRTRAN(loFile.Name,".jpg",TRANSFORM(INT(Seconds())) + ".jpg")
     ? loFile.Name + " " + lcNewName
     
     THIS.AssertTrue( loFtp.FtpRenameFile(loFile.Name,lcNewName), loFtp.cErrorMsg )
  ENDIF
ENDFOR

loFiles = loFtp.FtpListFiles("")

? "Deleting all 'sailbig_xxx.jpg' files"
FOR EACH loFile IN loFiles FOXOBJECT
  *IF (loFile.FileAttribute = 16)
  IF ATC("sailbig",loFile.Name) >0
	  ? "Deleting: " + loFile.Name + " - " + TRANSFORM(loFile.Length) + " - " + TRANSFORM(loFile.FileAttribute) + " -  " + TRANSFORM(loFile.LastWriteTime)
	  this.AssertTrue(loFtp.FtpDeleteFile(loFile.Name) == 0,loFtp.cErrorMsg)
  ENDIF	
ENDFOR

loFtp.FtpClose()

ENDFUNC
*   ChangeRenameAndList

************************************************************************
*  CreateAndRemoveFolder
****************************************
***  Function:
***    Assume:
***      Pass:
***    Return:
************************************************************************
FUNCTION CreateAndRemoveFolder()

*** Connect()
loFtp = THIS.StartSftpConnection()

this.AssertTrue(loFtp.FtpSetDirectory("SubFolder"),"SetDirectory failed: " + loFtp.cErrorMsg)

this.AssertTrue(loFtp.FtpCreateDirectory("SubFolder2"),"CreateDirectory Failed: " + loFtp.cErrorMsg)

this.AssertTrue(FILE(".\Tests\sailbig.jpg"),"File doesn't exist: sailbig.jpg")
this.AssertTrue(loFtp.FtpSendFileEx(".\Tests\sailbig.jpg","/SubFolder/SubFolder2/sailbig.jpg")==0,"SendFileEx Failed: " + loFtp.cErrorMsg)

this.AssertTrue(loFtp.FtpDeleteFile("SubFolder2/sailbig.jpg")==0, "DeleteFile Failed: " + loFtp.cErrorMsg)

this.AssertTrue(loFtp.FtpRemoveDirectory("Subfolder2"),"Remove Directory Failed: " + loFtp.cErrorMsg)


ENDFUNC
*   CreateAndRemoveFolder



************************************************************************
*  GetSFTP
****************************************
***  Function:
***    Assume:
***      Pass:
***    Return:
************************************************************************
PROTECTED FUNCTION StartSFTPConnection()

loFtp = CREATEOBJECT("wwSftp")

lcHost = "127.0.0.1"
lnPort = 23
lcUsername = "tester"
lcPassword = "password"


loFtp.cFtpServer = lcHost
loFtp.nFtpPort = lnPort
loFtp.cUsername = lcUsername
loFtp.cPassword = lcPassword
loFtp.nFtpPort = lnPort

loFtp.FtpConnect()

RETURN loFtp
ENDFUNC
*   StartSFTPConnection



ENDDEFINE
