Blame opae-libs/tests/opae-cxx/test_object_cxx_core.cpp

Packit 534379
// Copyright(c) 2018, Intel Corporation
Packit 534379
//
Packit 534379
// Redistribution  and  use  in source  and  binary  forms,  with  or  without
Packit 534379
// modification, are permitted provided that the following conditions are met:
Packit 534379
//
Packit 534379
// * Redistributions of  source code  must retain the  above copyright notice,
Packit 534379
//   this list of conditions and the following disclaimer.
Packit 534379
// * Redistributions in binary form must reproduce the above copyright notice,
Packit 534379
//   this list of conditions and the following disclaimer in the documentation
Packit 534379
//   and/or other materials provided with the distribution.
Packit 534379
// * Neither the name  of Intel Corporation  nor the names of its contributors
Packit 534379
//   may be used to  endorse or promote  products derived  from this  software
Packit 534379
//   without specific prior written permission.
Packit 534379
//
Packit 534379
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
Packit 534379
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING,  BUT NOT LIMITED TO,  THE
Packit 534379
// IMPLIED WARRANTIES OF  MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
Packit 534379
// ARE DISCLAIMED.  IN NO EVENT  SHALL THE COPYRIGHT OWNER  OR CONTRIBUTORS BE
Packit 534379
// LIABLE  FOR  ANY  DIRECT,  INDIRECT,  INCIDENTAL,  SPECIAL,  EXEMPLARY,  OR
Packit 534379
// CONSEQUENTIAL  DAMAGES  (INCLUDING,  BUT  NOT LIMITED  TO,  PROCUREMENT  OF
Packit 534379
// SUBSTITUTE GOODS OR SERVICES;  LOSS OF USE,  DATA, OR PROFITS;  OR BUSINESS
Packit 534379
// INTERRUPTION)  HOWEVER CAUSED  AND ON ANY THEORY  OF LIABILITY,  WHETHER IN
Packit 534379
// CONTRACT,  STRICT LIABILITY,  OR TORT  (INCLUDING NEGLIGENCE  OR OTHERWISE)
Packit 534379
// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,  EVEN IF ADVISED OF THE
Packit 534379
// POSSIBILITY OF SUCH DAMAGE.
Packit 534379
#include <opae/cxx/core/sysobject.h>
Packit 534379
Packit 534379
#include <array>
Packit 534379
#include <string>
Packit 534379
#include <vector>
Packit 534379
#include "gtest/gtest.h"
Packit 534379
#include "mock/test_system.h"
Packit 534379
Packit 534379
using namespace opae::testing;
Packit 534379
using namespace opae::fpga::types;
Packit 534379
Packit 534379
class sysobject_cxx_p : public ::testing::TestWithParam<std::string> {
Packit 534379
 protected:
Packit 534379
  sysobject_cxx_p() {}
Packit 534379
Packit 534379
  virtual void SetUp() override {
Packit 534379
    ASSERT_TRUE(test_platform::exists(GetParam()));
Packit 534379
    platform_ = test_platform::get(GetParam());
Packit 534379
    system_ = test_system::instance();
Packit 534379
    system_->initialize();
Packit 534379
    system_->prepare_syfs(platform_);
Packit 534379
    invalid_device_ = test_device::unknown();
Packit 534379
Packit 534379
    ASSERT_EQ(fpgaInitialize(NULL), FPGA_OK);
Packit 534379
Packit 534379
    properties::ptr_t props = properties::get(FPGA_ACCELERATOR);
Packit 534379
    props->device_id = platform_.devices[0].device_id;
Packit 534379
Packit 534379
    tokens_ = token::enumerate({props});
Packit 534379
    ASSERT_GT(tokens_.size(), 0);
Packit 534379
    handle_ = handle::open(tokens_[0], 0);
Packit 534379
    ASSERT_NE(handle_.get(), nullptr);
Packit 534379
Packit 534379
    properties::ptr_t props_dev = properties::get(FPGA_DEVICE);
Packit 534379
    props_dev->device_id = platform_.devices[0].device_id;
Packit 534379
Packit 534379
    tokens_dev_ = token::enumerate({props_dev});
Packit 534379
    ASSERT_GT(tokens_dev_.size(), 0);
Packit 534379
    handle_dev_ = handle::open(tokens_dev_[0], 0);
Packit 534379
    ASSERT_NE(handle_dev_.get(), nullptr);
Packit 534379
  }
Packit 534379
Packit 534379
  virtual void TearDown() override {
Packit 534379
    tokens_.clear();
Packit 534379
    tokens_dev_.clear();
Packit 534379
    if (handle_.get())
Packit 534379
      handle_->close();
Packit 534379
    handle_.reset();
Packit 534379
    handle_dev_.reset();
Packit 534379
    fpgaFinalize();
Packit 534379
    system_->finalize();
Packit 534379
  }
Packit 534379
Packit 534379
  std::vector<token::ptr_t> tokens_;
Packit 534379
  std::vector<token::ptr_t> tokens_dev_;
Packit 534379
  handle::ptr_t handle_;
Packit 534379
  handle::ptr_t handle_dev_;
Packit 534379
  test_platform platform_;
Packit 534379
  test_device invalid_device_;
Packit 534379
  test_system *system_;
Packit 534379
};
Packit 534379
Packit 534379
/**
Packit 534379
 * @btest token_object
Packit 534379
 * Given an enumerated token object
Packit 534379
 * When I get the afui_id as an object from
Packit 534379
 * the token
Packit 534379
 * And I  get the object's buffer using the bytes function
Packit 534379
 * Then the normalized GUID is the same as the normalized GUID of the test
Packit 534379
 * platform.
Packit 534379
 */
Packit 534379
TEST_P(sysobject_cxx_p, token_object) {
Packit 534379
  auto obj = sysobject::get(tokens_[0], "afu_id");
Packit 534379
  ASSERT_NE(obj.get(), nullptr);
Packit 534379
  auto bytes = obj->bytes();
Packit 534379
  ASSERT_NE(bytes.size(), 0);
Packit 534379
  auto guid_read = std::string(bytes.begin(), bytes.end());
Packit 534379
  auto afu_guid = std::string(platform_.devices[0].afu_guid);
Packit 534379
  system_->normalize_guid(guid_read);
Packit 534379
  system_->normalize_guid(afu_guid);
Packit 534379
  ASSERT_STREQ(afu_guid.c_str(), guid_read.c_str());
Packit 534379
}
Packit 534379
Packit 534379
/**
Packit 534379
 * @btest handle_object
Packit 534379
 * Given an open handle object
Packit 534379
 * When I get the afui_id as an object from
Packit 534379
 * the handle
Packit 534379
 * And I  get the object's buffer using the bytes function
Packit 534379
 * Then the normalized GUID is the same as the normalized GUID of the test
Packit 534379
 * platform.
Packit 534379
 */
Packit 534379
TEST_P(sysobject_cxx_p, handle_object) {
Packit 534379
  auto obj = sysobject::get(handle_, "afu_id");
Packit 534379
  ASSERT_NE(obj.get(), nullptr);
Packit 534379
  auto bytes = obj->bytes();
Packit 534379
  ASSERT_NE(bytes.size(), 0);
Packit 534379
  auto guid_read = std::string(bytes.begin(), bytes.end());
Packit 534379
  auto afu_guid = std::string(platform_.devices[0].afu_guid);
Packit 534379
  system_->normalize_guid(guid_read);
Packit 534379
  system_->normalize_guid(afu_guid);
Packit 534379
  ASSERT_STREQ(afu_guid.c_str(), guid_read.c_str());
Packit 534379
}
Packit 534379
Packit 534379
/**
Packit 534379
 * @btest handle_object_write
Packit 534379
 * Given an open handle object
Packit 534379
 * When I get a suboject from the handle
Packit 534379
 * And I  write a 64-bit value to it using its write64 function
Packit 534379
 * Then no exceptions are thrown
Packit 534379
 */
Packit 534379
TEST_P(sysobject_cxx_p, handle_object_write) {
Packit 534379
  std::string path = "iperf/fabric/freeze";;
Packit 534379
Packit 534379
  if (platform_.devices[0].device_id == 0x09c4 || 
Packit 534379
      platform_.devices[0].device_id == 0x09c5 ||
Packit 534379
      platform_.devices[0].device_id == 0x0b30) {
Packit 534379
    path = "dperf/fabric/freeze";
Packit 534379
  }
Packit 534379
Packit 534379
  auto obj = sysobject::get(handle_dev_, path);
Packit 534379
  ASSERT_NE(obj.get(), nullptr);
Packit 534379
  EXPECT_NO_THROW(obj->write64(0x1));
Packit 534379
  EXPECT_NO_THROW(obj->write64(0x0));
Packit 534379
}
Packit 534379
Packit 534379
/**
Packit 534379
 * @btest object_object
Packit 534379
 * Given an object from an enumerated token
Packit 534379
 * And an object from an open handle
Packit 534379
 * When I use read64 from both objects
Packit 534379
 * Then the values are the same
Packit 534379
 */
Packit 534379
TEST_P(sysobject_cxx_p, object_object) {
Packit 534379
  auto t_obj = sysobject::get(tokens_[0], "errors");
Packit 534379
  ASSERT_NE(t_obj.get(), nullptr);
Packit 534379
  auto t_value = t_obj->get("errors")->read64();
Packit 534379
  auto h_obj = sysobject::get(handle_, "errors");
Packit 534379
  ASSERT_NE(h_obj.get(), nullptr);
Packit 534379
  auto h_value = h_obj->get("errors")->read64();
Packit 534379
  EXPECT_EQ(t_value, h_value);
Packit 534379
  EXPECT_EQ(t_obj->get("abc").get(), nullptr);
Packit 534379
  EXPECT_EQ(h_obj->get("abc").get(), nullptr);
Packit 534379
}
Packit 534379
Packit 534379
/**
Packit 534379
 * @btest token_subobject_write
Packit 534379
 * Given an object from an enumerated token
Packit 534379
 * And a suboject from that object
Packit 534379
 * When I use write64 from the token subobject
Packit 534379
 * Then an invalid_param exception is thrown
Packit 534379
 */
Packit 534379
TEST_P(sysobject_cxx_p, token_subobject_write) {
Packit 534379
  auto t_obj = sysobject::get(tokens_[0], "errors");
Packit 534379
  ASSERT_NE(t_obj.get(), nullptr);
Packit 534379
  EXPECT_THROW(t_obj->get("errors")->write64(0x100),
Packit 534379
               opae::fpga::types::invalid_param);
Packit 534379
}
Packit 534379
Packit 534379
/**
Packit 534379
 * @btest handle_subobject_write
Packit 534379
 * Given an object from an open handle
Packit 534379
 * And a suboject from that object
Packit 534379
 * When I use write64 from the token subobject
Packit 534379
 * Then no exceptions are thrown
Packit 534379
 * And the value has changed from its original value
Packit 534379
 */
Packit 534379
TEST_P(sysobject_cxx_p, handle_subobject_write) {
Packit 534379
  std::string path = "iperf/fabric";;
Packit 534379
Packit 534379
  if (platform_.devices[0].device_id == 0x09c4 ||
Packit 534379
      platform_.devices[0].device_id == 0x09c5 ||
Packit 534379
      platform_.devices[0].device_id == 0x0b30) {
Packit 534379
    path = "dperf/fabric";
Packit 534379
  }
Packit 534379
Packit 534379
  auto h_obj = sysobject::get(handle_dev_, path);
Packit 534379
  ASSERT_NE(h_obj.get(), nullptr);
Packit 534379
  ASSERT_NO_THROW(h_obj->get("freeze")->read64(FPGA_OBJECT_SYNC));
Packit 534379
  ASSERT_NO_THROW(h_obj->get("freeze")->write64(0x1));
Packit 534379
  ASSERT_NO_THROW(h_obj->get("freeze")->read64(FPGA_OBJECT_SYNC));
Packit 534379
  ASSERT_NO_THROW(h_obj->get("freeze")->write64(0x0));
Packit 534379
  EXPECT_EQ(h_obj->get("freeze")->read64(FPGA_OBJECT_SYNC), 0x0);
Packit 534379
}
Packit 534379
Packit 534379
/**
Packit 534379
 * @btest read_bytes
Packit 534379
 * Given an enumerated token object
Packit 534379
 * And its afu_id as an object from the token
Packit 534379
 * And I  read an arbitrary number of bytes from an arbitrary offset
Packit 534379
 * Then the string made from those bytes are equal to the string made
Packit 534379
 * from the test_platform afu_id using the same size and offset
Packit 534379
 */
Packit 534379
TEST_P(sysobject_cxx_p, read_bytes) {
Packit 534379
  // get the test platform GUID and normalize it to exclude hyphens
Packit 534379
  std::string test_guid(platform_.devices[0].afu_guid);
Packit 534379
  system_->normalize_guid(test_guid, false);
Packit 534379
Packit 534379
  for (int i = 0; i < test_guid.size(); ++i) {
Packit 534379
    for (int  j = 1; j < test_guid.size() - i; j++) {
Packit 534379
      int offset = i;
Packit 534379
      int size = j;
Packit 534379
      ASSERT_LE(offset + size, test_guid.size());
Packit 534379
Packit 534379
      auto obj = sysobject::get(tokens_[0], "afu_id");
Packit 534379
      // get size bytes starting form the offset
Packit 534379
      auto bytes = obj->bytes(offset, size);
Packit 534379
      // make this a string
Packit 534379
      auto str1 = std::string(bytes.begin(), bytes.end());
Packit 534379
      // make substring from the offset and size used before
Packit 534379
      auto str2 = std::string(test_guid, offset, size);
Packit 534379
      ASSERT_TRUE(std::equal(str1.begin(), str1.end(), str2.begin(),
Packit 534379
                             [](char lhs, char rhs) {
Packit 534379
                               return std::tolower(lhs) == std::tolower(rhs);
Packit 534379
                             }));
Packit 534379
Packit 534379
    }
Packit 534379
Packit 534379
  }
Packit 534379
}
Packit 534379
Packit 534379
INSTANTIATE_TEST_CASE_P(sysobject_cxx, sysobject_cxx_p,
Packit 534379
         ::testing::ValuesIn(test_platform::platforms({ "skx-p","dcp-rc","dcp-vc" })));
Packit 534379