// A client SetFlag for a flag marked PlayerFlags.OnlySetByServer is ignored: the client's own flag component refuses // to set these (LWOFlagComponent::SendMessage), and none of the 205 live client SetFlag messages named one. #include "GameDependencies.h" #include "CDClientDatabase.h" #include "Character.h" #include "Entity.h" #include "MissionMessages.h" #include "User.h" #include #include namespace { constexpr int32_t SERVER_ONLY_FLAG = 1110; constexpr int32_t CLIENT_FLAG = 1111; constexpr int32_t UNLISTED_FLAG = 1112; } class SetFlagServerOnlyTests : public GameDependenciesTest { protected: void SetUp() override { SetUpDependencies(); CDClientDatabase::Connect(":memory:"); CDClientDatabase::ExecuteDML("CREATE TABLE PlayerFlags (id INTEGER, SessionOnly INTEGER, OnlySetByServer INTEGER, SessionZoneOnly INTEGER);"); CDClientDatabase::ExecuteDML("INSERT INTO PlayerFlags VALUES (1110, 0, 1, 0), (1111, 0, 0, 0);"); user = std::make_unique(UNASSIGNED_SYSTEM_ADDRESS, "tester", "key"); character = std::make_unique(1, user.get()); entity = std::make_unique(1, info); entity->SetCharacter(character.get()); character->SetEntity(entity.get()); } void TearDown() override { entity->SetCharacter(nullptr); entity.reset(); character.reset(); user.reset(); TearDownDependencies(); } void ClientSetsFlag(const int32_t flag, const bool value) { GameMessages::SetFlag setFlag; setFlag.iFlagID = flag; setFlag.bFlag = value; setFlag.Handle(*entity, UNASSIGNED_SYSTEM_ADDRESS); } std::unique_ptr user; std::unique_ptr character; std::unique_ptr entity; }; TEST_F(SetFlagServerOnlyTests, ClientCannotSetAServerOnlyFlag) { ClientSetsFlag(SERVER_ONLY_FLAG, true); EXPECT_FALSE(character->GetPlayerFlag(SERVER_ONLY_FLAG)); } // A flag the server set stays set: the client cannot clear it either, and nothing saved is dropped TEST_F(SetFlagServerOnlyTests, ClientCannotClearAServerOnlyFlag) { character->SetPlayerFlag(SERVER_ONLY_FLAG, true); ClientSetsFlag(SERVER_ONLY_FLAG, false); EXPECT_TRUE(character->GetPlayerFlag(SERVER_ONLY_FLAG)); } // Every other flag is set and cleared as before, with or without a PlayerFlags row TEST_F(SetFlagServerOnlyTests, OtherFlagsAreStillSetByTheClient) { for (const auto flag : { CLIENT_FLAG, UNLISTED_FLAG }) { ClientSetsFlag(flag, true); EXPECT_TRUE(character->GetPlayerFlag(flag)); ClientSetsFlag(flag, false); EXPECT_FALSE(character->GetPlayerFlag(flag)); } }