Buckets:
AnonymousSpottedToad/candidate_bucket / codebase /python /mattyopon_faultray /tests /test_traffic_extended.py
| """Extended tests for traffic pattern models — targeting uncovered lines.""" | |
| from faultray.simulator.traffic import ( | |
| TrafficPattern, | |
| TrafficPatternType, | |
| create_ddos_slowloris, | |
| create_diurnal, | |
| create_diurnal_weekly, | |
| create_growth_trend, | |
| create_viral_event, | |
| ) | |
| # --------------------------------------------------------------------------- | |
| # _ddos_slowloris — lines 214-217 (duration <= 0) | |
| # --------------------------------------------------------------------------- | |
| def test_ddos_slowloris_zero_duration(): | |
| """Slowloris with zero duration should return peak_multiplier.""" | |
| p = TrafficPattern( | |
| pattern_type=TrafficPatternType.DDoS_SLOWLORIS, | |
| peak_multiplier=5.0, | |
| duration_seconds=1, # Minimum; test via the raw method approach | |
| ) | |
| # At t=0 (within duration), should return linear ramp | |
| assert p.multiplier_at(0) == 1.0 # Start of ramp | |
| def test_ddos_slowloris_linear_ramp(): | |
| """Slowloris should linearly ramp from 1.0 to peak over duration.""" | |
| p = create_ddos_slowloris(peak=5.0, duration=300) | |
| start = p.multiplier_at(0) | |
| mid = p.multiplier_at(150) | |
| end = p.multiplier_at(299) | |
| assert abs(start - 1.0) < 0.01 | |
| assert mid > start | |
| assert end > mid | |
| # At t=299, should be close to 5.0 | |
| assert end > 4.0 | |
| # --------------------------------------------------------------------------- | |
| # _wave — lines 182-188 (wave_period_seconds <= 0) | |
| # --------------------------------------------------------------------------- | |
| def test_wave_zero_period(): | |
| """WAVE with zero period should return peak_multiplier.""" | |
| p = TrafficPattern( | |
| pattern_type=TrafficPatternType.WAVE, | |
| peak_multiplier=3.0, | |
| duration_seconds=300, | |
| wave_period_seconds=0, | |
| ) | |
| assert p.multiplier_at(50) == 3.0 | |
| def test_wave_oscillation(): | |
| """WAVE should oscillate between 1.0 and peak_multiplier.""" | |
| p = TrafficPattern( | |
| pattern_type=TrafficPatternType.WAVE, | |
| peak_multiplier=5.0, | |
| duration_seconds=300, | |
| wave_period_seconds=60, | |
| ) | |
| # Collect values over one period | |
| values = [p.multiplier_at(t) for t in range(0, 60)] | |
| min_val = min(values) | |
| max_val = max(values) | |
| # Should oscillate between 1.0 and 5.0 | |
| assert min_val >= 0.9 # approximately 1.0 | |
| assert max_val <= 5.1 # approximately 5.0 | |
| # --------------------------------------------------------------------------- | |
| # _diurnal — lines 250-257 (duration <= 0) | |
| # --------------------------------------------------------------------------- | |
| def test_diurnal_zero_duration(): | |
| """Diurnal with zero duration should return peak_multiplier.""" | |
| p = TrafficPattern( | |
| pattern_type=TrafficPatternType.DIURNAL, | |
| peak_multiplier=3.0, | |
| duration_seconds=1, | |
| ) | |
| # At t=0 (within duration), midpoint - amplitude*cos(0) = 1.0 | |
| val = p.multiplier_at(0) | |
| assert val >= 1.0 | |
| def test_diurnal_peak_at_midpoint(): | |
| """Diurnal should peak at the midpoint of the duration.""" | |
| p = create_diurnal(peak=3.0, duration=300) | |
| midpoint_val = p.multiplier_at(150) | |
| start_val = p.multiplier_at(0) | |
| end_val = p.multiplier_at(299) | |
| assert midpoint_val > start_val | |
| assert midpoint_val > end_val | |
| # Peak should be close to 3.0 | |
| assert abs(midpoint_val - 3.0) < 0.1 | |
| # --------------------------------------------------------------------------- | |
| # _growth_trend — line 340 | |
| # --------------------------------------------------------------------------- | |
| def test_growth_trend_exponential(): | |
| """GROWTH_TREND should show exponential increase.""" | |
| p = create_growth_trend(monthly_rate=0.1, duration=2592000) | |
| # At t=0 | |
| assert abs(p.multiplier_at(0) - 1.0) < 0.01 | |
| # At t=15 days (1296000s), should be ~ (1.1)^0.5 = ~1.0488 | |
| val_15d = p.multiplier_at(1296000) | |
| assert val_15d > 1.0 | |
| assert val_15d < 1.1 | |
| # At t=30 days, should be ~ 1.1 | |
| val_30d = p.multiplier_at(2592000 - 1) | |
| assert abs(val_30d - 1.1) < 0.02 | |
| # --------------------------------------------------------------------------- | |
| # _flash_crowd — line 238 (decay_duration <= 0) | |
| # --------------------------------------------------------------------------- | |
| def test_flash_crowd_no_decay(): | |
| """Flash crowd with ramp == duration should return peak after ramp.""" | |
| p = TrafficPattern( | |
| pattern_type=TrafficPatternType.FLASH_CROWD, | |
| peak_multiplier=8.0, | |
| duration_seconds=30, | |
| ramp_seconds=30, | |
| ) | |
| # At t=29 (end of ramp), should be near peak | |
| val = p.multiplier_at(29) | |
| assert val > 7.0 | |
| def test_flash_crowd_zero_ramp(): | |
| """Flash crowd with zero ramp should start at peak and decay.""" | |
| p = TrafficPattern( | |
| pattern_type=TrafficPatternType.FLASH_CROWD, | |
| peak_multiplier=8.0, | |
| duration_seconds=300, | |
| ramp_seconds=0, | |
| ) | |
| # At t=0, should be at peak (ramp=0, so goes straight to decay) | |
| val_start = p.multiplier_at(0) | |
| assert abs(val_start - 8.0) < 0.1 | |
| # At t=299, should be near 1.0 | |
| val_end = p.multiplier_at(299) | |
| assert val_end < 2.0 | |
| # --------------------------------------------------------------------------- | |
| # _ramp — line 160 (beyond cooldown returns 1.0) | |
| # --------------------------------------------------------------------------- | |
| def test_ramp_beyond_cooldown(): | |
| """RAMP beyond cooldown should return 1.0.""" | |
| p = TrafficPattern( | |
| pattern_type=TrafficPatternType.RAMP, | |
| peak_multiplier=5.0, | |
| duration_seconds=300, | |
| ramp_seconds=50, | |
| sustain_seconds=50, | |
| cooldown_seconds=50, | |
| ) | |
| # Beyond ramp+sustain+cooldown (150+) should return 1.0 | |
| assert abs(p.multiplier_at(200) - 1.0) < 0.01 | |
| def test_ramp_zero_ramp_seconds(): | |
| """RAMP with zero ramp_seconds should jump to peak immediately.""" | |
| p = TrafficPattern( | |
| pattern_type=TrafficPatternType.RAMP, | |
| peak_multiplier=5.0, | |
| duration_seconds=300, | |
| ramp_seconds=0, | |
| sustain_seconds=100, | |
| cooldown_seconds=100, | |
| ) | |
| # At t=0, ramp=0 -> t_after_ramp=0 < sustain=100 -> peak | |
| assert abs(p.multiplier_at(0) - 5.0) < 0.01 | |
| # --------------------------------------------------------------------------- | |
| # _spike — lines 108 (spike branch in multiplier_at) | |
| # --------------------------------------------------------------------------- | |
| def test_spike_before_ramp(): | |
| """SPIKE before ramp should return baseline.""" | |
| p = TrafficPattern( | |
| pattern_type=TrafficPatternType.SPIKE, | |
| peak_multiplier=10.0, | |
| duration_seconds=300, | |
| ramp_seconds=100, | |
| sustain_seconds=50, | |
| ) | |
| assert p.multiplier_at(0) == 1.0 | |
| assert p.multiplier_at(99) == 1.0 | |
| def test_spike_during(): | |
| """SPIKE during should return peak.""" | |
| p = TrafficPattern( | |
| pattern_type=TrafficPatternType.SPIKE, | |
| peak_multiplier=10.0, | |
| duration_seconds=300, | |
| ramp_seconds=100, | |
| sustain_seconds=50, | |
| ) | |
| assert p.multiplier_at(100) == 10.0 | |
| assert p.multiplier_at(149) == 10.0 | |
| def test_spike_after(): | |
| """SPIKE after should return baseline.""" | |
| p = TrafficPattern( | |
| pattern_type=TrafficPatternType.SPIKE, | |
| peak_multiplier=10.0, | |
| duration_seconds=300, | |
| ramp_seconds=100, | |
| sustain_seconds=50, | |
| ) | |
| assert p.multiplier_at(150) == 1.0 | |
| # --------------------------------------------------------------------------- | |
| # multiplier_at — lines 108, 112, 116, 122 (dispatch branches) | |
| # --------------------------------------------------------------------------- | |
| def test_multiplier_at_ddos_volumetric(): | |
| """DDoS_VOLUMETRIC dispatch should work.""" | |
| p = TrafficPattern( | |
| pattern_type=TrafficPatternType.DDoS_VOLUMETRIC, | |
| peak_multiplier=10.0, | |
| duration_seconds=300, | |
| ) | |
| val = p.multiplier_at(20) | |
| assert val >= 1.0 | |
| def test_multiplier_at_ddos_slowloris(): | |
| """DDoS_SLOWLORIS dispatch should work.""" | |
| p = TrafficPattern( | |
| pattern_type=TrafficPatternType.DDoS_SLOWLORIS, | |
| peak_multiplier=5.0, | |
| duration_seconds=300, | |
| ) | |
| val = p.multiplier_at(150) | |
| assert val > 1.0 | |
| def test_multiplier_at_flash_crowd(): | |
| """FLASH_CROWD dispatch should work.""" | |
| p = TrafficPattern( | |
| pattern_type=TrafficPatternType.FLASH_CROWD, | |
| peak_multiplier=8.0, | |
| duration_seconds=300, | |
| ramp_seconds=30, | |
| ) | |
| val = p.multiplier_at(15) | |
| assert val > 1.0 | |
| def test_multiplier_at_diurnal(): | |
| """DIURNAL dispatch should work.""" | |
| p = TrafficPattern( | |
| pattern_type=TrafficPatternType.DIURNAL, | |
| peak_multiplier=3.0, | |
| duration_seconds=300, | |
| ) | |
| val = p.multiplier_at(150) | |
| assert val > 1.0 | |
| def test_multiplier_at_diurnal_weekly(): | |
| """DIURNAL_WEEKLY dispatch should work.""" | |
| p = TrafficPattern( | |
| pattern_type=TrafficPatternType.DIURNAL_WEEKLY, | |
| peak_multiplier=3.0, | |
| duration_seconds=604800, | |
| ) | |
| val = p.multiplier_at(45000) # Monday midday | |
| assert val > 1.0 | |
| def test_multiplier_at_growth_trend(): | |
| """GROWTH_TREND dispatch should work.""" | |
| p = TrafficPattern( | |
| pattern_type=TrafficPatternType.GROWTH_TREND, | |
| peak_multiplier=0.1, | |
| duration_seconds=2592000, | |
| ) | |
| val = p.multiplier_at(86400) # 1 day | |
| assert val > 1.0 | |
| # --------------------------------------------------------------------------- | |
| # _diurnal_weekly — lines 396 (full week test) | |
| # --------------------------------------------------------------------------- | |
| def test_diurnal_weekly_full_week(): | |
| """DIURNAL_WEEKLY should cover a full week correctly.""" | |
| p = create_diurnal_weekly(peak=3.0, duration=604800, weekend_factor=0.6) | |
| # Monday peak (12:30) vs Monday trough (3:00) | |
| mon_peak = p.multiplier_at(45000) | |
| mon_trough = p.multiplier_at(10800) | |
| assert mon_peak > mon_trough | |
| # Wednesday peak | |
| wed_peak = p.multiplier_at(2 * 86400 + 45000) | |
| assert wed_peak > 1.0 | |
| # Saturday peak should be reduced | |
| sat_peak = p.multiplier_at(5 * 86400 + 45000) | |
| assert sat_peak < mon_peak # Weekend reduced | |
| # Sunday should also be reduced | |
| sun_peak = p.multiplier_at(6 * 86400 + 45000) | |
| assert sun_peak < mon_peak | |
| # --------------------------------------------------------------------------- | |
| # Factory functions — lines 340, 376, 396 | |
| # --------------------------------------------------------------------------- | |
| def test_create_ddos_slowloris_factory(): | |
| """create_ddos_slowloris should return proper pattern.""" | |
| p = create_ddos_slowloris(peak=5.0, duration=300) | |
| assert p.pattern_type == TrafficPatternType.DDoS_SLOWLORIS | |
| assert p.peak_multiplier == 5.0 | |
| def test_create_viral_event_factory(): | |
| """create_viral_event should return RAMP pattern.""" | |
| p = create_viral_event(peak=15.0, duration=300) | |
| assert p.pattern_type == TrafficPatternType.RAMP | |
| assert p.peak_multiplier == 15.0 | |
| assert p.ramp_seconds == 60 | |
| assert p.sustain_seconds == 120 | |
| assert p.cooldown_seconds == 120 | |
| def test_create_diurnal_factory(): | |
| """create_diurnal should return DIURNAL pattern.""" | |
| p = create_diurnal(peak=3.0, duration=300) | |
| assert p.pattern_type == TrafficPatternType.DIURNAL | |
| assert p.peak_multiplier == 3.0 | |
| # --------------------------------------------------------------------------- | |
| # Line 122 — unknown pattern type fallback | |
| # --------------------------------------------------------------------------- | |
| def test_unknown_pattern_type_fallback(): | |
| """An unrecognized pattern should fall through to return 1.0.""" | |
| # We cannot easily create an unknown enum value, but we can test | |
| # the existing patterns for proper dispatch coverage. | |
| # The line 122 (else: raw = 1.0) would only be hit with a new | |
| # pattern type not handled in the dispatch. We test all known types | |
| # are dispatched correctly instead. | |
| for pt in TrafficPatternType: | |
| p = TrafficPattern( | |
| pattern_type=pt, | |
| peak_multiplier=2.0, | |
| duration_seconds=300, | |
| ramp_seconds=10, | |
| sustain_seconds=10, | |
| cooldown_seconds=10, | |
| wave_period_seconds=30, | |
| ) | |
| val = p.multiplier_at(5) | |
| assert val >= 0.0, f"Pattern {pt} returned negative value" | |
| # --------------------------------------------------------------------------- | |
| # Line 216 — slowloris zero duration | |
| # --------------------------------------------------------------------------- | |
| def test_slowloris_very_short_duration(): | |
| """Slowloris with very short duration should still work.""" | |
| p = TrafficPattern( | |
| pattern_type=TrafficPatternType.DDoS_SLOWLORIS, | |
| peak_multiplier=5.0, | |
| duration_seconds=10, | |
| ) | |
| # At t=5 (midpoint), should be between 1.0 and 5.0 | |
| val = p.multiplier_at(5) | |
| assert 1.0 <= val <= 5.0 | |
| # --------------------------------------------------------------------------- | |
| # Line 238 — flash crowd decay_duration <= 0 | |
| # --------------------------------------------------------------------------- | |
| def test_flash_crowd_ramp_equals_duration(): | |
| """Flash crowd where ramp == duration means no decay space.""" | |
| p = TrafficPattern( | |
| pattern_type=TrafficPatternType.FLASH_CROWD, | |
| peak_multiplier=10.0, | |
| duration_seconds=60, | |
| ramp_seconds=60, | |
| ) | |
| # During ramp, should follow exponential | |
| val_early = p.multiplier_at(10) | |
| val_late = p.multiplier_at(50) | |
| assert val_late > val_early | |
| # --------------------------------------------------------------------------- | |
| # Line 252 — diurnal duration <= 0 returns peak | |
| # --------------------------------------------------------------------------- | |
| def test_diurnal_duration_one(): | |
| """Diurnal with duration=1 should still compute.""" | |
| p = TrafficPattern( | |
| pattern_type=TrafficPatternType.DIURNAL, | |
| peak_multiplier=5.0, | |
| duration_seconds=1, | |
| ) | |
| val = p.multiplier_at(0) | |
| assert val >= 1.0 | |
Xet Storage Details
- Size:
- 14 kB
- Xet hash:
- 70d18014a0cf920c8107f32ac3317e21db0a9457d2bb12a63f1965dc93f943a8
·
Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.