Blame external/opae-test/framework/mock/test_system.h

Packit 534379
// Copyright(c) 2017, 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
#ifndef _TEST_SYSTEM_H
Packit 534379
#define _TEST_SYSTEM_H
Packit 534379
Packit 534379
#include <stdio.h>
Packit 534379
#include <dirent.h>
Packit 534379
#include <unistd.h>
Packit 534379
#include <opae/fpga.h>
Packit 534379
#include <stddef.h>
Packit 534379
#include <sched.h>
Packit 534379
#include <map>
Packit 534379
#include <string>
Packit 534379
#include <vector>
Packit 534379
#include <json-c/json.h>
Packit 534379
#include <thread>
Packit 534379
#include <mutex>
Packit 534379
#include <atomic>
Packit 534379
#include "platform/fpga_hw.h"
Packit 534379
#include <glob.h>
Packit 534379
Packit 534379
extern "C" {
Packit 534379
extern void *__libc_malloc(size_t size);
Packit 534379
extern void *__libc_calloc(size_t nmemb, size_t size);
Packit 534379
}
Packit 534379
typedef struct stat stat_t;
Packit 534379
typedef int (*filter_func)(const struct dirent *);
Packit 534379
typedef int (*compare_func)(const struct dirent **, const struct dirent **);
Packit 534379
Packit 534379
namespace opae {
Packit 534379
namespace testing {
Packit 534379
Packit 534379
constexpr size_t KiB(size_t n) { return n * 1024; }
Packit 534379
constexpr size_t MiB(size_t n) { return n * 1024 * KiB(1); }
Packit 534379
Packit 534379
#ifndef UNUSED_PARAM
Packit 534379
#define UNUSED_PARAM(x) ((void)x)
Packit 534379
#endif  // UNUSED_PARAM
Packit 534379
Packit 534379
class mock_object {
Packit 534379
 public:
Packit 534379
  enum type_t { sysfs_attr = 0, fme, afu };
Packit 534379
  mock_object(const std::string &devpath, const std::string &sysclass,
Packit 534379
              uint32_t device_id, type_t type = sysfs_attr);
Packit 534379
  virtual ~mock_object() {}
Packit 534379
Packit 534379
  virtual int ioctl(int request, va_list arg) {
Packit 534379
    UNUSED_PARAM(request);
Packit 534379
    UNUSED_PARAM(arg);
Packit 534379
    throw std::logic_error("not implemented");
Packit 534379
    return 0;
Packit 534379
  }
Packit 534379
Packit 534379
  std::string sysclass() const { return sysclass_; }
Packit 534379
  uint32_t device_id() const { return device_id_; }
Packit 534379
  type_t type() const { return type_; }
Packit 534379
Packit 534379
 private:
Packit 534379
  std::string devpath_;
Packit 534379
  std::string sysclass_;
Packit 534379
  uint32_t device_id_;
Packit 534379
  type_t type_;
Packit 534379
};
Packit 534379
Packit 534379
class mock_fme : public mock_object {
Packit 534379
 public:
Packit 534379
  mock_fme(const std::string &devpath, const std::string &sysclass,
Packit 534379
           uint32_t device_id)
Packit 534379
      : mock_object(devpath, sysclass, device_id, fme) {}
Packit 534379
  virtual int ioctl(int request, va_list argp) override;
Packit 534379
};
Packit 534379
Packit 534379
class mock_port : public mock_object {
Packit 534379
 public:
Packit 534379
  mock_port(const std::string &devpath, const std::string &sysclass,
Packit 534379
            uint32_t device_id)
Packit 534379
      : mock_object(devpath, sysclass, device_id, fme) {}
Packit 534379
  virtual int ioctl(int request, va_list argp) override;
Packit 534379
};
Packit 534379
Packit 534379
template <int _R, long _E>
Packit 534379
static int dummy_ioctl(mock_object *, int, va_list) {
Packit 534379
  errno = _E;
Packit 534379
  return _R;
Packit 534379
}
Packit 534379
Packit 534379
class test_system {
Packit 534379
 public:
Packit 534379
  typedef int (*ioctl_handler_t)(mock_object *, int, va_list);
Packit 534379
  static test_system *instance();
Packit 534379
Packit 534379
  void set_root(const char *root);
Packit 534379
  std::string get_root();
Packit 534379
  std::string get_sysfs_path(const std::string &src;;
Packit 534379
  std::vector<uint8_t> assemble_gbs_header(const test_device &td);
Packit 534379
  std::vector<uint8_t> assemble_gbs_header(const test_device &td, const char* mdata);
Packit 534379
Packit 534379
  void initialize();
Packit 534379
  void finalize();
Packit 534379
  void prepare_syfs(const test_platform &platform);
Packit 534379
  int remove_sysfs();
Packit 534379
  int remove_sysfs_dir(const char *path = nullptr);
Packit 534379
  std::string get_sysfs_claass_path(const std::string &path);
Packit 534379
Packit 534379
  int open(const std::string &path, int flags);
Packit 534379
  int open(const std::string &path, int flags, mode_t m);
Packit 534379
  void invalidate_read(uint32_t after=0, const char *when_called_from=nullptr);
Packit 534379
  ssize_t read(int fd, void *buf, size_t count);
Packit 534379
Packit 534379
  FILE * fopen(const std::string &path, const std::string &mode);
Packit 534379
Packit 534379
  FILE * popen(const std::string &cmd, const std::string &type);
Packit 534379
  int pclose(FILE *stream);
Packit 534379
Packit 534379
  int close(int fd);
Packit 534379
  int ioctl(int fd, unsigned long request, va_list argp);
Packit 534379
Packit 534379
  DIR *opendir(const char *name);
Packit 534379
  ssize_t readlink(const char *path, char *buf, size_t bufsize);
Packit 534379
  int xstat(int ver, const char *path, stat_t *buf);
Packit 534379
  int lstat(int ver, const char *path, stat_t *buf);
Packit 534379
  int scandir(const char *dirp, struct dirent ***namelist, filter_func filter,
Packit 534379
              compare_func cmp);
Packit 534379
  int sched_setaffinity(pid_t pid, size_t cpusetsize,
Packit 534379
                        const cpu_set_t *mask);
Packit 534379
                        
Packit 534379
  int glob(const char *pattern, int flags,
Packit 534379
                int (*errfunc) (const char *epath, int eerrno),
Packit 534379
                glob_t *pglob);
Packit 534379
Packit 534379
  char *realpath(const char *inp, char *dst);
Packit 534379
                
Packit 534379
  void hijack_sched_setaffinity(int return_val, uint32_t after=0,
Packit 534379
                                const char *when_called_from=nullptr);
Packit 534379
Packit 534379
  void invalidate_malloc(uint32_t after=0, const char *when_called_from=nullptr);
Packit 534379
  void invalidate_calloc(uint32_t after=0, const char *when_called_from=nullptr);
Packit 534379
Packit 534379
  bool default_ioctl_handler(int request, ioctl_handler_t);
Packit 534379
  bool register_ioctl_handler(int request, ioctl_handler_t);
Packit 534379
Packit 534379
  FILE *register_file(const std::string &path);
Packit 534379
Packit 534379
  void normalize_guid(std::string &guid_str, bool with_hyphens = true);
Packit 534379
Packit 534379
 private:
Packit 534379
  test_system();
Packit 534379
  std::mutex fds_mutex_;
Packit 534379
  std::atomic_bool initialized_;
Packit 534379
  std::string root_;
Packit 534379
  std::map<int, mock_object *> fds_;
Packit 534379
  std::map<int, ioctl_handler_t> default_ioctl_handlers_;
Packit 534379
  std::map<int, ioctl_handler_t> ioctl_handlers_;
Packit 534379
  std::map<std::string, std::string> registered_files_;
Packit 534379
  std::map<FILE * , std::string> popen_requests_;
Packit 534379
  static test_system *instance_;
Packit 534379
Packit 534379
  typedef int (*open_func)(const char *pathname, int flags, ...);
Packit 534379
  typedef ssize_t (*read_func)(int fd, void *buf, size_t count);
Packit 534379
  typedef FILE * (*fopen_func)(const char *path, const char *mode);
Packit 534379
  typedef FILE * (*popen_func)(const char *cmd, const char *type);
Packit 534379
  typedef int (*pclose_func)(FILE *stream);
Packit 534379
  typedef int (*close_func)(int fd);
Packit 534379
  typedef int (*ioctl_func)(int fd, unsigned long request, char *argp);
Packit 534379
  typedef DIR *(*opendir_func)(const char *name);
Packit 534379
  typedef ssize_t (*readlink_func)(const char *pathname, char *buf,
Packit 534379
                                   size_t bufsiz);
Packit 534379
  typedef int (*__xstat_func)(int ver, const char *pathname, struct stat *buf);
Packit 534379
  typedef int (*scandir_func)(const char *, struct dirent ***, filter_func,
Packit 534379
                              compare_func);
Packit 534379
  typedef int (*sched_setaffinity_func)(pid_t pid, size_t cpusetsize,
Packit 534379
                                        const cpu_set_t *mask);
Packit 534379
  typedef char *(*realpath_func)(const char *, char *);
Packit 534379
Packit 534379
  typedef int (*glob_func)(const char *pattern, int flags,
Packit 534379
                           int (*errfunc)(const char *epath, int eerrno),
Packit 534379
                           glob_t *pglob);
Packit 534379
Packit 534379
  open_func open_;
Packit 534379
  open_func open_create_;
Packit 534379
  read_func read_;
Packit 534379
  fopen_func fopen_;
Packit 534379
  popen_func popen_;
Packit 534379
  pclose_func pclose_;
Packit 534379
  close_func close_;
Packit 534379
  ioctl_func ioctl_;
Packit 534379
  opendir_func opendir_;
Packit 534379
  readlink_func readlink_;
Packit 534379
  __xstat_func xstat_;
Packit 534379
  __xstat_func lstat_;
Packit 534379
  scandir_func scandir_;
Packit 534379
  sched_setaffinity_func sched_setaffinity_;
Packit 534379
  glob_func glob_;
Packit 534379
  realpath_func realpath_;
Packit 534379
Packit 534379
  bool hijack_sched_setaffinity_;
Packit 534379
  int hijack_sched_setaffinity_return_val_;
Packit 534379
  uint32_t hijack_sched_setaffinity_after_;
Packit 534379
  const char * hijack_sched_setaffinity_caller_;
Packit 534379
};
Packit 534379
Packit 534379
}  // end of namespace testing
Packit 534379
}  // end of namespace opae
Packit 534379
Packit 534379
#endif /* !_TEST_SYSTEM_H */