[automerger skipped] Improve test coverage for FakeValueGenerator. am: 83fe805275 am: 7f6fa2e136 -s ours

am skip reason: Merged-In I6df508e148ff0348fc28b467b4e5a70cf5727a27 with SHA-1 83fe805275 is already in history

Original change: https://googleplex-android-review.googlesource.com/c/platform/hardware/interfaces/+/16997312

Change-Id: I6b475407d2de204c988ba63883522221743cf0f0
diff --git a/automotive/vehicle/aidl/impl/fake_impl/GeneratorHub/include/JsonFakeValueGenerator.h b/automotive/vehicle/aidl/impl/fake_impl/GeneratorHub/include/JsonFakeValueGenerator.h
index 947eb4f..d421ac5 100644
--- a/automotive/vehicle/aidl/impl/fake_impl/GeneratorHub/include/JsonFakeValueGenerator.h
+++ b/automotive/vehicle/aidl/impl/fake_impl/GeneratorHub/include/JsonFakeValueGenerator.h
@@ -56,7 +56,7 @@
   private:
     size_t mEventIndex = 0;
     std::vector<aidl::android::hardware::automotive::vehicle::VehiclePropValue> mEvents;
-    long mLastEventTimestamp = 0;
+    int64_t mLastEventTimestamp = 0;
     int32_t mNumOfIterations = 0;
 
     void setBit(std::vector<uint8_t>& bytes, size_t idx);
diff --git a/automotive/vehicle/aidl/impl/fake_impl/GeneratorHub/src/JsonFakeValueGenerator.cpp b/automotive/vehicle/aidl/impl/fake_impl/GeneratorHub/src/JsonFakeValueGenerator.cpp
index ae92797..d4d52a5 100644
--- a/automotive/vehicle/aidl/impl/fake_impl/GeneratorHub/src/JsonFakeValueGenerator.cpp
+++ b/automotive/vehicle/aidl/impl/fake_impl/GeneratorHub/src/JsonFakeValueGenerator.cpp
@@ -213,7 +213,7 @@
     if (mLastEventTimestamp == 0) {
         mLastEventTimestamp = elapsedRealtimeNano();
     } else {
-        long nextEventTime = 0;
+        int64_t nextEventTime = 0;
         if (mEventIndex > 0) {
             // All events (start from 2nd one) are supposed to happen in the future with a delay
             // equals to the duration between previous and current event.
diff --git a/automotive/vehicle/aidl/impl/fake_impl/GeneratorHub/test/FakeVehicleHalValueGeneratorsTest.cpp b/automotive/vehicle/aidl/impl/fake_impl/GeneratorHub/test/FakeVehicleHalValueGeneratorsTest.cpp
index 674b078..cdfa8b2 100644
--- a/automotive/vehicle/aidl/impl/fake_impl/GeneratorHub/test/FakeVehicleHalValueGeneratorsTest.cpp
+++ b/automotive/vehicle/aidl/impl/fake_impl/GeneratorHub/test/FakeVehicleHalValueGeneratorsTest.cpp
@@ -66,7 +66,7 @@
         std::unique_lock<std::mutex> uniqueLock(mEventsLock);
         bool result = mCv.wait_for(uniqueLock, 10s, [this, count] {
             ScopedLockAssertion lockAssertion(mEventsLock);
-            return mEvents.size() == count;
+            return mEvents.size() >= count;
         });
 
         ASSERT_TRUE(result) << "didn't receive enough events";
@@ -126,7 +126,8 @@
     for (size_t i = 0; i < eventCount; i++) {
         events.push_back(VehiclePropValue{
                 .prop = static_cast<int32_t>(i),
-                .timestamp = timestamp + static_cast<int64_t>(50 * i),
+                // Generate 1 event every 1ms.
+                .timestamp = timestamp + static_cast<int64_t>(1000000 * i),
         });
     }
     generator->setEvents(events);
@@ -148,7 +149,8 @@
     for (size_t i = 0; i < eventCount; i++) {
         events.push_back(VehiclePropValue{
                 .prop = static_cast<int32_t>(i),
-                .timestamp = timestamp + static_cast<int64_t>(50 * i),
+                // Generate 1 event every 1ms.
+                .timestamp = timestamp + static_cast<int64_t>(1000000 * i),
         });
     }
     generator->setEvents(events);
@@ -158,8 +160,12 @@
     waitForEvents(1);
 
     getHub()->unregisterGenerator(0);
+    clearEvents();
 
-    ASSERT_LE(getEvents().size(), 2u)
+    std::this_thread::sleep_for(std::chrono::milliseconds(100));
+
+    // It is possible that one last event would be generated after unregistering.
+    ASSERT_LE(getEvents().size(), 1u)
             << "Must stop generating event after generator is unregistered";
 }
 
@@ -176,7 +182,6 @@
     waitForEvents(10);
     auto events = getEvents();
 
-    ASSERT_EQ(events.size(), 10u);
     int value = 30;
     for (size_t i = 0; i < 10; i++) {
         EXPECT_EQ(std::vector<float>({static_cast<float>(value)}), events[i].value.floatValues);
@@ -196,7 +201,7 @@
 
     waitForEvents(10);
     auto events = getEvents();
-    ASSERT_EQ(events.size(), 10u);
+
     int value = 30;
     for (size_t i = 0; i < 10; i++) {
         EXPECT_EQ(std::vector<int32_t>({value}), events[i].value.int32Values);
@@ -216,8 +221,7 @@
 
     waitForEvents(10);
     auto events = getEvents();
-    // We should get 10 events ideally, but let's be safe here.
-    ASSERT_EQ(events.size(), 10u);
+
     int value = 30;
     for (size_t i = 0; i < 10; i++) {
         EXPECT_EQ(std::vector<int64_t>({value}), events[i].value.int64Values);
@@ -237,7 +241,7 @@
 
     waitForEvents(10);
     auto events = getEvents();
-    ASSERT_EQ(events.size(), 10u);
+
     int value = 50;
     for (size_t i = 0; i < 10; i++) {
         EXPECT_EQ(std::vector<float>({static_cast<float>(value)}), events[i].value.floatValues);
@@ -258,7 +262,6 @@
 
     waitForEvents(10);
     auto events = getEvents();
-    ASSERT_EQ(events.size(), 10u);
 
     // Init value would be set to middleValue if given initValue is not valid.
     int value = 50;
@@ -269,7 +272,7 @@
 }
 
 TEST_F(FakeVehicleHalValueGeneratorsTest, testJsonFakeValueGenerator) {
-    long currentTime = elapsedRealtimeNano();
+    int64_t currentTime = elapsedRealtimeNano();
 
     std::unique_ptr<JsonFakeValueGenerator> generator =
             std::make_unique<JsonFakeValueGenerator>(getTestFilePath("prop.json"), 2);
@@ -306,7 +309,7 @@
     waitForEvents(expectedValues.size());
     auto events = getEvents();
 
-    long lastEventTime = currentTime;
+    int64_t lastEventTime = currentTime;
     for (auto& event : events) {
         EXPECT_GT(event.timestamp, lastEventTime);
         lastEventTime = event.timestamp;
@@ -325,7 +328,7 @@
 }
 
 TEST_F(FakeVehicleHalValueGeneratorsTest, testJsonFakeValueGeneratorUsingRequest) {
-    long currentTime = elapsedRealtimeNano();
+    int64_t currentTime = elapsedRealtimeNano();
 
     VehiclePropValue request = {.value = {
                                         .stringValue = getTestFilePath("prop.json"),
@@ -367,7 +370,7 @@
     waitForEvents(expectedValues.size());
     auto events = getEvents();
 
-    long lastEventTime = currentTime;
+    int64_t lastEventTime = currentTime;
     for (auto& event : events) {
         EXPECT_GT(event.timestamp, lastEventTime);
         lastEventTime = event.timestamp;
diff --git a/identity/support/include/android/hardware/identity/support/IdentityCredentialSupport.h b/identity/support/include/android/hardware/identity/support/IdentityCredentialSupport.h
index 82746d6..952b69a 100644
--- a/identity/support/include/android/hardware/identity/support/IdentityCredentialSupport.h
+++ b/identity/support/include/android/hardware/identity/support/IdentityCredentialSupport.h
@@ -407,6 +407,10 @@
 // may be smaller than |maxChunkSize|.
 vector<vector<uint8_t>> chunkVector(const vector<uint8_t>& content, size_t maxChunkSize);
 
+// Extract the issuer subject name from the leaf cert in the given chain,
+// returning it as DER-encoded bytes.
+optional<vector<uint8_t>> extractDerSubjectFromCertificate(const vector<uint8_t>& certificate);
+
 }  // namespace support
 }  // namespace identity
 }  // namespace hardware
diff --git a/identity/support/src/IdentityCredentialSupport.cpp b/identity/support/src/IdentityCredentialSupport.cpp
index 36ecdb0..4c2f186 100644
--- a/identity/support/src/IdentityCredentialSupport.cpp
+++ b/identity/support/src/IdentityCredentialSupport.cpp
@@ -209,38 +209,6 @@
     return keyPair;
 }
 
-// Extract the issuer subject name from the leaf cert in the given chain,
-// returning it as DER-encoded bytes.
-optional<vector<uint8_t>> extractDerSubjectFromCertificate(const vector<uint8_t>& certificate) {
-    const uint8_t* input = certificate.data();
-    X509_Ptr cert(d2i_X509(/*cert=*/nullptr, &input, certificate.size()));
-    if (!cert) {
-        LOG(ERROR) << "Failed to parse certificate";
-        return std::nullopt;
-    }
-
-    X509_NAME* subject = X509_get_subject_name(cert.get());
-    if (!subject) {
-        LOG(ERROR) << "Failed to retrieve subject name";
-        return std::nullopt;
-    }
-
-    int encodedSubjectLength = i2d_X509_NAME(subject, /*out=*/nullptr);
-    if (encodedSubjectLength < 0) {
-        LOG(ERROR) << "Error obtaining encoded subject name length";
-        return std::nullopt;
-    }
-
-    vector<uint8_t> encodedSubject(encodedSubjectLength);
-    uint8_t* out = encodedSubject.data();
-    if (encodedSubjectLength != i2d_X509_NAME(subject, &out)) {
-        LOG(ERROR) << "Error encoding subject name";
-        return std::nullopt;
-    }
-
-    return encodedSubject;
-}
-
 // Generates the attestation certificate with the parameters passed in.  Note
 // that the passed in |activeTimeMilliSeconds| |expireTimeMilliSeconds| are in
 // milli seconds since epoch.  We are setting them to milliseconds due to
@@ -900,7 +868,7 @@
     }
 
     optional<vector<uint8_t>> derIssuerSubject =
-            extractDerSubjectFromCertificate(attestationKeyCert);
+            support::extractDerSubjectFromCertificate(attestationKeyCert);
     if (!derIssuerSubject) {
         LOG(ERROR) << "Error error extracting issuer name from the given certificate chain";
         return std::nullopt;
@@ -2325,6 +2293,36 @@
     return testHardwareBoundKey;
 }
 
+optional<vector<uint8_t>> extractDerSubjectFromCertificate(const vector<uint8_t>& certificate) {
+    const uint8_t* input = certificate.data();
+    X509_Ptr cert(d2i_X509(/*cert=*/nullptr, &input, certificate.size()));
+    if (!cert) {
+        LOG(ERROR) << "Failed to parse certificate";
+        return std::nullopt;
+    }
+
+    X509_NAME* subject = X509_get_subject_name(cert.get());
+    if (!subject) {
+        LOG(ERROR) << "Failed to retrieve subject name";
+        return std::nullopt;
+    }
+
+    int encodedSubjectLength = i2d_X509_NAME(subject, /*out=*/nullptr);
+    if (encodedSubjectLength < 0) {
+        LOG(ERROR) << "Error obtaining encoded subject name length";
+        return std::nullopt;
+    }
+
+    vector<uint8_t> encodedSubject(encodedSubjectLength);
+    uint8_t* out = encodedSubject.data();
+    if (encodedSubjectLength != i2d_X509_NAME(subject, &out)) {
+        LOG(ERROR) << "Error encoding subject name";
+        return std::nullopt;
+    }
+
+    return encodedSubject;
+}
+
 }  // namespace support
 }  // namespace identity
 }  // namespace hardware