Fix order of hidl unlinkToDeath.
am: ba140f476f

Change-Id: Ib42a05ab4699933721167110ae3f30a5e5703674
diff --git a/Interface.cpp b/Interface.cpp
index eaf4ce3..8370c36 100644
--- a/Interface.cpp
+++ b/Interface.cpp
@@ -171,13 +171,13 @@
                 {IMPL_PROXY,
                     [](auto &out) {
                         out << "std::unique_lock<std::mutex> lock(_hidl_mMutex);\n"
-                            << "for (auto it = _hidl_mDeathRecipients.begin();"
-                            << "it != _hidl_mDeathRecipients.end();"
+                            << "for (auto it = _hidl_mDeathRecipients.rbegin();"
+                            << "it != _hidl_mDeathRecipients.rend();"
                             << "++it) {\n";
                         out.indent([&] {
                             out.sIf("(*it)->getRecipient() == recipient", [&] {
                                 out << "::android::status_t status = remote()->unlinkToDeath(*it);\n"
-                                    << "_hidl_mDeathRecipients.erase(it);\n"
+                                    << "_hidl_mDeathRecipients.erase(it.base()-1);\n"
                                     << "return status == ::android::OK;\n";
                                 });
                             }).endl();
diff --git a/test/hidl_test/hidl_test_client.cpp b/test/hidl_test/hidl_test_client.cpp
index c10f2ff..a58ef8e 100644
--- a/test/hidl_test/hidl_test_client.cpp
+++ b/test/hidl_test/hidl_test_client.cpp
@@ -571,28 +571,6 @@
     ASSERT_NE(manager, nullptr);
 }
 
-TEST_F(HidlTest, HashTest) {
-    static constexpr uint64_t kHashSize = 32u;
-    // unreleased interface has an empty hash
-    uint8_t ihash[kHashSize] = {0};
-    uint8_t ibase[kHashSize] = {189, 218, 182, 24,  77,  122, 52,  109, 166, 160, 125,
-                                192, 130, 140, 241, 154, 105, 111, 76,  170, 54,  17,
-                                197, 31,  46,  20,  86,  90,  20,  180, 15,  217};
-    auto service = IHash::getService(mode == PASSTHROUGH /* getStub */);
-    EXPECT_OK(service->getHashChain([&](const auto& chain) {
-        ASSERT_EQ(chain.size(), 2u);
-        EXPECT_EQ(chain[0].size(), kHashSize);
-        EXPECT_ARRAYEQ(ihash, chain[0], kHashSize);
-        EXPECT_EQ(chain[1].size(), kHashSize);
-        EXPECT_ARRAYEQ(ibase, chain[1], kHashSize);
-    }));
-    EXPECT_OK(manager->getHashChain([&](const auto& managerChain) {
-        EXPECT_EQ(managerChain[managerChain.size() - 1].size(), kHashSize);
-        EXPECT_ARRAYEQ(ibase, managerChain[managerChain.size() - 1], kHashSize)
-            << "Hash for IBase doesn't match!";
-    }));
-}
-
 TEST_F(HidlTest, ServiceListTest) {
     static const std::set<std::string> binderizedSet = {
         "android.hardware.tests.pointer@1.0::IPointer/pointer",
@@ -1613,6 +1591,10 @@
     sp<HidlDeathRecipient> recipient2 = new HidlDeathRecipient();
 
     EXPECT_TRUE(dyingBaz->linkToDeath(recipient, 0x1481));
+
+    EXPECT_TRUE(dyingBaz->linkToDeath(recipient, 0x1482));
+    EXPECT_TRUE(dyingBaz->unlinkToDeath(recipient));
+
     EXPECT_TRUE(dyingBaz->linkToDeath(recipient2, 0x2592));
     EXPECT_TRUE(dyingBaz->unlinkToDeath(recipient2));
 
diff --git a/test/java_test/src/com/android/commands/hidl_test_java/HidlTestJava.java b/test/java_test/src/com/android/commands/hidl_test_java/HidlTestJava.java
index 422b465..8999bc3 100644
--- a/test/java_test/src/com/android/commands/hidl_test_java/HidlTestJava.java
+++ b/test/java_test/src/com/android/commands/hidl_test_java/HidlTestJava.java
@@ -949,7 +949,12 @@
 
         final int cookie1 = 0x1481;
         final int cookie2 = 0x1482;
+        final int cookie3 = 0x1483;
         ExpectTrue(proxy.linkToDeath(recipient1, cookie1));
+
+        ExpectTrue(proxy.linkToDeath(recipient1, cookie3));
+        ExpectTrue(proxy.unlinkToDeath(recipient1));
+
         ExpectTrue(proxy.linkToDeath(recipient2, cookie2));
         ExpectTrue(proxy.unlinkToDeath(recipient2));
         try {