2 * Copyright (c) 2014 <copyright holder> <email>
4 * For license details, see the LICENSE file in the root of this project.
8 #include "GSSUnwrapTest.h"
9 #include "command_mocks/MockUnwrap.h"
10 #include "GSSUnwrap.h"
11 #include <gssapi/gssapi.h>
13 CPPUNIT_TEST_SUITE_REGISTRATION( GSSUnwrapTest );
16 * a mock of the gss_import_name call
18 * Basically, just copy the arguments over to/from the
19 * MockUnwrap global object
22 static OM_uint32 KRB5_CALLCONV
24 OM_uint32 *minor_status,
25 gss_ctx_id_t context_handle,
26 gss_buffer_t input_message_buffer,
27 gss_buffer_t output_message_buffer,
36 buffer = MockUnwrap::outputMessageBuffer.toString();
39 // Copy our input from the appropriate parameters to MockUnwrap
40 MockUnwrap::context_handle = context_handle;
41 MockUnwrap::inputMessageBuffer.setValue(input_message_buffer);
44 // copy our output to the appropriate parameters
45 *minor_status = MockUnwrap::minor_status;
46 *conf_state = MockUnwrap::conf_state;
47 *qop_state = MockUnwrap::qop_state;
48 output_message_buffer->length = buffer.length();
49 output_message_buffer->value = (void *)buffer.c_str();
53 return MockUnwrap::retVal;
56 void GSSUnwrapTest::setUp()
58 CppUnit::TestFixture::setUp();
62 void GSSUnwrapTest::testConstructor()
65 GSSUnwrap cmd = GSSUnwrap();
70 CPPUNIT_ASSERT_EQUAL_MESSAGE(
71 "The GSSUnwrap object has the wrong GSS function",
73 (void *)cmd.getGSSFunction()
80 void GSSUnwrapTest::testEmptyCall()
83 GSSUnwrap cmd = GSSUnwrap(&mock_unwrap);
84 GSSBuffer input((char *)"Input message");
85 gss_ctx_id_t desiredContext = (gss_ctx_id_t)rand();
89 cmd.setContextHandle(desiredContext);
90 cmd.setInputMessage(&input);
92 MockUnwrap::minor_status = 0;
93 MockUnwrap::retVal = 0;
94 MockUnwrap::conf_state = rand();
95 MockUnwrap::qop_state = GSS_C_QOP_DEFAULT;
96 MockUnwrap::outputMessageBuffer.setValue((char *)"Output message");
101 CPPUNIT_ASSERT_EQUAL_MESSAGE(
102 "The requested GSS context handle is not correct",
104 MockUnwrap::context_handle
107 CPPUNIT_ASSERT_EQUAL_MESSAGE(
108 "The input message was incorrect.",
110 MockUnwrap::inputMessageBuffer.toString()
115 CPPUNIT_ASSERT_EQUAL_MESSAGE(
116 "The conf_state flag was incorrect.",
117 MockUnwrap::qop_state,
121 CPPUNIT_ASSERT_EQUAL_MESSAGE(
122 "The conf_state flag was incorrect.",
123 MockUnwrap::conf_state,
127 CPPUNIT_ASSERT_EQUAL_MESSAGE(
128 "The input message was incorrect.",
129 cmd.getOutputMessage().toString(),
130 MockUnwrap::outputMessageBuffer.toString()
138 void GSSUnwrapTest::testConstructorWithJSONObject()
141 const char* input = "{\"method\": \"gss_wrap\", \
144 \"context_handle\": \"#######\", \
145 \"input_message\": \"mary had a little lamb\" \
148 json_error_t jsonErr;
149 JSONObject json = JSONObject::load(input, 0, &jsonErr);
151 GSSUnwrap cmd = GSSUnwrap(&json, &mock_unwrap);
157 CPPUNIT_ASSERT_EQUAL_MESSAGE(
158 "GSSUnwrap did not parse the input message argument correctly",
159 std::string("mary had a little lamb"),
160 cmd.getInputMessage().toString()
170 void GSSUnwrapTest::testJSONMarshal()
173 std::string output("dns@google.com");
175 gss_qop_t qopState = GSS_C_QOP_DEFAULT;
177 GSSUnwrap cmd = GSSUnwrap(&mock_unwrap);
181 MockUnwrap::minor_status = 0;
182 MockUnwrap::retVal = 0;
183 MockUnwrap::outputMessageBuffer.setValue(output);
184 MockUnwrap::conf_state = confState;
185 MockUnwrap::qop_state = qopState;
189 result = cmd.toJSON();
190 // std::cout << "\nGSSUnwrap JSON: \n" << result->dump() << "\n";
192 CPPUNIT_ASSERT_EQUAL_MESSAGE(
193 "The command name is incorrect",
194 std::string("gss_wrap"),
195 std::string( (*result)["command"].string() )
199 CPPUNIT_ASSERT_EQUAL_MESSAGE(
200 "The return value was reported incorrectly",
201 (int)MockUnwrap::retVal,
202 (int)( (*result)["return_values"]["major_status"].integer() )
205 CPPUNIT_ASSERT_EQUAL_MESSAGE(
206 "The minor_status value was reported incorrectly",
207 (int)MockUnwrap::minor_status,
208 (int)( (*result)["return_values"]["minor_status"].integer() )
211 CPPUNIT_ASSERT_EQUAL_MESSAGE(
212 "The output message was reported incorrectly",
213 MockUnwrap::outputMessageBuffer.toString(),
214 std::string( (*result)["return_values"]["output_message"].string() )
217 CPPUNIT_ASSERT_EQUAL_MESSAGE(
218 "The minor_status value was reported incorrectly",
220 (int)( (*result)["return_values"]["qop_state"].integer() )