|
| 1 | +// go-pn532 |
| 2 | +// Copyright (c) 2025 The Zaparoo Project Contributors. |
| 3 | +// SPDX-License-Identifier: LGPL-3.0-or-later |
| 4 | +// |
| 5 | +// This file is part of go-pn532. |
| 6 | +// |
| 7 | +// go-pn532 is free software; you can redistribute it and/or |
| 8 | +// modify it under the terms of the GNU Lesser General Public |
| 9 | +// License as published by the Free Software Foundation; either |
| 10 | +// version 3 of the License, or (at your option) any later version. |
| 11 | +// |
| 12 | +// go-pn532 is distributed in the hope that it will be useful, |
| 13 | +// but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 15 | +// Lesser General Public License for more details. |
| 16 | +// |
| 17 | +// You should have received a copy of the GNU Lesser General Public License |
| 18 | +// along with go-pn532; if not, write to the Free Software Foundation, |
| 19 | +// Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
| 20 | + |
| 21 | +package detection |
| 22 | + |
| 23 | +import ( |
| 24 | + "testing" |
| 25 | +) |
| 26 | + |
| 27 | +func TestIsPathIgnored(t *testing.T) { |
| 28 | + t.Parallel() |
| 29 | + |
| 30 | + tests := getPathIgnoredTests() |
| 31 | + |
| 32 | + for _, tt := range tests { |
| 33 | + t.Run(tt.name, func(t *testing.T) { |
| 34 | + t.Parallel() |
| 35 | + result := IsPathIgnored(tt.devicePath, tt.ignorePaths) |
| 36 | + if result != tt.expected { |
| 37 | + t.Errorf("IsPathIgnored(%q, %v) = %v, want %v", |
| 38 | + tt.devicePath, tt.ignorePaths, result, tt.expected) |
| 39 | + } |
| 40 | + }) |
| 41 | + } |
| 42 | +} |
| 43 | + |
| 44 | +type pathIgnoredTest struct { |
| 45 | + name string |
| 46 | + devicePath string |
| 47 | + ignorePaths []string |
| 48 | + expected bool |
| 49 | +} |
| 50 | + |
| 51 | +//nolint:funlen // Test data function, acceptable to be longer |
| 52 | +func getPathIgnoredTests() []pathIgnoredTest { |
| 53 | + basicTests := []pathIgnoredTest{ |
| 54 | + { |
| 55 | + name: "empty ignore list", |
| 56 | + devicePath: "/dev/ttyUSB0", |
| 57 | + ignorePaths: []string{}, |
| 58 | + expected: false, |
| 59 | + }, |
| 60 | + { |
| 61 | + name: "empty device path", |
| 62 | + devicePath: "", |
| 63 | + ignorePaths: []string{"/dev/ttyUSB0"}, |
| 64 | + expected: false, |
| 65 | + }, |
| 66 | + { |
| 67 | + name: "exact match unix path", |
| 68 | + devicePath: "/dev/ttyUSB0", |
| 69 | + ignorePaths: []string{"/dev/ttyUSB0"}, |
| 70 | + expected: true, |
| 71 | + }, |
| 72 | + { |
| 73 | + name: "exact match windows path", |
| 74 | + devicePath: "COM2", |
| 75 | + ignorePaths: []string{"COM2"}, |
| 76 | + expected: true, |
| 77 | + }, |
| 78 | + } |
| 79 | + |
| 80 | + caseTests := []pathIgnoredTest{ |
| 81 | + { |
| 82 | + name: "case insensitive match", |
| 83 | + devicePath: "/dev/ttyUSB0", |
| 84 | + ignorePaths: []string{"/DEV/TTYUSB0"}, |
| 85 | + expected: true, |
| 86 | + }, |
| 87 | + { |
| 88 | + name: "windows case insensitive", |
| 89 | + devicePath: "com2", |
| 90 | + ignorePaths: []string{"COM2"}, |
| 91 | + expected: true, |
| 92 | + }, |
| 93 | + } |
| 94 | + |
| 95 | + multipleTests := []pathIgnoredTest{ |
| 96 | + { |
| 97 | + name: "no match", |
| 98 | + devicePath: "/dev/ttyUSB1", |
| 99 | + ignorePaths: []string{"/dev/ttyUSB0"}, |
| 100 | + expected: false, |
| 101 | + }, |
| 102 | + { |
| 103 | + name: "multiple paths with match", |
| 104 | + devicePath: "/dev/ttyUSB1", |
| 105 | + ignorePaths: []string{"/dev/ttyUSB0", "/dev/ttyUSB1", "COM2"}, |
| 106 | + expected: true, |
| 107 | + }, |
| 108 | + { |
| 109 | + name: "multiple paths no match", |
| 110 | + devicePath: "/dev/ttyUSB2", |
| 111 | + ignorePaths: []string{"/dev/ttyUSB0", "/dev/ttyUSB1", "COM2"}, |
| 112 | + expected: false, |
| 113 | + }, |
| 114 | + } |
| 115 | + |
| 116 | + specialTests := []pathIgnoredTest{ |
| 117 | + { |
| 118 | + name: "i2c path format", |
| 119 | + devicePath: "/dev/i2c-1:0x24", |
| 120 | + ignorePaths: []string{"/dev/i2c-1:0x24"}, |
| 121 | + expected: true, |
| 122 | + }, |
| 123 | + { |
| 124 | + name: "spi path format", |
| 125 | + devicePath: "/dev/spidev0.0", |
| 126 | + ignorePaths: []string{"/dev/spidev0.0"}, |
| 127 | + expected: true, |
| 128 | + }, |
| 129 | + { |
| 130 | + name: "path with relative components", |
| 131 | + devicePath: "/dev/../dev/ttyUSB0", |
| 132 | + ignorePaths: []string{"/dev/ttyUSB0"}, |
| 133 | + expected: true, |
| 134 | + }, |
| 135 | + { |
| 136 | + name: "empty strings in ignore list", |
| 137 | + devicePath: "/dev/ttyUSB0", |
| 138 | + ignorePaths: []string{"", "/dev/ttyUSB0", ""}, |
| 139 | + expected: true, |
| 140 | + }, |
| 141 | + } |
| 142 | + |
| 143 | + result := make([]pathIgnoredTest, 0, len(basicTests)+len(caseTests)+len(multipleTests)+len(specialTests)) |
| 144 | + result = append(result, basicTests...) |
| 145 | + result = append(result, caseTests...) |
| 146 | + result = append(result, multipleTests...) |
| 147 | + result = append(result, specialTests...) |
| 148 | + return result |
| 149 | +} |
| 150 | + |
| 151 | +func TestOptionsWithIgnorePaths(t *testing.T) { |
| 152 | + t.Parallel() |
| 153 | + |
| 154 | + opts := DefaultOptions() |
| 155 | + if opts.IgnorePaths != nil { |
| 156 | + t.Errorf("DefaultOptions().IgnorePaths should be nil, got %v", opts.IgnorePaths) |
| 157 | + } |
| 158 | + |
| 159 | + // Test that we can set ignore paths |
| 160 | + opts.IgnorePaths = []string{"/dev/ttyUSB0", "COM2"} |
| 161 | + if len(opts.IgnorePaths) != 2 { |
| 162 | + t.Errorf("Expected 2 ignore paths, got %d", len(opts.IgnorePaths)) |
| 163 | + } |
| 164 | +} |
0 commit comments