package ftp

//import (
//	"fmt"
//	"testing"
//
//	"git.loafle.net/overflow/overflow_probe/discovery/discovery/scan/matcher/packet"
//	"git.loafle.net/overflow/overflow_probe/discovery/discovery/scan/matcher/scaninfo"
//	"git.loafle.net/overflow/overflow_probe/discovery/discovery/types"
//	log "github.com/cihub/seelog"
//	"net"
//)
//
////type FTP struct {
////    conn net.Conn
////    addr string
////
////    reader *bufio.Reader
////    writer *bufio.Writer
////}
////
////func (ftp *FTP) Close()  {
////    ftp.conn.Close()
////}
////
////func Connect(addr string) (*FTP, error)  {
////    var err error
////    var conn net.Conn
////
////    if conn, err = net.Dial("tcp", addr); err != nil {
////        return nil, err
////    }
////
////    writer := bufio.NewWriter(conn)
////    reader := bufio.NewReader(conn)
////
////    obj := &FTP{
////        conn:conn,
////        addr:addr,
////        reader:reader,
////        writer:writer,
////    }
////    recv, _ := obj.receive()
////
////    fmt.Println(recv)
////
////    return obj, nil
////
////}
////
////func (ftp *FTP) receive() (string, error) {
////    line, err := ftp.receiveLine()
////
////    if err != nil {
////        return line, err
////    }
////
////    fmt.Println("len : ", len(line))
////    fmt.Println("line[3] :", line[3])
////    //
////    //if (len(line) >= 4) && (line[3] == '-') {
////    //    closingCode := line[:3] + " "
////    //
////    //    for {
////    //        str, err := ftp.receiveLine()
////    //        fmt.Println("str pre: ", str)
////    //        line = line + str
////    //        fmt.Println("str after: ", line)
////    //        if err != nil {
////    //            return line, err
////    //        }
////    //
////    //        if len(str) < 4 {
////    //            fmt.Println("Uncorrectly terminated response")
////    //        }else {
////    //            if str[:4] == closingCode {
////    //                break
////    //            }
////    //        }
////    //    }
////    //}
////
////    ftp.ReadAndDiscard()
////
////    fmt.Println("receive line: ", line)
////    return line, err
////}
////
////func (ftp *FTP) ReadAndDiscard() (int, error) {
////    var i int
////    bufferSize := ftp.reader.Buffered()
////
////    for i = 0; i < bufferSize ; i++  {
////        if _, err := ftp.reader.ReadByte(); err != nil {
////            return i, err
////        }
////    }
////
////    return i, nil
////}
////
////func (ftp *FTP) send(command string, arguments ...interface{}) error {
////
////    command = fmt.Sprintf(command)
////    command += "\r\n"
////
////    if _, err := ftp.writer.WriteString(command); err != nil {
////        return err
////    }
////
////    if err := ftp.writer.Flush(); err != nil {
////        return err
////    }
////
////    return nil
////}
////
////func (ftp *FTP) cmd(expects string, command string, args ...interface{}) (line string, err error)  {
////
////    if err = ftp.send(command, args); err != nil {
////        return
////    }
////
////    if line, err = ftp.receive(); err != nil {
////        return
////    }
////
////
////    if !strings.HasPrefix(line, expects) {
////        err = errors.New(line)
////        return
////    }
////
////    return
////}
////
////func (ftp *FTP) receiveLine() (string, error)  {
////    line, err := ftp.reader.ReadString('\n')
////
////    log.Printf("< %s", line)
////
////    return line, err
////}
////
////func (ftp *FTP) Syst() (line string, err error) {
////    if err := ftp.send("SYST"); err != nil {
////        return "", err
////    }
////
////    if line, err = ftp.receive(); err != nil {
////        return
////    }
////
////    if !strings.HasPrefix(line, "215") {
////        err = errors.New(line)
////        return
////    }
////
////    return strings.SplitN(strings.TrimSpace(line), " ", 2)[1], nil
////}
//
////func TestFtp(t *testing.T)  {
////    var err error
////    var ftp *FTP
////    //var f *FTPMatcher
////
////    if ftp, err = Connect("192.168.1.202:21"); err != nil {
////        panic(err)
////    }
////
////    //f.Match(0, nil,nil)
////    ftp.Syst()
////    ftp.cmd("503","PASS ")
////    ftp.cmd("221","QUIT")
////    defer ftp.Close()
////}
//
//func TestMatchFTP(t *testing.T) {
//	ftm := NewFTPMatcher()
//	//fmt.Println(ftm)
//	//fmt.Println(ftm.sendPackets[0])
//
//	//log.LoadLogConfig("../../../../../../../../bin/log.xml")
//	//defer log.Flush()
//
//	port := types.NewPort("21", types.NewHost("192.168.1.202"), types.TYPE_TCP)
//	info := scaninfo.NewServiceScanInfo(port)
//
//	var ipport string
//	ipport = port.Host.Ip + ":" + string(port.Port)
//
//	log.Debug(ipport)
//
//	client, _ := net.Dial("tcp", ipport)
//
//	defer client.Close()
//
//	//reader := bufio.NewReader(client)
//	//writer := bufio.NewWriter(client)
//
//	fmt.Println(ftm.PacketCount())
//	//fmt.Println(reader.ReadString('\n'))
//
//	bytes := make([]byte, 512)
//
//	le, _ := client.Read(bytes)
//
//	fmt.Println(bytes)
//
//	b := ftm.Match(0, packet.NewPacket(bytes, le), nil)
//
//	fmt.Println(b)
//
//	for ii := 0; ii < ftm.PacketCount(); ii++ {
//		pack := ftm.Packet(ii)
//
//		fmt.Println(pack)
//
//		//writer.WriteString(pack)
//		client.Write(pack.Buffer)
//		//fmt.Println(reader.ReadString('\n'))
//		bytes := make([]byte, 512)
//
//		l, _ := client.Read(bytes)
//
//		//fmt.Println(bytes)
//
//		b := ftm.Match(ii+1, packet.NewPacket(bytes, l), info)
//
//		fmt.Println(b)
//
//	}
//
//	fmt.Println("Service Name : ", ftm.ServiceName())
//}
