2017-03-22 22:53:30 +08:00
|
|
|
#include <array>
|
|
|
|
#include <cstring>
|
|
|
|
#include <iostream>
|
2017-08-03 21:26:48 +08:00
|
|
|
#include <random>
|
2017-03-22 22:53:30 +08:00
|
|
|
#include <vector>
|
|
|
|
|
2017-06-21 17:29:13 +08:00
|
|
|
#include <glog/logging.h>
|
|
|
|
|
2017-03-22 22:53:30 +08:00
|
|
|
#include "gtest/gtest.h"
|
|
|
|
|
2017-03-28 18:42:04 +08:00
|
|
|
/**
|
|
|
|
* TODO (mferencevic): document
|
|
|
|
*/
|
2017-03-22 22:53:30 +08:00
|
|
|
class TestSocket {
|
|
|
|
public:
|
|
|
|
TestSocket(int socket) : socket(socket) {}
|
2017-03-28 18:42:04 +08:00
|
|
|
TestSocket(const TestSocket &s) : socket(s.id()){};
|
|
|
|
TestSocket(TestSocket &&other) { *this = std::forward<TestSocket>(other); }
|
2017-03-22 22:53:30 +08:00
|
|
|
|
2017-03-28 18:42:04 +08:00
|
|
|
TestSocket &operator=(TestSocket &&other) {
|
2017-03-22 22:53:30 +08:00
|
|
|
this->socket = other.socket;
|
|
|
|
other.socket = -1;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Close() { socket = -1; }
|
|
|
|
bool IsOpen() { return socket != -1; }
|
|
|
|
|
|
|
|
int id() const { return socket; }
|
|
|
|
|
2017-04-15 21:14:12 +08:00
|
|
|
bool Write(const std::string &str) { return Write(str.c_str(), str.size()); }
|
|
|
|
bool Write(const char *data, size_t len) {
|
2017-03-28 18:42:04 +08:00
|
|
|
return Write(reinterpret_cast<const uint8_t *>(data), len);
|
2017-03-22 22:53:30 +08:00
|
|
|
}
|
2017-04-15 21:14:12 +08:00
|
|
|
bool Write(const uint8_t *data, size_t len) {
|
|
|
|
if (!write_success_) return false;
|
2017-03-28 18:42:04 +08:00
|
|
|
for (size_t i = 0; i < len; ++i) output.push_back(data[i]);
|
2017-04-15 21:14:12 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-06-21 17:29:13 +08:00
|
|
|
void SetWriteSuccess(bool success) { write_success_ = success; }
|
2017-03-22 22:53:30 +08:00
|
|
|
|
|
|
|
std::vector<uint8_t> output;
|
|
|
|
|
|
|
|
protected:
|
|
|
|
int socket;
|
2017-04-15 21:14:12 +08:00
|
|
|
bool write_success_{true};
|
2017-03-22 22:53:30 +08:00
|
|
|
};
|
|
|
|
|
2017-03-28 18:42:04 +08:00
|
|
|
/**
|
|
|
|
* TODO (mferencevic): document
|
|
|
|
*/
|
|
|
|
class TestBuffer {
|
|
|
|
public:
|
|
|
|
TestBuffer(TestSocket &socket) : socket_(socket) {}
|
|
|
|
|
|
|
|
void Write(const uint8_t *data, size_t n) { socket_.Write(data, n); }
|
|
|
|
void Chunk() {}
|
2017-04-15 21:14:12 +08:00
|
|
|
bool Flush() { return true; }
|
2017-03-28 18:42:04 +08:00
|
|
|
|
|
|
|
private:
|
|
|
|
TestSocket &socket_;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* TODO (mferencevic): document
|
|
|
|
*/
|
|
|
|
void PrintOutput(std::vector<uint8_t> &output) {
|
2017-03-22 22:53:30 +08:00
|
|
|
fprintf(stderr, "output: ");
|
2017-03-28 18:42:04 +08:00
|
|
|
for (size_t i = 0; i < output.size(); ++i) {
|
2017-03-22 22:53:30 +08:00
|
|
|
fprintf(stderr, "%02X ", output[i]);
|
|
|
|
}
|
|
|
|
fprintf(stderr, "\n");
|
|
|
|
}
|
|
|
|
|
2017-03-28 18:42:04 +08:00
|
|
|
/**
|
|
|
|
* TODO (mferencevic): document
|
|
|
|
*/
|
|
|
|
void CheckOutput(std::vector<uint8_t> &output, const uint8_t *data,
|
|
|
|
uint64_t len, bool clear = true) {
|
|
|
|
if (clear)
|
|
|
|
ASSERT_EQ(len, output.size());
|
|
|
|
else
|
|
|
|
ASSERT_LE(len, output.size());
|
|
|
|
for (size_t i = 0; i < len; ++i) EXPECT_EQ(output[i], data[i]);
|
|
|
|
if (clear)
|
|
|
|
output.clear();
|
|
|
|
else
|
|
|
|
output.erase(output.begin(), output.begin() + len);
|
2017-03-22 22:53:30 +08:00
|
|
|
}
|
|
|
|
|
2017-03-28 18:42:04 +08:00
|
|
|
/**
|
|
|
|
* TODO (mferencevic): document
|
|
|
|
*/
|
|
|
|
void InitializeData(uint8_t *data, size_t size) {
|
2017-03-22 22:53:30 +08:00
|
|
|
std::random_device rd;
|
|
|
|
std::mt19937 gen(rd());
|
|
|
|
std::uniform_int_distribution<> dis(0, 255);
|
2017-03-28 18:42:04 +08:00
|
|
|
for (size_t i = 0; i < size; ++i) {
|
2017-03-22 22:53:30 +08:00
|
|
|
data[i] = dis(gen);
|
|
|
|
}
|
|
|
|
}
|