Browse Source

Hush test output. Helper for CP437.

Also helper for UnicodeWidthHeight.
Steve Thielemann 2 years ago
parent
commit
ef6074fdeb
3 changed files with 155 additions and 3 deletions
  1. 2 2
      door/door_test.go
  2. 11 0
      door/help_test.go
  3. 142 1
      door/input_test.go

+ 2 - 2
door/door_test.go

@@ -10,8 +10,8 @@ import (
 )
 
 // Should tests not delete the logfiles?
-const KEEP_LOGS bool = true
-const VERBOSE_TEST bool = true
+const KEEP_LOGS bool = false
+const VERBOSE_TEST bool = false
 
 func TestGoto(t *testing.T) {
 	GotoMap := map[string][]int{"\x1b[10;20H": {20, 10},

+ 11 - 0
door/help_test.go

@@ -1,6 +1,7 @@
 package door
 
 import (
+	"fmt"
 	"net"
 	"testing"
 	"time"
@@ -67,3 +68,13 @@ func clear_socket(socket net.Conn, t *testing.T) string {
 	}
 	return string(buffer[:r])
 }
+
+// Unicode detection response with given screen width/height.
+func UnicodeWidthHeight(width, height int) []byte {
+	return []byte(fmt.Sprintf("\x1b[1;1R\x1b[1;3R\x1b[%d;%dR", height, width))
+}
+
+// CP437 response with screen width/height. (80x25)
+func CP437WidthHeight(width, height int) []byte {
+	return []byte(fmt.Sprintf("\x1b[1;3R\x1b[1;4R\x1b[%d;%dR", height, width))
+}

+ 142 - 1
door/input_test.go

@@ -11,6 +11,147 @@ import (
 	"time"
 )
 
+func TestDoorCP437(t *testing.T) {
+	var tmpFile *os.File
+	var err error
+	tmpFile, err = os.CreateTemp("", "test-*")
+	if err != nil {
+		panic("Cannot create temporary file")
+	}
+
+	// Remember to clean up the file afterwards
+	if !KEEP_LOGS {
+		defer os.Remove(tmpFile.Name())
+	} else {
+		t.Logf("See: %s\n", tmpFile.Name())
+	}
+
+	// establish network socket connection to set Comm_handle
+	var server, client net.Conn
+	server, client = setupSockets()
+
+	// Ok, we have a server socket, and the client socket (that the door would talk to)
+
+	// CP437 80x25 response
+	buffer := CP437WidthHeight(80, 25)
+	_, err = server.Write(buffer)
+	if err != nil {
+		t.Error("server.Write:", err)
+	}
+	time.Sleep(time.Millisecond)
+
+	// Access Fd (File descriptor) of client for dropfile
+	var fd int = socket_to_fd(client)
+	defer close_fd(fd)
+
+	// Create door32.sys file
+	dfc := DropfileConfig{2, fd, 1800, "Test BBSID", 1701, "Real Username", "Handle", 880, 28, 0, 12}
+
+	_, err = 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))
+	if err != nil {
+		t.Error("tmpFile.WriteString:", err)
+	}
+	err = tmpFile.Close()
+	if err != nil {
+		t.Error("tmpFile.Close:", err)
+	}
+
+	d := Door{} // Deprecated: ReaderCanClose: true}
+
+	// Because we're not the only one calling door.Init(), the
+	// door global variables might be from a previous test run.
+
+	Unicode = false
+	CP437 = false
+	Full_CP437 = false
+	Width = 0
+	Height = 0
+
+	// If I call d.Init() more then once flag complains about flag redefined.
+	// Reset flags
+	flag.CommandLine = flag.NewFlagSet(os.Args[0], flag.ExitOnError)
+	// preset commandline args so door can init
+	os.Args = []string{"door", "-d", tmpFile.Name()}
+	d.Init("input-test")
+
+	defer d.Close()
+	// clean up logfile - (filename is from Init name + node #)
+	if !KEEP_LOGS {
+		defer os.Remove("input-test-12.log")
+	}
+
+	// Ok!
+	if Unicode {
+		t.Errorf("Unicode is %t", Unicode)
+	}
+
+	if !CP437 {
+		t.Errorf("Expect CP437 true, is %t", CP437)
+	}
+
+	if Width != 80 {
+		t.Errorf("Width not 80: %d", Width)
+	}
+
+	if Height != 25 {
+		t.Errorf("Height not 25: %d", Height)
+	}
+
+	clear_socket(server, t)
+	// This should all be received by above (I think)...
+
+	t.Logf("server close")
+	log.Println("server close")
+
+	server.Close()
+
+	time.Sleep(time.Millisecond)
+	keyWait := time.Duration(50 * time.Millisecond)
+
+	_, _, err = d.WaitKey(keyWait)
+	if err != ErrDisconnected {
+		t.Errorf("Expected ErrDisconnected, got %#v", err)
+	}
+
+	if !d.Disconnect() {
+		t.Errorf("Disconnected flag shows: %t (should be true)", d.Disconnect())
+	}
+
+	/*
+		if !d.HasKey() {
+			t.Error("HasKey should return true (disconnected).")
+		}
+	*/
+
+	_, _, err = d.WaitKey(time.Millisecond) // GetKey()
+
+	if err != ErrDisconnected {
+		t.Errorf("Expected ErrDisconnected, got %#v", err)
+	}
+
+	t.Logf("client close")
+	client.Close()
+	time.Sleep(time.Millisecond)
+
+	t.Logf("Input on closed server and client")
+	var blank string = d.Input(5)
+
+	if blank != "" {
+		t.Errorf("Input should return blank (hangup).")
+	}
+
+	_, _, err = d.WaitKey(time.Millisecond) // getch()
+
+	if err != ErrDisconnected {
+		t.Errorf("Expected ErrDisconnected, got %#v", err)
+	}
+
+	d.Write("\x00")
+	time.Sleep(time.Millisecond)
+}
+
 func TestDoorInputConnection(t *testing.T) {
 	var tmpFile *os.File
 	var err error
@@ -33,7 +174,7 @@ func TestDoorInputConnection(t *testing.T) {
 	// Ok, we have a server socket, and the client socket (that the door would talk to)
 
 	// unicode 190x43 response
-	buffer := []byte("\x1b[1;1R\x1b[2;3R\x1b[43;190R")
+	buffer := UnicodeWidthHeight(190, 43)
 	_, err = server.Write(buffer)
 	if err != nil {
 		t.Error("server.Write:", err)