test.rs

Overview

This file contains unit tests focused on verifying the correctness of configuration deserialization and validation within the system. Specifically, it tests the parsing and instantiation of network and general configuration structs from JSON strings. The tests ensure that the configuration parsing logic correctly maps JSON fields to Rust types, such as socket addresses and paths, and validates default values for optional fields.

The module is conditionally compiled and only included during test builds (#[cfg(test)]), serving as part of the project's internal verification suite.


Module: tests

Purpose

The tests module encapsulates test cases related to configuration parsing for network and local settings. It uses Rust's built-in test framework and the anyhow crate for error handling in test functions.

Imports


Test Functions

test_network_config() -> anyhow::Result<()>

Description

Tests deserialization of a JSON string into a NetworkConfig struct. It verifies that various network-related fields are parsed correctly and that default values are properly set when not explicitly provided.

Parameters

Returns

Behavior and Assertions

Usage Example

let config_str = r#"{
    "node_advertise_addr": "0.0.0.0:8500",
    "api_addr": "127.0.0.1:8600",
    "api_advertise_addr": "http://node0:8600",
    "gossip_seeds": []
}"#;
let config: NetworkConfig = serde_json::from_str(config_str)?;
assert_eq!(config.bind, SocketAddr::from(([127, 0, 0, 1], 8500)));

test_config_load() -> anyhow::Result<()>

Description

Tests deserialization of a comprehensive JSON configuration string into the main Config struct. This test verifies correct parsing of both network and local configuration sections, including addresses, file paths, node identifiers, and global timing parameters.

Parameters

Returns

Behavior and Assertions

Usage Example

let config_str = r#"{
    "network": {
        "node_advertise_addr": "0.0.0.0:8500",
        "api_addr": "127.0.0.1:8600",
        "api_advertise_addr": "https://node0:8600",
        "gossip_seeds": []
    },
    "local": {
        "node_id": "81a6bea128f5e03843362e55fd574c42a8e457dd553498cbc8ec7e14966d20a3",
        "blockchain_config_path": "../bc_config.json",
        "key_path": "key1.json",
        "zerostate_path": "./zerostate",
        "external_state_share_local_base_dir": "/tmp",
        "parallelization_level": 20,
        "split_state": false,
        "block_keeper_seed_path": "block_keeper.keys.json",
        "block_cache_size": 20,
        "state_cache_size": 10,
        "message_storage_path": "message_strage",
        "rate_limit_on_incoming_block_req": 1000,
        "ext_messages_cache_size": 10,
        "node_wallet_pubkey": "hex_string"
    }
}"#;
let config: Config = serde_json::from_str(config_str)?;
assert_eq!(config.local.blockchain_config_path, PathBuf::from("../bc_config.json"));

Implementation Details and Algorithms


Interaction with Other System Components


Visual Diagram: Test Module Structure and Function Relationships

flowchart TD
TestsModule[tests mod]
TestNetworkConfig["test_network_config()"]
TestConfigLoad["test_config_load()"]
ConfigStruct[Config Struct]
NetworkConfigStruct[NetworkConfig Struct]
NodeIdentifierClass[NodeIdentifier]
TestsModule --> TestNetworkConfig
TestsModule --> TestConfigLoad
TestNetworkConfig --> NetworkConfigStruct
TestConfigLoad --> ConfigStruct
TestConfigLoad --> NodeIdentifierClass

This flowchart illustrates the tests module containing two test functions. Each test function depends on configuration-related structs or classes for validation. The dependencies indicate how the tests interact with the configuration and node identity components of the system.