Rearrange stuff
This commit is contained in:
parent
518f578298
commit
d7e2aee3a3
21 changed files with 20 additions and 139 deletions
63
test/tests/proxy_test.cpp
Normal file
63
test/tests/proxy_test.cpp
Normal file
|
|
@ -0,0 +1,63 @@
|
|||
// ProxyTest.cpp
|
||||
#include <gtest/gtest.h>
|
||||
#include "Proxy.h"
|
||||
#include "socket_mock.h"
|
||||
|
||||
class ProxyTest : public ::testing::Test {
|
||||
protected:
|
||||
MockSocket mock_socket;
|
||||
Proxy* proxy;
|
||||
|
||||
void SetUp() override {
|
||||
proxy = new Proxy(&mock_socket);
|
||||
}
|
||||
|
||||
void TearDown() override {
|
||||
delete proxy;
|
||||
}
|
||||
};
|
||||
|
||||
TEST_F(ProxyTest, HandlesMailFromCommand) {
|
||||
std::string peer_address = "127.0.0.1";
|
||||
|
||||
EXPECT_CALL(mock_socket, connect("server-host", 25));
|
||||
EXPECT_CALL(mock_socket, canRead(0.2)).WillOnce(testing::Return(true));
|
||||
EXPECT_CALL(mock_socket, readLine()).WillOnce(testing::Return("MAIL FROM:<test@example.com>"));
|
||||
EXPECT_CALL(mock_socket, writeLine("MAIL FROM:<test@example.com>"));
|
||||
EXPECT_CALL(mock_socket, writeLine("250 OK"));
|
||||
|
||||
proxy->run(peer_address);
|
||||
}
|
||||
|
||||
TEST_F(ProxyTest, HandlesRcptToCommand) {
|
||||
std::string peer_address = "127.0.0.1";
|
||||
|
||||
EXPECT_CALL(mock_socket, connect("server-host", 25));
|
||||
EXPECT_CALL(mock_socket, canRead(0.2)).WillRepeatedly(testing::Return(true));
|
||||
EXPECT_CALL(mock_socket, readLine())
|
||||
.WillOnce(testing::Return("MAIL FROM:<test@example.com>"))
|
||||
.WillOnce(testing::Return("RCPT TO:<receiver@example.com>"))
|
||||
.WillOnce(testing::Return(""));
|
||||
|
||||
EXPECT_CALL(mock_socket, writeLine("MAIL FROM:<test@example.com>"));
|
||||
EXPECT_CALL(mock_socket, writeLine("RCPT TO:<receiver@example.com>"));
|
||||
EXPECT_CALL(mock_socket, writeLine("250 OK"));
|
||||
|
||||
proxy->run(peer_address);
|
||||
}
|
||||
|
||||
TEST_F(ProxyTest, HandlesEmptyLine) {
|
||||
std::string peer_address = "127.0.0.1";
|
||||
|
||||
EXPECT_CALL(mock_socket, connect("server-host", 25));
|
||||
EXPECT_CALL(mock_socket, canRead(0.2)).WillOnce(testing::Return(true));
|
||||
EXPECT_CALL(mock_socket, readLine()).WillOnce(testing::Return(""));
|
||||
|
||||
proxy->run(peer_address);
|
||||
// Expect no further actions to occur
|
||||
}
|
||||
|
||||
int main(int argc, char** argv) {
|
||||
::testing::InitGoogleMock(&argc, argv);
|
||||
return RUN_ALL_TESTS();
|
||||
}
|
||||
98
test/tests/spf_test.cpp
Normal file
98
test/tests/spf_test.cpp
Normal file
|
|
@ -0,0 +1,98 @@
|
|||
#include <gtest/gtest.h>
|
||||
#include "Spf.h"
|
||||
|
||||
#include "libspf2_mock.h"
|
||||
|
||||
// Mock the SPF server responses for unit testing
|
||||
class MockSPFServer {
|
||||
public:
|
||||
static void initialize() {
|
||||
// Initialize any mock data here
|
||||
}
|
||||
|
||||
static void cleanup() {
|
||||
// Cleanup if needed
|
||||
}
|
||||
|
||||
static SPF_response_t* mockResponse(SPF_result_t result) {
|
||||
SPF_response_t* response = new SPF_response_t; // Replace with actual allocation
|
||||
response->result = result; // Set the desired mock result
|
||||
return response;
|
||||
}
|
||||
};
|
||||
|
||||
// Test Fixture for SPF tests
|
||||
class SpfTest : public ::testing::Test {
|
||||
protected:
|
||||
Spf* spf;
|
||||
|
||||
void SetUp() override {
|
||||
// Create a new Spf instance before each test
|
||||
spf = new Spf();
|
||||
}
|
||||
|
||||
void TearDown() override {
|
||||
// Clean up after each test
|
||||
delete spf;
|
||||
spf->deinitialize();
|
||||
}
|
||||
};
|
||||
|
||||
// Test the construction of the Spf object
|
||||
TEST_F(SpfTest, Construction) {
|
||||
EXPECT_NO_THROW({
|
||||
Spf testSpf;
|
||||
});
|
||||
}
|
||||
|
||||
// Test SPF querying with valid parameters
|
||||
TEST_F(SpfTest, QueryValidParameters) {
|
||||
// Assuming the mocked SPF server is set up to return a valid response
|
||||
MockSPFServer::initialize();
|
||||
|
||||
bool result = spf->query("192.0.2.1", "mail.example.com", "test@example.com");
|
||||
EXPECT_TRUE(result);
|
||||
|
||||
MockSPFServer::cleanup();
|
||||
}
|
||||
|
||||
// Test SPF querying with failed result
|
||||
TEST_F(SpfTest, QueryFailResult) {
|
||||
// Mock the response to return a fail result here
|
||||
SPF_response_t* response = MockSPFServer::mockResponse(SPF_RESULT_FAIL);
|
||||
|
||||
bool result = spf->query("198.51.100.1", "fail.example.com", "test@fail.com");
|
||||
EXPECT_FALSE(result);
|
||||
|
||||
delete response; // Clean up mocked response
|
||||
}
|
||||
|
||||
// Test SPF querying with an empty HELO string
|
||||
TEST_F(SpfTest, QueryEmptyHelo) {
|
||||
EXPECT_THROW({
|
||||
spf->query("192.0.2.1", "", "test@example.com");
|
||||
}, std::runtime_error);
|
||||
}
|
||||
|
||||
// Test SPF querying with invalid IP format
|
||||
TEST_F(SpfTest, QueryInvalidIPAddress) {
|
||||
EXPECT_THROW({
|
||||
spf->query("invalid_ip", "mail.example.com", "test@example.com");
|
||||
}, std::runtime_error);
|
||||
}
|
||||
|
||||
// Test SPF request failure
|
||||
TEST_F(SpfTest, QueryRequestFailure) {
|
||||
// Here you might want to simulate a situation
|
||||
// where the request cannot be created or fails due to some other reason.
|
||||
spf->deinitialize(); // Ensure the server is cleaned up before running this.
|
||||
|
||||
EXPECT_THROW({
|
||||
spf->query("192.0.2.1", "mail.example.com", "test@example.com");
|
||||
}, std::runtime_error);
|
||||
}
|
||||
|
||||
int main(int argc, char **argv) {
|
||||
::testing::InitGoogleTest(&argc, argv);
|
||||
return RUN_ALL_TESTS();
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue