|
| 1 | +package repo |
| 2 | + |
| 3 | +import ( |
| 4 | + "reflect" |
| 5 | + "testing" |
| 6 | +) |
| 7 | + |
| 8 | +// createTestBugCheckRepo creates a test repository with sample BugCheck data |
| 9 | +func createTestBugCheckRepo() BugCheckRepo { |
| 10 | + return BugCheckRepo{ |
| 11 | + { |
| 12 | + Code: 0x0000000A, |
| 13 | + Name: "IRQL_NOT_LESS_OR_EQUAL", |
| 14 | + URL: "https://docs.microsoft.com/en-us/windows-hardware/drivers/debugger/bug-check-0xa--irql-not-less-or-equal", |
| 15 | + Description: "This indicates that Microsoft Windows or a kernel-mode driver accessed paged memory at DISPATCH_LEVEL or above.", |
| 16 | + Parameters: []string{ |
| 17 | + "Memory address that was referenced", |
| 18 | + "IRQL that was required", |
| 19 | + "Type of access (0 = read, 1 = write)", |
| 20 | + "Address of instruction which referenced memory", |
| 21 | + }, |
| 22 | + }, |
| 23 | + { |
| 24 | + Code: 0x0000001E, |
| 25 | + Name: "KMODE_EXCEPTION_NOT_HANDLED", |
| 26 | + URL: "https://docs.microsoft.com/en-us/windows-hardware/drivers/debugger/bug-check-0x1e--kmode-exception-not-handled", |
| 27 | + Description: "This indicates that a kernel-mode program generated an exception which the error handler did not catch.", |
| 28 | + Parameters: []string{ |
| 29 | + "The exception code that was not handled", |
| 30 | + "The address at which the exception occurred", |
| 31 | + "Parameter 0 of the exception", |
| 32 | + "Parameter 1 of the exception", |
| 33 | + }, |
| 34 | + }, |
| 35 | + { |
| 36 | + Code: 0x00000050, |
| 37 | + Name: "PAGE_FAULT_IN_NONPAGED_AREA", |
| 38 | + URL: "https://docs.microsoft.com/en-us/windows-hardware/drivers/debugger/bug-check-0x50--page-fault-in-nonpaged-area", |
| 39 | + Description: "This indicates that invalid system memory has been referenced.", |
| 40 | + Parameters: []string{}, |
| 41 | + }, |
| 42 | + { |
| 43 | + Code: 0x000000D1, |
| 44 | + Name: "DRIVER_IRQL_NOT_LESS_OR_EQUAL", |
| 45 | + URL: "https://docs.microsoft.com/en-us/windows-hardware/drivers/debugger/bug-check-0xd1--driver-irql-not-less-or-equal", |
| 46 | + Description: "This indicates that a kernel-mode driver attempted to access pageable memory at a process IRQL that was too high.", |
| 47 | + Parameters: []string{ |
| 48 | + "Memory address referenced", |
| 49 | + "IRQL at time of reference", |
| 50 | + "Type of access (0 = read, 1 = write)", |
| 51 | + "Address of instruction which referenced memory", |
| 52 | + }, |
| 53 | + }, |
| 54 | + } |
| 55 | +} |
| 56 | + |
| 57 | +func TestBugCheckRepo_FindBugCheckCode(t *testing.T) { |
| 58 | + repo := createTestBugCheckRepo() |
| 59 | + |
| 60 | + tests := []struct { |
| 61 | + name string |
| 62 | + code uint32 |
| 63 | + expected []BugCheck |
| 64 | + }{ |
| 65 | + { |
| 66 | + name: "find IRQL_NOT_LESS_OR_EQUAL", |
| 67 | + code: 0x0000000A, |
| 68 | + expected: []BugCheck{ |
| 69 | + { |
| 70 | + Code: 0x0000000A, |
| 71 | + Name: "IRQL_NOT_LESS_OR_EQUAL", |
| 72 | + URL: "https://docs.microsoft.com/en-us/windows-hardware/drivers/debugger/bug-check-0xa--irql-not-less-or-equal", |
| 73 | + Description: "This indicates that Microsoft Windows or a kernel-mode driver accessed paged memory at DISPATCH_LEVEL or above.", |
| 74 | + Parameters: []string{ |
| 75 | + "Memory address that was referenced", |
| 76 | + "IRQL that was required", |
| 77 | + "Type of access (0 = read, 1 = write)", |
| 78 | + "Address of instruction which referenced memory", |
| 79 | + }, |
| 80 | + }, |
| 81 | + }, |
| 82 | + }, |
| 83 | + { |
| 84 | + name: "find PAGE_FAULT_IN_NONPAGED_AREA", |
| 85 | + code: 0x00000050, |
| 86 | + expected: []BugCheck{ |
| 87 | + { |
| 88 | + Code: 0x00000050, |
| 89 | + Name: "PAGE_FAULT_IN_NONPAGED_AREA", |
| 90 | + URL: "https://docs.microsoft.com/en-us/windows-hardware/drivers/debugger/bug-check-0x50--page-fault-in-nonpaged-area", |
| 91 | + Description: "This indicates that invalid system memory has been referenced.", |
| 92 | + Parameters: []string{}, |
| 93 | + }, |
| 94 | + }, |
| 95 | + }, |
| 96 | + { |
| 97 | + name: "find non-existent code", |
| 98 | + code: 0x00000999, |
| 99 | + expected: []BugCheck{}, |
| 100 | + }, |
| 101 | + } |
| 102 | + |
| 103 | + for _, tt := range tests { |
| 104 | + t.Run(tt.name, func(t *testing.T) { |
| 105 | + result := repo.FindBugCheckCode(tt.code) |
| 106 | + if !reflect.DeepEqual(result, tt.expected) { |
| 107 | + t.Errorf("BugCheckRepo.FindBugCheckCode(0x%08X) = %v, expected %v", tt.code, result, tt.expected) |
| 108 | + } |
| 109 | + }) |
| 110 | + } |
| 111 | +} |
| 112 | + |
| 113 | +func TestBugCheckRepo_FindBugCheckString(t *testing.T) { |
| 114 | + repo := createTestBugCheckRepo() |
| 115 | + |
| 116 | + tests := []struct { |
| 117 | + name string |
| 118 | + search string |
| 119 | + expected int // Expected number of matches |
| 120 | + }{ |
| 121 | + { |
| 122 | + name: "find by exact name", |
| 123 | + search: "IRQL_NOT_LESS_OR_EQUAL", |
| 124 | + expected: 1, |
| 125 | + }, |
| 126 | + { |
| 127 | + name: "find by partial name - case insensitive", |
| 128 | + search: "irql", |
| 129 | + expected: 1, // Only matches IRQL_NOT_LESS_OR_EQUAL (case sensitive search) |
| 130 | + }, |
| 131 | + { |
| 132 | + name: "find by partial name - uppercase", |
| 133 | + search: "PAGE_FAULT", |
| 134 | + expected: 1, |
| 135 | + }, |
| 136 | + { |
| 137 | + name: "find by partial name - lowercase", |
| 138 | + search: "page_fault", |
| 139 | + expected: 1, |
| 140 | + }, |
| 141 | + { |
| 142 | + name: "find by single character", |
| 143 | + search: "K", |
| 144 | + expected: 1, // Should match KMODE_EXCEPTION_NOT_HANDLED |
| 145 | + }, |
| 146 | + { |
| 147 | + name: "find non-existent string", |
| 148 | + search: "NONEXISTENT", |
| 149 | + expected: 0, |
| 150 | + }, |
| 151 | + { |
| 152 | + name: "empty search string", |
| 153 | + search: "", |
| 154 | + expected: 4, // Should match all (empty string is prefix of all strings) |
| 155 | + }, |
| 156 | + } |
| 157 | + |
| 158 | + for _, tt := range tests { |
| 159 | + t.Run(tt.name, func(t *testing.T) { |
| 160 | + result := repo.FindBugCheckString(tt.search) |
| 161 | + if len(result) != tt.expected { |
| 162 | + t.Errorf("BugCheckRepo.FindBugCheckString(%q) returned %d matches, expected %d", tt.search, len(result), tt.expected) |
| 163 | + } |
| 164 | + }) |
| 165 | + } |
| 166 | +} |
| 167 | + |
| 168 | +func TestBugCheckRepo_FindCode(t *testing.T) { |
| 169 | + repo := createTestBugCheckRepo() |
| 170 | + |
| 171 | + tests := []struct { |
| 172 | + name string |
| 173 | + code uint32 |
| 174 | + expected []ErrorInfo |
| 175 | + }{ |
| 176 | + { |
| 177 | + name: "find IRQL_NOT_LESS_OR_EQUAL as ErrorInfo", |
| 178 | + code: 0x0000000A, |
| 179 | + expected: []ErrorInfo{ |
| 180 | + { |
| 181 | + Code: 0x0000000A, |
| 182 | + Name: "IRQL_NOT_LESS_OR_EQUAL", |
| 183 | + Description: "This indicates that Microsoft Windows or a kernel-mode driver accessed paged memory at DISPATCH_LEVEL or above.", |
| 184 | + }, |
| 185 | + }, |
| 186 | + }, |
| 187 | + { |
| 188 | + name: "find PAGE_FAULT_IN_NONPAGED_AREA as ErrorInfo", |
| 189 | + code: 0x00000050, |
| 190 | + expected: []ErrorInfo{ |
| 191 | + { |
| 192 | + Code: 0x00000050, |
| 193 | + Name: "PAGE_FAULT_IN_NONPAGED_AREA", |
| 194 | + Description: "This indicates that invalid system memory has been referenced.", |
| 195 | + }, |
| 196 | + }, |
| 197 | + }, |
| 198 | + { |
| 199 | + name: "find non-existent code as ErrorInfo", |
| 200 | + code: 0x00000999, |
| 201 | + expected: []ErrorInfo{}, |
| 202 | + }, |
| 203 | + } |
| 204 | + |
| 205 | + for _, tt := range tests { |
| 206 | + t.Run(tt.name, func(t *testing.T) { |
| 207 | + result := repo.FindCode(tt.code) |
| 208 | + if !reflect.DeepEqual(result, tt.expected) { |
| 209 | + t.Errorf("BugCheckRepo.FindCode(0x%08X) = %v, expected %v", tt.code, result, tt.expected) |
| 210 | + } |
| 211 | + }) |
| 212 | + } |
| 213 | +} |
| 214 | + |
| 215 | +func TestRepo_FindBugCheck(t *testing.T) { |
| 216 | + repo := Repo{ |
| 217 | + BugCheck: createTestBugCheckRepo(), |
| 218 | + } |
| 219 | + |
| 220 | + tests := []struct { |
| 221 | + name string |
| 222 | + code uint32 |
| 223 | + expected []ErrorInfo |
| 224 | + }{ |
| 225 | + { |
| 226 | + name: "find through Repo.FindBugCheck", |
| 227 | + code: 0x0000000A, |
| 228 | + expected: []ErrorInfo{ |
| 229 | + { |
| 230 | + Code: 0x0000000A, |
| 231 | + Name: "IRQL_NOT_LESS_OR_EQUAL", |
| 232 | + Description: "This indicates that Microsoft Windows or a kernel-mode driver accessed paged memory at DISPATCH_LEVEL or above.", |
| 233 | + }, |
| 234 | + }, |
| 235 | + }, |
| 236 | + { |
| 237 | + name: "find non-existent through Repo.FindBugCheck", |
| 238 | + code: 0x00000999, |
| 239 | + expected: []ErrorInfo{}, |
| 240 | + }, |
| 241 | + } |
| 242 | + |
| 243 | + for _, tt := range tests { |
| 244 | + t.Run(tt.name, func(t *testing.T) { |
| 245 | + result := repo.FindBugCheck(tt.code) |
| 246 | + if !reflect.DeepEqual(result, tt.expected) { |
| 247 | + t.Errorf("Repo.FindBugCheck(0x%08X) = %v, expected %v", tt.code, result, tt.expected) |
| 248 | + } |
| 249 | + }) |
| 250 | + } |
| 251 | +} |
| 252 | + |
| 253 | +func TestBugCheck_ErrorInfo(t *testing.T) { |
| 254 | + bugCheck := BugCheck{ |
| 255 | + Code: 0x0000000A, |
| 256 | + Name: "IRQL_NOT_LESS_OR_EQUAL", |
| 257 | + URL: "https://example.com", |
| 258 | + Description: "Test description", |
| 259 | + Parameters: []string{"param1", "param2"}, |
| 260 | + } |
| 261 | + |
| 262 | + expected := ErrorInfo{ |
| 263 | + Code: 0x0000000A, |
| 264 | + Name: "IRQL_NOT_LESS_OR_EQUAL", |
| 265 | + Description: "Test description", |
| 266 | + } |
| 267 | + |
| 268 | + result := bugCheck.ErrorInfo() |
| 269 | + if !reflect.DeepEqual(result, expected) { |
| 270 | + t.Errorf("BugCheck.ErrorInfo() = %v, expected %v", result, expected) |
| 271 | + } |
| 272 | +} |
| 273 | + |
| 274 | +// Test edge cases |
| 275 | +func TestBugCheckRepo_EdgeCases(t *testing.T) { |
| 276 | + tests := []struct { |
| 277 | + name string |
| 278 | + repo BugCheckRepo |
| 279 | + code uint32 |
| 280 | + expected int |
| 281 | + }{ |
| 282 | + { |
| 283 | + name: "empty repository", |
| 284 | + repo: BugCheckRepo{}, |
| 285 | + code: 0x0000000A, |
| 286 | + expected: 0, |
| 287 | + }, |
| 288 | + { |
| 289 | + name: "nil repository", |
| 290 | + repo: nil, |
| 291 | + code: 0x0000000A, |
| 292 | + expected: 0, |
| 293 | + }, |
| 294 | + { |
| 295 | + name: "repository with duplicate codes", |
| 296 | + repo: BugCheckRepo{ |
| 297 | + {Code: 0x0000000A, Name: "DUPLICATE_1", Description: "First"}, |
| 298 | + {Code: 0x0000000A, Name: "DUPLICATE_2", Description: "Second"}, |
| 299 | + }, |
| 300 | + code: 0x0000000A, |
| 301 | + expected: 2, |
| 302 | + }, |
| 303 | + } |
| 304 | + |
| 305 | + for _, tt := range tests { |
| 306 | + t.Run(tt.name, func(t *testing.T) { |
| 307 | + result := tt.repo.FindBugCheckCode(tt.code) |
| 308 | + if len(result) != tt.expected { |
| 309 | + t.Errorf("BugCheckRepo.FindBugCheckCode() returned %d matches, expected %d", len(result), tt.expected) |
| 310 | + } |
| 311 | + }) |
| 312 | + } |
| 313 | +} |
| 314 | + |
| 315 | +// Benchmark tests |
| 316 | +func BenchmarkBugCheckRepo_FindBugCheckCode(b *testing.B) { |
| 317 | + repo := createTestBugCheckRepo() |
| 318 | + |
| 319 | + for i := 0; i < b.N; i++ { |
| 320 | + _ = repo.FindBugCheckCode(0x0000000A) |
| 321 | + } |
| 322 | +} |
| 323 | + |
| 324 | +func BenchmarkBugCheckRepo_FindBugCheckString(b *testing.B) { |
| 325 | + repo := createTestBugCheckRepo() |
| 326 | + |
| 327 | + for i := 0; i < b.N; i++ { |
| 328 | + _ = repo.FindBugCheckString("IRQL") |
| 329 | + } |
| 330 | +} |
| 331 | + |
| 332 | +func BenchmarkBugCheckRepo_FindCode(b *testing.B) { |
| 333 | + repo := createTestBugCheckRepo() |
| 334 | + |
| 335 | + for i := 0; i < b.N; i++ { |
| 336 | + _ = repo.FindCode(0x0000000A) |
| 337 | + } |
| 338 | +} |
| 339 | + |
| 340 | +func BenchmarkBugCheck_String(b *testing.B) { |
| 341 | + bugCheck := BugCheck{ |
| 342 | + Code: 0x0000000A, |
| 343 | + Name: "IRQL_NOT_LESS_OR_EQUAL", |
| 344 | + Description: "Test description", |
| 345 | + Parameters: []string{"param1", "param2", "param3"}, |
| 346 | + } |
| 347 | + |
| 348 | + for i := 0; i < b.N; i++ { |
| 349 | + _ = bugCheck.String() |
| 350 | + } |
| 351 | +} |
| 352 | + |
| 353 | +// Example tests |
| 354 | +func ExampleBugCheckRepo_FindBugCheckCode() { |
| 355 | + repo := BugCheckRepo{ |
| 356 | + {Code: 0x0000000A, Name: "IRQL_NOT_LESS_OR_EQUAL", Description: "IRQL error"}, |
| 357 | + } |
| 358 | + |
| 359 | + matches := repo.FindBugCheckCode(0x0000000A) |
| 360 | + if len(matches) > 0 { |
| 361 | + println(matches[0].Name) // IRQL_NOT_LESS_OR_EQUAL |
| 362 | + } |
| 363 | +} |
| 364 | + |
| 365 | +func ExampleBugCheckRepo_FindBugCheckString() { |
| 366 | + repo := BugCheckRepo{ |
| 367 | + {Code: 0x0000000A, Name: "IRQL_NOT_LESS_OR_EQUAL", Description: "IRQL error"}, |
| 368 | + {Code: 0x000000D1, Name: "DRIVER_IRQL_NOT_LESS_OR_EQUAL", Description: "Driver IRQL error"}, |
| 369 | + } |
| 370 | + |
| 371 | + matches := repo.FindBugCheckString("IRQL") |
| 372 | + println(len(matches)) // 2 |
| 373 | +} |
0 commit comments