|
| 1 | +// Copyright 2024 The Periph Authors. All rights reserved. |
| 2 | +// Use of this source code is governed under the Apache License, Version 2.0 |
| 3 | +// that can be found in the LICENSE file. |
| 4 | + |
| 5 | +// Basic tests. More complete test is contained in the |
| 6 | +// periph.io/x/cmd/periph-smoketest/gpiosmoketest |
| 7 | +// folder. |
| 8 | + |
| 9 | +//go:build linux |
| 10 | + |
| 11 | +package gpioioctl |
| 12 | + |
| 13 | +import ( |
| 14 | + "log" |
| 15 | + "testing" |
| 16 | + |
| 17 | + "periph.io/x/conn/v3/gpio" |
| 18 | + "periph.io/x/conn/v3/gpio/gpioreg" |
| 19 | +) |
| 20 | + |
| 21 | +var testLine *GPIOLine |
| 22 | + |
| 23 | +func init() { |
| 24 | + var err error |
| 25 | + |
| 26 | + if len(Chips) == 0 { |
| 27 | + /* |
| 28 | + During pipeline builds, GPIOChips may not be available, or |
| 29 | + it may build on another OS. In that case, mock in enough |
| 30 | + for a test to pass. |
| 31 | + */ |
| 32 | + line := GPIOLine{ |
| 33 | + number: 0, |
| 34 | + name: "DummyGPIOLine", |
| 35 | + consumer: "", |
| 36 | + edge: gpio.NoEdge, |
| 37 | + pull: gpio.PullNoChange, |
| 38 | + direction: LineDirNotSet, |
| 39 | + } |
| 40 | + |
| 41 | + chip := GPIOChip{name: "DummyGPIOChip", |
| 42 | + path: "/dev/gpiochipdummy", |
| 43 | + label: "Dummy GPIOChip for Testing Purposes", |
| 44 | + lineCount: 1, |
| 45 | + lines: []*GPIOLine{&line}, |
| 46 | + } |
| 47 | + Chips = append(Chips, &chip) |
| 48 | + if err = gpioreg.Register(&line); err != nil { |
| 49 | + nameStr := chip.Name() |
| 50 | + lineStr := line.String() |
| 51 | + log.Println("chip", nameStr, " gpioreg.Register(line) ", lineStr, " returned ", err) |
| 52 | + } |
| 53 | + } |
| 54 | +} |
| 55 | + |
| 56 | +func TestChips(t *testing.T) { |
| 57 | + chip := Chips[0] |
| 58 | + t.Log(chip.String()) |
| 59 | + if len(chip.Name()) == 0 { |
| 60 | + t.Error("chip.Name() is 0 length") |
| 61 | + } |
| 62 | + if len(chip.Path()) == 0 { |
| 63 | + t.Error("chip path is 0 length") |
| 64 | + } |
| 65 | + if len(chip.Label()) == 0 { |
| 66 | + t.Error("chip label is 0 length!") |
| 67 | + } |
| 68 | + if len(chip.Lines()) != chip.LineCount() { |
| 69 | + t.Errorf("Incorrect line count. Found: %d for LineCount, Returned Lines length=%d", chip.LineCount(), len(chip.Lines())) |
| 70 | + } |
| 71 | + for _, line := range chip.Lines() { |
| 72 | + if len(line.Consumer()) == 0 && len(line.Name()) > 0 { |
| 73 | + testLine = line |
| 74 | + break |
| 75 | + } |
| 76 | + } |
| 77 | + if testLine == nil { |
| 78 | + t.Error("Error finding unused line for testing!") |
| 79 | + } |
| 80 | + for _, c := range Chips { |
| 81 | + s := c.String() |
| 82 | + if len(s) == 0 { |
| 83 | + t.Error("Error calling chip.String(). No output returned!") |
| 84 | + } else { |
| 85 | + t.Log(s) |
| 86 | + } |
| 87 | + |
| 88 | + } |
| 89 | + |
| 90 | +} |
| 91 | + |
| 92 | +func TestGPIORegistryByName(t *testing.T) { |
| 93 | + if testLine == nil { |
| 94 | + return |
| 95 | + } |
| 96 | + outLine := gpioreg.ByName(testLine.Name()) |
| 97 | + if outLine == nil { |
| 98 | + t.Fatalf("Error retrieving GPIO Line %s", testLine.Name()) |
| 99 | + } |
| 100 | + if outLine.Name() != testLine.Name() { |
| 101 | + t.Errorf("Error checking name. Expected %s, received %s", testLine.Name(), outLine.Name()) |
| 102 | + } |
| 103 | + |
| 104 | + if outLine.Number() < 0 || outLine.Number() >= len(Chips[0].Lines()) { |
| 105 | + t.Errorf("Invalid chip number %d received for %s", outLine.Number(), testLine.Name()) |
| 106 | + } |
| 107 | +} |
| 108 | + |
| 109 | +func TestNumber(t *testing.T) { |
| 110 | + chip := Chips[0] |
| 111 | + if testLine == nil { |
| 112 | + return |
| 113 | + } |
| 114 | + l := chip.ByName(testLine.Name()) |
| 115 | + if l == nil { |
| 116 | + t.Fatalf("Error retrieving GPIO Line %s", testLine.Name()) |
| 117 | + } |
| 118 | + if l.Number() < 0 || l.Number() >= chip.LineCount() { |
| 119 | + t.Errorf("line.Number() returned value (%d) out of range", l.Number()) |
| 120 | + } |
| 121 | + l2 := chip.ByNumber(l.Number()) |
| 122 | + if l2 == nil { |
| 123 | + t.Errorf("retrieve Line from chip by number %d failed.", l.Number()) |
| 124 | + } |
| 125 | + |
| 126 | +} |
| 127 | + |
| 128 | +func TestString(t *testing.T) { |
| 129 | + if testLine == nil { |
| 130 | + return |
| 131 | + } |
| 132 | + line := gpioreg.ByName(testLine.Name()) |
| 133 | + if line == nil { |
| 134 | + t.Fatalf("Error retrieving GPIO Line %s", testLine.Name()) |
| 135 | + } |
| 136 | + s := line.String() |
| 137 | + if len(s) == 0 { |
| 138 | + t.Errorf("GPIOLine.String() failed.") |
| 139 | + } |
| 140 | +} |
0 commit comments