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)
37 buffer = MockGetMic::outputToken.toString();
40 // Copy our input from the appropriate parameters to MockGetMic
41 MockGetMic::context_handle = context_handle;
42 MockGetMic::qop_req = qop_req;
43 MockGetMic::inputMessage.setValue((char *)message_buffer->value, message_buffer->length);
46 // copy our output to the appropriate parameters
47 *minor_status = MockGetMic::minor_status;
48 message_token->length = buffer.length();
49 message_token->value = (void *)buffer.c_str();
53 return MockGetMic::retVal;
56 void GSSGetMicTest::setUp()
58 CppUnit::TestFixture::setUp();
62 void GSSGetMicTest::testConstructor()
65 GSSGetMic cmd = GSSGetMic();
70 CPPUNIT_ASSERT_EQUAL_MESSAGE(
71 "The GSSGetMic object has the wrong GSS function",
73 (void *)cmd.getGSSFunction()
80 void GSSGetMicTest::testEmptyCall()
83 GSSGetMic cmd = GSSGetMic(&mock_get_mic);
84 GSSBuffer input((char *)"Input message");
85 std::string out("Output Message");
86 gss_qop_t desiredQop = rand();
87 gss_ctx_id_t desiredContext = (gss_ctx_id_t)rand();
91 cmd.setContextHandle(desiredContext);
92 cmd.setQopReq( desiredQop );
93 cmd.setInputMessage(&input);
95 MockGetMic::minor_status = 0;
96 MockGetMic::retVal = 0;
97 MockGetMic::outputToken.setValue(out);
102 CPPUNIT_ASSERT_EQUAL_MESSAGE(
103 "The requested GSS context handle is not correct",
105 MockGetMic::context_handle
108 CPPUNIT_ASSERT_EQUAL_MESSAGE(
109 "The qop_req flag was incorrect.",
114 CPPUNIT_ASSERT_EQUAL_MESSAGE(
115 "The input message was incorrect.",
117 MockGetMic::inputMessage.toString()
121 CPPUNIT_ASSERT_EQUAL_MESSAGE(
122 "The output message token was incorrect.",
124 cmd.getOutputToken().toString()
132 void GSSGetMicTest::testConstructorWithJSONObject()
135 GSSContext context((gss_ctx_id_t)rand(), true);
136 std::string key = GSSContextCache::instance()->store(context);
138 std::string input = "{ \
139 \"context_handle\": \"" + key + "\", \
140 \"qop_req\": \"GSS_C_QOP_DEFAULT\", \
141 \"input_message\": \"mary had a little lamb\" \
143 json_error_t jsonErr;
144 JSONObject json = JSONObject::load(input.c_str(), 0, &jsonErr);
146 GSSGetMic cmd = GSSGetMic(&json, &mock_get_mic);
152 CPPUNIT_ASSERT_EQUAL_MESSAGE(
153 "GSSGetMic did not parse the qop_req argument correctly",
158 CPPUNIT_ASSERT_EQUAL_MESSAGE(
159 "GSSGetMic did not parse the input message argument correctly",
160 std::string("mary had a little lamb"),
161 cmd.getInputMessage().toString()
164 CPPUNIT_ASSERT_EQUAL_MESSAGE(
165 "GSSGetMic did not retrieve the context handle correctly",
166 context.getContext(),
167 cmd.getContextHandle()
175 /* Desired JSON output:
178 * "command": "gss_get_mic",
183 * "output_token": "asdf"
188 void GSSGetMicTest::testJSONMarshal()
191 std::string output("dns@google.com");
192 GSSBuffer input( (char*)"abc" );
194 GSSGetMic cmd = GSSGetMic(&mock_get_mic);
198 MockGetMic::minor_status = 0;
199 MockGetMic::retVal = 0;
200 MockGetMic::outputToken.setValue(output);
202 cmd.setContextHandle(GSS_C_NO_CONTEXT);
203 cmd.setInputMessage( &input );
204 cmd.setQopReq(GSS_C_QOP_DEFAULT);
208 result = cmd.toJSON();
209 // std::cout << "\nGSSGetMic JSON: \n" << result->dump() << "\n";
211 CPPUNIT_ASSERT_EQUAL_MESSAGE(
212 "The return value was reported incorrectly",
213 (int)MockGetMic::retVal,
214 (int)( (*result)["major_status"].integer() )
217 CPPUNIT_ASSERT_EQUAL_MESSAGE(
218 "The minor_status value was reported incorrectly",
219 (int)MockGetMic::minor_status,
220 (int)( (*result)["minor_status"].integer() )
223 CPPUNIT_ASSERT_EQUAL_MESSAGE(
224 "The output message was reported incorrectly",
226 std::string( (*result)["output_token"].string() )