gopkg.in/hugelgupf/u-root.v9@v9.0.0-20180831063832-3f6f1057f09b/cmds/io/io_test.go (about)

     1  // Copyright 2012-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 main
     6  
     7  import (
     8  	"fmt"
     9  	"testing"
    10  )
    11  
    12  type op struct {
    13  	name   string
    14  	addr   uint64
    15  	size   int
    16  	retval []byte
    17  	val    interface{}
    18  	want   uint64
    19  	outerr error
    20  	inerr  error
    21  }
    22  
    23  func (o op) Seek(addr int64, whence int) (int64, error) {
    24  	if whence != 0 {
    25  		return -1, fmt.Errorf("Fix your seek methed")
    26  	}
    27  	if addr == 9 && whence == 0 {
    28  		return -1, fmt.Errorf("Seek: illegal seek to %d", addr)
    29  	}
    30  	if o.addr != uint64(addr) {
    31  		return -1, fmt.Errorf("Seek: asked to seek to %v, want %v", addr, o.addr)
    32  	}
    33  	return addr, nil
    34  }
    35  
    36  func (o op) Read(b []byte) (int, error) {
    37  	// check equality right here.
    38  	return len(o.retval), o.inerr
    39  }
    40  
    41  func (o op) Write(b []byte) (int, error) {
    42  	if len(b) > 8 {
    43  		return -1, fmt.Errorf("Bad write size: %d bytes", len(b))
    44  	}
    45  	// check equality right here.
    46  	return len(b), o.outerr
    47  }
    48  
    49  // checkError checks two error cases to make sure they match
    50  // in some reasonable way, since there are four cases ...
    51  func checkError(msg string, got, want error) error {
    52  	if got == nil && want == nil {
    53  		return nil
    54  	}
    55  	if got != nil && want != nil && got.Error() == want.Error() {
    56  		return nil
    57  	}
    58  	return fmt.Errorf("%s: Got %v, want %v", msg, got, want)
    59  }
    60  
    61  func TestIO(t *testing.T) {
    62  	var ops = []op{
    63  		{name: "Test bad seek", addr: 9, size: -1, outerr: fmt.Errorf("out: bad address 9: Seek: illegal seek to 9")},
    64  		{name: "Write and Read byte", val: uint8(1), want: 1, retval: []byte{1}},
    65  		{name: "Write and Read 16 bits", val: uint16(0x12), want: 0x12, retval: []byte{1, 2}},
    66  		{name: "Write and Read 32 bits", val: uint32(0x1234), want: 0x1234, retval: []byte{1, 2, 3, 4}},
    67  		{name: "Write and Read 64 bits", val: uint64(0x12345678), want: 0x12345678, retval: []byte{1, 2, 3, 4, 5, 6, 7, 8}},
    68  	}
    69  
    70  	for i, o := range ops {
    71  		err := out(o, o.addr, o.val)
    72  		if err := checkError(o.name, err, o.outerr); err != nil {
    73  			t.Errorf("%v", err)
    74  		}
    75  		if i == 0 {
    76  			continue
    77  		}
    78  		err = in(o, o.addr, o.val)
    79  		if err := checkError(o.name, err, o.inerr); err != nil {
    80  			t.Errorf("%v", err)
    81  		}
    82  		var val uint64
    83  		switch oval := o.val.(type) {
    84  		case uint8:
    85  			val = uint64(oval)
    86  		case uint16:
    87  			val = uint64(oval)
    88  		case uint32:
    89  			val = uint64(oval)
    90  		case uint64:
    91  			val = oval
    92  		default:
    93  			t.Fatalf("Can't handle %T for %v command", t, o)
    94  		}
    95  
    96  		if val != o.want {
    97  			t.Errorf("Write and read: got %v, want %v", val, o.want)
    98  		}
    99  	}
   100  }