audiohal: Fix UAF of HAL devices in Stream objects
am: 936279e1ff

Change-Id: Id78777accd072722b04d6b0bdc8bf9c219cec861
diff --git a/bluetooth/1.0/default/vendor_interface.cc b/bluetooth/1.0/default/vendor_interface.cc
index 68cac5f..bb66460 100644
--- a/bluetooth/1.0/default/vendor_interface.cc
+++ b/bluetooth/1.0/default/vendor_interface.cc
@@ -16,8 +16,6 @@
 
 #include "vendor_interface.h"
 
-#include <assert.h>
-
 #define LOG_TAG "android.hardware.bluetooth@1.0-impl"
 #include <android-base/logging.h>
 #include <cutils/properties.h>
@@ -165,7 +163,7 @@
     InitializeCompleteCallback initialize_complete_cb,
     PacketReadCallback event_cb, PacketReadCallback acl_cb,
     PacketReadCallback sco_cb) {
-  assert(!g_vendor_interface);
+  CHECK(!g_vendor_interface);
   g_vendor_interface = new VendorInterface();
   return g_vendor_interface->Open(initialize_complete_cb, event_cb, acl_cb,
                                   sco_cb);
diff --git a/bluetooth/1.0/vts/functional/VtsHalBluetoothV1_0TargetTest.cpp b/bluetooth/1.0/vts/functional/VtsHalBluetoothV1_0TargetTest.cpp
index 9a4efae..6156553 100644
--- a/bluetooth/1.0/vts/functional/VtsHalBluetoothV1_0TargetTest.cpp
+++ b/bluetooth/1.0/vts/functional/VtsHalBluetoothV1_0TargetTest.cpp
@@ -23,9 +23,8 @@
 #include <hardware/bluetooth.h>
 #include <utils/Log.h>
 
+#include <VtsHalHidlTargetCallbackBase.h>
 #include <VtsHalHidlTargetTestBase.h>
-#include <condition_variable>
-#include <mutex>
 #include <queue>
 
 using ::android::hardware::bluetooth::V1_0::IBluetoothHci;
@@ -92,6 +91,11 @@
 #define ACL_BROADCAST_ACTIVE_SLAVE (0x1 << 4)
 #define ACL_PACKET_BOUNDARY_COMPLETE (0x3 << 6)
 
+constexpr char kCallbackNameAclEventReceived[] = "aclDataReceived";
+constexpr char kCallbackNameHciEventReceived[] = "hciEventReceived";
+constexpr char kCallbackNameInitializationComplete[] = "initializationComplete";
+constexpr char kCallbackNameScoEventReceived[] = "scoDataReceived";
+
 class ThroughputLogger {
  public:
   ThroughputLogger(std::string task)
@@ -121,7 +125,8 @@
  public:
   virtual void SetUp() override {
     // currently test passthrough mode only
-    bluetooth = ::testing::VtsHalHidlTargetTestBase::getService<IBluetoothHci>();
+    bluetooth =
+        ::testing::VtsHalHidlTargetTestBase::getService<IBluetoothHci>();
     ASSERT_NE(bluetooth, nullptr);
     ALOGI("%s: getService() for bluetooth is %s", __func__,
           bluetooth->isRemote() ? "remote" : "local");
@@ -135,10 +140,6 @@
     max_sco_data_packets = 0;
 
     initialized = false;
-    initialized_count = 0;
-    event_count = 0;
-    acl_count = 0;
-    sco_count = 0;
     event_cb_count = 0;
     acl_cb_count = 0;
     sco_cb_count = 0;
@@ -146,7 +147,18 @@
     ASSERT_EQ(initialized, false);
     bluetooth->initialize(bluetooth_cb);
 
-    wait_for_init_callback();
+    bluetooth_cb->SetWaitTimeout(kCallbackNameInitializationComplete,
+                                 WAIT_FOR_INIT_TIMEOUT);
+    bluetooth_cb->SetWaitTimeout(kCallbackNameHciEventReceived,
+                                 WAIT_FOR_HCI_EVENT_TIMEOUT);
+    bluetooth_cb->SetWaitTimeout(kCallbackNameAclEventReceived,
+                                 WAIT_FOR_ACL_DATA_TIMEOUT);
+    bluetooth_cb->SetWaitTimeout(kCallbackNameScoEventReceived,
+                                 WAIT_FOR_SCO_DATA_TIMEOUT);
+
+    EXPECT_TRUE(
+        bluetooth_cb->WaitForCallback(kCallbackNameInitializationComplete)
+            .first);
 
     ASSERT_EQ(initialized, true);
   }
@@ -171,82 +183,10 @@
   void wait_for_command_complete_event(hidl_vec<uint8_t> cmd);
   int wait_for_completed_packets_event(uint16_t handle);
 
-  // Inform the test about the initialization callback
-  inline void notify_initialized() {
-    std::unique_lock<std::mutex> lock(initialized_mutex);
-    initialized_count++;
-    initialized_condition.notify_one();
-  }
-
-  // Test code calls this function to wait for the init callback
-  inline void wait_for_init_callback() {
-    std::unique_lock<std::mutex> lock(initialized_mutex);
-
-    auto start_time = std::chrono::steady_clock::now();
-    while (initialized_count == 0)
-      if (initialized_condition.wait_until(lock,
-                                     start_time + WAIT_FOR_INIT_TIMEOUT) ==
-          std::cv_status::timeout)
-        return;
-    initialized_count--;
-  }
-
-  // Inform the test about an event callback
-  inline void notify_event_received() {
-    std::unique_lock<std::mutex> lock(event_mutex);
-    event_count++;
-    event_condition.notify_one();
-  }
-
-  // Test code calls this function to wait for an event callback
-  inline void wait_for_event() {
-    std::unique_lock<std::mutex> lock(event_mutex);
-
-    auto start_time = std::chrono::steady_clock::now();
-    while (event_count == 0)
-      if (event_condition.wait_until(lock,
-                                     start_time + WAIT_FOR_HCI_EVENT_TIMEOUT) ==
-          std::cv_status::timeout)
-        return;
-    event_count--;
-  }
-
-  // Inform the test about an acl data callback
-  inline void notify_acl_data_received() {
-    std::unique_lock<std::mutex> lock(acl_mutex);
-    acl_count++;
-    acl_condition.notify_one();
-  }
-
-  // Test code calls this function to wait for an acl data callback
-  inline void wait_for_acl() {
-    std::unique_lock<std::mutex> lock(acl_mutex);
-
-    while (acl_count == 0)
-      acl_condition.wait_until(
-          lock, std::chrono::steady_clock::now() + WAIT_FOR_ACL_DATA_TIMEOUT);
-    acl_count--;
-  }
-
-  // Inform the test about a sco data callback
-  inline void notify_sco_data_received() {
-    std::unique_lock<std::mutex> lock(sco_mutex);
-    sco_count++;
-    sco_condition.notify_one();
-  }
-
-  // Test code calls this function to wait for a sco data callback
-  inline void wait_for_sco() {
-    std::unique_lock<std::mutex> lock(sco_mutex);
-
-    while (sco_count == 0)
-      sco_condition.wait_until(
-          lock, std::chrono::steady_clock::now() + WAIT_FOR_SCO_DATA_TIMEOUT);
-    sco_count--;
-  }
-
   // A simple test implementation of BluetoothHciCallbacks.
-  class BluetoothHciCallbacks : public IBluetoothHciCallbacks {
+  class BluetoothHciCallbacks
+      : public ::testing::VtsHalHidlTargetCallbackBase<BluetoothHidlTest>,
+        public IBluetoothHciCallbacks {
     BluetoothHidlTest& parent_;
 
    public:
@@ -256,7 +196,7 @@
 
     Return<void> initializationComplete(Status status) override {
       parent_.initialized = (status == Status::SUCCESS);
-      parent_.notify_initialized();
+      NotifyFromCallback(kCallbackNameInitializationComplete);
       ALOGV("%s (status = %d)", __func__, static_cast<int>(status));
       return Void();
     };
@@ -265,7 +205,7 @@
         const ::android::hardware::hidl_vec<uint8_t>& event) override {
       parent_.event_cb_count++;
       parent_.event_queue.push(event);
-      parent_.notify_event_received();
+      NotifyFromCallback(kCallbackNameHciEventReceived);
       ALOGV("Event received (length = %d)", static_cast<int>(event.size()));
       return Void();
     };
@@ -274,7 +214,7 @@
         const ::android::hardware::hidl_vec<uint8_t>& data) override {
       parent_.acl_cb_count++;
       parent_.acl_queue.push(data);
-      parent_.notify_acl_data_received();
+      NotifyFromCallback(kCallbackNameAclEventReceived);
       return Void();
     };
 
@@ -282,13 +222,13 @@
         const ::android::hardware::hidl_vec<uint8_t>& data) override {
       parent_.sco_cb_count++;
       parent_.sco_queue.push(data);
-      parent_.notify_sco_data_received();
+      NotifyFromCallback(kCallbackNameScoEventReceived);
       return Void();
     };
   };
 
   sp<IBluetoothHci> bluetooth;
-  sp<IBluetoothHciCallbacks> bluetooth_cb;
+  sp<BluetoothHciCallbacks> bluetooth_cb;
   std::queue<hidl_vec<uint8_t>> event_queue;
   std::queue<hidl_vec<uint8_t>> acl_queue;
   std::queue<hidl_vec<uint8_t>> sco_queue;
@@ -303,20 +243,6 @@
   int max_sco_data_packet_length;
   int max_acl_data_packets;
   int max_sco_data_packets;
-
- private:
-  std::mutex initialized_mutex;
-  std::mutex event_mutex;
-  std::mutex sco_mutex;
-  std::mutex acl_mutex;
-  std::condition_variable initialized_condition;
-  std::condition_variable event_condition;
-  std::condition_variable sco_condition;
-  std::condition_variable acl_condition;
-  int initialized_count;
-  int event_count;
-  int sco_count;
-  int acl_count;
 };
 
 // A class for test environment setup (kept since this file is a template).
@@ -334,7 +260,8 @@
   int status_event_count = 0;
   hidl_vec<uint8_t> event;
   do {
-    wait_for_event();
+    EXPECT_TRUE(
+        bluetooth_cb->WaitForCallback(kCallbackNameHciEventReceived).first);
     EXPECT_LT(static_cast<size_t>(0), event_queue.size());
     if (event_queue.size() == 0) {
       event.resize(0);
@@ -366,7 +293,8 @@
   hidl_vec<uint8_t> cmd = COMMAND_HCI_READ_BUFFER_SIZE;
   bluetooth->sendHciCommand(cmd);
 
-  wait_for_event();
+  EXPECT_TRUE(
+      bluetooth_cb->WaitForCallback(kCallbackNameHciEventReceived).first);
 
   EXPECT_LT(static_cast<size_t>(0), event_queue.size());
   if (event_queue.size() == 0) return;
@@ -420,7 +348,8 @@
     bluetooth->sendHciCommand(cmd);
 
     // Check the loopback of the HCI packet
-    wait_for_event();
+    EXPECT_TRUE(
+        bluetooth_cb->WaitForCallback(kCallbackNameHciEventReceived).first);
     hidl_vec<uint8_t> event = event_queue.front();
     event_queue.pop();
     size_t compare_length =
@@ -456,7 +385,8 @@
     bluetooth->sendScoData(sco_vector);
 
     // Check the loopback of the SCO packet
-    wait_for_sco();
+    EXPECT_TRUE(
+        bluetooth_cb->WaitForCallback(kCallbackNameScoEventReceived).first);
     hidl_vec<uint8_t> sco_loopback = sco_queue.front();
     sco_queue.pop();
 
@@ -501,7 +431,8 @@
     bluetooth->sendAclData(acl_vector);
 
     // Check the loopback of the ACL packet
-    wait_for_acl();
+    EXPECT_TRUE(
+        bluetooth_cb->WaitForCallback(kCallbackNameAclEventReceived).first);
     hidl_vec<uint8_t> acl_loopback = acl_queue.front();
     acl_queue.pop();
 
@@ -527,7 +458,8 @@
 
 // Return the number of completed packets reported by the controller.
 int BluetoothHidlTest::wait_for_completed_packets_event(uint16_t handle) {
-  wait_for_event();
+  EXPECT_TRUE(
+      bluetooth_cb->WaitForCallback(kCallbackNameHciEventReceived).first);
   int packets_processed = 0;
   while (event_queue.size() > 0) {
     hidl_vec<uint8_t> event = event_queue.front();
@@ -554,7 +486,8 @@
   int connection_event_count = 0;
   hidl_vec<uint8_t> event;
   do {
-    wait_for_event();
+    EXPECT_TRUE(
+        bluetooth_cb->WaitForCallback(kCallbackNameHciEventReceived).first);
     event = event_queue.front();
     event_queue.pop();
     EXPECT_GT(event.size(),
@@ -592,7 +525,7 @@
 }
 
 // Empty test: Initialize()/Close() are called in SetUp()/TearDown().
-TEST_F(BluetoothHidlTest, InitializeAndClose) { }
+TEST_F(BluetoothHidlTest, InitializeAndClose) {}
 
 // Send an HCI Reset with sendHciCommand and wait for a command complete event.
 TEST_F(BluetoothHidlTest, HciReset) {
@@ -607,7 +540,8 @@
   hidl_vec<uint8_t> cmd = COMMAND_HCI_READ_LOCAL_VERSION_INFORMATION;
   bluetooth->sendHciCommand(cmd);
 
-  wait_for_event();
+  EXPECT_TRUE(
+      bluetooth_cb->WaitForCallback(kCallbackNameHciEventReceived).first);
 
   hidl_vec<uint8_t> event = event_queue.front();
   event_queue.pop();
@@ -627,7 +561,8 @@
   hidl_vec<uint8_t> cmd = COMMAND_HCI_SHOULD_BE_UNKNOWN;
   bluetooth->sendHciCommand(cmd);
 
-  wait_for_event();
+  EXPECT_TRUE(
+      bluetooth_cb->WaitForCallback(kCallbackNameHciEventReceived).first);
 
   hidl_vec<uint8_t> event = event_queue.front();
   event_queue.pop();
diff --git a/radio/1.0/vts/functional/Android.bp b/radio/1.0/vts/functional/Android.bp
index 24e3926..7808de1 100644
--- a/radio/1.0/vts/functional/Android.bp
+++ b/radio/1.0/vts/functional/Android.bp
@@ -44,3 +44,27 @@
         "-g",
     ],
 }
+
+cc_test {
+    name: "VtsHalSapV1_0TargetTest",
+    defaults: ["hidl_defaults"],
+    srcs: ["sap_callback.cpp",
+           "sap_hidl_hal_api.cpp",
+           "sap_hidl_hal_test.cpp",
+           "VtsHalSapV1_0TargetTest.cpp"],
+    shared_libs: [
+        "libbase",
+        "liblog",
+        "libcutils",
+        "libhidlbase",
+        "libhidltransport",
+        "libnativehelper",
+        "libutils",
+        "android.hardware.radio@1.0",
+    ],
+    static_libs: ["VtsHalHidlTargetTestBase"],
+    cflags: [
+        "-O0",
+        "-g",
+    ],
+}
diff --git a/radio/1.0/vts/functional/VtsHalSapV1_0TargetTest.cpp b/radio/1.0/vts/functional/VtsHalSapV1_0TargetTest.cpp
new file mode 100644
index 0000000..f902588
--- /dev/null
+++ b/radio/1.0/vts/functional/VtsHalSapV1_0TargetTest.cpp
@@ -0,0 +1,28 @@
+/*
+ * Copyright (C) 2017 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include<sap_hidl_hal_utils.h>
+
+int main(int argc, char** argv) {
+    // Add Sim-access Profile Hidl Environment
+    ::testing::AddGlobalTestEnvironment(new SapHidlEnvironment);
+    ::testing::InitGoogleTest(&argc, argv);
+
+    int status = RUN_ALL_TESTS();
+    LOG(INFO) << "Test result = " << status;
+
+    return status;
+}
diff --git a/radio/1.0/vts/functional/sap_callback.cpp b/radio/1.0/vts/functional/sap_callback.cpp
new file mode 100644
index 0000000..563d066
--- /dev/null
+++ b/radio/1.0/vts/functional/sap_callback.cpp
@@ -0,0 +1,92 @@
+/*
+ * Copyright (C) 2017 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include<sap_hidl_hal_utils.h>
+
+SapCallback::SapCallback(SapHidlTest& parent) : parent(parent) {
+}
+
+Return<void> SapCallback::connectResponse(int32_t token, SapConnectRsp /*sapConnectRsp*/,
+        int32_t /*maxMsgSize*/) {
+    sapResponseToken = token;
+    parent.notify();
+    return Void();
+}
+
+Return<void> SapCallback::disconnectResponse(int32_t token) {
+    sapResponseToken = token;
+    parent.notify();
+    return Void();
+}
+
+Return<void> SapCallback::disconnectIndication(int32_t /*token*/,
+        SapDisconnectType /*disconnectType*/) {
+    return Void();
+}
+
+Return<void> SapCallback::apduResponse(int32_t token, SapResultCode resultCode,
+        const ::android::hardware::hidl_vec<uint8_t>& /*apduRsp*/) {
+    sapResponseToken = token;
+    sapResultCode = resultCode;
+    parent.notify();
+    return Void();
+}
+
+Return<void> SapCallback::transferAtrResponse(int32_t token, SapResultCode resultCode,
+        const ::android::hardware::hidl_vec<uint8_t>& /*atr*/) {
+    sapResponseToken = token;
+    sapResultCode = resultCode;
+    parent.notify();
+    return Void();
+}
+
+Return<void> SapCallback::powerResponse(int32_t token, SapResultCode resultCode) {
+    sapResponseToken = token;
+    sapResultCode = resultCode;
+    parent.notify();
+    return Void();
+}
+
+Return<void> SapCallback::resetSimResponse(int32_t token, SapResultCode resultCode) {
+    sapResponseToken = token;
+    sapResultCode = resultCode;
+    parent.notify();
+    return Void();
+}
+
+Return<void> SapCallback::statusIndication(int32_t /*token*/, SapStatus /*status*/) {
+    return Void();
+}
+
+Return<void> SapCallback::transferCardReaderStatusResponse(int32_t token,
+            SapResultCode resultCode, int32_t /*cardReaderStatus*/) {
+    sapResponseToken = token;
+    sapResultCode = resultCode;
+    parent.notify();
+    return Void();
+}
+
+Return<void> SapCallback::errorResponse(int32_t /*token*/) {
+    return Void();
+}
+
+Return<void> SapCallback::transferProtocolResponse(int32_t token,
+            SapResultCode resultCode) {
+    sapResponseToken = token;
+    sapResultCode = resultCode;
+    parent.notify();
+    return Void();
+}
diff --git a/radio/1.0/vts/functional/sap_hidl_hal_api.cpp b/radio/1.0/vts/functional/sap_hidl_hal_api.cpp
new file mode 100644
index 0000000..e806bd7
--- /dev/null
+++ b/radio/1.0/vts/functional/sap_hidl_hal_api.cpp
@@ -0,0 +1,131 @@
+/*
+ * Copyright (C) 2017 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include<sap_hidl_hal_utils.h>
+
+/*
+ * Test ISap.connectReq() for the response returned.
+ */
+TEST_F(SapHidlTest, connectReq) {
+    int32_t token = 0;
+    int32_t maxMsgSize = 100;
+
+    sap->connectReq(++token, maxMsgSize);
+    EXPECT_EQ(std::cv_status::no_timeout, wait());
+    EXPECT_EQ(sapCb->sapResponseToken, token);
+}
+
+/*
+ * Test IRadio.disconnectReq() for the response returned
+ */
+TEST_F(SapHidlTest, disconnectReq) {
+    int32_t token = 0;
+
+    sap->disconnectReq(++token);
+    EXPECT_EQ(std::cv_status::no_timeout, wait());
+    EXPECT_EQ(sapCb->sapResponseToken, token);
+}
+
+/*
+ * Test IRadio.apduReq() for the response returned.
+ */
+TEST_F(SapHidlTest, apduReq) {
+    int32_t token = 0;
+    SapApduType sapApduType = SapApduType::APDU;
+    android::hardware::hidl_vec<uint8_t> command = {};
+
+    sap->apduReq(++token, sapApduType, command);
+    EXPECT_EQ(std::cv_status::no_timeout, wait());
+    EXPECT_EQ(sapCb->sapResponseToken, token);
+
+    ASSERT_TRUE(SapResultCode::CARD_NOT_ACCESSSIBLE == sapCb->sapResultCode ||
+              SapResultCode::CARD_ALREADY_POWERED_OFF == sapCb->sapResultCode ||
+              SapResultCode::CARD_REMOVED == sapCb->sapResultCode);
+}
+
+/*
+ * Test IRadio.transferAtrReq() for the response returned.
+ */
+TEST_F(SapHidlTest, transferAtrReq) {
+    int32_t token = 0;
+
+    sap->transferAtrReq(++token);
+    EXPECT_EQ(std::cv_status::no_timeout, wait());
+    EXPECT_EQ(sapCb->sapResponseToken, token);
+
+    ASSERT_TRUE(SapResultCode::DATA_NOT_AVAILABLE == sapCb->sapResultCode ||
+              SapResultCode::CARD_ALREADY_POWERED_OFF == sapCb->sapResultCode ||
+              SapResultCode::CARD_REMOVED == sapCb->sapResultCode);
+}
+
+/*
+ * Test IRadio.powerReq() for the response returned.
+ */
+TEST_F(SapHidlTest, powerReq) {
+    int32_t token = 0;
+    bool state = true;
+
+    sap->powerReq(++token, state);
+    EXPECT_EQ(std::cv_status::no_timeout, wait());
+    EXPECT_EQ(sapCb->sapResponseToken, token);
+
+    ASSERT_TRUE(SapResultCode::CARD_NOT_ACCESSSIBLE == sapCb->sapResultCode ||
+              SapResultCode::CARD_ALREADY_POWERED_OFF == sapCb->sapResultCode ||
+              SapResultCode::CARD_REMOVED == sapCb->sapResultCode ||
+              SapResultCode::CARD_ALREADY_POWERED_ON == sapCb->sapResultCode);
+}
+
+/*
+ * Test IRadio.resetSimReq() for the response returned.
+ */
+TEST_F(SapHidlTest, resetSimReq) {
+    int32_t token = 0;
+
+    sap->resetSimReq(++token);
+    EXPECT_EQ(std::cv_status::no_timeout, wait());
+    EXPECT_EQ(sapCb->sapResponseToken, token);
+
+    ASSERT_TRUE(SapResultCode::CARD_NOT_ACCESSSIBLE == sapCb->sapResultCode ||
+              SapResultCode::CARD_ALREADY_POWERED_OFF == sapCb->sapResultCode ||
+              SapResultCode::CARD_REMOVED == sapCb->sapResultCode);
+}
+
+/*
+ * Test IRadio.transferCardReaderStatusReq() for the response returned.
+ */
+TEST_F(SapHidlTest, transferCardReaderStatusReq) {
+    int32_t token = 0;
+
+    sap->transferCardReaderStatusReq(++token);
+    EXPECT_EQ(std::cv_status::no_timeout, wait());
+    EXPECT_EQ(sapCb->sapResponseToken, token);
+
+    EXPECT_EQ(SapResultCode::DATA_NOT_AVAILABLE, sapCb->sapResultCode);
+}
+
+/*
+ * Test IRadio.setTransferProtocolReq() for the response returned.
+ */
+TEST_F(SapHidlTest, setTransferProtocolReq) {
+    int32_t token = 0;
+    SapTransferProtocol sapTransferProtocol = SapTransferProtocol::T0;
+
+    sap->setTransferProtocolReq(++token, sapTransferProtocol);
+    EXPECT_EQ(std::cv_status::no_timeout, wait());
+    EXPECT_EQ(sapCb->sapResponseToken, token);
+
+    EXPECT_EQ(SapResultCode::NOT_SUPPORTED, sapCb->sapResultCode);
+}
diff --git a/radio/1.0/vts/functional/sap_hidl_hal_test.cpp b/radio/1.0/vts/functional/sap_hidl_hal_test.cpp
new file mode 100644
index 0000000..a67c5b6
--- /dev/null
+++ b/radio/1.0/vts/functional/sap_hidl_hal_test.cpp
@@ -0,0 +1,54 @@
+/*
+ * Copyright (C) 2017 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include<sap_hidl_hal_utils.h>
+
+void SapHidlTest::SetUp() {
+    sap = ::testing::VtsHalHidlTargetTestBase::getService<ISap>(hidl_string("sap_uim_socket1"));
+    ASSERT_NE(sap, nullptr);
+
+    sapCb = new SapCallback(*this);
+    ASSERT_NE(sapCb, nullptr);
+
+    count = 0;
+
+    sap->setCallback(sapCb);
+}
+
+void SapHidlTest::TearDown() {
+}
+
+void SapHidlTest::notify() {
+    std::unique_lock<std::mutex> lock(mtx);
+    count++;
+    cv.notify_one();
+}
+
+std::cv_status SapHidlTest::wait() {
+    std::unique_lock<std::mutex> lock(mtx);
+
+    std::cv_status status = std::cv_status::no_timeout;
+    auto now = std::chrono::system_clock::now();
+    while (count == 0) {
+        status = cv.wait_until(lock, now + std::chrono::seconds(TIMEOUT_PERIOD));
+        if (status == std::cv_status::timeout) {
+            return status;
+        }
+    }
+    count--;
+    return status;
+}
+
diff --git a/radio/1.0/vts/functional/sap_hidl_hal_utils.h b/radio/1.0/vts/functional/sap_hidl_hal_utils.h
new file mode 100644
index 0000000..e3efa50
--- /dev/null
+++ b/radio/1.0/vts/functional/sap_hidl_hal_utils.h
@@ -0,0 +1,109 @@
+/*
+ * Copyright (C) 2017 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include <android-base/logging.h>
+
+#include <VtsHalHidlTargetTestBase.h>
+#include <chrono>
+#include <condition_variable>
+#include <mutex>
+
+#include <android/hardware/radio/1.0/ISap.h>
+#include <android/hardware/radio/1.0/ISapCallback.h>
+#include <android/hardware/radio/1.0/types.h>
+
+using namespace ::android::hardware::radio::V1_0;
+
+using ::android::hardware::hidl_string;
+using ::android::hardware::hidl_vec;
+using ::android::hardware::Return;
+using ::android::hardware::Void;
+using ::android::sp;
+
+#define TIMEOUT_PERIOD 40
+
+class SapHidlTest;
+
+/* Callback class for sap response */
+class SapCallback : public ISapCallback {
+private:
+    SapHidlTest& parent;
+
+public:
+    SapResultCode sapResultCode;
+    int32_t sapResponseToken;
+
+    SapCallback(SapHidlTest& parent);
+
+    virtual ~SapCallback() = default;
+
+    Return<void> connectResponse(int32_t token, SapConnectRsp sapConnectRsp, int32_t maxMsgSize);
+
+    Return<void> disconnectResponse(int32_t token);
+
+    Return<void> disconnectIndication(int32_t token, SapDisconnectType disconnectType);
+
+    Return<void> apduResponse(int32_t token, SapResultCode resultCode,
+            const ::android::hardware::hidl_vec<uint8_t>& apduRsp);
+
+    Return<void> transferAtrResponse(int32_t token, SapResultCode resultCode,
+            const ::android::hardware::hidl_vec<uint8_t>& atr);
+
+    Return<void> powerResponse(int32_t token, SapResultCode resultCode);
+
+    Return<void> resetSimResponse(int32_t token, SapResultCode resultCode);
+
+    Return<void> statusIndication(int32_t token, SapStatus status);
+
+    Return<void> transferCardReaderStatusResponse(int32_t token, SapResultCode resultCode,
+            int32_t cardReaderStatus);
+
+    Return<void> errorResponse(int32_t token);
+
+    Return<void> transferProtocolResponse(int32_t token, SapResultCode resultCode);
+};
+
+// The main test class for Sap HIDL.
+class SapHidlTest : public ::testing::VtsHalHidlTargetTestBase {
+private:
+    std::mutex mtx;
+    std::condition_variable cv;
+    int count;
+
+public:
+    virtual void SetUp() override;
+
+    virtual void TearDown() override;
+
+    /* Used as a mechanism to inform the test about data/event callback */
+    void notify();
+
+    /* Test code calls this function to wait for response */
+    std::cv_status wait();
+
+    /* Sap service */
+    sp<ISap> sap;
+
+    /* Sap Callback object */
+    sp<SapCallback> sapCb;
+};
+
+// A class for test environment setup
+class SapHidlEnvironment : public ::testing::Environment {
+public:
+    virtual void SetUp() {}
+    virtual void TearDown() {}
+};