gitlab.com/apertussolutions/u-root@v7.0.0+incompatible/pkg/gpio/gpio_linux_test.go (about)

     1  // Copyright 2019 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 gpio
     6  
     7  import (
     8  	"testing"
     9  
    10  	"github.com/u-root/u-root/pkg/testutil"
    11  )
    12  
    13  // GPIO allocations so the tests don't conflict with each other:
    14  //
    15  //   TestExport - 9 and 10
    16  //   TestReadValue - 11
    17  //   TestGetPinID - 12
    18  
    19  func TestReadValue(t *testing.T) {
    20  	testutil.SkipIfNotRoot(t)
    21  
    22  	const gpioNum = 11
    23  
    24  	if err := Export(gpioNum); err != nil {
    25  		t.Fatal(err)
    26  	}
    27  
    28  	for _, want := range []Value{Low, High} {
    29  		if err := SetOutputValue(gpioNum, want); err != nil {
    30  			t.Fatal(err)
    31  		}
    32  
    33  		if val, err := ReadValue(gpioNum); err != nil {
    34  			t.Fatal(err)
    35  		} else if val != want {
    36  			t.Errorf("ReadValue(%d) = %v, want %v", gpioNum, val, want)
    37  		}
    38  	}
    39  }
    40  
    41  func TestExport(t *testing.T) {
    42  	testutil.SkipIfNotRoot(t)
    43  
    44  	if err := Export(10); err != nil {
    45  		t.Errorf("Could not export pin 10: %v", err)
    46  	}
    47  
    48  	// Only 10-20 are valid GPIOs in the mock chip.
    49  	if err := Export(9); err == nil {
    50  		t.Errorf("Export(pin 9) should have failed, got nil")
    51  	}
    52  }
    53  
    54  func TestGetPinID(t *testing.T) {
    55  	testutil.SkipIfNotRoot(t)
    56  
    57  	// Base is 10, so we expect 10+2.
    58  	if pin, err := GetPinID("gpio-mockup-A", 2); err != nil {
    59  		t.Errorf("GetPinID(gpio-mockup-A, 2) = %v, want nil", err)
    60  	} else if pin != 12 {
    61  		t.Errorf("GetPinID(gpio-mockup-A, 2) = %v, want 12", pin)
    62  	}
    63  
    64  	// There are only 10 GPIOs, so expect this to fail.
    65  	if _, err := GetPinID("gpio-mockup-A", 12); err == nil {
    66  		t.Errorf("GetPinID(gpio-mockup-A, 12) = nil, but wanted error")
    67  	}
    68  
    69  }