Mr. Cluey
: Classes : CFtpIn the course of Internet Testing, one task which often comes up is that of transfering a file from one machine to another. Most often, this is done using the File Transfer Protocol, and the applications which use this protocol are usually refered to as FTP.
An Internet based client server application might have the clients move files to the server, where the server application can then begin to manipulate them. Testing the system as a whole is generally straight forward, but if you want to conduct unit tests on the server, you will need some way to move the files.
Enter the CFtp class, a winclass which allows you to move files to the server from within your scripts.
To begin, let's concern ourselves with the interface to the CFtp class.
An FTP application can generally do a lot of different things - open connections, close connections, upload and download files, rename files, move about on a system, etc. Each of the methods that you need should be wrapped in a method - the object here is to make the qap script look like FTP itself.
Here is an example of the code the interface that I needed:
type CFTP is window ;
winclass CFtp
{
// FTP Commands
open( string Server ) {;}
disconnect () {;}
lcd ( string dir ) {;}
put ( string fileSource, string fileTarget optional ) {;}
get ( string fileSource, string fileTarget optional ) {;}
quit () {;}
// Specialized Methods
Upload( string Server, string fileSrc, string fileTarget optional) {;}
}
Upload was the only function I actually needed - it is nothing more than a wrapper around open and put. To move a file from the test machine (where QAP is running) to the FTP Server, we simply do this:
CFTP MyFtp ; MyFtp.Upload( TheServer, fileLocalName, fileRemoteName ) ;
A Download function would be coded similarly. If you needed to work with a server which did not allow Anonymous FTP, you could add a User() method to connect to the server with a particular username and password. Add more specialized methods as you identify your needs.
How can we make all of this work? That depends, at least in part, on where we are, and how macho we really want to be. For instance, on a windows platform, you could write the code into the class to create the socket connections and keep track of all of the handles itself.
Since I really don't have all that much interest in that sort of thing, I took the easy route, using the ftp application that comes with 32 bit windows. This means actually treating the class as a window application.
winclass CFtp : DialogBox
{
// Implementation Specific Code...
tag "C:\WINNT\System32\ftp.exe";
Invoke ()
{
MainWin( this.WndTag ).Start( "ftp" ) ;
}
Dismiss ()
{
this.quit() ;
}
Command ( string cmd )
{
this.TypeKeys( cmd ) ;
this.TypeKeys( KEY_ENTER ) ;
}
}
When you launch FTP from the command line in 32 bit windows, you get a DOS command window to play with, so that's how this particular class is going to be implemented. The funky looking Invoke() method is just to make certain that QAP starts the application without forgetting that it is looking for a dialog box.
The window we are working with isn't particularly sophisticated. To tell it to do something, we just type in the command. Is this adequate for our needs here? Of course! After all, we are trying to use ftp, not test it.
The Command method should be a big hint as to how the ftp methods are going to be implemented. Inside each method we create the string which the user would type at the keyboard. We then pass it to Command(). For instance, here is the open command.
open( string Server )
{
Command( "open {Server}" ) ;
// Respond to the request for a username
Command( "anonymous" ) ;
// Respond to the request for a password
Command( "qap@silknet.com" ) ;
}
Of course, the dos based FTP client is not the only possible implementation. You could dive into the system api, you could use some other ftp client, or even a telnet client (!) if you are familiar enough with the actual protocol.
The most important thing to keep in mind is that you want the whole thing to be small. For the most part, we are just looking for an easy way to move a file from one machine to another.
When uploading files, I completely ignore the various messages that ftp sends back to me (yes, this means that I may miss ftp errors - if I were testing ftp, this would be unacceptable). In fact, I just want the whole thing to work invisibly. I don't quite accomplish that, but I get pretty close with this code
winclass CFtp
{
// Specialized Methods
Upload( string Server, string fileSrc, string fileTarget optional)
{
this.Invoke () ;
this.open( Server ) ;
this.put( fileSrc, fileTarget ) ;
this.Dismiss () ;
}
}
| Mr. Cluey : Classes : CFtp |