ros_mac_authentication_test.cpp
Go to the documentation of this file.
1 #include <fstream>
2 #include <gtest/gtest.h>
3 #include <openssl/sha.h>
4 #include <ros/ros.h>
5 #include <rosauth/Authentication.h>
6 #include <sstream>
7 #include <string>
8 
9 using namespace std;
10 using namespace ros;
11 
13 
14 TEST(RosHashAuthentication, validAuthentication)
15 {
16  string secret = "abcdefghijklmnop";
17  string client_ip = "192.168.1.101";
18  string dest_ip = "192.168.1.111";
19  string rand = "xyzabc";
20  Time now = Time::now();
21  string user_level = "admin";
22  Time end = Time::now();
23  end.sec += 120;
24 
25  // create the string to hash
26  stringstream ss;
27  ss << secret << client_ip << dest_ip << rand << now.sec << user_level << end.sec;
28  string local_hash = ss.str();
29  unsigned char sha512_hash[SHA512_DIGEST_LENGTH];
30  SHA512((unsigned char *)local_hash.c_str(), local_hash.length(), sha512_hash);
31 
32  // convert to a hex string to compare
33  char hex[SHA512_DIGEST_LENGTH * 2];
34  for (int i = 0; i < SHA512_DIGEST_LENGTH; i++)
35  sprintf(&hex[i * 2], "%02x", sha512_hash[i]);
36 
37  // make the request
38  rosauth::Authentication srv;
39  srv.request.mac = string(hex);
40  srv.request.client = client_ip;
41  srv.request.dest = dest_ip;
42  srv.request.rand = rand;
43  srv.request.t = now;
44  srv.request.level = user_level;
45  srv.request.end = end;
46 
47  EXPECT_TRUE(client.call(srv));
48  EXPECT_TRUE(srv.response.authenticated);
49 }
50 
51 // Run all the tests that were declared with TEST()
52 int main(int argc, char **argv)
53 {
54  testing::InitGoogleTest(&argc, argv);
55 
56  // initialize ROS and the node
57  init(argc, argv, "ros_hash_authentication_test");
58  NodeHandle node;
59 
60  // setup the service client
61  client = node.serviceClient<rosauth::Authentication>("authenticate");
62 
63  return RUN_ALL_TESTS();
64 }
ServiceClient serviceClient(const std::string &service_name, bool persistent=false, const M_string &header_values=M_string())
TEST(RosHashAuthentication, validAuthentication)
void init(const M_string &remappings)
bool call(MReq &req, MRes &res)
int main(int argc, char **argv)
string secret
ServiceClient client


rosauth
Author(s): Russell Toris
autogenerated on Wed Jun 5 2019 20:18:45