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 = "{\"method\": \"gss_get_mic\", \
141 \"context_handle\": \"" + key + "\", \
142 \"qop_req\": \"GSS_C_QOP_DEFAULT\", \
143 \"input_message\": \"mary had a little lamb\" \
146 json_error_t jsonErr;
147 JSONObject json = JSONObject::load(input.c_str(), 0, &jsonErr);
149 GSSGetMic cmd = GSSGetMic(&json, &mock_get_mic);
155 CPPUNIT_ASSERT_EQUAL_MESSAGE(
156 "GSSGetMic did not parse the qop_req argument correctly",
161 CPPUNIT_ASSERT_EQUAL_MESSAGE(
162 "GSSGetMic did not parse the input message argument correctly",
163 std::string("mary had a little lamb"),
164 cmd.getInputMessage().toString()
167 CPPUNIT_ASSERT_EQUAL_MESSAGE(
168 "GSSGetMic did not retrieve the context handle correctly",
169 context.getContext(),
170 cmd.getContextHandle()
178 /* Desired JSON output:
181 * "command": "gss_get_mic",
186 * "output_token": "asdf"
191 void GSSGetMicTest::testJSONMarshal()
194 std::string output("dns@google.com");
195 GSSBuffer input( (char*)"abc" );
197 GSSGetMic cmd = GSSGetMic(&mock_get_mic);
201 MockGetMic::minor_status = 0;
202 MockGetMic::retVal = 0;
203 MockGetMic::outputToken.setValue(output);
205 cmd.setContextHandle(GSS_C_NO_CONTEXT);
206 cmd.setInputMessage( &input );
207 cmd.setQopReq(GSS_C_QOP_DEFAULT);
211 result = cmd.toJSON();
212 // std::cout << "\nGSSGetMic JSON: \n" << result->dump() << "\n";
214 CPPUNIT_ASSERT_EQUAL_MESSAGE(
215 "The command name is incorrect",
216 std::string("gss_get_mic"),
217 std::string( (*result)["command"].string() )
221 CPPUNIT_ASSERT_EQUAL_MESSAGE(
222 "The return value was reported incorrectly",
223 (int)MockGetMic::retVal,
224 (int)( (*result)["return_values"]["major_status"].integer() )
227 CPPUNIT_ASSERT_EQUAL_MESSAGE(
228 "The minor_status value was reported incorrectly",
229 (int)MockGetMic::minor_status,
230 (int)( (*result)["return_values"]["minor_status"].integer() )
233 CPPUNIT_ASSERT_EQUAL_MESSAGE(
234 "The output message was reported incorrectly",
236 std::string( (*result)["return_values"]["output_token"].string() )