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)
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 = *MockWrap::outputMessageBuffer.toGss();
54 return MockWrap::retVal;
57 void GSSWrapTest::setUp()
59 CppUnit::TestFixture::setUp();
63 void GSSWrapTest::testConstructor()
66 GSSWrap cmd = GSSWrap();
71 CPPUNIT_ASSERT_EQUAL_MESSAGE(
72 "The GSSWrap object has the wrong GSS function",
74 (void *)cmd.getGSSFunction()
81 void GSSWrapTest::testEmptyCall()
84 GSSWrap cmd = GSSWrap(&mock_wrap);
85 GSSBuffer input((char *)"Input message");
86 gss_qop_t desiredQop = rand();
87 int desiredConf = rand();
88 gss_ctx_id_t desiredContext = (gss_ctx_id_t)( (long)0 | rand() );
92 cmd.setContextHandle(desiredContext);
93 cmd.setConfReq( desiredConf );
94 cmd.setQopReq( desiredQop );
95 cmd.setInputMessage(&input);
97 MockWrap::minor_status = 0;
99 MockWrap::conf_state = rand();
100 MockWrap::outputMessageBuffer.setValue((char *)"Output message");
105 CPPUNIT_ASSERT_EQUAL_MESSAGE(
106 "The requested GSS context handle is not correct",
108 MockWrap::context_handle
111 CPPUNIT_ASSERT_EQUAL_MESSAGE(
112 "The conf_req flag was incorrect.",
114 MockWrap::conf_req_flag
117 CPPUNIT_ASSERT_EQUAL_MESSAGE(
118 "The qop_req flag was incorrect.",
123 CPPUNIT_ASSERT_EQUAL_MESSAGE(
124 "The input message was incorrect.",
126 MockWrap::inputMessageBuffer.toString()
131 CPPUNIT_ASSERT_EQUAL_MESSAGE(
132 "The conf_state flag was incorrect.",
133 MockWrap::conf_state,
137 CPPUNIT_ASSERT_EQUAL_MESSAGE(
138 "The input message was incorrect.",
139 cmd.getOutputMessage().toString(),
140 MockWrap::outputMessageBuffer.toString()
149 * Example JSON input:
151 * "method": "gss_wrap",
153 * "context_handle": "stuff",
154 * "conf_req": "TRUE",
155 * "qop_req": "GSS_C_QOP_DEFAULT",
156 * "input_message": "mary had a little lamb"
161 void GSSWrapTest::testConstructorWithJSONObject()
164 GSSContext context( (gss_ctx_id_t)( (long)0 | rand()),
166 std::string key = GSSContextCache::instance()->store(context);
168 std::string input = "{ \
169 \"context_handle\": \"" + key + "\", \
170 \"conf_req\": \"TRUE\", \
171 \"qop_req\": \"GSS_C_QOP_DEFAULT\", \
172 \"input_message\": \"mary had a little lamb\" \
174 json_error_t jsonErr;
175 JSONObject json = JSONObject::load(input.c_str(), 0, &jsonErr);
177 GSSWrap cmd = GSSWrap(&json, &mock_wrap);
183 CPPUNIT_ASSERT_EQUAL_MESSAGE(
184 "GSSWrap did not retrive the GSS context correctly",
185 context.getContext(),
189 CPPUNIT_ASSERT_EQUAL_MESSAGE(
190 "GSSWrap did not parse the conf_req argument correctly",
195 CPPUNIT_ASSERT_EQUAL_MESSAGE(
196 "GSSWrap did not parse the qop_req argument correctly",
201 CPPUNIT_ASSERT_EQUAL_MESSAGE(
202 "GSSWrap did not parse the input message argument correctly",
203 std::string("mary had a little lamb"),
204 cmd.getInputMessage().toString()
213 /* Desired JSON output:
216 * "command": "gss_wrap",
221 * "conf_state": "TRUE",
222 * "output_message": "asdf"
226 void GSSWrapTest::testJSONMarshal()
229 std::string output("dns@google.com");
232 GSSWrap cmd = GSSWrap(&mock_wrap);
236 MockWrap::minor_status = 0;
237 MockWrap::retVal = 0;
238 MockWrap::outputMessageBuffer.setValue(output);
239 MockWrap::conf_state = confState;
243 result = cmd.toJSON();
244 // std::cout << "\nGSSWrap JSON: \n" << result->dump() << "\n";
247 CPPUNIT_ASSERT_EQUAL_MESSAGE(
248 "The return value was reported incorrectly",
249 (int)MockWrap::retVal,
250 (int)( (*result)["major_status"].integer() )
253 CPPUNIT_ASSERT_EQUAL_MESSAGE(
254 "The minor_status value was reported incorrectly",
255 (int)MockWrap::minor_status,
256 (int)( (*result)["minor_status"].integer() )
259 CPPUNIT_ASSERT_EQUAL_MESSAGE(
260 "The output message was reported incorrectly",
261 MockWrap::outputMessageBuffer.toString(),
262 std::string( (*result)["output_message"].string() )