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 = "{ \
171 \"context_handle\": \"" + key + "\", \
172 \"conf_req\": \"TRUE\", \
173 \"qop_req\": \"GSS_C_QOP_DEFAULT\", \
174 \"input_message\": \"mary had a little lamb\" \
176 json_error_t jsonErr;
177 JSONObject json = JSONObject::load(input.c_str(), 0, &jsonErr);
179 GSSWrap cmd = GSSWrap(&json, &mock_wrap);
185 CPPUNIT_ASSERT_EQUAL_MESSAGE(
186 "GSSWrap did not retrive the GSS context correctly",
187 context.getContext(),
191 CPPUNIT_ASSERT_EQUAL_MESSAGE(
192 "GSSWrap did not parse the conf_req argument correctly",
197 CPPUNIT_ASSERT_EQUAL_MESSAGE(
198 "GSSWrap did not parse the qop_req argument correctly",
203 CPPUNIT_ASSERT_EQUAL_MESSAGE(
204 "GSSWrap did not parse the input message argument correctly",
205 std::string("mary had a little lamb"),
206 cmd.getInputMessage().toString()
215 /* Desired JSON output:
218 * "command": "gss_wrap",
223 * "conf_state": "TRUE",
224 * "output_message": "asdf"
228 void GSSWrapTest::testJSONMarshal()
231 std::string output("dns@google.com");
234 GSSWrap cmd = GSSWrap(&mock_wrap);
238 MockWrap::minor_status = 0;
239 MockWrap::retVal = 0;
240 MockWrap::outputMessageBuffer.setValue(output);
241 MockWrap::conf_state = confState;
245 result = cmd.toJSON();
246 // std::cout << "\nGSSWrap JSON: \n" << result->dump() << "\n";
249 CPPUNIT_ASSERT_EQUAL_MESSAGE(
250 "The return value was reported incorrectly",
251 (int)MockWrap::retVal,
252 (int)( (*result)["major_status"].integer() )
255 CPPUNIT_ASSERT_EQUAL_MESSAGE(
256 "The minor_status value was reported incorrectly",
257 (int)MockWrap::minor_status,
258 (int)( (*result)["minor_status"].integer() )
261 CPPUNIT_ASSERT_EQUAL_MESSAGE(
262 "The output message was reported incorrectly",
263 MockWrap::outputMessageBuffer.toString(),
264 std::string( (*result)["output_message"].string() )