示例#1
0
        public void FTPRestTest()
        {
            // 启动 FTP 服务器
            FtpServer.StartServiceThread();

            IPEndPoint server = CommandHelper.AddressParser("(127,0,0,1,0,21)");
            Client     client = new Client(server, "anonymous", "*****@*****.**");

            client.Connect();

            Random random = new Random();

            // 生成文件内容
            string fileContents = (Guid.NewGuid().ToString() + Guid.NewGuid().ToString());

            byte[] fileContentsBytes = System.Text.Encoding.UTF8.GetBytes(fileContents);
            int    filesize          = fileContentsBytes.Length;

            // 生成文件名
            string remoteFilename = Guid.NewGuid().ToString() + ".txt";
            string remotePath     = '/' + remoteFilename;

            // 随机一个中断点
            int uploadBreakpoint = random.Next(36) + 17;

            // 分两次上传数据
            byte[] uploadContentA = Encoding.UTF8.GetBytes(fileContents.Substring(0, uploadBreakpoint));
            client.Upload(remotePath, uploadContentA, 0);
            byte[] uploadContentB = Encoding.UTF8.GetBytes(fileContents.Substring(uploadBreakpoint));
            client.Upload(remotePath, uploadContentB, uploadBreakpoint);

            // 随机一个中断点
            int downloadBreakpoint = random.Next(36) + 17;

            // 一次性完整下载
            byte[] downloadContentFull = client.Download(remotePath, 0);
            string downloadContent     = Encoding.UTF8.GetString(downloadContentFull);

            // 检验上传正确
            Assert.AreEqual(fileContents, downloadContent);


            byte[] downloadContentPart = client.Download(remotePath, downloadBreakpoint);
            downloadContent = Encoding.UTF8.GetString(downloadContentPart);
            // 检验下载正确
            Assert.AreEqual(fileContents.Substring(downloadBreakpoint), downloadContent);

            client.Disconnect();
        }
示例#2
0
        public void TestFTPClientAll()
        {
            FtpServer.StartServiceThread();

            IPEndPoint server = CommandHelper.AddressParser("(127,0,0,1,0,21)");
            Client     client = new Client(server, "anonymous", "*****@*****.**");

            client.Connect();


            // 测试文件信息

            // 随便生成个文件名
            string filename = "/" + Guid.NewGuid().ToString() + ".txt";

            // 测试文件名
            string testFile = "test.txt";

            // 本地文件路径
            string localDir  = @"E:\";
            string localPath = localDir + testFile;

            // 远程文件路径
            string remoteDir  = "/temp/";
            string remotePath = remoteDir + testFile;

            // 随便生成个文件数据
            string fileContents = Guid.NewGuid().ToString() + "\r\n" + Guid.NewGuid().ToString();

            byte[] fileContentsBytes = System.Text.Encoding.UTF8.GetBytes(fileContents);

            // 计算一下长度待会儿用
            int filesize = fileContentsBytes.Length;


            ////////////////////////////////////////////////////////////////////////////////////////////////
            List <FileInfo> list;

            // 测试文件上传
            TestUpload(client, filename, fileContentsBytes);

            // 测试目录列表
            list = TestList(client, "/");
            int flag = 0;

            foreach (var aFileInfo in list)
            {
                if (aFileInfo.FileName == filename.Substring(1))
                {
                    Assert.AreEqual(filesize, aFileInfo.Size);
                    flag = 1;
                    break;
                }
            }

            Assert.AreEqual(1, flag);

            // 测试本地文件上传(路径上传)
            TestUpload(client, localPath, remotePath);

            // 本地文件长度
            long localFilesize = 0;

            using (FileStream fs = new FileStream(localPath, FileMode.Open, FileAccess.Read))
            {
                try
                {
                    byte[] buff = new byte[fs.Length];
                    fs.Read(buff, 0, (int)fs.Length);
                    localFilesize = buff.Length;
                }
                catch (Exception ex)
                {
                    throw ex;
                }
            }

            // 测试目录列表(路径上传)
            list = TestList(client, remoteDir);
            flag = 0;
            foreach (var aFileInfo in list)
            {
                if (aFileInfo.FileName == testFile)
                {
                    Assert.AreEqual(localFilesize, aFileInfo.Size);
                    flag = 1;
                    break;
                }
            }

            Assert.AreEqual(1, flag);

            // 测试文件下载
            byte[] downloadedFile = TestDownload(client, filename);
            Assert.AreEqual(System.Text.Encoding.UTF8.GetString(downloadedFile), fileContents);

            // 测试文件下载(路径下载)
            TestDownload(client, localPath, remotePath);

            // 计算下载文件长度
            long downloadFilesize = 0;

            using (FileStream fs = new FileStream(localPath, FileMode.Open, FileAccess.Read))
            {
                try
                {
                    byte[] buff = new byte[fs.Length];
                    fs.Read(buff, 0, (int)fs.Length);
                    downloadFilesize = buff.Length;
                }
                catch (Exception ex)
                {
                    throw ex;
                }
            }

            // 测试文件是否一致(路径下载)
            list = TestList(client, remoteDir);
            foreach (var aFileInfo in list)
            {
                if (aFileInfo.FileName == testFile)
                {
                    Assert.AreEqual(downloadFilesize, aFileInfo.Size);
                    break;
                }
            }

            // 测试目录删除
            string deleteDirectory = "/temp/deletetest";

            client.CreateDirectory(deleteDirectory);
            TestDeleteDirectory(client, deleteDirectory);

            // 测试文件删除
            TestDelete(client, filename);

            // 测试文件列表
            TestList(client, "/");
            flag = 0;
            foreach (var aFileInfo in list)
            {
                if (aFileInfo.FileName == filename)
                {
                    flag = 1;
                    break;
                }
            }

            Assert.AreEqual(0, flag);
        }