|
| 1 | +package dataplane |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/hex" |
| 5 | + "testing" |
| 6 | +) |
| 7 | + |
| 8 | +func TestGenerateLabelMask(t *testing.T) { |
| 9 | + // The expected results are derived from the nftables debug output, |
| 10 | + // serialized as a 16-byte Big-Endian array (MSW first, LSW last). |
| 11 | + tests := []struct { |
| 12 | + name string |
| 13 | + bitIndex int |
| 14 | + expected string // Expected 16-byte hex string |
| 15 | + }{ |
| 16 | + { |
| 17 | + name: "Bit 10 (LSW)", |
| 18 | + bitIndex: 10, |
| 19 | + // Bit 10 is 2^10 = 0x400. This is in the LSW (last 8 bytes). |
| 20 | + expected: "00000000000000000000000000000400", |
| 21 | + }, |
| 22 | + { |
| 23 | + name: "Bit 126 (MSW)", |
| 24 | + bitIndex: 126, |
| 25 | + // Bit 126 is 2^62 within the 64-bit MSW (first 8 bytes). 0x4000000000000000 |
| 26 | + expected: "40000000000000000000000000000000", |
| 27 | + }, |
| 28 | + { |
| 29 | + name: "Bit 127 (MSW)", |
| 30 | + bitIndex: 127, |
| 31 | + // Bit 127 is 2^63 within the 64-bit MSW (first 8 bytes). 0x8000000000000000 |
| 32 | + expected: "80000000000000000000000000000000", |
| 33 | + }, |
| 34 | + { |
| 35 | + name: "Bit 0 (LSW Start)", |
| 36 | + bitIndex: 0, |
| 37 | + // 2^0 = 0x1. In the LSW (last byte). |
| 38 | + expected: "00000000000000000000000000000001", |
| 39 | + }, |
| 40 | + { |
| 41 | + name: "Bit 63 (LSW End)", |
| 42 | + bitIndex: 63, |
| 43 | + // 2^63 = 0x8000000000000000. In the LSW (last 8 bytes). |
| 44 | + expected: "00000000000000008000000000000000", |
| 45 | + }, |
| 46 | + { |
| 47 | + name: "Bit 64 (MSW Start)", |
| 48 | + bitIndex: 64, |
| 49 | + // 2^0 (within the MSW). In the MSW (first 8 bytes). |
| 50 | + expected: "00000000000000010000000000000000", |
| 51 | + }, |
| 52 | + { |
| 53 | + name: "Out of Range (128)", |
| 54 | + bitIndex: 128, |
| 55 | + // Expected 16 zero bytes: "00...00" |
| 56 | + expected: "00000000000000000000000000000000", |
| 57 | + }, |
| 58 | + { |
| 59 | + name: "Out of Range (-1)", |
| 60 | + bitIndex: -1, |
| 61 | + // Expected 16 zero bytes: "00...00" |
| 62 | + expected: "00000000000000000000000000000000", |
| 63 | + }, |
| 64 | + } |
| 65 | + |
| 66 | + for _, tt := range tests { |
| 67 | + t.Run(tt.name, func(t *testing.T) { |
| 68 | + // Call the function |
| 69 | + result := generateLabelMask(tt.bitIndex) |
| 70 | + |
| 71 | + // Convert result to hex string for easy comparison |
| 72 | + actualHex := hex.EncodeToString(result) |
| 73 | + |
| 74 | + // Compare the actual result with the expected hex string |
| 75 | + if actualHex != tt.expected { |
| 76 | + t.Errorf("generateLabelMask() for index %d:\n Got: %v\n Want: %v", tt.bitIndex, actualHex, tt.expected) |
| 77 | + } |
| 78 | + }) |
| 79 | + } |
| 80 | +} |
| 81 | + |
| 82 | +// TestClearLabelBit tests the clearLabelBit function across various scenarios. |
| 83 | +func TestClearLabelBit(t *testing.T) { |
| 84 | + // Helper function to convert a hex string to a byte slice |
| 85 | + mustDecodeHex := func(s string) []byte { |
| 86 | + b, err := hex.DecodeString(s) |
| 87 | + if err != nil { |
| 88 | + panic(err) |
| 89 | + } |
| 90 | + return b |
| 91 | + } |
| 92 | + |
| 93 | + // A base label with bits 10, 63, 64, and 127 set. |
| 94 | + // Bit 127 (MSW: 0x8000000000000000) |
| 95 | + // Bit 64 (MSW: 0x0000000000000001) |
| 96 | + // Bit 63 (LSW: 0x8000000000000000) |
| 97 | + // Bit 10 (LSW: 0x0000000000000400) |
| 98 | + // Base Hex: 80000000000000018000000000000400 |
| 99 | + baseLabelHex := "80000000000000018000000000000400" |
| 100 | + baseLabel := mustDecodeHex(baseLabelHex) |
| 101 | + |
| 102 | + tests := []struct { |
| 103 | + name string |
| 104 | + initialLabel []byte |
| 105 | + bitIndex int |
| 106 | + expectedHex string |
| 107 | + expectChange bool // Used to verify if the original array remains untouched |
| 108 | + }{ |
| 109 | + { |
| 110 | + name: "Clear Bit 10 (LSW Middle)", |
| 111 | + initialLabel: baseLabel, |
| 112 | + bitIndex: 10, |
| 113 | + // Expected: Bit 10 (0x400) cleared -> 8000...018000...0000 |
| 114 | + expectedHex: "80000000000000018000000000000000", |
| 115 | + expectChange: true, |
| 116 | + }, |
| 117 | + { |
| 118 | + name: "Clear Bit 127 (MSW End)", |
| 119 | + initialLabel: baseLabel, |
| 120 | + bitIndex: 127, |
| 121 | + // Expected: Bit 127 (0x80...) cleared -> 0000...018000...0400 |
| 122 | + expectedHex: "00000000000000018000000000000400", |
| 123 | + expectChange: true, |
| 124 | + }, |
| 125 | + { |
| 126 | + name: "Clear Bit 63 (LSW End Boundary)", |
| 127 | + initialLabel: baseLabel, |
| 128 | + bitIndex: 63, |
| 129 | + // Expected: Bit 63 (0x80...) cleared -> 8000...010000...0400 |
| 130 | + expectedHex: "80000000000000010000000000000400", |
| 131 | + expectChange: true, |
| 132 | + }, |
| 133 | + { |
| 134 | + name: "Clear Bit 64 (MSW Start Boundary)", |
| 135 | + initialLabel: baseLabel, |
| 136 | + bitIndex: 64, |
| 137 | + // Expected: Bit 64 (0x01) cleared -> 8000...008000...0400 |
| 138 | + expectedHex: "80000000000000008000000000000400", |
| 139 | + expectChange: true, |
| 140 | + }, |
| 141 | + { |
| 142 | + name: "Clear Bit 0 (LSW Start Boundary)", |
| 143 | + initialLabel: mustDecodeHex("00000000000000000000000000000001"), // Only bit 0 set |
| 144 | + bitIndex: 0, |
| 145 | + // Expected: All zeros |
| 146 | + expectedHex: "00000000000000000000000000000000", |
| 147 | + expectChange: true, |
| 148 | + }, |
| 149 | + { |
| 150 | + name: "Clear Bit Already Zero (Bit 50)", |
| 151 | + initialLabel: baseLabel, |
| 152 | + bitIndex: 50, // Bit 50 is zero in the base label |
| 153 | + expectedHex: baseLabelHex, |
| 154 | + expectChange: true, // A copy is still returned, but the content is the same |
| 155 | + }, |
| 156 | + { |
| 157 | + name: "Out of Range (128)", |
| 158 | + initialLabel: baseLabel, |
| 159 | + bitIndex: 128, |
| 160 | + expectedHex: baseLabelHex, |
| 161 | + expectChange: true, // A copy is still returned, but the content is the same |
| 162 | + }, |
| 163 | + { |
| 164 | + name: "Out of Range (-1)", |
| 165 | + initialLabel: baseLabel, |
| 166 | + bitIndex: -1, |
| 167 | + expectedHex: baseLabelHex, |
| 168 | + expectChange: true, // A copy is still returned, but the content is the same |
| 169 | + }, |
| 170 | + { |
| 171 | + name: "Invalid Length (10 bytes)", |
| 172 | + initialLabel: mustDecodeHex("F0F0F0F0F0"), // Only 5 bytes |
| 173 | + bitIndex: 10, |
| 174 | + expectedHex: "00000000000000000000000000000000", // Should return 16 zero bytes |
| 175 | + expectChange: true, |
| 176 | + }, |
| 177 | + } |
| 178 | + |
| 179 | + for _, tt := range tests { |
| 180 | + t.Run(tt.name, func(t *testing.T) { |
| 181 | + // Save the original hex string for verification |
| 182 | + originalHex := hex.EncodeToString(tt.initialLabel) |
| 183 | + |
| 184 | + // Execute the function |
| 185 | + result := clearLabelBit(tt.initialLabel, tt.bitIndex) |
| 186 | + |
| 187 | + actualHex := hex.EncodeToString(result) |
| 188 | + if actualHex != tt.expectedHex { |
| 189 | + t.Errorf("Result Mismatch for index %d:\n Got: %s\n Want: %s", tt.bitIndex, actualHex, tt.expectedHex) |
| 190 | + } |
| 191 | + |
| 192 | + if len(tt.initialLabel) == 16 && originalHex != hex.EncodeToString(tt.initialLabel) { |
| 193 | + t.Errorf("Original array was modified!\n Initial: %s\n After call: %s", originalHex, hex.EncodeToString(tt.initialLabel)) |
| 194 | + } |
| 195 | + }) |
| 196 | + } |
| 197 | +} |
0 commit comments