blob: 1e934e901043bc8127b3d52464521f02b49bb88c [file] [log] [blame] [edit]
#include <unistd.h>
#include <filesystem> // NOLINT
#include <fstream>
#include <memory>
#include <string>
#include <system_error> // NOLINT
#include "absl/time/clock.h"
#include "absl/time/time.h"
#include "grpcblob_server.h"
#include "smbios.grpc.pb.h"
#include <gtest/gtest.h>
#include "grpcpp/client_context.h"
#include "grpcpp/create_channel.h"
#include "grpcpp/security/credentials.h"
#include "grpcpp/server_context.h"
#include "grpcpp/support/status.h"
#ifndef EXPECT_OK
#define EXPECT_OK(x) EXPECT_TRUE(x.ok())
#endif
namespace blobs {
namespace {
using ::grpc::Channel;
using ::grpc::ClientContext;
using ::phosphor::smbios::SmbiosTransferRequest;
using ::phosphor::smbios::SmbiosTransferResponse;
using phosphor::smbios::grpc_gen::SmbiosTransferService;
class SmbiosTest : public ::testing::Test {
protected:
static void SetUpTestSuite() {
server_ = std::make_unique<GrpcBlobServer>("/tmp");
server_->start();
std::shared_ptr<Channel> channel = grpc::CreateChannel(
"localhost:10166", grpc::InsecureChannelCredentials());
smbios_stub_ = SmbiosTransferService::NewStub(channel);
}
static void TearDownTestSuite() {
absl::SleepFor(absl::Seconds(5));
server_->stop();
server_.reset();
smbios_stub_.reset();
}
static void createFile(const std::filesystem::path& path) {
std::error_code ec;
std::filesystem::path dirPath = path.parent_path();
// Create the directories recursively
bool created = std::filesystem::create_directories(dirPath, ec);
ASSERT_TRUE(created || !ec) << "Failed to create directory: " << dirPath
<< " Error: " << ec.message();
std::ofstream filename(path);
ASSERT_TRUE(filename.good())
<< "Failed to create file: " << path;
}
static void removeFile(const std::filesystem::path& path) {
std::error_code ec;
if (std::filesystem::exists(path)) {
std::filesystem::remove(path, ec);
ASSERT_FALSE(std::filesystem::exists(path))
<< "Failed to remove file: " << path;
}
}
static std::unique_ptr<GrpcBlobServer> server_;
static std::unique_ptr<SmbiosTransferService::Stub> smbios_stub_;
};
std::unique_ptr<GrpcBlobServer> SmbiosTest::server_;
std::unique_ptr<SmbiosTransferService::Stub> SmbiosTest::smbios_stub_;
TEST_F(SmbiosTest, SmbiosTransferInvalidData) {
SmbiosTransferRequest request;
SmbiosTransferResponse response;
ClientContext context;
// Create an invalid SMBIOS entry point (23 bytes)
std::string entry_point(23, 'A');
request.set_smbios_entry_point(entry_point);
// Create a valid SMBIOS structure table (minimum 40 bytes)
std::string structure_table(60, 'B');
request.set_smbios_structure_table(structure_table);
auto status = smbios_stub_->SmbiosTransfer(&context, request, &response);
EXPECT_FALSE(status.ok());
}
TEST_F(SmbiosTest, SmbiosTransferValidData) {
SmbiosTransferRequest request;
SmbiosTransferResponse response;
ClientContext context;
// Create a pid file
createFile("/tmp/var/run/smbiosmdrv2app.pid");
std::ofstream pid_file("/tmp/var/run/smbiosmdrv2app.pid");
pid_file << 12345;
pid_file.close();
// Create a valid SMBIOS entry point (24 bytes)
std::string entry_point(24, 'A');
request.set_smbios_entry_point(entry_point);
// Create a valid SMBIOS structure table (minimum 40 bytes)
std::string structure_table(60, 'B');
request.set_smbios_structure_table(structure_table);
EXPECT_OK(smbios_stub_->SmbiosTransfer(&context, request, &response));
EXPECT_TRUE(
std::filesystem::exists("/tmp/var/lib/smbios/smbios-remote-host-3"));
removeFile("/tmp/var/lib/smbios/smbios-remote-host-3");
removeFile("/tmp/var/run/smbiosmdrv2app.pid");
}
} // namespace
} // namespace blobs