Blame opae-libs/tests/xfpga/test_token_list_c.cpp

Packit 534379
// Copyright(c) 2017-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
Packit 534379
Packit 534379
extern "C" {
Packit 534379
#include "token_list_int.h"
Packit 534379
int xfpga_plugin_initialize(void);
Packit 534379
int xfpga_plugin_finalize(void);
Packit 534379
}
Packit 534379
Packit 534379
#include "gtest/gtest.h"
Packit 534379
#include "sysfs_int.h"
Packit 534379
#include "mock/test_system.h"
Packit 534379
#include "types_int.h"
Packit 534379
#include "sysfs_int.h"
Packit 534379
Packit 534379
Packit 534379
extern pthread_mutex_t global_lock;
Packit 534379
Packit 534379
using namespace opae::testing;
Packit 534379
Packit 534379
class token_list_c_p : public ::testing::TestWithParam<std::string> {
Packit 534379
 protected:
Packit 534379
  token_list_c_p() {}
Packit 534379
  virtual ~token_list_c_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
    ASSERT_EQ(xfpga_plugin_initialize(), FPGA_OK);
Packit 534379
Packit 534379
    ASSERT_GT(sysfs_device_count(), 0);
Packit 534379
    sysfs_fpga_region *fme = nullptr;
Packit 534379
    sysfs_fpga_region *port = nullptr;
Packit 534379
    for (int i = 0; i < sysfs_device_count(); ++i) {
Packit 534379
      fme = (fme == nullptr) ? sysfs_get_device(i)->fme : fme;
Packit 534379
      port = (port == nullptr) ? sysfs_get_device(i)->port : port;
Packit 534379
      if (fme && port) break;
Packit 534379
    }
Packit 534379
    ASSERT_NE(fme, nullptr);
Packit 534379
    ASSERT_NE(port, nullptr);
Packit 534379
Packit 534379
    sysfs_fme = std::string(fme->sysfs_path);
Packit 534379
    dev_fme = std::string("/dev/") + std::string(fme->sysfs_name);
Packit 534379
    sysfs_port = std::string(port->sysfs_path);
Packit 534379
    dev_port = std::string("/dev/") + std::string(port->sysfs_name);
Packit 534379
  }
Packit 534379
  virtual void TearDown() override {
Packit 534379
    xfpga_plugin_finalize();
Packit 534379
    token_cleanup();
Packit 534379
    system_->finalize();
Packit 534379
  }
Packit 534379
Packit 534379
  test_platform platform_;
Packit 534379
  test_system* system_;
Packit 534379
  std::string sysfs_fme;
Packit 534379
  std::string dev_fme;
Packit 534379
  std::string sysfs_port;
Packit 534379
  std::string dev_port;
Packit 534379
};
Packit 534379
Packit 534379
TEST_P(token_list_c_p, simple_case) {
Packit 534379
  auto fme = token_add(sysfs_fme.c_str(), dev_fme.c_str());
Packit 534379
  ASSERT_NE(fme, nullptr);
Packit 534379
  auto port = token_add(sysfs_port.c_str(), dev_port.c_str());
Packit 534379
  ASSERT_NE(port, nullptr);
Packit 534379
Packit 534379
  auto parent = token_get_parent(port);
Packit 534379
  EXPECT_EQ(parent, fme);
Packit 534379
Packit 534379
  parent = token_get_parent(fme);
Packit 534379
  EXPECT_EQ(nullptr, parent);
Packit 534379
}
Packit 534379
Packit 534379
TEST_P(token_list_c_p, invalid_mutex) {
Packit 534379
  pthread_mutex_destroy(&global_lock);
Packit 534379
  auto fme = token_add(sysfs_fme.c_str(), dev_fme.c_str());
Packit 534379
  EXPECT_EQ(fme, nullptr);
Packit 534379
  pthread_mutex_init(&global_lock, NULL);
Packit 534379
Packit 534379
  auto port = token_add(sysfs_port.c_str(), dev_port.c_str());
Packit 534379
  ASSERT_NE(port, nullptr);
Packit 534379
Packit 534379
  pthread_mutex_destroy(&global_lock);
Packit 534379
  auto parent = token_get_parent(port);
Packit 534379
  EXPECT_EQ(nullptr, parent);
Packit 534379
  pthread_mutex_init(&global_lock, NULL);
Packit 534379
Packit 534379
  pthread_mutex_destroy(&global_lock);
Packit 534379
  token_cleanup();
Packit 534379
  pthread_mutex_init(&global_lock, NULL);
Packit 534379
  parent = token_get_parent(port);
Packit 534379
  EXPECT_EQ(parent, fme);
Packit 534379
}
Packit 534379
Packit 534379
TEST_P(token_list_c_p, invalid_paths) {
Packit 534379
  // paths missing dot
Packit 534379
  std::string sysfs_fme_invalid =
Packit 534379
      "/sys/class/fpga/intel-fpga-dev/intel-fpga-fme";
Packit 534379
  std::string dev_fme_invalid = "/dev/intel-fpga-fme";
Packit 534379
  std::string sysfs_port_invalid =
Packit 534379
      "/sys/class/fpga/intel-fpga-dev/intel-fpga-port";
Packit 534379
  std::string dev_port_invalid = "/dev/intel-fpga-port";
Packit 534379
  auto fme = token_add(sysfs_fme_invalid.c_str(), dev_fme_invalid.c_str());
Packit 534379
  EXPECT_EQ(fme, nullptr);
Packit 534379
Packit 534379
  // paths with dot, but non-decimal character afterwards
Packit 534379
  sysfs_fme_invalid += ".z";
Packit 534379
  sysfs_port_invalid += ".z";
Packit 534379
  fme = token_add(sysfs_fme_invalid.c_str(), dev_fme_invalid.c_str());
Packit 534379
  EXPECT_EQ(fme, nullptr);
Packit 534379
Packit 534379
  // get a parent of a bogus token
Packit 534379
  _fpga_token *port = new struct _fpga_token;
Packit 534379
  memset(port, 0, sizeof(struct _fpga_token));
Packit 534379
  std::copy(sysfs_port_invalid.begin(), sysfs_port_invalid.end(),
Packit 534379
            &port->sysfspath[0]);
Packit 534379
  auto parent = token_get_parent(port);
Packit 534379
  EXPECT_EQ(parent, nullptr);
Packit 534379
  delete port;
Packit 534379
Packit 534379
  // invalidate malloc
Packit 534379
Packit 534379
  test_system::instance()->invalidate_malloc(0, "token_add");
Packit 534379
  fme = token_add(sysfs_fme.c_str(), dev_fme.c_str());
Packit 534379
  ASSERT_EQ(fme, nullptr);
Packit 534379
}
Packit 534379
Packit 534379
INSTANTIATE_TEST_CASE_P(token_list_c, token_list_c_p,
Packit 534379
                        ::testing::ValuesIn(test_platform::keys(true)));