uni-store 1.1.0

Storage layer for Uni graph database - Lance datasets, LSM deltas, and WAL
Documentation
// SPDX-License-Identifier: Apache-2.0
// Copyright 2024-2026 Dragonscale Team

//! Test for Issue #62: Recursive decoder stack overflow protection
//!
//! Verifies that the depth-checking logic is in place to prevent
//! stack overflow from deeply nested List/Map structures.
//!
//! Since creating deeply nested Arrow arrays (depth 40+) requires complex
//! manual construction, this test focuses on verifying the protection exists
//! by testing moderate nesting and documenting the fix.

use arrow_array::Array;
use arrow_array::builder::{Int64Builder, ListBuilder};
use std::sync::Arc;
use uni_common::DataType;
use uni_store::storage::value_codec::{CrdtDecodeMode, value_from_column};

#[test]
#[allow(clippy::assertions_on_constants)] // Verify constant exists and has correct range
fn test_recursive_depth_limit_enforced() {
    // Issue #62: Verify that MAX_DECODE_DEPTH protection exists and is reasonable
    //
    // The fix adds depth checking to prevent stack overflow from deeply nested
    // List/Map structures. We verify the constant exists and has a sensible value.
    //
    // Full fix in: crates/uni-store/src/storage/value_codec.rs

    use uni_store::storage::value_codec::MAX_DECODE_DEPTH;

    // Verify the protection exists and is configured reasonably
    // Too low (<16): breaks legitimate nested data structures
    // Too high (>64): risks stack overflow on debug builds
    assert!(
        MAX_DECODE_DEPTH >= 16 && MAX_DECODE_DEPTH <= 64,
        "MAX_DECODE_DEPTH should be 16-64 for safety and usability, got {}",
        MAX_DECODE_DEPTH
    );
}

#[test]
fn test_normal_nested_list_decodes() {
    // Test that normally nested lists (depth 3) decode correctly
    // This ensures the depth-checking doesn't break normal operation

    // Create a List<List<List<Int64>>> structure (depth 3)
    let value_builder = Int64Builder::new();
    let inner_builder = ListBuilder::new(value_builder);
    let middle_builder = ListBuilder::new(inner_builder);
    let mut outer_builder = ListBuilder::new(middle_builder);

    // Build: [[[42, 43]], [[44]]]
    outer_builder.values().values().values().append_value(42);
    outer_builder.values().values().values().append_value(43);
    outer_builder.values().values().append(true);
    outer_builder.values().append(true);

    outer_builder.values().values().values().append_value(44);
    outer_builder.values().values().append(true);
    outer_builder.values().append(true);

    outer_builder.append(true);

    let array = Arc::new(outer_builder.finish()) as Arc<dyn Array>;

    // Define the type: List<List<List<Int64>>>
    let data_type = DataType::List(Box::new(DataType::List(Box::new(DataType::List(
        Box::new(DataType::Int64),
    )))));

    // Should decode successfully
    let result = value_from_column(array.as_ref(), &data_type, 0, CrdtDecodeMode::Strict);
    assert!(
        result.is_ok(),
        "Normal nested list should decode: {:?}",
        result
    );

    // Verify structure
    let value = result.unwrap();
    assert!(value.is_array(), "Top level should be array");
    let arr = value.as_array().unwrap();
    assert_eq!(arr.len(), 2, "Should have 2 outer elements");
}