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)( (long)0 | 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)( (long)0 | rand() ),
134 std::string key = GSSContextCache::instance()->store(context);
136 std::string input = "{ \
137 \"context_handle\": \"" + key + "\", \
138 \"qop_req\": \"GSS_C_QOP_DEFAULT\", \
139 \"input_message\": \"mary had a little lamb\" \
141 json_error_t jsonErr;
142 JSONObject json = JSONObject::load(input.c_str(), 0, &jsonErr);
144 GSSGetMic cmd = GSSGetMic(&json, &mock_get_mic);
150 CPPUNIT_ASSERT_EQUAL_MESSAGE(
151 "GSSGetMic did not parse the qop_req argument correctly",
156 CPPUNIT_ASSERT_EQUAL_MESSAGE(
157 "GSSGetMic did not parse the input message argument correctly",
158 std::string("mary had a little lamb"),
159 cmd.getInputMessage().toString()
162 CPPUNIT_ASSERT_EQUAL_MESSAGE(
163 "GSSGetMic did not retrieve the context handle correctly",
164 context.getContext(),
165 cmd.getContextHandle()
173 /* Desired JSON output:
176 * "command": "gss_get_mic",
181 * "output_token": "asdf"
186 void GSSGetMicTest::testJSONMarshal()
189 std::string output("dns@google.com");
190 GSSBuffer input( (char*)"abc" );
192 GSSGetMic cmd = GSSGetMic(&mock_get_mic);
196 MockGetMic::minor_status = 0;
197 MockGetMic::retVal = 0;
198 MockGetMic::outputToken.setValue(output);
200 cmd.setContextHandle(GSS_C_NO_CONTEXT);
201 cmd.setInputMessage( &input );
202 cmd.setQopReq(GSS_C_QOP_DEFAULT);
206 result = cmd.toJSON();
207 // std::cout << "\nGSSGetMic JSON: \n" << result->dump() << "\n";
209 CPPUNIT_ASSERT_EQUAL_MESSAGE(
210 "The return value was reported incorrectly",
211 (int)MockGetMic::retVal,
212 (int)( (*result)["major_status"].integer() )
215 CPPUNIT_ASSERT_EQUAL_MESSAGE(
216 "The minor_status value was reported incorrectly",
217 (int)MockGetMic::minor_status,
218 (int)( (*result)["minor_status"].integer() )
221 CPPUNIT_ASSERT_EQUAL_MESSAGE(
222 "The output message was reported incorrectly",
224 std::string( (*result)["output_token"].string() )