|
9 | 9 |
|
10 | 10 | --*/
|
11 | 11 |
|
| 12 | +#define QUIC_UNIT_TESTS |
| 13 | + |
12 | 14 | #include "main.h"
|
13 | 15 | #ifdef QUIC_CLOG
|
14 | 16 | #include "SettingsTest.cpp.clog.h"
|
@@ -335,6 +337,247 @@ TEST(SettingsTest, StreamRecvWindowDefaultGetsOverridenByIndividualLimits)
|
335 | 337 | #define SETTINGS_SIZE_THRU_FIELD(SettingsType, Field) \
|
336 | 338 | (FIELD_OFFSET(SettingsType, Field) + sizeof(((SettingsType*)0)->Field))
|
337 | 339 |
|
| 340 | +TEST(SettingsTest, QuicSettingsSetDefault_DoesNotOverwriteSetFields) |
| 341 | +{ |
| 342 | + QUIC_SETTINGS_INTERNAL Settings; |
| 343 | + CxPlatZeroMemory(&Settings, sizeof(Settings)); |
| 344 | + |
| 345 | + // Set a few fields and mark them as set |
| 346 | + Settings.IsSet.SendBufferingEnabled = 1; |
| 347 | + Settings.SendBufferingEnabled = 0; |
| 348 | + Settings.IsSet.PacingEnabled = 1; |
| 349 | + Settings.PacingEnabled = 0; |
| 350 | + QuicSettingsSetDefault(&Settings); |
| 351 | + |
| 352 | + // These should not be overwritten |
| 353 | + ASSERT_EQ(Settings.SendBufferingEnabled, 0); |
| 354 | + ASSERT_EQ(Settings.PacingEnabled, 0); |
| 355 | + |
| 356 | + // But an unset field should be set to default |
| 357 | + ASSERT_EQ(Settings.MigrationEnabled, QUIC_DEFAULT_MIGRATION_ENABLED); |
| 358 | +} |
| 359 | + |
| 360 | +class QuicStorageSettingScopeGuard { |
| 361 | +public: |
| 362 | + static |
| 363 | + QuicStorageSettingScopeGuard Create( |
| 364 | + _In_opt_ const char* StorageName = nullptr) |
| 365 | + { |
| 366 | + return QuicStorageSettingScopeGuard(StorageName); |
| 367 | + } |
| 368 | + |
| 369 | + QuicStorageSettingScopeGuard(const QuicStorageSettingScopeGuard&) = delete; |
| 370 | + QuicStorageSettingScopeGuard& operator=(const QuicStorageSettingScopeGuard&) = delete; |
| 371 | + |
| 372 | + QuicStorageSettingScopeGuard( |
| 373 | + _In_ QuicStorageSettingScopeGuard&& Other) noexcept : m_Storage(Other.m_Storage) |
| 374 | + { |
| 375 | + Other.m_Storage = nullptr; |
| 376 | + } |
| 377 | + |
| 378 | + QuicStorageSettingScopeGuard& operator=( |
| 379 | + _In_ QuicStorageSettingScopeGuard&& Other) |
| 380 | + { |
| 381 | + if (this == &Other) |
| 382 | + { |
| 383 | + return Other; |
| 384 | + } |
| 385 | + ClearAndClose(m_Storage); |
| 386 | + m_Storage = Other.m_Storage; |
| 387 | + Other.m_Storage = nullptr; |
| 388 | + return *this; |
| 389 | + } |
| 390 | + |
| 391 | + ~QuicStorageSettingScopeGuard() |
| 392 | + { |
| 393 | + ClearAndClose(m_Storage); |
| 394 | + } |
| 395 | + |
| 396 | + operator CXPLAT_STORAGE*() const { |
| 397 | + return m_Storage; |
| 398 | + } |
| 399 | + |
| 400 | +private: |
| 401 | + QuicStorageSettingScopeGuard( |
| 402 | + _In_opt_ const char* StorageName) |
| 403 | + { |
| 404 | + EXPECT_EQ( |
| 405 | + QUIC_STATUS_SUCCESS, |
| 406 | + CxPlatStorageOpen( |
| 407 | + StorageName, |
| 408 | + nullptr, |
| 409 | + nullptr, |
| 410 | + CXPLAT_STORAGE_OPEN_FLAG_DELETE | CXPLAT_STORAGE_OPEN_FLAG_WRITE | CXPLAT_STORAGE_OPEN_FLAG_CREATE, |
| 411 | + &m_Storage)); |
| 412 | + EXPECT_NE(m_Storage, nullptr); |
| 413 | + } |
| 414 | + |
| 415 | + void ClearAndClose( |
| 416 | + _In_opt_ CXPLAT_STORAGE* Storage) |
| 417 | + { |
| 418 | + if (Storage != nullptr) { |
| 419 | + EXPECT_EQ(QUIC_STATUS_SUCCESS, CxPlatStorageClear(Storage)); |
| 420 | + CxPlatStorageClose(Storage); |
| 421 | + } |
| 422 | + } |
| 423 | + |
| 424 | + CXPLAT_STORAGE* m_Storage = nullptr; |
| 425 | +}; |
| 426 | + |
| 427 | +// --- Test: QuicSettingsLoad sets fields from storage --- |
| 428 | +TEST(SettingsTest, QuicSettingsLoad_SetsFieldsFromStorage) |
| 429 | +{ |
| 430 | + CXPLAT_STORAGE* TestStorage = NULL; |
| 431 | + |
| 432 | + QUIC_STATUS Status = |
| 433 | + CxPlatStorageOpen( |
| 434 | + "Apps\\MsQuicUnitTestStorage", |
| 435 | + nullptr, |
| 436 | + nullptr, |
| 437 | + CXPLAT_STORAGE_OPEN_FLAG_CREATE, |
| 438 | + &TestStorage); |
| 439 | + |
| 440 | + if (Status == QUIC_STATUS_NOT_SUPPORTED) { |
| 441 | + GTEST_SKIP() << "Skipping test because storage is not available. Status:" << Status; |
| 442 | + } |
| 443 | + CxPlatStorageClose(TestStorage); |
| 444 | + |
| 445 | + ASSERT_EQ(Status, QUIC_STATUS_SUCCESS); |
| 446 | + |
| 447 | + QuicStorageSettingScopeGuard StorageGuard = |
| 448 | + QuicStorageSettingScopeGuard::Create("Apps\\MsQuicUnitTestStorage"); |
| 449 | + |
| 450 | + uint32_t Value = 0; |
| 451 | + ASSERT_EQ( |
| 452 | + QUIC_STATUS_SUCCESS, |
| 453 | + CxPlatStorageWriteValue( |
| 454 | + StorageGuard, |
| 455 | + QUIC_SETTING_SEND_BUFFERING_DEFAULT, |
| 456 | + CXPLAT_STORAGE_TYPE_UINT32, |
| 457 | + sizeof(Value), |
| 458 | + (uint8_t*)&Value)); |
| 459 | + |
| 460 | + ASSERT_EQ( |
| 461 | + QUIC_STATUS_SUCCESS, |
| 462 | + CxPlatStorageWriteValue( |
| 463 | + StorageGuard, |
| 464 | + QUIC_SETTING_SEND_PACING_DEFAULT, |
| 465 | + CXPLAT_STORAGE_TYPE_UINT32, |
| 466 | + sizeof(Value), |
| 467 | + (uint8_t*)&Value)); |
| 468 | + |
| 469 | + ASSERT_EQ( |
| 470 | + QUIC_STATUS_SUCCESS, |
| 471 | + CxPlatStorageWriteValue( |
| 472 | + StorageGuard, |
| 473 | + QUIC_SETTING_MIGRATION_ENABLED, |
| 474 | + CXPLAT_STORAGE_TYPE_UINT32, |
| 475 | + sizeof(Value), |
| 476 | + (uint8_t*)&Value)); |
| 477 | + |
| 478 | + Value = 7; |
| 479 | + ASSERT_EQ( |
| 480 | + QUIC_STATUS_SUCCESS, |
| 481 | + CxPlatStorageWriteValue( |
| 482 | + StorageGuard, |
| 483 | + QUIC_SETTING_MAX_OPERATIONS_PER_DRAIN, |
| 484 | + CXPLAT_STORAGE_TYPE_UINT32, |
| 485 | + sizeof(Value), |
| 486 | + (uint8_t*)&Value)); |
| 487 | + |
| 488 | + QUIC_SETTINGS_INTERNAL Settings; |
| 489 | + CxPlatZeroMemory(&Settings, sizeof(Settings)); |
| 490 | + QuicSettingsLoad(&Settings, StorageGuard); |
| 491 | + |
| 492 | + // Check that the values were loaded |
| 493 | + ASSERT_EQ(Settings.SendBufferingEnabled, 0u); |
| 494 | + ASSERT_EQ(Settings.PacingEnabled, 0u); |
| 495 | + ASSERT_EQ(Settings.MigrationEnabled, 0u); |
| 496 | + ASSERT_EQ(Settings.MaxOperationsPerDrain, 7u); |
| 497 | + |
| 498 | + QuicSettingsDumpNew(&Settings); |
| 499 | +} |
| 500 | + |
| 501 | +// --- Test: QuicSettingsLoad does not overwrite set fields --- |
| 502 | +TEST(SettingsTest, QuicSettingsLoad_DoesNotOverwriteSetFields) |
| 503 | +{ |
| 504 | + CXPLAT_STORAGE* TestStorage = NULL; |
| 505 | + |
| 506 | + QUIC_STATUS Status = |
| 507 | + CxPlatStorageOpen( |
| 508 | + "Apps\\MsQuicUnitTestStorage", |
| 509 | + nullptr, |
| 510 | + nullptr, |
| 511 | + CXPLAT_STORAGE_OPEN_FLAG_CREATE, |
| 512 | + &TestStorage); |
| 513 | + |
| 514 | + if (Status == QUIC_STATUS_NOT_SUPPORTED) { |
| 515 | + GTEST_SKIP() << "Skipping test because storage is not available. Status:" << Status; |
| 516 | + } |
| 517 | + CxPlatStorageClose(TestStorage); |
| 518 | + |
| 519 | + ASSERT_EQ(Status, QUIC_STATUS_SUCCESS); |
| 520 | + |
| 521 | + QuicStorageSettingScopeGuard StorageGuard = |
| 522 | + QuicStorageSettingScopeGuard::Create("Apps\\MsQuicUnitTestStorage"); |
| 523 | + |
| 524 | + uint32_t Value = 0; |
| 525 | + ASSERT_EQ( |
| 526 | + QUIC_STATUS_SUCCESS, |
| 527 | + CxPlatStorageWriteValue( |
| 528 | + StorageGuard, |
| 529 | + QUIC_SETTING_SEND_BUFFERING_DEFAULT, |
| 530 | + CXPLAT_STORAGE_TYPE_UINT32, |
| 531 | + sizeof(Value), |
| 532 | + (uint8_t*)&Value)); |
| 533 | + |
| 534 | + QUIC_SETTINGS_INTERNAL Settings; |
| 535 | + CxPlatZeroMemory(&Settings, sizeof(Settings)); |
| 536 | + |
| 537 | + // Mark SendBufferingEnabled as set |
| 538 | + Settings.IsSet.SendBufferingEnabled = 1; |
| 539 | + Settings.SendBufferingEnabled = 1; |
| 540 | + |
| 541 | + QuicSettingsLoad(&Settings, StorageGuard); |
| 542 | + |
| 543 | + // Should not be overwritten |
| 544 | + ASSERT_EQ(Settings.SendBufferingEnabled, 1u); |
| 545 | +} |
| 546 | + |
| 547 | +// --- Test: QuicSettingsLoad uses default if storage missing --- |
| 548 | +TEST(SettingsTest, QuicSettingsLoad_UsesDefaultIfStorageMissing) |
| 549 | +{ |
| 550 | + CXPLAT_STORAGE* TestStorage = NULL; |
| 551 | + |
| 552 | + QUIC_STATUS Status = |
| 553 | + CxPlatStorageOpen( |
| 554 | + "Apps\\MsQuicUnitTestStorage", |
| 555 | + nullptr, |
| 556 | + nullptr, |
| 557 | + CXPLAT_STORAGE_OPEN_FLAG_CREATE, |
| 558 | + &TestStorage); |
| 559 | + |
| 560 | + if (Status == QUIC_STATUS_NOT_SUPPORTED) { |
| 561 | + GTEST_SKIP() << "Skipping test because storage is not available. Status:" << Status; |
| 562 | + } |
| 563 | + CxPlatStorageClose(TestStorage); |
| 564 | + |
| 565 | + ASSERT_EQ(Status, QUIC_STATUS_SUCCESS); |
| 566 | + |
| 567 | + QuicStorageSettingScopeGuard StorageGuard = |
| 568 | + QuicStorageSettingScopeGuard::Create("Apps\\MsQuicUnitTestStorage"); |
| 569 | + |
| 570 | + QUIC_SETTINGS_INTERNAL Settings; |
| 571 | + CxPlatZeroMemory(&Settings, sizeof(Settings)); |
| 572 | + QuicSettingsLoad(&Settings, StorageGuard); |
| 573 | + |
| 574 | + // Should use default |
| 575 | + ASSERT_EQ(Settings.SendBufferingEnabled, QUIC_DEFAULT_SEND_BUFFERING_ENABLE); |
| 576 | + ASSERT_EQ(Settings.PacingEnabled, QUIC_DEFAULT_SEND_PACING); |
| 577 | + ASSERT_EQ(Settings.MigrationEnabled, QUIC_DEFAULT_MIGRATION_ENABLED); |
| 578 | + |
| 579 | +} |
| 580 | + |
338 | 581 | TEST(SettingsTest, SettingsSizesGet)
|
339 | 582 | {
|
340 | 583 | uint8_t Buffer[sizeof(QUIC_SETTINGS) * 2];
|
|
0 commit comments