package door import ( "fmt" "io/ioutil" "net" "os" "testing" ) func TestDoorInputConnection(t *testing.T) { tmpFile, err := ioutil.TempFile(os.TempDir(), "test-") if err != nil { panic("Cannot create temporary file") } // Remember to clean up the file afterwards defer os.Remove(tmpFile.Name()) // establish network socket connection to set Comm_handle sock, err := net.Listen("tcp", "127.0.0.1:0") if err != nil { panic(err) } defer sock.Close() // Get address of listening socket address := sock.Addr().String() client, err := net.Dial("tcp", address) if err != nil { panic(err) } defer client.Close() server, err := sock.Accept() if err != nil { panic(err) } defer server.Close() sock.Close() // Ok, we have a server socket, and the client socket (that the door would talk to) // t.Logf("Server: %#v\n", server) // t.Logf("Client: %#v\n", client) // unicode 190x43 response buffer := []byte("\x1b[1;1R\x1b[2;3R\x1b[43;190R") server.Write(buffer) // Access Fd (File descriptor) of client for dropfile client_conn := client.(*net.TCPConn) client_file, err := client_conn.File() var fd int = int(client_file.Fd()) // Create door32.sys file dfc := DropfileConfig{2, fd, 1800, "Test BBSID", 1701, "Real Username", "Handle", 880, 28, 0, 12} tmpFile.WriteString(fmt.Sprintf("%d\n%d\n%d\n%s\n%d\n%s\n%s\n%d\n%d\n%d\n%d\n", dfc.Comm_type, dfc.Comm_handle, dfc.Baudrate, dfc.BBSID, dfc.User_number, dfc.Real_name, dfc.Handle, dfc.Security_level, dfc.Time_left, dfc.Emulation, dfc.Node)) tmpFile.Close() d := Door{} // preset commandline args so door can init os.Args = []string{"door", "-d", tmpFile.Name()} d.Init() // Ok! if !Unicode { t.Errorf("Unicode not true %t", Unicode) } if Width != 190 { t.Errorf("Width not 190: %d", Width) } if Height != 43 { t.Errorf("Height not 43: %d", Height) } keytest := map[string][]int{ "\x1b": []int{0x1b}, "\x0d\x00": []int{0x0d}, "\x0d\x0a": []int{0x0d}, "\x0dCQ": []int{0x0d, 'C', 'Q'}, "\x0dA": []int{0x0d, 'A'}, "\x0dCAT": []int{0x0d, 'C', 'A', 'T'}, "\x00\x50\x00\x48\x00\x4b\x00\x4d": []int{XKEY_DOWN_ARROW, XKEY_UP_ARROW, XKEY_LEFT_ARROW, XKEY_RIGHT_ARROW}, "\x00\x47\x00\x4f\x00\x49\x00\x51": []int{XKEY_HOME, XKEY_END, XKEY_PGUP, XKEY_PGDN}, "\x00\x3b\x00\x3c\x00\x3d\x00\x3e": []int{XKEY_F1, XKEY_F2, XKEY_F3, XKEY_F4}, "\x00\x3f\x00\x40\x00\x41\x00\x42": []int{XKEY_F5, XKEY_F6, XKEY_F7, XKEY_F8}, "\x00\x43\x00\x44\x00\x52\x00\x53": []int{XKEY_F9, XKEY_F10, XKEY_INSERT, XKEY_DELETE}, "\x1b[A\x1b[B\x1b[C\x1b[D": []int{XKEY_UP_ARROW, XKEY_DOWN_ARROW, XKEY_RIGHT_ARROW, XKEY_LEFT_ARROW}, "\x1b[H\x1b[F\x1b[K\x1b[V\x1b[U": []int{XKEY_HOME, XKEY_END, XKEY_END, XKEY_PGUP, XKEY_PGDN}, "\x1b[5~\x1b[6~": []int{XKEY_PGUP, XKEY_PGDN}, "\x1b[@\x1b[2~\x1b[3~": []int{XKEY_INSERT, XKEY_INSERT, XKEY_DELETE}, "\x1bOP\x1bOQ\x1bOR\x1bOS": []int{XKEY_F1, XKEY_F2, XKEY_F3, XKEY_F4}, "\x1b[15~\x1b[17~\x1b[18~\x1b[19~": []int{XKEY_F5, XKEY_F6, XKEY_F7, XKEY_F8}, "\x1b[20~\x1b[21~\x1b[23~\x1b[24~": []int{XKEY_F9, XKEY_F10, XKEY_F11, XKEY_F12}, } for send, get := range keytest { buffer := []byte(send) server.Write(buffer) recv := make([]int, 0) for { input := d.WaitKey(0, 50) if input != -1 { recv = append(recv, input) } else { break } } if len(recv) != len(get) { t.Errorf("Send %#v, LEN expected %#v, got %#v", send, get, recv) } else { matches := true for idx, i := range get { if recv[idx] != i { matches = false break } } if !matches { t.Errorf("Send %#v, MATCH expected %#v, got %#v", send, get, recv) } } } timeout := d.WaitKey(0, 50) if timeout != -1 { t.Errorf("Expected timeout, got %d / %X", timeout, timeout) } else { t.Logf("Ok! Buffer should be empty! -1 (timeout)") } // Input test buffer = []byte("1234567890\r") server.Write(buffer) input := d.Input(5) if input != "12345" { t.Errorf("Expected Input(5) = 12345, but got %#v", input) } buffer = []byte("12345678\x08\x089\r") server.Write(buffer) input = d.Input(5) if input != "1239" { t.Errorf("Expected Input(5) = 1239, but got %#v", input) } buffer = []byte("12\x08\x08\x08987\x00\x48654321\r") server.Write(buffer) input = d.Input(5) if input != "98765" { t.Errorf("Expected Input(5) = 98765, but got %#v", input) } server.Close() hungup := d.WaitKey(1, 0) if hungup != -2 { t.Errorf("Expected -2 (hangup), got %d", hungup) } if !d.Disconnected { t.Errorf("Disconnected flag shows: %t (should be true)", d.Disconnected) } client.Close() } func TestDisplayInput(t *testing.T) { verify := map[int]string{1: " \x08", 2: " \x08\x08", 5: " \x08\x08\x08\x08\x08", } for count, expect := range verify { got := DisplayInput(count) if expect != got { t.Errorf("DisplayInput %d, expected %#v, got %#v", count, expect, got) } } }