2 * Copyright (c) 2014 <copyright holder> <email>
4 * For license details, see the LICENSE file in the root of this project.
10 #include "GSSUnwrapTest.h"
11 #include "command_mocks/MockUnwrap.h"
12 #include "GSSUnwrap.h"
13 #include <datamodel/GSSContext.h>
14 #include <cache/GSSContextCache.h>
15 #include <gssapi/gssapi.h>
18 CPPUNIT_TEST_SUITE_REGISTRATION( GSSUnwrapTest );
21 * a mock of the gss_import_name call
23 * Basically, just copy the arguments over to/from the
24 * MockUnwrap global object
27 static OM_uint32 KRB5_CALLCONV
29 OM_uint32 *minor_status,
30 gss_ctx_id_t context_handle,
31 gss_buffer_t input_message_buffer,
32 gss_buffer_t output_message_buffer,
41 buffer = MockUnwrap::outputMessageBuffer.toString();
44 // Copy our input from the appropriate parameters to MockUnwrap
45 MockUnwrap::context_handle = context_handle;
46 MockUnwrap::inputMessageBuffer.setValue(input_message_buffer);
49 // copy our output to the appropriate parameters
50 *minor_status = MockUnwrap::minor_status;
51 *conf_state = MockUnwrap::conf_state;
52 *qop_state = MockUnwrap::qop_state;
53 output_message_buffer->length = buffer.length();
54 output_message_buffer->value = (void *)buffer.c_str();
58 return MockUnwrap::retVal;
61 void GSSUnwrapTest::setUp()
63 CppUnit::TestFixture::setUp();
67 void GSSUnwrapTest::testConstructor()
70 GSSUnwrap cmd = GSSUnwrap();
75 CPPUNIT_ASSERT_EQUAL_MESSAGE(
76 "The GSSUnwrap object has the wrong GSS function",
78 (void *)cmd.getGSSFunction()
85 void GSSUnwrapTest::testEmptyCall()
88 GSSUnwrap cmd = GSSUnwrap(&mock_unwrap);
89 GSSBuffer input((char *)"Input message");
90 gss_ctx_id_t desiredContext = (gss_ctx_id_t)rand();
94 cmd.setContextHandle(desiredContext);
95 cmd.setInputMessage(&input);
97 MockUnwrap::minor_status = 0;
98 MockUnwrap::retVal = 0;
99 MockUnwrap::conf_state = rand();
100 MockUnwrap::qop_state = GSS_C_QOP_DEFAULT;
101 MockUnwrap::outputMessageBuffer.setValue((char *)"Output message");
106 CPPUNIT_ASSERT_EQUAL_MESSAGE(
107 "The requested GSS context handle is not correct",
109 MockUnwrap::context_handle
112 CPPUNIT_ASSERT_EQUAL_MESSAGE(
113 "The input message was incorrect.",
115 MockUnwrap::inputMessageBuffer.toString()
120 CPPUNIT_ASSERT_EQUAL_MESSAGE(
121 "The conf_state flag was incorrect.",
122 MockUnwrap::qop_state,
126 CPPUNIT_ASSERT_EQUAL_MESSAGE(
127 "The conf_state flag was incorrect.",
128 MockUnwrap::conf_state,
132 CPPUNIT_ASSERT_EQUAL_MESSAGE(
133 "The input message was incorrect.",
134 cmd.getOutputMessage().toString(),
135 MockUnwrap::outputMessageBuffer.toString()
143 void GSSUnwrapTest::testConstructorWithJSONObject()
146 GSSContext context((gss_ctx_id_t)rand(), true);
147 std::string key = GSSContextCache::instance()->store(context);
148 std::string input = "{ \
149 \"context_handle\": \"" + key + "\", \
150 \"input_message\": \"mary had a little lamb\" \
152 json_error_t jsonErr;
153 JSONObject json = JSONObject::load(input.c_str(), 0, &jsonErr);
155 GSSUnwrap cmd = GSSUnwrap(&json, &mock_unwrap);
161 CPPUNIT_ASSERT_EQUAL_MESSAGE(
162 "GSSUnwrap did not retrieve the GSS context correctly",
163 context.getContext(),
164 cmd.getContextHandle()
167 CPPUNIT_ASSERT_EQUAL_MESSAGE(
168 "GSSUnwrap did not parse the input message argument correctly",
169 std::string("mary had a little lamb"),
170 cmd.getInputMessage().toString()
180 void GSSUnwrapTest::testJSONMarshal()
183 std::string output("dns@google.com");
185 gss_qop_t qopState = GSS_C_QOP_DEFAULT;
187 GSSUnwrap cmd = GSSUnwrap(&mock_unwrap);
191 MockUnwrap::minor_status = 0;
192 MockUnwrap::retVal = 0;
193 MockUnwrap::outputMessageBuffer.setValue(output);
194 MockUnwrap::conf_state = confState;
195 MockUnwrap::qop_state = qopState;
199 result = cmd.toJSON();
200 // std::cout << "\nGSSUnwrap JSON: \n" << result->dump() << "\n";
203 CPPUNIT_ASSERT_EQUAL_MESSAGE(
204 "The return value was reported incorrectly",
205 (int)MockUnwrap::retVal,
206 (int)( (*result)["major_status"].integer() )
209 CPPUNIT_ASSERT_EQUAL_MESSAGE(
210 "The minor_status value was reported incorrectly",
211 (int)MockUnwrap::minor_status,
212 (int)( (*result)["minor_status"].integer() )
215 CPPUNIT_ASSERT_EQUAL_MESSAGE(
216 "The output message was reported incorrectly",
217 MockUnwrap::outputMessageBuffer.toString(),
218 std::string( (*result)["output_message"].string() )
221 CPPUNIT_ASSERT_EQUAL_MESSAGE(
222 "The minor_status value was reported incorrectly",
224 (int)( (*result)["qop_state"].integer() )