1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466
|
"""
Test the Settings object.
"""
from __future__ import annotations
import pytest
from hypothesis import assume, given
from hypothesis.strategies import booleans, builds, fixed_dictionaries, integers
import h2.errors
import h2.exceptions
import h2.settings
class TestSettings:
"""
Test the Settings object behaves as expected.
"""
def test_settings_defaults_client(self) -> None:
"""
The Settings object begins with the appropriate defaults for clients.
"""
s = h2.settings.Settings(client=True)
assert s[h2.settings.SettingCodes.HEADER_TABLE_SIZE] == 4096
assert s[h2.settings.SettingCodes.ENABLE_PUSH] == 1
assert s[h2.settings.SettingCodes.INITIAL_WINDOW_SIZE] == 65535
assert s[h2.settings.SettingCodes.MAX_FRAME_SIZE] == 16384
assert s[h2.settings.SettingCodes.ENABLE_CONNECT_PROTOCOL] == 0
def test_settings_defaults_server(self) -> None:
"""
The Settings object begins with the appropriate defaults for servers.
"""
s = h2.settings.Settings(client=False)
assert s[h2.settings.SettingCodes.HEADER_TABLE_SIZE] == 4096
assert s[h2.settings.SettingCodes.ENABLE_PUSH] == 0
assert s[h2.settings.SettingCodes.INITIAL_WINDOW_SIZE] == 65535
assert s[h2.settings.SettingCodes.MAX_FRAME_SIZE] == 16384
assert s[h2.settings.SettingCodes.ENABLE_CONNECT_PROTOCOL] == 0
@pytest.mark.parametrize("client", [True, False])
def test_can_set_initial_values(self, client) -> None:
"""
The Settings object can be provided initial values that override the
defaults.
"""
overrides = {
h2.settings.SettingCodes.HEADER_TABLE_SIZE: 8080,
h2.settings.SettingCodes.MAX_FRAME_SIZE: 16388,
h2.settings.SettingCodes.MAX_CONCURRENT_STREAMS: 100,
h2.settings.SettingCodes.MAX_HEADER_LIST_SIZE: 2**16,
h2.settings.SettingCodes.ENABLE_CONNECT_PROTOCOL: 1,
}
s = h2.settings.Settings(client=client, initial_values=overrides)
assert s[h2.settings.SettingCodes.HEADER_TABLE_SIZE] == 8080
assert s[h2.settings.SettingCodes.ENABLE_PUSH] == bool(client)
assert s[h2.settings.SettingCodes.INITIAL_WINDOW_SIZE] == 65535
assert s[h2.settings.SettingCodes.MAX_FRAME_SIZE] == 16388
assert s[h2.settings.SettingCodes.MAX_CONCURRENT_STREAMS] == 100
assert s[h2.settings.SettingCodes.MAX_HEADER_LIST_SIZE] == 2**16
assert s[h2.settings.SettingCodes.ENABLE_CONNECT_PROTOCOL] == 1
@pytest.mark.parametrize(
("setting", "value"),
[
(h2.settings.SettingCodes.ENABLE_PUSH, 2),
(h2.settings.SettingCodes.ENABLE_PUSH, -1),
(h2.settings.SettingCodes.INITIAL_WINDOW_SIZE, -1),
(h2.settings.SettingCodes.INITIAL_WINDOW_SIZE, 2**34),
(h2.settings.SettingCodes.MAX_FRAME_SIZE, 1),
(h2.settings.SettingCodes.MAX_FRAME_SIZE, 2**30),
(h2.settings.SettingCodes.MAX_HEADER_LIST_SIZE, -1),
(h2.settings.SettingCodes.ENABLE_CONNECT_PROTOCOL, -1),
],
)
def test_cannot_set_invalid_initial_values(self, setting, value) -> None:
"""
The Settings object can be provided initial values that override the
defaults.
"""
overrides = {setting: value}
with pytest.raises(h2.exceptions.InvalidSettingsValueError):
h2.settings.Settings(initial_values=overrides)
def test_applying_value_doesnt_take_effect_immediately(self) -> None:
"""
When a value is applied to the settings object, it doesn't immediately
take effect.
"""
s = h2.settings.Settings(client=True)
s[h2.settings.SettingCodes.HEADER_TABLE_SIZE] == 8000
assert s[h2.settings.SettingCodes.HEADER_TABLE_SIZE] == 4096
def test_acknowledging_values(self) -> None:
"""
When we acknowledge settings, the values change.
"""
s = h2.settings.Settings(client=True)
old_settings = dict(s)
new_settings = {
h2.settings.SettingCodes.HEADER_TABLE_SIZE: 4000,
h2.settings.SettingCodes.ENABLE_PUSH: 0,
h2.settings.SettingCodes.INITIAL_WINDOW_SIZE: 60,
h2.settings.SettingCodes.MAX_FRAME_SIZE: 16385,
h2.settings.SettingCodes.ENABLE_CONNECT_PROTOCOL: 1,
}
s.update(new_settings)
assert dict(s) == old_settings
s.acknowledge()
assert dict(s) == new_settings
def test_acknowledging_returns_the_changed_settings(self) -> None:
"""
Acknowledging settings returns the changes.
"""
s = h2.settings.Settings(client=True)
s[h2.settings.SettingCodes.HEADER_TABLE_SIZE] = 8000
s[h2.settings.SettingCodes.ENABLE_PUSH] = 0
changes = s.acknowledge()
assert len(changes) == 2
table_size_change = (
changes[h2.settings.SettingCodes.HEADER_TABLE_SIZE]
)
push_change = changes[h2.settings.SettingCodes.ENABLE_PUSH]
assert table_size_change.setting == (
h2.settings.SettingCodes.HEADER_TABLE_SIZE
)
assert table_size_change.original_value == 4096
assert table_size_change.new_value == 8000
assert push_change.setting == h2.settings.SettingCodes.ENABLE_PUSH
assert push_change.original_value == 1
assert push_change.new_value == 0
def test_acknowledging_only_returns_changed_settings(self) -> None:
"""
Acknowledging settings does not return unchanged settings.
"""
s = h2.settings.Settings(client=True)
s[h2.settings.SettingCodes.INITIAL_WINDOW_SIZE] = 70
changes = s.acknowledge()
assert len(changes) == 1
assert list(changes.keys()) == [
h2.settings.SettingCodes.INITIAL_WINDOW_SIZE,
]
def test_deleting_values_deletes_all_of_them(self) -> None:
"""
When we delete a key we lose all state about it.
"""
s = h2.settings.Settings(client=True)
s[h2.settings.SettingCodes.HEADER_TABLE_SIZE] == 8000
del s[h2.settings.SettingCodes.HEADER_TABLE_SIZE]
with pytest.raises(KeyError):
s[h2.settings.SettingCodes.HEADER_TABLE_SIZE]
def test_length_correctly_reported(self) -> None:
"""
Length is related only to the number of keys.
"""
s = h2.settings.Settings(client=True)
assert len(s) == 5
s[h2.settings.SettingCodes.HEADER_TABLE_SIZE] == 8000
assert len(s) == 5
s.acknowledge()
assert len(s) == 5
del s[h2.settings.SettingCodes.HEADER_TABLE_SIZE]
assert len(s) == 4
def test_new_values_work(self) -> None:
"""
New values initially don't appear
"""
s = h2.settings.Settings(client=True)
s[80] = 81
with pytest.raises(KeyError):
s[80]
def test_new_values_follow_basic_acknowledgement_rules(self) -> None:
"""
A new value properly appears when acknowledged.
"""
s = h2.settings.Settings(client=True)
s[80] = 81
changed_settings = s.acknowledge()
assert s[80] == 81
assert len(changed_settings) == 1
changed = changed_settings[80]
assert changed.setting == 80
assert changed.original_value is None
assert changed.new_value == 81
def test_single_values_arent_affected_by_acknowledgement(self) -> None:
"""
When acknowledged, unchanged settings remain unchanged.
"""
s = h2.settings.Settings(client=True)
assert s[h2.settings.SettingCodes.HEADER_TABLE_SIZE] == 4096
s.acknowledge()
assert s[h2.settings.SettingCodes.HEADER_TABLE_SIZE] == 4096
def test_settings_getters(self) -> None:
"""
Getters exist for well-known settings.
"""
s = h2.settings.Settings(client=True)
assert s.header_table_size == (
s[h2.settings.SettingCodes.HEADER_TABLE_SIZE]
)
assert s.enable_push == s[h2.settings.SettingCodes.ENABLE_PUSH]
assert s.initial_window_size == (
s[h2.settings.SettingCodes.INITIAL_WINDOW_SIZE]
)
assert s.max_frame_size == s[h2.settings.SettingCodes.MAX_FRAME_SIZE]
assert s.max_concurrent_streams == 2**32 + 1 # A sensible default.
assert s.max_header_list_size is None
assert s.enable_connect_protocol == s[
h2.settings.SettingCodes.ENABLE_CONNECT_PROTOCOL
]
def test_settings_setters(self) -> None:
"""
Setters exist for well-known settings.
"""
s = h2.settings.Settings(client=True)
s.header_table_size = 0
s.enable_push = 1
s.initial_window_size = 2
s.max_frame_size = 16385
s.max_concurrent_streams = 4
s.max_header_list_size = 2**16
s.enable_connect_protocol = 1
s.acknowledge()
assert s[h2.settings.SettingCodes.HEADER_TABLE_SIZE] == 0
assert s[h2.settings.SettingCodes.ENABLE_PUSH] == 1
assert s[h2.settings.SettingCodes.INITIAL_WINDOW_SIZE] == 2
assert s[h2.settings.SettingCodes.MAX_FRAME_SIZE] == 16385
assert s[h2.settings.SettingCodes.MAX_CONCURRENT_STREAMS] == 4
assert s[h2.settings.SettingCodes.MAX_HEADER_LIST_SIZE] == 2**16
assert s[h2.settings.SettingCodes.ENABLE_CONNECT_PROTOCOL] == 1
@given(integers())
def test_cannot_set_invalid_values_for_enable_push(self, val) -> None:
"""
SETTINGS_ENABLE_PUSH only allows two values: 0, 1.
"""
assume(val not in (0, 1))
s = h2.settings.Settings()
with pytest.raises(h2.exceptions.InvalidSettingsValueError) as e:
s.enable_push = val
s.acknowledge()
assert e.value.error_code == h2.errors.ErrorCodes.PROTOCOL_ERROR
assert s.enable_push == 1
with pytest.raises(h2.exceptions.InvalidSettingsValueError) as e:
s[h2.settings.SettingCodes.ENABLE_PUSH] = val
s.acknowledge()
assert e.value.error_code == h2.errors.ErrorCodes.PROTOCOL_ERROR
assert s[h2.settings.SettingCodes.ENABLE_PUSH] == 1
@given(integers())
def test_cannot_set_invalid_vals_for_initial_window_size(self, val) -> None:
"""
SETTINGS_INITIAL_WINDOW_SIZE only allows values between 0 and 2**32 - 1
inclusive.
"""
s = h2.settings.Settings()
if 0 <= val <= 2**31 - 1:
s.initial_window_size = val
s.acknowledge()
assert s.initial_window_size == val
else:
with pytest.raises(h2.exceptions.InvalidSettingsValueError) as e:
s.initial_window_size = val
s.acknowledge()
assert (
e.value.error_code == h2.errors.ErrorCodes.FLOW_CONTROL_ERROR
)
assert s.initial_window_size == 65535
with pytest.raises(h2.exceptions.InvalidSettingsValueError) as e:
s[h2.settings.SettingCodes.INITIAL_WINDOW_SIZE] = val
s.acknowledge()
assert (
e.value.error_code == h2.errors.ErrorCodes.FLOW_CONTROL_ERROR
)
assert s[h2.settings.SettingCodes.INITIAL_WINDOW_SIZE] == 65535
@given(integers())
def test_cannot_set_invalid_values_for_max_frame_size(self, val) -> None:
"""
SETTINGS_MAX_FRAME_SIZE only allows values between 2**14 and 2**24 - 1.
"""
s = h2.settings.Settings()
if 2**14 <= val <= 2**24 - 1:
s.max_frame_size = val
s.acknowledge()
assert s.max_frame_size == val
else:
with pytest.raises(h2.exceptions.InvalidSettingsValueError) as e:
s.max_frame_size = val
s.acknowledge()
assert e.value.error_code == h2.errors.ErrorCodes.PROTOCOL_ERROR
assert s.max_frame_size == 16384
with pytest.raises(h2.exceptions.InvalidSettingsValueError) as e:
s[h2.settings.SettingCodes.MAX_FRAME_SIZE] = val
s.acknowledge()
assert e.value.error_code == h2.errors.ErrorCodes.PROTOCOL_ERROR
assert s[h2.settings.SettingCodes.MAX_FRAME_SIZE] == 16384
@given(integers())
def test_cannot_set_invalid_values_for_max_header_list_size(self, val) -> None:
"""
SETTINGS_MAX_HEADER_LIST_SIZE only allows non-negative values.
"""
s = h2.settings.Settings()
if val >= 0:
s.max_header_list_size = val
s.acknowledge()
assert s.max_header_list_size == val
else:
with pytest.raises(h2.exceptions.InvalidSettingsValueError) as e:
s.max_header_list_size = val
s.acknowledge()
assert e.value.error_code == h2.errors.ErrorCodes.PROTOCOL_ERROR
assert s.max_header_list_size is None
with pytest.raises(h2.exceptions.InvalidSettingsValueError) as e:
s[h2.settings.SettingCodes.MAX_HEADER_LIST_SIZE] = val
s.acknowledge()
assert e.value.error_code == h2.errors.ErrorCodes.PROTOCOL_ERROR
with pytest.raises(KeyError):
s[h2.settings.SettingCodes.MAX_HEADER_LIST_SIZE]
@given(integers())
def test_cannot_set_invalid_values_for_enable_connect_protocol(self, val) -> None:
"""
SETTINGS_ENABLE_CONNECT_PROTOCOL only allows two values: 0, 1.
"""
assume(val not in (0, 1))
s = h2.settings.Settings()
with pytest.raises(h2.exceptions.InvalidSettingsValueError) as e:
s.enable_connect_protocol = val
s.acknowledge()
assert e.value.error_code == h2.errors.ErrorCodes.PROTOCOL_ERROR
assert s.enable_connect_protocol == 0
with pytest.raises(h2.exceptions.InvalidSettingsValueError) as e:
s[h2.settings.SettingCodes.ENABLE_CONNECT_PROTOCOL] = val
s.acknowledge()
assert e.value.error_code == h2.errors.ErrorCodes.PROTOCOL_ERROR
assert s[h2.settings.SettingCodes.ENABLE_CONNECT_PROTOCOL] == 0
class TestSettingsEquality:
"""
A class defining tests for the standard implementation of == and != .
"""
SettingsStrategy = builds(
h2.settings.Settings,
client=booleans(),
initial_values=fixed_dictionaries({
h2.settings.SettingCodes.HEADER_TABLE_SIZE:
integers(0, 2**32 - 1),
h2.settings.SettingCodes.ENABLE_PUSH: integers(0, 1),
h2.settings.SettingCodes.INITIAL_WINDOW_SIZE:
integers(0, 2**31 - 1),
h2.settings.SettingCodes.MAX_FRAME_SIZE:
integers(2**14, 2**24 - 1),
h2.settings.SettingCodes.MAX_CONCURRENT_STREAMS:
integers(0, 2**32 - 1),
h2.settings.SettingCodes.MAX_HEADER_LIST_SIZE:
integers(0, 2**32 - 1),
}),
)
@given(settings=SettingsStrategy)
def test_equality_reflexive(self, settings) -> None:
"""
An object compares equal to itself using the == operator and the !=
operator.
"""
assert (settings == settings)
assert settings == settings
@given(settings=SettingsStrategy, o_settings=SettingsStrategy)
def test_equality_multiple(self, settings, o_settings) -> None:
"""
Two objects compare themselves using the == operator and the !=
operator.
"""
if settings == o_settings:
assert settings == o_settings
assert settings == o_settings
else:
assert settings != o_settings
assert settings != o_settings
@given(settings=SettingsStrategy)
def test_another_type_equality(self, settings) -> None:
"""
The object does not compare equal to an object of an unrelated type
(which does not implement the comparison) using the == operator.
"""
obj = object()
assert (settings != obj)
assert settings != obj
@given(settings=SettingsStrategy)
def test_delegated_eq(self, settings) -> None:
"""
The result of comparison is delegated to the right-hand operand if
it is of an unrelated type.
"""
class Delegate:
def __eq__(self, other):
return [self]
def __ne__(self, other):
return [self]
delg = Delegate()
assert (settings == delg) == [delg]
assert (settings != delg) == [delg]
|