2 * Copyright (c) 2014 <copyright holder> <email>
4 * For license details, see the LICENSE file in the root of this project.
8 #include "GSSGetMicTest.h"
9 #include "command_mocks/MockGetMic.h"
10 #include "GSSGetMic.h"
11 #include <datamodel/GSSContext.h>
12 #include <cache/GSSContextCache.h>
13 #include <gssapi/gssapi.h>
15 CPPUNIT_TEST_SUITE_REGISTRATION( GSSGetMicTest );
18 * a mock of the gss_import_name call
20 * Basically, just copy the arguments over to/from the
21 * MockGetMic global object
24 static OM_uint32 KRB5_CALLCONV
26 OM_uint32 *minor_status,
27 gss_ctx_id_t context_handle,
29 gss_buffer_t message_buffer,
30 gss_buffer_t message_token)
38 // Copy our input from the appropriate parameters to MockGetMic
39 MockGetMic::context_handle = context_handle;
40 MockGetMic::qop_req = qop_req;
41 MockGetMic::inputMessage.setValue((char *)message_buffer->value, message_buffer->length);
44 // copy our output to the appropriate parameters
45 *minor_status = MockGetMic::minor_status;
46 *message_token = *MockGetMic::outputToken.toGss();
50 return MockGetMic::retVal;
53 void GSSGetMicTest::setUp()
55 CppUnit::TestFixture::setUp();
59 void GSSGetMicTest::testConstructor()
62 GSSGetMic cmd = GSSGetMic();
67 CPPUNIT_ASSERT_EQUAL_MESSAGE(
68 "The GSSGetMic object has the wrong GSS function",
70 (void *)cmd.getGSSFunction()
77 void GSSGetMicTest::testEmptyCall()
80 GSSGetMic cmd = GSSGetMic(&mock_get_mic);
81 GSSBuffer input((char *)"Input message");
82 std::string out("Output Message");
83 gss_qop_t desiredQop = rand();
84 gss_ctx_id_t desiredContext = (gss_ctx_id_t)rand();
88 cmd.setContextHandle(desiredContext);
89 cmd.setQopReq( desiredQop );
90 cmd.setInputMessage(&input);
92 MockGetMic::minor_status = 0;
93 MockGetMic::retVal = 0;
94 MockGetMic::outputToken.setValue(out);
99 CPPUNIT_ASSERT_EQUAL_MESSAGE(
100 "The requested GSS context handle is not correct",
102 MockGetMic::context_handle
105 CPPUNIT_ASSERT_EQUAL_MESSAGE(
106 "The qop_req flag was incorrect.",
111 CPPUNIT_ASSERT_EQUAL_MESSAGE(
112 "The input message was incorrect.",
114 MockGetMic::inputMessage.toString()
118 CPPUNIT_ASSERT_EQUAL_MESSAGE(
119 "The output message token was incorrect.",
121 cmd.getOutputToken().toString()
129 void GSSGetMicTest::testConstructorWithJSONObject()
132 GSSContext context((gss_ctx_id_t)rand(), true);
133 std::string key = GSSContextCache::instance()->store(context);
135 std::string input = "{ \
136 \"context_handle\": \"" + key + "\", \
137 \"qop_req\": \"GSS_C_QOP_DEFAULT\", \
138 \"input_message\": \"mary had a little lamb\" \
140 json_error_t jsonErr;
141 JSONObject json = JSONObject::load(input.c_str(), 0, &jsonErr);
143 GSSGetMic cmd = GSSGetMic(&json, &mock_get_mic);
149 CPPUNIT_ASSERT_EQUAL_MESSAGE(
150 "GSSGetMic did not parse the qop_req argument correctly",
155 CPPUNIT_ASSERT_EQUAL_MESSAGE(
156 "GSSGetMic did not parse the input message argument correctly",
157 std::string("mary had a little lamb"),
158 cmd.getInputMessage().toString()
161 CPPUNIT_ASSERT_EQUAL_MESSAGE(
162 "GSSGetMic did not retrieve the context handle correctly",
163 context.getContext(),
164 cmd.getContextHandle()
172 /* Desired JSON output:
175 * "command": "gss_get_mic",
180 * "output_token": "asdf"
185 void GSSGetMicTest::testJSONMarshal()
188 std::string output("dns@google.com");
189 GSSBuffer input( (char*)"abc" );
191 GSSGetMic cmd = GSSGetMic(&mock_get_mic);
195 MockGetMic::minor_status = 0;
196 MockGetMic::retVal = 0;
197 MockGetMic::outputToken.setValue(output);
199 cmd.setContextHandle(GSS_C_NO_CONTEXT);
200 cmd.setInputMessage( &input );
201 cmd.setQopReq(GSS_C_QOP_DEFAULT);
205 result = cmd.toJSON();
206 // std::cout << "\nGSSGetMic JSON: \n" << result->dump() << "\n";
208 CPPUNIT_ASSERT_EQUAL_MESSAGE(
209 "The return value was reported incorrectly",
210 (int)MockGetMic::retVal,
211 (int)( (*result)["major_status"].integer() )
214 CPPUNIT_ASSERT_EQUAL_MESSAGE(
215 "The minor_status value was reported incorrectly",
216 (int)MockGetMic::minor_status,
217 (int)( (*result)["minor_status"].integer() )
220 CPPUNIT_ASSERT_EQUAL_MESSAGE(
221 "The output message was reported incorrectly",
223 std::string( (*result)["output_token"].string() )