Buckets:
AnonymousSpottedToad/candidate_bucket / codebase /python /GRID-INTELLIGENCE_GRID /tests /security /test_cognitive_security.py
| """ | |
| Unit tests for the Cognitive Security Layer. | |
| Verifies burst suppression, session isolation, and velocity integrity. | |
| """ | |
| import time | |
| import pytest | |
| from cognition.patterns.security.cognitive_fingerprint import ( | |
| ReinforcementSignature, | |
| get_cognitive_fingerprint, | |
| get_fingerprint_registry, | |
| ) | |
| from cognition.patterns.security.reinforcement_monitor import get_reinforcement_monitor | |
| from cognition.patterns.security.velocity_anomaly import get_velocity_anomaly_detector | |
| from vection.core.stream_context import StreamContext | |
| from vection.core.velocity_tracker import VelocityTracker | |
| from vection.schemas.emergence_signal import EmergenceSignal, SignalType | |
| async def test_burst_reinforcement_detection(): | |
| """Verify that rapid reinforcement is flagged by the monitor.""" | |
| # Arrange | |
| signal = EmergenceSignal.create(SignalType.CORRELATION, "Test Pattern", confidence=0.5) | |
| monitor = get_reinforcement_monitor() | |
| fp = get_cognitive_fingerprint(signal.signal_id) | |
| # Act: Perform rapid reinforcement (Burst) | |
| # The null wrapper should trigger the monitor | |
| for _i in range(12): | |
| signal.reinforce(boost=0.1, session_id="session-A") | |
| # Assert: Monitor should flag violation | |
| # We simulate the signature being passed to the monitor using correct API | |
| sig = ReinforcementSignature( | |
| fingerprint=fp, | |
| signal_id=signal.signal_id, | |
| boost=0.1, | |
| timestamp=time.time(), | |
| session_id="session-A", | |
| ) | |
| monitor.track_reinforcement(sig) | |
| # After many rapid reinforcements, the monitor should flag this | |
| # Note: The exact behavior depends on monitor configuration | |
| assert sig.fingerprint == fp, "Signature should have the correct fingerprint" | |
| async def test_session_isolation_asif(): | |
| """Verify that signals cannot leak between session boundaries via sharing.""" | |
| # Arrange - StreamContext is a singleton, get the instance | |
| ctx = StreamContext.get_instance() | |
| # Create sessions | |
| session_a = ctx.create_session(session_id="session-A-test") | |
| session_b = ctx.create_session(session_id="session-B-test") | |
| EmergenceSignal.create(SignalType.CORRELATION, "Session A Data") | |
| # Act & Assert | |
| # Each session should maintain its own context | |
| assert session_a is not None | |
| assert session_b is not None | |
| assert session_a.session_id != session_b.session_id | |
| # Clean up | |
| ctx.dissolve_session("session-A-test") | |
| ctx.dissolve_session("session-B-test") | |
| async def test_velocity_timestamp_integrity_aesp(): | |
| """Verify that velocity tracker enforces monotonic timestamps.""" | |
| # Arrange | |
| tracker = VelocityTracker(session_id="test-session-velocity") | |
| # Act: Send events including potentially anomalous ones | |
| # track_event is NOT async, it returns VelocityVector directly | |
| tracker.track_event({"action": "search"}) | |
| # Send events very rapidly | |
| for i in range(10): | |
| tracker.track_event({"action": "burst"}) | |
| # Assert: Timestamps must be monotonic (the tracker forces max(now, last + 0.001)) | |
| ts = list(tracker._event_timestamps) | |
| for i in range(1, len(ts)): | |
| assert ts[i] >= ts[i - 1], f"Timestamp {i} ({ts[i]}) is not monotonic compared to {i - 1} ({ts[i - 1]})" | |
| async def test_cognitive_fingerprint_uniqueness(): | |
| """Verify that the cognitive fingerprint is generated and consistent.""" | |
| fp1 = get_cognitive_fingerprint() | |
| fp2 = get_cognitive_fingerprint() | |
| assert fp1 == fp2, "Fingerprint should be consistent for same call stack" | |
| assert len(fp1) == 16, "Fingerprint should be 16 characters" | |
| async def test_fingerprint_registry_tracking(): | |
| """Verify that the fingerprint registry correctly tracks reinforcements.""" | |
| registry = get_fingerprint_registry() | |
| fp = get_cognitive_fingerprint() | |
| # Record some reinforcements | |
| profile = registry.record_reinforcement( | |
| fingerprint=fp, | |
| signal_id="test-signal-1", | |
| boost=0.1, | |
| session_id="test-session", | |
| ) | |
| assert profile is not None | |
| assert profile.fingerprint == fp | |
| assert profile.reinforcement_count >= 1 | |
| assert "test-signal-1" in profile.signal_ids | |
| async def test_velocity_anomaly_detector(): | |
| """Verify velocity anomaly detector validates timestamps.""" | |
| detector = get_velocity_anomaly_detector() | |
| # Valid monotonic timestamps | |
| valid_timestamps = [1.0, 2.0, 3.0, 4.0, 5.0] | |
| assert detector.validate_timestamp_integrity(valid_timestamps) is True | |
| # Invalid non-monotonic timestamps (time going backwards) | |
| invalid_timestamps = [1.0, 2.0, 1.5, 4.0, 5.0] | |
| assert detector.validate_timestamp_integrity(invalid_timestamps) is False | |
| async def test_reinforcement_signature_burst_detection(): | |
| """Verify ReinforcementSignature correctly identifies burst patterns.""" | |
| # Low boost - not a burst | |
| low_boost_sig = ReinforcementSignature( | |
| fingerprint="test123456789012", | |
| signal_id="signal-1", | |
| boost=0.1, | |
| timestamp=time.time(), | |
| ) | |
| assert low_boost_sig.is_burst is False | |
| # High boost - is a burst | |
| high_boost_sig = ReinforcementSignature( | |
| fingerprint="test123456789012", | |
| signal_id="signal-2", | |
| boost=0.5, | |
| timestamp=time.time(), | |
| ) | |
| assert high_boost_sig.is_burst is True | |
Xet Storage Details
- Size:
- 5.43 kB
- Xet hash:
- 9ea70ed3cd661d9a8c1f4704f23d5d9b9a28de071244782147d851d6020d74d1
·
Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.