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,
42 // Copy our input from the appropriate parameters to MockUnwrap
43 MockUnwrap::context_handle = context_handle;
44 MockUnwrap::inputMessageBuffer.setValue(input_message_buffer);
47 // copy our output to the appropriate parameters
48 *minor_status = MockUnwrap::minor_status;
49 *conf_state = MockUnwrap::conf_state;
50 *qop_state = MockUnwrap::qop_state;
51 *output_message_buffer = *MockUnwrap::outputMessageBuffer.toGss();
55 return MockUnwrap::retVal;
58 void GSSUnwrapTest::setUp()
60 CppUnit::TestFixture::setUp();
64 void GSSUnwrapTest::testConstructor()
67 GSSUnwrap cmd = GSSUnwrap();
72 CPPUNIT_ASSERT_EQUAL_MESSAGE(
73 "The GSSUnwrap object has the wrong GSS function",
75 (void *)cmd.getGSSFunction()
82 void GSSUnwrapTest::testEmptyCall()
85 GSSUnwrap cmd = GSSUnwrap(&mock_unwrap);
86 GSSBuffer input((char *)"Input message");
87 gss_ctx_id_t desiredContext = (gss_ctx_id_t)( (long)0 | rand());
91 cmd.setContextHandle(desiredContext);
92 cmd.setInputMessage(&input);
94 MockUnwrap::minor_status = 0;
95 MockUnwrap::retVal = 0;
96 MockUnwrap::conf_state = rand();
97 MockUnwrap::qop_state = GSS_C_QOP_DEFAULT;
98 MockUnwrap::outputMessageBuffer.setValue((char *)"Output message");
103 CPPUNIT_ASSERT_EQUAL_MESSAGE(
104 "The requested GSS context handle is not correct",
106 MockUnwrap::context_handle
109 CPPUNIT_ASSERT_EQUAL_MESSAGE(
110 "The input message was incorrect.",
112 MockUnwrap::inputMessageBuffer.toString()
117 CPPUNIT_ASSERT_EQUAL_MESSAGE(
118 "The conf_state flag was incorrect.",
119 MockUnwrap::qop_state,
123 CPPUNIT_ASSERT_EQUAL_MESSAGE(
124 "The conf_state flag was incorrect.",
125 MockUnwrap::conf_state,
129 CPPUNIT_ASSERT_EQUAL_MESSAGE(
130 "The input message was incorrect.",
131 cmd.getOutputMessage().toString(),
132 MockUnwrap::outputMessageBuffer.toString()
140 void GSSUnwrapTest::testConstructorWithJSONObject()
143 GSSContext context((gss_ctx_id_t)( (long)0 | rand()),
145 std::string key = GSSContextCache::instance()->store(context);
146 std::string input = "{ \
147 \"context_handle\": \"" + key + "\", \
148 \"input_message\": \"mary had a little lamb\" \
150 json_error_t jsonErr;
151 JSONObject json = JSONObject::load(input.c_str(), 0, &jsonErr);
153 GSSUnwrap cmd = GSSUnwrap(&json, &mock_unwrap);
159 CPPUNIT_ASSERT_EQUAL_MESSAGE(
160 "GSSUnwrap did not retrieve the GSS context correctly",
161 context.getContext(),
162 cmd.getContextHandle()
165 CPPUNIT_ASSERT_EQUAL_MESSAGE(
166 "GSSUnwrap did not parse the input message argument correctly",
167 std::string("mary had a little lamb"),
168 cmd.getInputMessage().toString()
178 void GSSUnwrapTest::testJSONMarshal()
181 std::string output("dns@google.com");
183 gss_qop_t qopState = GSS_C_QOP_DEFAULT;
185 GSSUnwrap cmd = GSSUnwrap(&mock_unwrap);
189 MockUnwrap::minor_status = 0;
190 MockUnwrap::retVal = 0;
191 MockUnwrap::outputMessageBuffer.setValue(output);
192 MockUnwrap::conf_state = confState;
193 MockUnwrap::qop_state = qopState;
197 result = cmd.toJSON();
198 // std::cout << "\nGSSUnwrap JSON: \n" << result->dump() << "\n";
201 CPPUNIT_ASSERT_EQUAL_MESSAGE(
202 "The return value was reported incorrectly",
203 (int)MockUnwrap::retVal,
204 (int)( (*result)["major_status"].integer() )
207 CPPUNIT_ASSERT_EQUAL_MESSAGE(
208 "The minor_status value was reported incorrectly",
209 (int)MockUnwrap::minor_status,
210 (int)( (*result)["minor_status"].integer() )
213 CPPUNIT_ASSERT_EQUAL_MESSAGE(
214 "The output message was reported incorrectly",
215 MockUnwrap::outputMessageBuffer.toString(),
216 std::string( (*result)["output_message"].string() )
219 CPPUNIT_ASSERT_EQUAL_MESSAGE(
220 "The minor_status value was reported incorrectly",
222 (int)( (*result)["qop_state"].integer() )