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 <datamodel/GSSContext.h>
12 #include <cache/GSSContextCache.h>
13 #include <gssapi/gssapi.h>
15 CPPUNIT_TEST_SUITE_REGISTRATION( GSSWrapTest );
18 * a mock of the gss_import_name call
20 * Basically, just copy the arguments over to/from the
21 * MockWrap global object
24 static OM_uint32 KRB5_CALLCONV
26 OM_uint32 *minor_status,
27 gss_ctx_id_t context_handle,
30 gss_buffer_t input_message_buffer,
32 gss_buffer_t output_message_buffer)
39 buffer = MockWrap::outputMessageBuffer.toString();
42 // Copy our input from the appropriate parameters to MockWrap
43 MockWrap::context_handle = context_handle;
44 MockWrap::conf_req_flag = conf_req_flag;
45 MockWrap::qop_req = qop_req;
46 MockWrap::inputMessageBuffer.setValue(input_message_buffer);
49 // copy our output to the appropriate parameters
50 *minor_status = MockWrap::minor_status;
51 *conf_state = MockWrap::conf_state;
52 output_message_buffer->length = buffer.length();
53 output_message_buffer->value = (void *)buffer.c_str();
57 return MockWrap::retVal;
60 void GSSWrapTest::setUp()
62 CppUnit::TestFixture::setUp();
66 void GSSWrapTest::testConstructor()
69 GSSWrap cmd = GSSWrap();
74 CPPUNIT_ASSERT_EQUAL_MESSAGE(
75 "The GSSWrap object has the wrong GSS function",
77 (void *)cmd.getGSSFunction()
84 void GSSWrapTest::testEmptyCall()
87 GSSWrap cmd = GSSWrap(&mock_wrap);
88 GSSBuffer input((char *)"Input message");
89 gss_qop_t desiredQop = rand();
90 int desiredConf = rand();
91 gss_ctx_id_t desiredContext = (gss_ctx_id_t)rand();
95 cmd.setContextHandle(desiredContext);
96 cmd.setConfReq( desiredConf );
97 cmd.setQopReq( desiredQop );
98 cmd.setInputMessage(&input);
100 MockWrap::minor_status = 0;
101 MockWrap::retVal = 0;
102 MockWrap::conf_state = rand();
103 MockWrap::outputMessageBuffer.setValue((char *)"Output message");
108 CPPUNIT_ASSERT_EQUAL_MESSAGE(
109 "The requested GSS context handle is not correct",
111 MockWrap::context_handle
114 CPPUNIT_ASSERT_EQUAL_MESSAGE(
115 "The conf_req flag was incorrect.",
117 MockWrap::conf_req_flag
120 CPPUNIT_ASSERT_EQUAL_MESSAGE(
121 "The qop_req flag was incorrect.",
126 CPPUNIT_ASSERT_EQUAL_MESSAGE(
127 "The input message was incorrect.",
129 MockWrap::inputMessageBuffer.toString()
134 CPPUNIT_ASSERT_EQUAL_MESSAGE(
135 "The conf_state flag was incorrect.",
136 MockWrap::conf_state,
140 CPPUNIT_ASSERT_EQUAL_MESSAGE(
141 "The input message was incorrect.",
142 cmd.getOutputMessage().toString(),
143 MockWrap::outputMessageBuffer.toString()
152 * Example JSON input:
154 * "method": "gss_wrap",
156 * "context_handle": "stuff",
157 * "conf_req": "TRUE",
158 * "qop_req": "GSS_C_QOP_DEFAULT",
159 * "input_message": "mary had a little lamb"
164 void GSSWrapTest::testConstructorWithJSONObject()
167 GSSContext context( (gss_ctx_id_t)rand(), true );
168 std::string key = GSSContextCache::instance()->store(context);
170 std::string input = "{\"method\": \"gss_wrap\", \
173 \"context_handle\": \"" + key + "\", \
174 \"conf_req\": \"TRUE\", \
175 \"qop_req\": \"GSS_C_QOP_DEFAULT\", \
176 \"input_message\": \"mary had a little lamb\" \
179 json_error_t jsonErr;
180 JSONObject json = JSONObject::load(input.c_str(), 0, &jsonErr);
182 GSSWrap cmd = GSSWrap(&json, &mock_wrap);
188 CPPUNIT_ASSERT_EQUAL_MESSAGE(
189 "GSSWrap did not retrive the GSS context correctly",
190 context.getContext(),
194 CPPUNIT_ASSERT_EQUAL_MESSAGE(
195 "GSSWrap did not parse the conf_req argument correctly",
200 CPPUNIT_ASSERT_EQUAL_MESSAGE(
201 "GSSWrap did not parse the qop_req argument correctly",
206 CPPUNIT_ASSERT_EQUAL_MESSAGE(
207 "GSSWrap did not parse the input message argument correctly",
208 std::string("mary had a little lamb"),
209 cmd.getInputMessage().toString()
218 /* Desired JSON output:
221 * "command": "gss_wrap",
226 * "conf_state": "TRUE",
227 * "output_message": "asdf"
231 void GSSWrapTest::testJSONMarshal()
234 std::string output("dns@google.com");
237 GSSWrap cmd = GSSWrap(&mock_wrap);
241 MockWrap::minor_status = 0;
242 MockWrap::retVal = 0;
243 MockWrap::outputMessageBuffer.setValue(output);
244 MockWrap::conf_state = confState;
248 result = cmd.toJSON();
249 // std::cout << "\nGSSWrap JSON: \n" << result->dump() << "\n";
251 CPPUNIT_ASSERT_EQUAL_MESSAGE(
252 "The command name is incorrect",
253 std::string("gss_wrap"),
254 std::string( (*result)["command"].string() )
258 CPPUNIT_ASSERT_EQUAL_MESSAGE(
259 "The return value was reported incorrectly",
260 (int)MockWrap::retVal,
261 (int)( (*result)["return_values"]["major_status"].integer() )
264 CPPUNIT_ASSERT_EQUAL_MESSAGE(
265 "The minor_status value was reported incorrectly",
266 (int)MockWrap::minor_status,
267 (int)( (*result)["return_values"]["minor_status"].integer() )
270 CPPUNIT_ASSERT_EQUAL_MESSAGE(
271 "The output message was reported incorrectly",
272 MockWrap::outputMessageBuffer.toString(),
273 std::string( (*result)["return_values"]["output_message"].string() )