mbdavid / LiteDB

LiteDB - A .NET NoSQL Document Store in a single data file
http://www.litedb.org
MIT License
8.35k stars 1.22k forks source link

Add unit tests for handling invalid LiteDB files #2507

Open PanWuming opened 2 weeks ago

PanWuming commented 2 weeks ago

Description

This PR adds four unit tests to handle scenarios where LiteDB attempts to open invalid database files and streams:

Issues

These tests are intended to address issue #2501.

Bugs Discovered

  1. Test_AddDatabase_InvalidDatabase:

    • Bug: The test expected a LiteDB.LiteException to be thrown.
    • Actual: No exception was thrown.
    • Details:

      Assert.Throws() Failure
      Expected: typeof(LiteDB.LiteException)
      Actual:   (No exception was thrown)
      
  2. Test_AddDatabase_InvalidDatabase_LargeFile:

    • Bug: The test expected a LiteDB.LiteException to be thrown.
    • Actual: A System.ArgumentOutOfRangeException was thrown.
    • Details:
      Assert.Throws() Failure
      Expected: typeof(LiteDB.LiteException)
      Actual:   typeof(System.ArgumentOutOfRangeException): Ticks must be between DateTime.MinValue.Ticks and DateTime.MaxValue.Ticks. (Parameter 'ticks')
  3. Test_AddDatabase_InvalidDatabase_MemoryStream:

    • Bug: The test expected a LiteDB.LiteException to be thrown.
    • Actual: A System.NotSupportedException was thrown.
    • Details:
      Assert.IsType() Failure
      Expected: LiteDB.LiteException
      Actual:   System.NotSupportedException
  4. Test_AddDatabase_InvalidDatabase_LargeFile_MemoryStream:

    • Bug: The test expected a LiteDB.LiteException to be thrown.
    • Actual: A System.ArgumentOutOfRangeException was thrown.
    • Details:
      Assert.IsType() Failure
      Expected: LiteDB.LiteException
      Actual:   System.ArgumentOutOfRangeException

These findings indicate that LiteDB is not handling invalid files and streams as expected, leading to different types of exceptions or no exceptions at all.

JKamsker commented 2 weeks ago

Does it also work with memorystreams? I prefer not to write on my disk.

PanWuming commented 2 weeks ago

Does it also work with memorystreams? I prefer not to write on my disk.

Thank you for your feedback. I have updated the PR with the requested MemoryStream tests and included exception outputs for debugging. Please review the latest changes.