-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathtest_OperatorFactory.cpp
More file actions
155 lines (122 loc) · 4.55 KB
/
test_OperatorFactory.cpp
File metadata and controls
155 lines (122 loc) · 4.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
#define BOOST_TEST_MAIN
#define BOOST_TEST_MODULE operator_factory
#include <boost/test/unit_test.hpp>
// Local public includes
#include "common/IAuthenticationManager.hpp"
#include "common/IMessage.hpp"
#include "common/IOperator.hpp"
#include "common/MessageFactory.hpp"
#include "common/OperatorFactory.hpp"
#include "common/OperatorTypes.hpp"
#include "common/DynaLog.hpp"
// Third party includes
#include <google/protobuf/stubs/common.h>
// Standard includes
#include <iostream>
#include <string>
#include <unordered_map>
using namespace SDMS;
struct GlobalProtobufTeardown {
~GlobalProtobufTeardown() {
// This is the teardown function that runs once at the end
google::protobuf::ShutdownProtobufLibrary();
}
};
// Declare a global fixture instance
BOOST_GLOBAL_FIXTURE(GlobalProtobufTeardown);
class DummyAuthManager : public IAuthenticationManager {
private:
std::unordered_map<std::string, int> m_counters;
/**
* Methods only available via the interface
**/
virtual void incrementKeyAccessCounter(const std::string &pub_key, LogContext log_context) final {
++m_counters.at(pub_key);
}
virtual bool hasKey(const std::string &pub_key, LogContext log_context) const {
return m_counters.count(pub_key);
}
// Just assume all keys map to the anon_uid
virtual std::string getUID(const std::string &, LogContext log_context) const {
return "authenticated_uid";
}
virtual void purge() final {
std::cout << "Purge not implemented" << std::endl;
}
public:
/**
* Method for adding known keys
**/
void addKey(const std::string pub_key) { m_counters[pub_key] = 0; }
int getAccessCount(const std::string &pub_key) {
if (m_counters.count(pub_key)) {
return m_counters.at(pub_key);
}
return 0;
}
};
BOOST_AUTO_TEST_SUITE(OperatorFactoryTest)
BOOST_AUTO_TEST_CASE(testing_OperatorFactoryAnon) {
OperatorFactory operator_factory;
DummyAuthManager dummy_manager;
// Pass in const DummyAuthManager * to std::any
std::any argument = dynamic_cast<IAuthenticationManager *>(&dummy_manager);
auto auth_operator =
operator_factory.create(OperatorType::Authenticator, argument);
BOOST_CHECK(auth_operator->type() == OperatorType::Authenticator);
MessageFactory msg_factory;
auto msg = msg_factory.create(MessageType::GOOGLE_PROTOCOL_BUFFER);
msg->set(MessageAttribute::KEY, "bad_key");
// Because "bad_key" is:
// 1. not a known key or an authenticated key
//
// After running execute the UID will be "anon_bad_key"
auth_operator->execute(*msg);
BOOST_CHECK(
std::get<std::string>(msg->get(MessageAttribute::ID)).compare("anon") ==
0);
// Should not have been incremented because it is an unknown anonymous user
// key
BOOST_CHECK(dummy_manager.getAccessCount("bad_key") == 0);
}
BOOST_AUTO_TEST_CASE(testing_OperatorFactoryKnown) {
OperatorFactory oper_factory;
DummyAuthManager dummy_manager;
// Add a known key
dummy_manager.addKey("skeleton_key");
// Pass in const DummyAuthManager * to std::any
std::any argument = dynamic_cast<IAuthenticationManager *>(&dummy_manager);
auto auth_operator =
oper_factory.create(OperatorType::Authenticator, argument);
BOOST_CHECK(auth_operator->type() == OperatorType::Authenticator);
MessageFactory msg_factory;
auto msg = msg_factory.create(MessageType::GOOGLE_PROTOCOL_BUFFER);
msg->set(MessageAttribute::KEY, "skeleton_key");
// Because "George" is:
// 1. not a key
// 2. not an authenticated key
//
// After running execute the UID will be "anon_George"
auth_operator->execute(*msg);
BOOST_CHECK(std::get<std::string>(msg->get(MessageAttribute::ID))
.compare("authenticated_uid") == 0);
// Should be incremented because user key was known
BOOST_CHECK(dummy_manager.getAccessCount("skeleton_key") == 1);
}
BOOST_AUTO_TEST_CASE(testing_RouterBookKeepingOperator) {
OperatorFactory oper_factory;
std::string client_id = "my_nice_proxy_id";
std::any obfuscated_id = client_id;
auto router_book_keeping_operator =
oper_factory.create(OperatorType::RouterBookKeeping, obfuscated_id);
BOOST_CHECK(router_book_keeping_operator->type() ==
OperatorType::RouterBookKeeping);
MessageFactory msg_factory;
auto msg = msg_factory.create(MessageType::GOOGLE_PROTOCOL_BUFFER);
msg->set(MessageAttribute::KEY, "skeleton_key");
// Should add "my_nice_proxy_id" to the routes
router_book_keeping_operator->execute(*msg);
BOOST_CHECK(msg->getRoutes().size() == 1);
BOOST_CHECK(msg->getRoutes().front().compare(client_id) == 0);
}
BOOST_AUTO_TEST_SUITE_END()