JD2022-TU1/main/extern/UbiServices/custom/testsUnit/testSessionInfo.cpp

234 lines
9.7 KiB
C++

////////////////////////////////////////////////////////////////////////////////
// (c) Copyright, Ubisoft Entertainment Inc. || Created by Technology Group //
////////////////////////////////////////////////////////////////////////////////
#include "tests/config.h"
#include "tests/fixture/authenticationFixture.h"
#include "tests/fixture/memoryCheckFixture.h"
#include <ubiservices/core/configs/platformName.h>
#include <ubiservices/core/JSON/jsonWriter.h>
#include <ubiservices/core/helpers/helpers.h>
#include <ubiservices/facadeInternal.h>
#include <ubiservices/services/authentication/jobs/jobGetSessionInfo.h>
#include <ubiservices/services/authentication/sessionInfo.h>
#include "ubiservices/services/authentication/sessionInfoPrivate.h"
#include "ubiservices/services/profile/profileTypeHelper.h"
#include <ubiservices/sessionManager.h>
#if defined(US_TARGET_SWITCH)
#include <nn/account.h>
#endif
namespace US_NS {;
US_DEFINE_TEST(TestSessionInfo);
#if !defined(US_DLL)
typedef AuthenticationFixture<1, true> SESSION_INFO_LOGIN;
typedef LeakChecker SESSION_INFO_UNIT;
namespace TestSessionInfo_BF
{
const char* SESSION_INFO_TICKET = "validTicket";
const char* SESSION_INFO_SPACE_ID = "12121212-3434-5656-7878-909090909090";
const char* SESSION_INFO_ENVIRONMENT = "UAT";
const char* SESSION_INFO_EXPIRATION_DATE = "2005-12-06T14:30:15";
const char* SESSION_INFO_SERVER_TIME = "2005-12-06T11:30:o15";
const char* SESSION_INFO_CLIENT_IP = "127.0.0.1";
const char* SESSION_INFO_CLIENT_IP_COUNTRY = "BS";
const char* SESSION_INFO_SESSION_ID = "C7F338F1-43EF-4719-A051-24ABA98810C6";
#if defined(US_SDK_S2S)
const char* SESSION_INFO_APPLICATION_ID = "12345678-1234-5678-0000-123456789999";
const char* SESSION_INFO_DECRYPTION_IV = "DUMMY_DECRYPTION_IV";
const char* SESSION_INFO_DECRYPTION_KEY = "DUMMY_DECRYPTION_KEY";
const char* SESSION_INFO_KEY = "DUMMY_KEY";
#else
const char* SESSION_INFO_PROFILE_ID = "4d9061de-7c20-4cc9-9f55-b8d6228da6ae";
const char* SESSION_INFO_USER_ID = "4d9061de-7c20-4cc9-9f55-b8d6228da6ae";
const char* SESSION_INFO_USERNAME = "ussdk001";
const char* SESSION_INFO_NAME_ON_PLATFORM = "ussdk001";
#endif
JsonWriter generateValidJson()
{
JsonWriter writer;
writer["ticket"] = SESSION_INFO_TICKET;
writer["spaceId"] = SESSION_INFO_SPACE_ID;
writer["environment"] = SESSION_INFO_ENVIRONMENT;
writer["expiration"] = SESSION_INFO_EXPIRATION_DATE;
writer["serverTime"] = SESSION_INFO_SERVER_TIME;
writer["clientIp"] = SESSION_INFO_CLIENT_IP;
writer["clientIpCountry"] = SESSION_INFO_CLIENT_IP_COUNTRY;
writer["sessionId"] = SESSION_INFO_SESSION_ID;
#if defined(US_SDK_S2S)
writer["applicationId"] = SESSION_INFO_APPLICATION_ID;
JsonWriter sessionKey;
sessionKey["decryptionIV"] = SESSION_INFO_DECRYPTION_IV;
sessionKey["decryptionKey"] = SESSION_INFO_DECRYPTION_KEY;
sessionKey["key"] = SESSION_INFO_KEY;
writer["sessionKey"] = sessionKey.getJson();
#else
writer["profileId"] = SESSION_INFO_PROFILE_ID;
writer["userId"] = SESSION_INFO_USER_ID;
writer["username"] = SESSION_INFO_USERNAME;
writer["nameOnPlatform"] = SESSION_INFO_NAME_ON_PLATFORM;
writer["platformType"] = ProfileTypeHelper::getProfileTypeDefaultAsString();
#endif
return writer;
}
}
#if defined(US_TARGET_SWITCH)
/// Note: Test is manual because tester must update the online subscription status of the user manually.
/// Refer to https://nsom.nntt.mng.nintendo.net/accounts to manager users' online subscriptions.
TEST_F(SESSION_INFO_LOGIN, DISABLED_MANUAL_US_UpdateSessionInfoSuccess)
{
ASSERT_TRUE(getFacadeInternal().getManager().hasValidSessionInfoInternal());
SessionInfo sessionInfoAtLogin = getFacade().getAuthenticationClient().getSessionInfo();
PlayerCredentials credentialsAtLogin = *getFacadeInternal().getManager().getStoredCredentials();
// 1. Change subscription status while pop-up is displayed
// 2. Close Pop-up
nn::account::ShowLicenseRequirementsForNetworkService(getFacadeInternal().getManager().getStoredCredentials()->getHandle()); // Displays e-shop pop-up
ASSERT_TRUE(AsyncHelper::waitSuccess(getFacade().getAuthenticationClient().updateSessionInfoWithLatestFirstPartyData()));
SessionInfo sessionInfoAfterUpdate = getFacade().getAuthenticationClient().getSessionInfo();
PlayerCredentials credentialsAfterUpdate = *getFacadeInternal().getManager().getStoredCredentials();
// same session id
EXPECT_EQ(sessionInfoAtLogin.getSessionId(), sessionInfoAfterUpdate.getSessionId());
// updated first-party data ticket
EXPECT_NE(sessionInfoAtLogin.getTicket(), sessionInfoAfterUpdate.getTicket());
EXPECT_NE(credentialsAtLogin.getConsoleTicket(), credentialsAfterUpdate.getConsoleTicket());
PRINT_LINE(String::formatText(" Ticket at login: %s", sessionInfoAtLogin.getTicket().getUtf8()).getUtf8());
PRINT_LINE(String::formatText("Ticket after update: %s", sessionInfoAfterUpdate.getTicket().getUtf8()).getUtf8());
}
TEST_F(SESSION_INFO_LOGIN, US_CheckSecondCallToJobGetSessionInfoDoesNotChangeSessionId)
{
SessionInfo sessionInfoAtLogin = getFacade().getAuthenticationClient().getSessionInfo();
const SmartPtr<PlayerCredentials>& credentials = getFacadeInternal().getManager().getStoredCredentials();
AsyncResultInternal<SessionInfo> result("Test::JobGetSessionInfo::SameSessionId");
result.startTask(US_NEW(JobGetSessionInfo, result, FacadeInterface(getFacadeInternal()), *credentials));
ASSERT_TRUE(AsyncHelper::waitSuccess(result));
SessionInfo sessionInfoAfterUpdate = getFacade().getAuthenticationClient().getSessionInfo();
EXPECT_EQ(sessionInfoAtLogin.getSessionId(), sessionInfoAfterUpdate.getSessionId());
}
#endif
TEST_F(SESSION_INFO_UNIT, LOCAL_ValidParsing)
{
SessionInfo sessionInfo;
EXPECT_TRUE(SessionInfoPrivate::extractData(TestSessionInfo_BF::generateValidJson().getJson(), sessionInfo));
EXPECT_EQ(String(TestSessionInfo_BF::SESSION_INFO_TICKET), sessionInfo.getTicket());
#if !defined(US_SDK_S2S)
EXPECT_EQ(ProfileId(TestSessionInfo_BF::SESSION_INFO_PROFILE_ID), sessionInfo.getProfileId());
EXPECT_EQ(UserId(TestSessionInfo_BF::SESSION_INFO_USER_ID), sessionInfo.getUserId());
EXPECT_EQ(String(TestSessionInfo_BF::SESSION_INFO_NAME_ON_PLATFORM), sessionInfo.getNameOnPlatform());
#endif
EXPECT_TRUE(sessionInfo.getEnvironmentCode() == Environment::Uat);
EXPECT_EQ(SessionId(TestSessionInfo_BF::SESSION_INFO_SESSION_ID), sessionInfo.getSessionId());
EXPECT_EQ(TestSessionInfo_BF::SESSION_INFO_CLIENT_IP_COUNTRY, sessionInfo.getClientIPCountry());
}
TEST_F(SESSION_INFO_UNIT, LOCAL_SessionInfoParsingExtraField)
{
JsonWriter writer = TestSessionInfo_BF::generateValidJson();
writer["extraField"] = "extraValue";
SessionInfo dummy;
EXPECT_TRUE(SessionInfoPrivate::extractData(Json(writer.renderContent()), dummy));
}
TEST_F(SESSION_INFO_UNIT, LOCAL_SessionInfoParsingCheckExpiration)
{
SessionInfo info;
EXPECT_TRUE(SessionInfoPrivate::extractData(TestSessionInfo_BF::generateValidJson().getJson(), info));
EXPECT_TRUE(!info.isExpired());
}
TEST_F(SESSION_INFO_UNIT, LOCAL_SessionExpired)
{
struct Local
{
static Json generateValidJson1SecSession()
{
JsonWriter writer = TestSessionInfo_BF::generateValidJson();
writer["expiration"] = "2005-12-06T14:30:17";
writer["serverTime"] = "2005-12-06T14:30:16";
return writer.getJson();
}
};
SessionInfo info;
EXPECT_TRUE(SessionInfoPrivate::extractData(Local::generateValidJson1SecSession(), info));
US_TEST_NS::Helper::sleep(1500); // Wait for 2 sec for the session to expire
EXPECT_TRUE(info.isExpired());
}
TEST_F(SESSION_INFO_UNIT, LOCAL_SessionInvalid)
{
SessionInfo dummy;
EXPECT_FALSE(SessionInfoPrivate::extractData(JsonWriter("").getJson(), dummy));
EXPECT_FALSE(SessionInfoPrivate::extractData(JsonWriter("{").getJson(), dummy));
EXPECT_FALSE(SessionInfoPrivate::extractData(JsonWriter("dummy:dummy").getJson(), dummy));
}
TEST_F(SESSION_INFO_UNIT, LOCAL_ParsingClientIPCountryInvalid)
{
// valid json
JsonWriter writer = TestSessionInfo_BF::generateValidJson();
{
// valid json
SessionInfo sessionInfo;
EXPECT_TRUE(SessionInfoPrivate::extractData(writer.getJson(), sessionInfo));
}
{
// set invalid value: string with length different than 2
SessionInfo sessionInfo;
writer["clientIpCountry"] = "invalid";
EXPECT_TRUE(SessionInfoPrivate::extractData(writer.getJson(), sessionInfo));
EXPECT_EQ("invalid", sessionInfo.getClientIPCountry());
}
{
// go back to the valid value
SessionInfo sessionInfo;
writer["clientIpCountry"] = "BS";
EXPECT_TRUE(SessionInfoPrivate::extractData(writer.getJson(), sessionInfo));
EXPECT_EQ("BS", sessionInfo.getClientIPCountry());
}
{
// set invalid value: numeric instead of string
SessionInfo sessionInfo;
writer["clientIpCountry"] = 10;
EXPECT_FALSE(SessionInfoPrivate::extractData(writer.getJson(), sessionInfo));
EXPECT_EQ("", sessionInfo.getClientIPCountry());
}
{
// go back to the valid value
SessionInfo sessionInfo;
writer["clientIpCountry"] = "BS";
EXPECT_TRUE(SessionInfoPrivate::extractData(writer.getJson(), sessionInfo));
EXPECT_EQ("BS", sessionInfo.getClientIPCountry());
}
{
// set empty string
SessionInfo sessionInfo;
writer["clientIpCountry"] = "";
EXPECT_TRUE(SessionInfoPrivate::extractData(writer.getJson(), sessionInfo));
EXPECT_EQ("", sessionInfo.getClientIPCountry());
}
}
#endif
};