github.com/blueinnovationsgroup/can-go@v0.0.0-20230518195432-d0567cda0028/pkg/candevice/device_integration_test.go (about)

     1  //go:build integration
     2  
     3  package candevice
     4  
     5  import (
     6  	"fmt"
     7  	"testing"
     8  )
     9  
    10  const (
    11  	bitrate125K = 125000
    12  	bitrate250K = 250000
    13  )
    14  
    15  func TestSetBitrate(t *testing.T) {
    16  	d, err := New("can0")
    17  	if err != nil {
    18  		t.Fatal("couldn't set up device:", err)
    19  	}
    20  	defer d.SetDown()
    21  
    22  	if err := setBitrate(d, bitrate125K); err != nil {
    23  		t.Fatal(err)
    24  	}
    25  	if err := setBitrate(d, bitrate250K); err != nil {
    26  		t.Fatal(err)
    27  	}
    28  
    29  	// Set bitrate on device which is up
    30  	if err := d.SetUp(); err != nil {
    31  		t.Fatal(err)
    32  	}
    33  	if err := setBitrate(d, bitrate125K); err == nil {
    34  		t.Fatal("setting bitrate on device which is up succeeded")
    35  	}
    36  	if err := d.SetDown(); err != nil {
    37  		t.Fatal(err)
    38  	}
    39  
    40  	// Invalid bitrate
    41  	if err := setBitrate(d, 0); err == nil {
    42  		t.Fatal("setting invalid bitrate succeeded")
    43  	}
    44  }
    45  
    46  func TestSetUpDown(t *testing.T) {
    47  	d, err := New("can0")
    48  	if err != nil {
    49  		t.Fatal("couldn't set up device:", err)
    50  	}
    51  	defer d.SetDown()
    52  
    53  	if err := d.SetBitrate(bitrate125K); err != nil {
    54  		t.Fatal(err)
    55  	}
    56  
    57  	// Set up twice and set down twice. This checks that calling it twice has no effect
    58  	if err := setUp(d); err != nil {
    59  		t.Fatal(err)
    60  	}
    61  	if err := setUp(d); err != nil {
    62  		t.Fatal(err)
    63  	}
    64  	if err := setDown(d); err != nil {
    65  		t.Fatal(err)
    66  	}
    67  	if err := setDown(d); err != nil {
    68  		t.Fatal(err)
    69  	}
    70  }
    71  
    72  func setBitrate(d *Device, bitrate uint32) error {
    73  	if err := d.SetBitrate(bitrate); err != nil {
    74  		return err
    75  	}
    76  	if err := setUp(d); err != nil {
    77  		return err
    78  	}
    79  	actualBitrate, err := d.Bitrate()
    80  	if err != nil {
    81  		return err
    82  	}
    83  	if err := setDown(d); err != nil {
    84  		return err
    85  	}
    86  	if actualBitrate != bitrate {
    87  		return fmt.Errorf("expected bitrate: %d, actual: %d", bitrate, bitrate)
    88  	}
    89  	return nil
    90  }
    91  
    92  func setUp(d *Device) error {
    93  	if err := d.SetUp(); err != nil {
    94  		return err
    95  	}
    96  
    97  	isUp, err := d.IsUp()
    98  	if err != nil {
    99  		return err
   100  	}
   101  	if !isUp {
   102  		return fmt.Errorf("device not up after calling SetUp()")
   103  	}
   104  	return nil
   105  }
   106  
   107  func setDown(d *Device) error {
   108  	if err := d.SetDown(); err != nil {
   109  		return err
   110  	}
   111  	isUp, err := d.IsUp()
   112  	if err != nil {
   113  		return err
   114  	}
   115  	if isUp {
   116  		return fmt.Errorf("device not down after calling SetDown()")
   117  	}
   118  	return nil
   119  }