|
| 1 | +// Copyright (c) 2026 Meter, Inc. |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at: |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package statsclient |
| 16 | + |
| 17 | +import ( |
| 18 | + "testing" |
| 19 | + "unsafe" |
| 20 | + |
| 21 | + "go.fd.io/govpp/adapter" |
| 22 | +) |
| 23 | + |
| 24 | +// dirEntryBuf lays out a directory vector holding the given names, for both |
| 25 | +// segment versions. The vector is preceded by its length, as VPP lays it out. |
| 26 | +func dirEntryBufV1(t *testing.T, names []string) (dirVector, func()) { |
| 27 | + t.Helper() |
| 28 | + sz := int(unsafe.Sizeof(statSegDirectoryEntryV1{})) |
| 29 | + buf := make([]byte, 8+sz*len(names)) |
| 30 | + *(*uint32)(unsafe.Pointer(&buf[0])) = uint32(len(names)) |
| 31 | + base := unsafe.Pointer(&buf[8]) |
| 32 | + for i, n := range names { |
| 33 | + e := (*statSegDirectoryEntryV1)(unsafe.Add(base, i*sz)) |
| 34 | + copy(e.name[:], n) |
| 35 | + e.name[len(n)] = 0 |
| 36 | + e.directoryType = 2 // SimpleCounterVector |
| 37 | + } |
| 38 | + return dirVector(base), func() { runtimeKeepAlive(buf) } |
| 39 | +} |
| 40 | + |
| 41 | +func dirEntryBufV2(t *testing.T, names []string) (dirVector, func()) { |
| 42 | + t.Helper() |
| 43 | + sz := int(unsafe.Sizeof(statSegDirectoryEntryV2{})) |
| 44 | + buf := make([]byte, 8+sz*len(names)) |
| 45 | + *(*uint32)(unsafe.Pointer(&buf[0])) = uint32(len(names)) |
| 46 | + base := unsafe.Pointer(&buf[8]) |
| 47 | + for i, n := range names { |
| 48 | + e := (*statSegDirectoryEntryV2)(unsafe.Add(base, i*sz)) |
| 49 | + copy(e.name[:], n) |
| 50 | + e.name[len(n)] = 0 |
| 51 | + e.directoryType = 2 // SimpleCounterVector |
| 52 | + } |
| 53 | + return dirVector(base), func() { runtimeKeepAlive(buf) } |
| 54 | +} |
| 55 | + |
| 56 | +func runtimeKeepAlive(b []byte) { _ = b } |
| 57 | + |
| 58 | +// TestStatDirOnIndexMatches covers both segment versions. v1 is easy to leave |
| 59 | +// untested - the fake segment harness is v2-only - and it is exactly where a |
| 60 | +// regression would go unnoticed. |
| 61 | +func TestStatDirOnIndexMatches(t *testing.T) { |
| 62 | + names := []string{"/if/rx", "/node/errors", "/sys/heartbeat"} |
| 63 | + |
| 64 | + for _, tc := range []struct { |
| 65 | + version string |
| 66 | + build func(*testing.T, []string) (dirVector, func()) |
| 67 | + seg statSegment |
| 68 | + }{ |
| 69 | + {"v1", dirEntryBufV1, &statSegmentV1{}}, |
| 70 | + {"v2", dirEntryBufV2, &statSegmentV2{sharedHeader: make([]byte, 1<<12)}}, |
| 71 | + } { |
| 72 | + t.Run(tc.version, func(t *testing.T) { |
| 73 | + v, keep := tc.build(t, names) |
| 74 | + defer keep() |
| 75 | + |
| 76 | + for i, n := range names { |
| 77 | + _, typ, ok := tc.seg.StatDirOnIndexMatches(v, uint32(i), []byte(n)) |
| 78 | + if !ok { |
| 79 | + t.Errorf("index %d (%q): reported no match", i, n) |
| 80 | + } |
| 81 | + if typ == adapter.Unknown { |
| 82 | + t.Errorf("index %d (%q): matched but type is Unknown", i, n) |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | + // A name that is a prefix of the stored one must NOT match: the |
| 87 | + // length check is what stops /if/rx matching /if/rx-unicast. |
| 88 | + if _, _, ok := tc.seg.StatDirOnIndexMatches(v, 0, []byte("/if/r")); ok { |
| 89 | + t.Error("a prefix of the stored name matched") |
| 90 | + } |
| 91 | + // Nor an extension of it. |
| 92 | + if _, _, ok := tc.seg.StatDirOnIndexMatches(v, 0, []byte("/if/rx-unicast")); ok { |
| 93 | + t.Error("an extension of the stored name matched") |
| 94 | + } |
| 95 | + // Wrong entry at the right length. |
| 96 | + if _, _, ok := tc.seg.StatDirOnIndexMatches(v, 0, []byte("/if/tx")); ok { |
| 97 | + t.Error("a different name of equal length matched") |
| 98 | + } |
| 99 | + // Empty want against a non-empty entry. |
| 100 | + if _, _, ok := tc.seg.StatDirOnIndexMatches(v, 0, nil); ok { |
| 101 | + t.Error("an empty name matched a populated entry") |
| 102 | + } |
| 103 | + // A mismatch must report Unknown rather than a plausible type. |
| 104 | + if _, typ, _ := tc.seg.StatDirOnIndexMatches(v, 0, []byte("/if/tx")); typ != adapter.Unknown { |
| 105 | + t.Errorf("mismatch reported type %v, want Unknown", typ) |
| 106 | + } |
| 107 | + }) |
| 108 | + } |
| 109 | +} |
| 110 | + |
| 111 | +// TestStatDirOnIndexMatchesAgreesWithGet pins the two accessors together: the |
| 112 | +// in-place comparison must accept exactly the names GetStatDirOnIndex reports. |
| 113 | +func TestStatDirOnIndexMatchesAgreesWithGet(t *testing.T) { |
| 114 | + f := newFakeSegment(t, fakeGroupValues(8)) |
| 115 | + sc := f.client() |
| 116 | + v := sc.GetDirectoryVector() |
| 117 | + if v == nil { |
| 118 | + t.Fatal("nil directory vector") |
| 119 | + } |
| 120 | + for i := uint32(0); i < 10; i++ { |
| 121 | + _, name, typ := sc.GetStatDirOnIndex(v, i) |
| 122 | + if len(name) == 0 { |
| 123 | + continue |
| 124 | + } |
| 125 | + _, mTyp, ok := sc.StatDirOnIndexMatches(v, i, name) |
| 126 | + if !ok { |
| 127 | + t.Errorf("index %d: GetStatDirOnIndex says %q, Matches disagrees", i, name) |
| 128 | + } |
| 129 | + if mTyp != typ { |
| 130 | + t.Errorf("index %d (%q): type %v vs %v", i, name, mTyp, typ) |
| 131 | + } |
| 132 | + } |
| 133 | +} |
0 commit comments