cryptohome: populate keyset index on load

Since now all the keyset loading goes through a single path, enforce
keyset_index population on the path. That will later allow us to get rid
of almost all cases of passing raw index to the functions.

BUG=chromium:1124561
TEST=unittest

Change-Id: Ib7bc747eb71f2d648031bd607ac59e87ae197080
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform2/+/2465547
Tested-by: Daniil Lunev <dlunev@chromium.org>
Commit-Queue: Daniil Lunev <dlunev@chromium.org>
Reviewed-by: Leo Lai <cylai@google.com>
diff --git a/cryptohome/homedirs.cc b/cryptohome/homedirs.cc
index cf84fbe..038096b 100644
--- a/cryptohome/homedirs.cc
+++ b/cryptohome/homedirs.cc
@@ -301,7 +301,6 @@
       continue;
     }
     if (GetSerializedKeysetLabel(vk->serialized(), index) == key_label) {
-      vk->set_legacy_index(index);
       return vk.release();
     }
   }
@@ -971,6 +970,7 @@
     LOG(ERROR) << "Failed to load keyset file for user " << obfuscated_user;
     return false;
   }
+  keyset->set_legacy_index(index);
   return true;
 }
 
diff --git a/cryptohome/homedirs_unittest.cc b/cryptohome/homedirs_unittest.cc
index 5d47439..c092493 100644
--- a/cryptohome/homedirs_unittest.cc
+++ b/cryptohome/homedirs_unittest.cc
@@ -733,6 +733,8 @@
       // which isn't used in a test.
       EXPECT_CALL(*active_vks_[i], Load(keyset_paths_[0]))
           .WillRepeatedly(Return(true));
+      EXPECT_CALL(*active_vks_[i], set_legacy_index(_))
+          .Times(testing::AnyNumber());
     }
     active_vk_ = active_vks_[0];