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)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)rand(), true );
165 std::string key = GSSContextCache::instance()->store(context);
167 std::string input = "{ \
168 \"context_handle\": \"" + key + "\", \
169 \"conf_req\": \"TRUE\", \
170 \"qop_req\": \"GSS_C_QOP_DEFAULT\", \
171 \"input_message\": \"mary had a little lamb\" \
173 json_error_t jsonErr;
174 JSONObject json = JSONObject::load(input.c_str(), 0, &jsonErr);
176 GSSWrap cmd = GSSWrap(&json, &mock_wrap);
182 CPPUNIT_ASSERT_EQUAL_MESSAGE(
183 "GSSWrap did not retrive the GSS context correctly",
184 context.getContext(),
188 CPPUNIT_ASSERT_EQUAL_MESSAGE(
189 "GSSWrap did not parse the conf_req argument correctly",
194 CPPUNIT_ASSERT_EQUAL_MESSAGE(
195 "GSSWrap did not parse the qop_req argument correctly",
200 CPPUNIT_ASSERT_EQUAL_MESSAGE(
201 "GSSWrap did not parse the input message argument correctly",
202 std::string("mary had a little lamb"),
203 cmd.getInputMessage().toString()
212 /* Desired JSON output:
215 * "command": "gss_wrap",
220 * "conf_state": "TRUE",
221 * "output_message": "asdf"
225 void GSSWrapTest::testJSONMarshal()
228 std::string output("dns@google.com");
231 GSSWrap cmd = GSSWrap(&mock_wrap);
235 MockWrap::minor_status = 0;
236 MockWrap::retVal = 0;
237 MockWrap::outputMessageBuffer.setValue(output);
238 MockWrap::conf_state = confState;
242 result = cmd.toJSON();
243 // std::cout << "\nGSSWrap JSON: \n" << result->dump() << "\n";
246 CPPUNIT_ASSERT_EQUAL_MESSAGE(
247 "The return value was reported incorrectly",
248 (int)MockWrap::retVal,
249 (int)( (*result)["major_status"].integer() )
252 CPPUNIT_ASSERT_EQUAL_MESSAGE(
253 "The minor_status value was reported incorrectly",
254 (int)MockWrap::minor_status,
255 (int)( (*result)["minor_status"].integer() )
258 CPPUNIT_ASSERT_EQUAL_MESSAGE(
259 "The output message was reported incorrectly",
260 MockWrap::outputMessageBuffer.toString(),
261 std::string( (*result)["output_message"].string() )