Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add "hasInstanceID" to diagnostic data #17

Merged
merged 1 commit into from
Nov 23, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions psicash.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -495,6 +495,7 @@ json PsiCash::GetDiagnosticInfo(bool lite) const {
json j = json::object();

j["test"] = test_;
j["hasInstanceID"] = user_data_->HasInstanceID();
j["isLoggedOutAccount"] = user_data_->GetIsLoggedOutAccount();
j["validTokenTypes"] = user_data_->ValidTokenTypes();
j["isAccount"] = IsAccount();
Expand Down
8 changes: 8 additions & 0 deletions psicash_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1100,6 +1100,7 @@ TEST_F(TestPsiCash, GetDiagnosticInfo) {
ASSERT_FALSE(err);

auto want = R"|({
"hasInstanceID":true,
"balance":0,
"isAccount":false,
"isLoggedOutAccount":false,
Expand All @@ -1113,6 +1114,7 @@ TEST_F(TestPsiCash, GetDiagnosticInfo) {
ASSERT_EQ(j, want);

want = R"|({
"hasInstanceID":true,
"balance":0,
"isAccount":false,
"isLoggedOutAccount":false,
Expand All @@ -1132,6 +1134,7 @@ TEST_F(TestPsiCash, GetDiagnosticInfo) {
ASSERT_FALSE(err);

auto want = R"|({
"hasInstanceID":true,
"balance":0,
"isAccount":false,
"isLoggedOutAccount":false,
Expand All @@ -1145,6 +1148,7 @@ TEST_F(TestPsiCash, GetDiagnosticInfo) {
ASSERT_EQ(j, want);

want = R"|({
"hasInstanceID":true,
"balance":0,
"isAccount":false,
"isLoggedOutAccount":false,
Expand All @@ -1163,6 +1167,7 @@ TEST_F(TestPsiCash, GetDiagnosticInfo) {
pc.user_data().SetAuthTokens({{"a", {"a"}}, {"b", {"b"}}, {"c", {"c"}}}, true, "username");
// pc.user_data().SetServerTimeDiff() // too hard to do reliably
want = R"|({
"hasInstanceID":true,
"balance":12345,
"isAccount":true,
"isLoggedOutAccount":false,
Expand All @@ -1175,6 +1180,7 @@ TEST_F(TestPsiCash, GetDiagnosticInfo) {
j = pc.GetDiagnosticInfo(/*lite=*/false);
ASSERT_EQ(j, want);
want = R"|({
"hasInstanceID":true,
"balance":12345,
"isAccount":true,
"isLoggedOutAccount":false,
Expand All @@ -1188,6 +1194,7 @@ TEST_F(TestPsiCash, GetDiagnosticInfo) {

pc.user_data().DeleteUserData(/*is_logged_out_account=*/true);
want = R"|({
"hasInstanceID":true,
"balance":0,
"isAccount":true,
"isLoggedOutAccount":true,
Expand All @@ -1200,6 +1207,7 @@ TEST_F(TestPsiCash, GetDiagnosticInfo) {
j = pc.GetDiagnosticInfo(/*lite=*/false);
ASSERT_EQ(j, want);
want = R"|({
"hasInstanceID":true,
"balance":0,
"isAccount":true,
"isLoggedOutAccount":true,
Expand Down
5 changes: 5 additions & 0 deletions userdata.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,11 @@ std::string UserData::GetInstanceID() const {
return *v;
}

bool UserData::HasInstanceID() const {
auto v = datastore_.Get<string>(kInstanceIDPtr);
return !!v && v->length() > 0;
}

bool UserData::GetIsLoggedOutAccount() const {
auto v = datastore_.Get<bool>(kIsLoggedOutAccountPtr);
if (!v) {
Expand Down
2 changes: 2 additions & 0 deletions userdata.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,8 @@ class UserData {
error::Error DeleteUserData(bool isLoggedOutAccount);

std::string GetInstanceID() const;
/// Returns true if there is a non-empty instance ID in the datstore.
bool HasInstanceID() const;

bool GetIsLoggedOutAccount() const;
error::Error SetIsLoggedOutAccount(bool v);
Expand Down
40 changes: 40 additions & 0 deletions userdata_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,46 @@ TEST_F(TestUserData, GetInstanceID)
ASSERT_NE(v1, v2);
}

TEST_F(TestUserData, HasInstanceID)
{
auto temp_dir = GetTempDir();

{
UserData ud;
auto err = ud.Init(temp_dir.c_str(), dev);
ASSERT_FALSE(err);
ASSERT_TRUE(ud.HasInstanceID());

// Clear should generate a new instance ID
ud.Clear();
ASSERT_TRUE(ud.HasInstanceID());
}

{
auto instance_id_ptr = "/instance/instanceID"_json_pointer;

Datastore ds;
auto err = ds.Init(temp_dir, GetSuffix(dev));
ASSERT_FALSE(err);

auto instance_id = ds.Get<string>(instance_id_ptr);
ASSERT_TRUE(instance_id);
ASSERT_THAT(*instance_id, Not(IsEmpty()));

// Clear the instanceID so that it's absent later
err = ds.Set(instance_id_ptr, nullptr);
ASSERT_FALSE(err);
}

{
// Now we shouldn't have an instance ID
UserData ud;
auto err = ud.Init(temp_dir.c_str(), dev);
ASSERT_FALSE(err);
ASSERT_FALSE(ud.HasInstanceID());
}
}

TEST_F(TestUserData, IsLoggedOutAccount)
{
UserData ud;
Expand Down