ros_mac_authentication_test.cpp
Go to the documentation of this file.
00001 #include <fstream>
00002 #include <gtest/gtest.h>
00003 #include <openssl/sha.h>
00004 #include <ros/ros.h>
00005 #include <rosauth/Authentication.h>
00006 #include <sstream>
00007 #include <string>
00008 
00009 using namespace std;
00010 using namespace ros;
00011 
00012 ServiceClient client;
00013 
00014 TEST(RosHashAuthentication, validAuthentication)
00015 {
00016   string secret = "abcdefghijklmnop";
00017   string client_ip = "192.168.1.101";
00018   string dest_ip = "192.168.1.111";
00019   string rand = "xyzabc";
00020   Time now = Time::now();
00021   string user_level = "admin";
00022   Time end = Time::now();
00023   end.sec += 120;
00024 
00025   // create the string to hash
00026   stringstream ss;
00027   ss << secret << client_ip << dest_ip << rand << now.sec << user_level << end.sec;
00028   string local_hash = ss.str();
00029   unsigned char sha512_hash[SHA512_DIGEST_LENGTH];
00030   SHA512((unsigned char *)local_hash.c_str(), local_hash.length(), sha512_hash);
00031 
00032   // convert to a hex string to compare
00033   char hex[SHA512_DIGEST_LENGTH * 2];
00034   for (int i = 0; i < SHA512_DIGEST_LENGTH; i++)
00035     sprintf(&hex[i * 2], "%02x", sha512_hash[i]);
00036 
00037   // make the request
00038   rosauth::Authentication srv;
00039   srv.request.mac = string(hex);
00040   srv.request.client = client_ip;
00041   srv.request.dest = dest_ip;
00042   srv.request.rand = rand;
00043   srv.request.t = now;
00044   srv.request.level = user_level;
00045   srv.request.end = end;
00046 
00047   EXPECT_TRUE(client.call(srv));
00048   EXPECT_TRUE(srv.response.authenticated);
00049 }
00050 
00051 // Run all the tests that were declared with TEST()
00052 int main(int argc, char **argv)
00053 {
00054   testing::InitGoogleTest(&argc, argv);
00055 
00056   // initialize ROS and the node
00057   init(argc, argv, "ros_hash_authentication_test");
00058   NodeHandle node;
00059 
00060   // setup the service client
00061   client = node.serviceClient<rosauth::Authentication>("authenticate");
00062 
00063   return RUN_ALL_TESTS();
00064 }


rosauth
Author(s): Russell Toris
autogenerated on Thu Feb 25 2016 11:48:45