github.com/jlowellwofford/u-root@v1.0.0/pkg/termios/termios_test.go (about)

     1  // Copyright 2015-2017 the u-root Authors. All rights reserved
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  // Package termios implements basic termios operations including getting
     6  // a termio struct, a winsize struct, and setting raw mode.
     7  // To set raw mode and then restore, one can do:
     8  // t, err := termios.Raw()
     9  // do things
    10  // t.Set()
    11  package termios
    12  
    13  import (
    14  	"encoding/json"
    15  	"os"
    16  	"reflect"
    17  	"strings"
    18  	"testing"
    19  )
    20  
    21  func TestNew(t *testing.T) {
    22  	if _, err := New(); os.IsNotExist(err) {
    23  		t.Skipf("No /dev/tty here.")
    24  	} else if err != nil {
    25  		t.Errorf("TestNew: want nil, got %v", err)
    26  	}
    27  
    28  }
    29  
    30  func TestRaw(t *testing.T) {
    31  	tty, err := New()
    32  	if os.IsNotExist(err) {
    33  		t.Skipf("No /dev/tty here.")
    34  	} else if err != nil {
    35  		t.Fatalf("TestRaw new: want nil, got %v", err)
    36  	}
    37  	term, err := tty.Get()
    38  	if err != nil {
    39  		t.Fatalf("TestRaw get: want nil, got %v", err)
    40  	}
    41  
    42  	n, err := tty.Raw()
    43  	if err != nil {
    44  		t.Fatalf("TestRaw raw: want nil, got %v", err)
    45  	}
    46  	if !reflect.DeepEqual(term, n) {
    47  		t.Fatalf("TestRaw: New(%v) and Raw(%v) should be equal, are not", t, n)
    48  	}
    49  	if err := tty.Set(n); err != nil {
    50  		t.Fatalf("TestRaw restore mode: want nil, got %v", err)
    51  	}
    52  	n, err = tty.Get()
    53  	if err != nil {
    54  		t.Fatalf("TestRaw second call to New(): want nil, got %v", err)
    55  	}
    56  	if !reflect.DeepEqual(term, n) {
    57  		t.Fatalf("TestRaw: After Raw restore: New(%v) and check(%v) should be equal, are not", term, n)
    58  	}
    59  }
    60  
    61  // Test proper unmarshaling and consistent, repeatable output from String()
    62  func TestString(t *testing.T) {
    63  	// This JSON is from a real device.
    64  	j := `{
    65  	"Ispeed": 0,
    66  	"Ospeed": 0,
    67  	"Row": 72,
    68  	"Col": 238,
    69  	"CC": {
    70  		"eof": 4,
    71  		"eol": 255,
    72  		"eol2": 255,
    73  		"erase": 127,
    74  		"intr": 3,
    75  		"kill": 21,
    76  		"lnext": 22,
    77  		"min": 0,
    78  		"quit": 28,
    79  		"start": 17,
    80  		"stop": 19,
    81  		"susp": 26,
    82  		"werase": 23,
    83  		"time": 3
    84  	},
    85  	"Opts": {
    86  		"xcase": false,
    87  		"brkint": true,
    88  		"clocal": false,
    89  		"cread": true,
    90  		"cstopb": false,
    91  		"echo": true,
    92  		"echoctl": true,
    93  		"echoe": true,
    94  		"echok": true,
    95  		"echoke": true,
    96  		"echonl": false,
    97  		"echoprt": false,
    98  		"flusho": false,
    99  		"hupcl": false,
   100  		"icanon": true,
   101  		"icrnl": true,
   102  		"iexten": true,
   103  		"ignbrk": false,
   104  		"igncr": false,
   105  		"ignpar": true,
   106  		"imaxbel": true,
   107  		"inlcr": false,
   108  		"inpck": false,
   109  		"isig": true,
   110  		"istrip": false,
   111  		"iuclc": false,
   112  		"iutf8": true,
   113  		"ixany": false,
   114  		"ixoff": false,
   115  		"ixon": true,
   116  		"noflsh": false,
   117  		"ocrnl": false,
   118  		"ofdel": false,
   119  		"ofill": false,
   120  		"olcuc": false,
   121  		"onlcr": true,
   122  		"onlret": false,
   123  		"onocr": false,
   124  		"opost": true,
   125  		"parenb": false,
   126  		"parmrk": false,
   127  		"parodd": false,
   128  		"pendin": true,
   129  		"tostop": false
   130  	}
   131  }`
   132  	s := `speed:0 rows:72 cols:238 brkint:1 clocal:0 cread:1 cstopb:0 echo:1 echoctl:1 echoe:1 echok:1 echoke:1 echonl:0 echoprt:0 eof:0x04 eol2:0xff eol:0xff erase:0x7f flusho:0 hupcl:0 icanon:1 icrnl:1 iexten:1 ignbrk:0 igncr:0 ignpar:1 imaxbel:1 inlcr:0 inpck:0 intr:0x03 isig:1 istrip:0 iuclc:0 iutf8:1 ixany:0 ixoff:0 ixon:1 kill:0x15 lnext:0x16 min:0x00 noflsh:0 ocrnl:0 ofdel:0 ofill:0 olcuc:0 onlcr:1 onlret:0 onocr:0 opost:1 parenb:0 parmrk:0 parodd:0 pendin:1 quit:0x1c start:0x11 stop:0x13 susp:0x1a time:0x03 tostop:0 werase:0x17 xcase:0`
   133  	var g *tty = &tty{}
   134  	if err := json.Unmarshal([]byte(j), g); err != nil {
   135  		t.Fatalf("stty load: %v", err)
   136  	}
   137  
   138  	if g.String() != s {
   139  		t.Errorf("GTTY: want '%v', got '%v'", s, g.String())
   140  		as := strings.Split(s, " ")
   141  		ag := strings.Split(g.String(), " ")
   142  		if len(as) != len(ag) {
   143  			t.Fatalf("Wrong # elements in gtty: want %d, got %d", len(as), len(ag))
   144  		}
   145  		for i := range as {
   146  			t.Errorf("want %s got %s Same %v", as[i], ag[i], as[i] == ag[i])
   147  		}
   148  
   149  	}
   150  
   151  }