Blame tests/hello_fpga/test_hello_fpga_c.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
Packit 534379
#include <opae/fpga.h>
Packit 534379
Packit 534379
extern "C" {
Packit 534379
Packit 534379
#include <json-c/json.h>
Packit 534379
#include <uuid/uuid.h>
Packit 534379
Packit 534379
void print_err(const char *s, fpga_result res);
Packit 534379
Packit 534379
struct config {
Packit 534379
  struct target {
Packit 534379
    int bus;
Packit 534379
  } target;
Packit 534379
  int open_flags;
Packit 534379
};
Packit 534379
extern struct config config;
Packit 534379
Packit 534379
fpga_result parse_args(int argc, char *argv[]);
Packit 534379
Packit 534379
fpga_result find_fpga(fpga_guid afu_guid,
Packit 534379
                      fpga_token *accelerator_token,
Packit 534379
                      uint32_t *num_matches_accelerators);
Packit 534379
Packit 534379
fpga_result get_bus(fpga_token tok, uint8_t *bus);
Packit 534379
Packit 534379
int hello_fpga_main(int argc, char *argv[]);
Packit 534379
Packit 534379
}
Packit 534379
Packit 534379
#define INVALID_AFU_ID "00000000-0000-0000-0000-000000000000"
Packit 534379
Packit 534379
#include <config.h>
Packit 534379
Packit 534379
#include <iostream>
Packit 534379
#include <vector>
Packit 534379
#include <cstdio>
Packit 534379
#include <cstdlib>
Packit 534379
#include <cstring>
Packit 534379
#include <errno.h>
Packit 534379
#include <unistd.h>
Packit 534379
#include <getopt.h>
Packit 534379
#include <stdarg.h>
Packit 534379
#include "gtest/gtest.h"
Packit 534379
#include "mock/test_system.h"
Packit 534379
#include <linux/ioctl.h>
Packit 534379
#include "intel-fpga.h"
Packit 534379
#include "fpga-dfl.h"
Packit 534379
Packit 534379
using namespace opae::testing;
Packit 534379
Packit 534379
int mmio_ioctl(mock_object * m, int request, va_list argp){
Packit 534379
    int retval = -1;
Packit 534379
    errno = EINVAL;
Packit 534379
    UNUSED_PARAM(m);
Packit 534379
    UNUSED_PARAM(request);
Packit 534379
    struct fpga_port_region_info *rinfo = va_arg(argp, struct fpga_port_region_info *);
Packit 534379
    if (!rinfo) {
Packit 534379
      FPGA_MSG("rinfo is NULL");
Packit 534379
      goto out_EINVAL;
Packit 534379
    }
Packit 534379
    if (rinfo->argsz != sizeof(*rinfo)) {
Packit 534379
      FPGA_MSG("wrong structure size");
Packit 534379
      goto out_EINVAL;
Packit 534379
    }
Packit 534379
    if (rinfo->index > 1 ) {
Packit 534379
      FPGA_MSG("unsupported MMIO index");
Packit 534379
      goto out_EINVAL;
Packit 534379
    }
Packit 534379
    if (rinfo->padding != 0) {
Packit 534379
      FPGA_MSG("unsupported padding");
Packit 534379
      goto out_EINVAL;
Packit 534379
    }
Packit 534379
    rinfo->flags = FPGA_REGION_READ | FPGA_REGION_WRITE | FPGA_REGION_MMAP;
Packit 534379
    rinfo->size = 0x40000;
Packit 534379
    rinfo->offset = 0;
Packit 534379
    retval = 0;
Packit 534379
    errno = 0;
Packit 534379
out:
Packit 534379
    return retval;
Packit 534379
Packit 534379
out_EINVAL:
Packit 534379
    retval = -1;
Packit 534379
    errno = EINVAL;
Packit 534379
    goto out;
Packit 534379
}
Packit 534379
Packit 534379
class hello_fpga_c_p : public ::testing::TestWithParam<std::string> {
Packit 534379
 protected:
Packit 534379
  hello_fpga_c_p() {}
Packit 534379
Packit 534379
  virtual void SetUp() override {
Packit 534379
    std::string platform_key = GetParam();
Packit 534379
    ASSERT_TRUE(test_platform::exists(platform_key));
Packit 534379
    platform_ = test_platform::get(platform_key);
Packit 534379
    system_ = test_system::instance();
Packit 534379
    system_->initialize();
Packit 534379
    system_->prepare_syfs(platform_);
Packit 534379
Packit 534379
    EXPECT_EQ(fpgaInitialize(NULL), FPGA_OK);
Packit 534379
Packit 534379
    optind = 0;
Packit 534379
    config_ = config;
Packit 534379
  }
Packit 534379
Packit 534379
  virtual void TearDown() override {
Packit 534379
    config = config_;
Packit 534379
    fpgaFinalize();
Packit 534379
    system_->finalize();
Packit 534379
  }
Packit 534379
Packit 534379
  struct config config_;
Packit 534379
  test_platform platform_;
Packit 534379
  test_system *system_;
Packit 534379
};
Packit 534379
Packit 534379
/**
Packit 534379
 * @test       print_err
Packit 534379
 * @brief      Test: print_err
Packit 534379
 * @details    print_err prints the given string and
Packit 534379
 *             the decoded representation of the fpga_result
Packit 534379
 *             to stderr.
Packit 534379
 */
Packit 534379
TEST_P(hello_fpga_c_p, print_err) {
Packit 534379
  print_err("msg", FPGA_OK);
Packit 534379
}
Packit 534379
Packit 534379
/**
Packit 534379
 * @test       parse_args0
Packit 534379
 * @brief      Test: parse_args
Packit 534379
 * @details    When passed an invalid command option,
Packit 534379
 *             parse_args prints a message and
Packit 534379
 *             returns a value other than FPGA_OK.
Packit 534379
 */
Packit 534379
TEST_P(hello_fpga_c_p, parse_args0) {
Packit 534379
  char zero[20];
Packit 534379
  char one[20];
Packit 534379
  strcpy(zero, "hello_fpga");
Packit 534379
  strcpy(one, "-Y");
Packit 534379
Packit 534379
  char *argv[] = { zero, one };
Packit 534379
Packit 534379
  EXPECT_NE(parse_args(2, argv), FPGA_OK);
Packit 534379
}
Packit 534379
Packit 534379
/**
Packit 534379
 * @test       parse_args1
Packit 534379
 * @brief      Test: parse_args
Packit 534379
 * @details    When given a command option that requires a param,
Packit 534379
 *             omitting the required param causes parse_args to
Packit 534379
 *             return a value other than FPGA_OK.
Packit 534379
 */
Packit 534379
TEST_P(hello_fpga_c_p, parse_args1) {
Packit 534379
  char zero[20];
Packit 534379
  char one[20];
Packit 534379
  strcpy(zero, "hello_fpga");
Packit 534379
  strcpy(one, "-B");
Packit 534379
Packit 534379
  char *argv[] = { zero, one };
Packit 534379
Packit 534379
  EXPECT_NE(parse_args(2, argv), FPGA_OK);
Packit 534379
}
Packit 534379
Packit 534379
/**
Packit 534379
 * @test       parse_args2
Packit 534379
 * @brief      Test: parse_args
Packit 534379
 * @details    When given valid command options,
Packit 534379
 *             parse_args populates the global config struct,
Packit 534379
 *             and returns FPGA_OK.
Packit 534379
 */
Packit 534379
TEST_P(hello_fpga_c_p, parse_args2) {
Packit 534379
  char zero[20];
Packit 534379
  char one[20];
Packit 534379
  char two[20];
Packit 534379
  char three[20];
Packit 534379
  strcpy(zero, "hello_fpga");
Packit 534379
  strcpy(one, "-B");
Packit 534379
  strcpy(two, "3");
Packit 534379
  strcpy(three, "-s");
Packit 534379
Packit 534379
  char *argv[] = { zero, one, two, three };
Packit 534379
Packit 534379
  EXPECT_EQ(parse_args(4, argv), FPGA_OK);
Packit 534379
  EXPECT_EQ(config.target.bus, 3);
Packit 534379
  EXPECT_EQ(config.open_flags, FPGA_OPEN_SHARED);
Packit 534379
}
Packit 534379
Packit 534379
/**
Packit 534379
 * @test       find_fpga0
Packit 534379
 * @brief      Test: find_fpga
Packit 534379
 * @details    When passed a guid that matches no device,
Packit 534379
 *             find_fpga sets *num_matches_accelerators to 0,
Packit 534379
 *             and the fn returns FPGA_OK.
Packit 534379
 */
Packit 534379
TEST_P(hello_fpga_c_p, find_fpga0) {
Packit 534379
  fpga_guid guid;
Packit 534379
  fpga_token tok = nullptr;
Packit 534379
  uint32_t matches = 0xff;
Packit 534379
Packit 534379
  config.target.bus = platform_.devices[0].bus;
Packit 534379
Packit 534379
  ASSERT_EQ(uuid_parse(INVALID_AFU_ID, guid), 0);
Packit 534379
  EXPECT_EQ(find_fpga(guid, &tok, &matches), FPGA_OK);
Packit 534379
  EXPECT_EQ(tok, nullptr);
Packit 534379
  EXPECT_EQ(matches, 0);
Packit 534379
}
Packit 534379
Packit 534379
/**
Packit 534379
 * @test       get_bus0
Packit 534379
 * @brief      Test: get_bus
Packit 534379
 * @details    When passed a valid fpga_token,
Packit 534379
 *             get_bus retrieves the associated bus into *bus,
Packit 534379
 *             and the fn returns FPGA_OK.
Packit 534379
 */
Packit 534379
TEST_P(hello_fpga_c_p, get_bus0) {
Packit 534379
  fpga_guid guid;
Packit 534379
  fpga_token tok = nullptr;
Packit 534379
  uint32_t matches = 0xff;
Packit 534379
Packit 534379
  config.target.bus = platform_.devices[0].bus;
Packit 534379
Packit 534379
  ASSERT_EQ(uuid_parse(platform_.devices[0].afu_guid, guid), 0);
Packit 534379
  EXPECT_EQ(find_fpga(guid, &tok, &matches), FPGA_OK);
Packit 534379
  ASSERT_NE(tok, nullptr);
Packit 534379
  ASSERT_GT(matches, 0);
Packit 534379
Packit 534379
  uint8_t bus = 0xff;
Packit 534379
  EXPECT_EQ(get_bus(tok, &bus), FPGA_OK);
Packit 534379
  EXPECT_EQ(bus, platform_.devices[0].bus);
Packit 534379
Packit 534379
  EXPECT_EQ(fpgaDestroyToken(&tok), FPGA_OK);
Packit 534379
}
Packit 534379
Packit 534379
/**
Packit 534379
 * @test       main0
Packit 534379
 * @brief      Test: hello_fpga_main
Packit 534379
 * @details    When given an invalid command option,
Packit 534379
 *             hello_fpga_main displays an error message,
Packit 534379
 *             and the fn returns non-zero.
Packit 534379
 */
Packit 534379
TEST_P(hello_fpga_c_p, main0) {
Packit 534379
  char zero[20];
Packit 534379
  char one[20];
Packit 534379
  strcpy(zero, "hello_fpga");
Packit 534379
  strcpy(one, "-Y");
Packit 534379
Packit 534379
  char *argv[] = { zero, one };
Packit 534379
Packit 534379
  EXPECT_NE(hello_fpga_main(2, argv), 0);
Packit 534379
}
Packit 534379
Packit 534379
INSTANTIATE_TEST_CASE_P(hello_fpga_c, hello_fpga_c_p,
Packit 534379
                        ::testing::ValuesIn(test_platform::keys(true)));
Packit 534379
Packit 534379
class mock_hello_fpga_c_p : public hello_fpga_c_p {
Packit 534379
 protected:
Packit 534379
  mock_hello_fpga_c_p() {}
Packit 534379
};
Packit 534379
Packit 534379
/**
Packit 534379
 * @test       main1
Packit 534379
 * @brief      Test: hello_fpga_main
Packit 534379
 * @details    When given a valid command line,
Packit 534379
 *             hello_fpga_main (mock) runs the NLB0 workload.
Packit 534379
 *             The workload times out in a mock environment,
Packit 534379
 *             causing hello_fpga_main to return FPGA_EXCEPTION.
Packit 534379
 */
Packit 534379
TEST_P(mock_hello_fpga_c_p, main1) {
Packit 534379
  char zero[20];
Packit 534379
  char one[20];
Packit 534379
  char two[20];
Packit 534379
  strcpy(zero, "hello_fpga");
Packit 534379
  strcpy(one, "-B");
Packit 534379
  sprintf(two, "%d", platform_.devices[0].bus);
Packit 534379
Packit 534379
  char *argv[] = { zero, one, two };
Packit 534379
Packit 534379
  system_->register_ioctl_handler(FPGA_PORT_GET_REGION_INFO, mmio_ioctl);
Packit 534379
  system_->register_ioctl_handler(DFL_FPGA_PORT_GET_REGION_INFO, mmio_ioctl);
Packit 534379
Packit 534379
  EXPECT_EQ(hello_fpga_main(3, argv), FPGA_EXCEPTION);
Packit 534379
}
Packit 534379
Packit 534379
INSTANTIATE_TEST_CASE_P(mock_hello_fpga_c, mock_hello_fpga_c_p,
Packit 534379
                        ::testing::ValuesIn(test_platform::mock_platforms({"skx-p", "dcp-rc"})));
Packit 534379
Packit 534379
class hw_hello_fpga_c_p : public mock_hello_fpga_c_p {
Packit 534379
 protected:
Packit 534379
  hw_hello_fpga_c_p() {}
Packit 534379
};
Packit 534379
Packit 534379
/**
Packit 534379
 * @test       main1
Packit 534379
 * @brief      Test: hello_fpga_main
Packit 534379
 * @details    When given a valid command line,
Packit 534379
 *             hello_fpga_main (hw) runs the NLB0 workload,
Packit 534379
 *             and the fn returns FPGA_OK.
Packit 534379
 */
Packit 534379
TEST_P(hw_hello_fpga_c_p, main1) {
Packit 534379
  char zero[20];
Packit 534379
  char one[20];
Packit 534379
  char two[20];
Packit 534379
  strcpy(zero, "hello_fpga");
Packit 534379
  strcpy(one, "-B");
Packit 534379
  sprintf(two, "%d", platform_.devices[0].bus);
Packit 534379
Packit 534379
  char *argv[] = { zero, one, two };
Packit 534379
Packit 534379
  EXPECT_EQ(hello_fpga_main(3, argv), FPGA_OK);
Packit 534379
}
Packit 534379
Packit 534379
INSTANTIATE_TEST_CASE_P(hw_hello_fpga_c, hw_hello_fpga_c_p,
Packit 534379
                        ::testing::ValuesIn(test_platform::hw_platforms({"skx-p","dcp-rc"})));