| 349 | } |
| 350 | |
| 351 | func TestHintFileReaderErrorHandling(t *testing.T) { |
| 352 | t.Run("Open non-existent file", func(t *testing.T) { |
| 353 | reader := &HintFileReader{} |
| 354 | readerPath := filepath.Join("non", "existent", "path", "test.hint") |
| 355 | err := reader.Open(readerPath) |
| 356 | if err == nil { |
| 357 | t.Error("Expected error when opening non-existent file") |
| 358 | } |
| 359 | }) |
| 360 | |
| 361 | t.Run("Read without opening file", func(t *testing.T) { |
| 362 | reader := &HintFileReader{} |
| 363 | _, err := reader.Read() |
| 364 | if err != ErrHintFileEntryInvalid { |
| 365 | t.Errorf("Expected ErrHintFileEntryInvalid, got %v", err) |
| 366 | } |
| 367 | }) |
| 368 | |
| 369 | t.Run("Read corrupted file", func(t *testing.T) { |
| 370 | tmpDir, err := os.MkdirTemp("", "hintfile-test") |
| 371 | if err != nil { |
| 372 | t.Fatalf("Failed to create temp dir: %v", err) |
| 373 | } |
| 374 | defer func() { _ = os.RemoveAll(tmpDir) }() |
| 375 | |
| 376 | corruptedPath := filepath.Join(tmpDir, "corrupted.hint") |
| 377 | // Create a corrupted file with invalid data |
| 378 | err = os.WriteFile(corruptedPath, []byte{0xFF, 0xFF, 0xFF}, 0644) |
| 379 | if err != nil { |
| 380 | t.Fatalf("Failed to create corrupted file: %v", err) |
| 381 | } |
| 382 | |
| 383 | reader := &HintFileReader{} |
| 384 | err = reader.Open(corruptedPath) |
| 385 | if err != nil { |
| 386 | t.Fatalf("Failed to open corrupted file: %v", err) |
| 387 | } |
| 388 | defer func() { |
| 389 | if err := reader.Close(); err != nil { |
| 390 | t.Errorf("Failed to close corrupted file reader: %v", err) |
| 391 | } |
| 392 | }() |
| 393 | |
| 394 | _, err = reader.Read() |
| 395 | if err == nil { |
| 396 | t.Error("Expected error when reading corrupted file") |
| 397 | } |
| 398 | }) |
| 399 | |
| 400 | t.Run("Read empty file", func(t *testing.T) { |
| 401 | tmpDir, err := os.MkdirTemp("", "hintfile-test") |
| 402 | if err != nil { |
| 403 | t.Fatalf("Failed to create temp dir: %v", err) |
| 404 | } |
| 405 | defer func() { _ = os.RemoveAll(tmpDir) }() |
| 406 | |
| 407 | emptyPath := filepath.Join(tmpDir, "empty.hint") |
| 408 | // Create an empty file |