|
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 |
* test_utils.h
|
|
Packit |
534379 |
*/
|
|
Packit |
534379 |
#pragma once
|
|
Packit |
534379 |
#include <json-c/json.h>
|
|
Packit |
534379 |
#include <regex.h>
|
|
Packit |
534379 |
#include <memory>
|
|
Packit |
534379 |
#include <string>
|
|
Packit |
534379 |
|
|
Packit |
534379 |
namespace opae {
|
|
Packit |
534379 |
namespace testing {
|
|
Packit |
534379 |
|
|
Packit |
534379 |
class match_t {
|
|
Packit |
534379 |
public:
|
|
Packit |
534379 |
typedef std::shared_ptr<match_t> ptr_t;
|
|
Packit |
534379 |
match_t(const std::string &str, const std::vector<std::string> &matches)
|
|
Packit |
534379 |
: string_(str), groups_(matches) {}
|
|
Packit |
534379 |
|
|
Packit |
534379 |
std::vector<std::string> &groups() { return groups_; }
|
|
Packit |
534379 |
|
|
Packit |
534379 |
std::string group(uint32_t idx) { return groups_[idx]; }
|
|
Packit |
534379 |
|
|
Packit |
534379 |
std::string str() { return string_; }
|
|
Packit |
534379 |
|
|
Packit |
534379 |
private:
|
|
Packit |
534379 |
std::string string_;
|
|
Packit |
534379 |
std::vector<std::string> groups_;
|
|
Packit |
534379 |
};
|
|
Packit |
534379 |
|
|
Packit |
534379 |
template <uint64_t _M = 32>
|
|
Packit |
534379 |
class regex {
|
|
Packit |
534379 |
public:
|
|
Packit |
534379 |
typedef std::shared_ptr<regex> ptr_t;
|
|
Packit |
534379 |
|
|
Packit |
534379 |
static regex::ptr_t create(const std::string &pattern, int flags = 0) {
|
|
Packit |
534379 |
regex::ptr_t m(new regex(pattern));
|
|
Packit |
534379 |
if (regcomp(&m->regex_, pattern.c_str(), flags | REG_EXTENDED)) {
|
|
Packit |
534379 |
m.reset();
|
|
Packit |
534379 |
}
|
|
Packit |
534379 |
return m;
|
|
Packit |
534379 |
}
|
|
Packit |
534379 |
|
|
Packit |
534379 |
~regex() { regfree(®ex_); }
|
|
Packit |
534379 |
|
|
Packit |
534379 |
match_t::ptr_t match(const std::string str, int flags = 0) {
|
|
Packit |
534379 |
std::vector<std::string> matches;
|
|
Packit |
534379 |
match_t::ptr_t m;
|
|
Packit |
534379 |
auto res =
|
|
Packit |
534379 |
regexec(®ex_, str.c_str(), matches_.size(), matches_.data(), flags);
|
|
Packit |
534379 |
if (!res) {
|
|
Packit |
534379 |
for (const auto &m : matches_) {
|
|
Packit |
534379 |
if (m.rm_so >= 0) {
|
|
Packit |
534379 |
matches.push_back(str.substr(m.rm_so, m.rm_eo - m.rm_so));
|
|
Packit |
534379 |
} else {
|
|
Packit |
534379 |
break;
|
|
Packit |
534379 |
}
|
|
Packit |
534379 |
}
|
|
Packit |
534379 |
m.reset(new match_t(str, matches));
|
|
Packit |
534379 |
} else {
|
|
Packit |
534379 |
regerror(res, ®ex_, err_, 128);
|
|
Packit |
534379 |
}
|
|
Packit |
534379 |
return m;
|
|
Packit |
534379 |
}
|
|
Packit |
534379 |
|
|
Packit |
534379 |
std::string error() { return std::string(err_); }
|
|
Packit |
534379 |
|
|
Packit |
534379 |
private:
|
|
Packit |
534379 |
regex() = delete;
|
|
Packit |
534379 |
regex(const std::string &pattern) : pattern_(pattern) {}
|
|
Packit |
534379 |
std::string pattern_;
|
|
Packit |
534379 |
regex_t regex_;
|
|
Packit |
534379 |
char err_[128];
|
|
Packit |
534379 |
std::array<regmatch_t, _M> matches_;
|
|
Packit |
534379 |
};
|
|
Packit |
534379 |
|
|
Packit |
534379 |
class jobject {
|
|
Packit |
534379 |
public:
|
|
Packit |
534379 |
jobject() { obj_ = json_object_new_object(); }
|
|
Packit |
534379 |
|
|
Packit |
534379 |
jobject(json_object *obj) { obj_ = obj; }
|
|
Packit |
534379 |
|
|
Packit |
534379 |
jobject(int32_t v) : jobject(json_object_new_int(v)) {}
|
|
Packit |
534379 |
jobject(int64_t v) : jobject(json_object_new_int64(v)) {}
|
|
Packit |
534379 |
jobject(double v) : jobject(json_object_new_double(v)) {}
|
|
Packit |
534379 |
jobject(const char *v) : jobject(json_object_new_string(v)) {}
|
|
Packit |
534379 |
jobject(const std::string &v) : jobject(json_object_new_string(v.c_str())) {}
|
|
Packit |
534379 |
jobject(const std::string &k, jobject o) : jobject() {
|
|
Packit |
534379 |
json_object_object_add(obj_, k.c_str(), o.obj_);
|
|
Packit |
534379 |
}
|
|
Packit |
534379 |
jobject(std::initializer_list<jobject> arr) {
|
|
Packit |
534379 |
obj_ = json_object_new_array();
|
|
Packit |
534379 |
for (auto &o : arr) {
|
|
Packit |
534379 |
json_object_array_add(obj_, o.obj_);
|
|
Packit |
534379 |
}
|
|
Packit |
534379 |
}
|
|
Packit |
534379 |
|
|
Packit |
534379 |
jobject(const jobject &other) {
|
|
Packit |
534379 |
if (&other != this) {
|
|
Packit |
534379 |
obj_ = other.obj_;
|
|
Packit |
534379 |
}
|
|
Packit |
534379 |
}
|
|
Packit |
534379 |
|
|
Packit |
534379 |
jobject &operator=(const jobject &other) {
|
|
Packit |
534379 |
if (&other != this) {
|
|
Packit |
534379 |
obj_ = other.obj_;
|
|
Packit |
534379 |
}
|
|
Packit |
534379 |
return *this;
|
|
Packit |
534379 |
}
|
|
Packit |
534379 |
|
|
Packit |
534379 |
virtual ~jobject() {}
|
|
Packit |
534379 |
|
|
Packit |
534379 |
void put() { json_object_put(obj_); }
|
|
Packit |
534379 |
|
|
Packit |
534379 |
void get() { json_object_get(obj_); }
|
|
Packit |
534379 |
|
|
Packit |
534379 |
virtual jobject &operator()(const std::string &key, jobject j) {
|
|
Packit |
534379 |
json_object_object_add(obj_, key.c_str(), j.obj_);
|
|
Packit |
534379 |
return *this;
|
|
Packit |
534379 |
}
|
|
Packit |
534379 |
|
|
Packit |
534379 |
virtual const char *c_str() { return json_object_to_json_string(obj_); }
|
|
Packit |
534379 |
|
|
Packit |
534379 |
protected:
|
|
Packit |
534379 |
json_object *obj_;
|
|
Packit |
534379 |
};
|
|
Packit |
534379 |
|
|
Packit |
534379 |
|
|
Packit |
534379 |
} // end of namespace testing
|
|
Packit |
534379 |
} // end of namespace opae
|