2 * Copyright (c) 2014 <copyright holder> <email>
4 * For license details, see the LICENSE file in the root of this project.
8 #include "GSSWrapTest.h"
9 #include "command_mocks/MockWrap.h"
11 #include <gssapi/gssapi.h>
13 CPPUNIT_TEST_SUITE_REGISTRATION( GSSWrapTest );
16 * a mock of the gss_import_name call
18 * Basically, just copy the arguments over to/from the
19 * MockWrap global object
22 static OM_uint32 KRB5_CALLCONV
24 OM_uint32 *minor_status,
25 gss_ctx_id_t context_handle,
28 gss_buffer_t input_message_buffer,
30 gss_buffer_t output_message_buffer)
37 buffer = MockWrap::outputMessageBuffer.toString();
40 // Copy our input from the appropriate parameters to MockWrap
41 MockWrap::context_handle = context_handle;
42 MockWrap::conf_req_flag = conf_req_flag;
43 MockWrap::qop_req = qop_req;
44 MockWrap::inputMessageBuffer.setValue(input_message_buffer);
47 // copy our output to the appropriate parameters
48 *minor_status = MockWrap::minor_status;
49 *conf_state = MockWrap::conf_state;
50 output_message_buffer->length = buffer.length();
51 output_message_buffer->value = (void *)buffer.c_str();
55 return MockWrap::retVal;
58 void GSSWrapTest::setUp()
60 CppUnit::TestFixture::setUp();
64 void GSSWrapTest::testConstructor()
67 GSSWrap cmd = GSSWrap();
72 CPPUNIT_ASSERT_EQUAL_MESSAGE(
73 "The GSSWrap object has the wrong GSS function",
75 (void *)cmd.getGSSFunction()
82 void GSSWrapTest::testEmptyCall()
85 GSSWrap cmd = GSSWrap(&mock_wrap);
86 GSSBuffer input((char *)"Input message");
87 gss_qop_t desiredQop = rand();
88 int desiredConf = rand();
89 gss_ctx_id_t desiredContext = (gss_ctx_id_t)rand();
93 cmd.setContextHandle(desiredContext);
94 cmd.setConfReq( desiredConf );
95 cmd.setQopReq( desiredQop );
96 cmd.setInputMessage(&input);
98 MockWrap::minor_status = 0;
100 MockWrap::conf_state = rand();
101 MockWrap::outputMessageBuffer.setValue((char *)"Output message");
106 CPPUNIT_ASSERT_EQUAL_MESSAGE(
107 "The requested GSS context handle is not correct",
109 MockWrap::context_handle
112 CPPUNIT_ASSERT_EQUAL_MESSAGE(
113 "The conf_req flag was incorrect.",
115 MockWrap::conf_req_flag
118 CPPUNIT_ASSERT_EQUAL_MESSAGE(
119 "The qop_req flag was incorrect.",
124 CPPUNIT_ASSERT_EQUAL_MESSAGE(
125 "The input message was incorrect.",
127 MockWrap::inputMessageBuffer.toString()
132 CPPUNIT_ASSERT_EQUAL_MESSAGE(
133 "The conf_state flag was incorrect.",
134 MockWrap::conf_state,
138 CPPUNIT_ASSERT_EQUAL_MESSAGE(
139 "The input message was incorrect.",
140 cmd.getOutputMessage().toString(),
141 MockWrap::outputMessageBuffer.toString()
149 void GSSWrapTest::testConstructorWithJSONObject()
152 const char* input = "{\"method\": \"gss_wrap\", \
155 \"context_handle\": \"#######\", \
156 \"conf_req\": \"TRUE\", \
157 \"qop_req\": \"GSS_C_QOP_DEFAULT\", \
158 \"input_message\": \"mary had a little lamb\" \
161 json_error_t jsonErr;
162 JSONObject json = JSONObject::load(input, 0, &jsonErr);
164 GSSWrap cmd = GSSWrap(&json, &mock_wrap);
170 CPPUNIT_ASSERT_EQUAL_MESSAGE(
171 "GSSWrap did not parse the conf_req argument correctly",
176 CPPUNIT_ASSERT_EQUAL_MESSAGE(
177 "GSSWrap did not parse the qop_req argument correctly",
182 CPPUNIT_ASSERT_EQUAL_MESSAGE(
183 "GSSWrap did not parse the input message argument correctly",
184 std::string("mary had a little lamb"),
185 cmd.getInputMessage().toString()
195 void GSSWrapTest::testJSONMarshal()
198 std::string output("dns@google.com");
201 GSSWrap cmd = GSSWrap(&mock_wrap);
205 MockWrap::minor_status = 0;
206 MockWrap::retVal = 0;
207 MockWrap::outputMessageBuffer.setValue(output);
208 MockWrap::conf_state = confState;
212 result = cmd.toJSON();
213 // std::cout << "\nGSSWrap JSON: \n" << result->dump() << "\n";
215 CPPUNIT_ASSERT_EQUAL_MESSAGE(
216 "The command name is incorrect",
217 std::string("gss_wrap"),
218 std::string( (*result)["command"].string() )
222 CPPUNIT_ASSERT_EQUAL_MESSAGE(
223 "The return value was reported incorrectly",
224 (int)MockWrap::retVal,
225 (int)( (*result)["return_values"]["major_status"].integer() )
228 CPPUNIT_ASSERT_EQUAL_MESSAGE(
229 "The minor_status value was reported incorrectly",
230 (int)MockWrap::minor_status,
231 (int)( (*result)["return_values"]["minor_status"].integer() )
234 CPPUNIT_ASSERT_EQUAL_MESSAGE(
235 "The output message was reported incorrectly",
236 MockWrap::outputMessageBuffer.toString(),
237 std::string( (*result)["return_values"]["output_message"].string() )