2 * Copyright (c) 2014, 2015 JANET(UK)
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * 3. Neither the name of JANET(UK) nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
23 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
24 * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
25 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
26 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
27 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
29 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
31 * OF THE POSSIBILITY OF SUCH DAMAGE.
35 #include "GSSWrapTest.h"
36 #include "command_mocks/MockWrap.h"
38 #include <datamodel/GSSContext.h>
39 #include <cache/GSSContextCache.h>
40 #include <gssapi/gssapi.h>
42 CPPUNIT_TEST_SUITE_REGISTRATION( GSSWrapTest );
45 * a mock of the gss_import_name call
47 * Basically, just copy the arguments over to/from the
48 * MockWrap global object
51 static OM_uint32 KRB5_CALLCONV
53 OM_uint32 *minor_status,
54 gss_ctx_id_t context_handle,
57 gss_buffer_t input_message_buffer,
59 gss_buffer_t output_message_buffer)
67 // Copy our input from the appropriate parameters to MockWrap
68 MockWrap::context_handle = context_handle;
69 MockWrap::conf_req_flag = conf_req_flag;
70 MockWrap::qop_req = qop_req;
71 MockWrap::inputMessageBuffer.setValue(input_message_buffer);
74 // copy our output to the appropriate parameters
75 *minor_status = MockWrap::minor_status;
76 *conf_state = MockWrap::conf_state;
77 *output_message_buffer = *MockWrap::outputMessageBuffer.toGss();
81 return MockWrap::retVal;
84 void GSSWrapTest::setUp()
86 CppUnit::TestFixture::setUp();
90 void GSSWrapTest::testConstructor()
93 GSSWrap cmd = GSSWrap();
98 CPPUNIT_ASSERT_EQUAL_MESSAGE(
99 "The GSSWrap object has the wrong GSS function",
101 (void *)cmd.getGSSFunction()
108 void GSSWrapTest::testEmptyCall()
111 GSSWrap cmd = GSSWrap(&mock_wrap);
112 GSSBuffer input((char *)"Input message");
113 gss_qop_t desiredQop = rand();
114 int desiredConf = rand();
115 gss_ctx_id_t desiredContext = (gss_ctx_id_t)( (long)0 | rand() );
119 cmd.setContextHandle(desiredContext);
120 cmd.setConfReq( desiredConf );
121 cmd.setQopReq( desiredQop );
122 cmd.setInputMessage(&input);
124 MockWrap::minor_status = 0;
125 MockWrap::retVal = 0;
126 MockWrap::conf_state = rand();
127 MockWrap::outputMessageBuffer.setValue((char *)"Output message");
132 CPPUNIT_ASSERT_EQUAL_MESSAGE(
133 "The requested GSS context handle is not correct",
135 MockWrap::context_handle
138 CPPUNIT_ASSERT_EQUAL_MESSAGE(
139 "The conf_req flag was incorrect.",
141 MockWrap::conf_req_flag
144 CPPUNIT_ASSERT_EQUAL_MESSAGE(
145 "The qop_req flag was incorrect.",
150 CPPUNIT_ASSERT_EQUAL_MESSAGE(
151 "The input message was incorrect.",
153 MockWrap::inputMessageBuffer.toString()
158 CPPUNIT_ASSERT_EQUAL_MESSAGE(
159 "The conf_state flag was incorrect.",
160 MockWrap::conf_state,
164 CPPUNIT_ASSERT_EQUAL_MESSAGE(
165 "The input message was incorrect.",
166 cmd.getOutputMessage().toString(),
167 MockWrap::outputMessageBuffer.toString()
176 * Example JSON input:
178 * "method": "gss_wrap",
180 * "context_handle": "stuff",
181 * "conf_req": "TRUE",
182 * "qop_req": "GSS_C_QOP_DEFAULT",
183 * "input_message": "mary had a little lamb"
188 void GSSWrapTest::testConstructorWithJSONObject()
191 GSSContext context( (gss_ctx_id_t)( (long)0 | rand()),
193 std::string key = GSSContextCache::instance()->store(context);
195 std::string input = "{ \
196 \"context_handle\": \"" + key + "\", \
197 \"conf_req\": \"TRUE\", \
198 \"qop_req\": \"GSS_C_QOP_DEFAULT\", \
199 \"input_message\": \"mary had a little lamb\" \
201 json_error_t jsonErr;
202 JSONObject json = JSONObject::load(input.c_str(), 0, &jsonErr);
204 GSSWrap cmd = GSSWrap(&json, &mock_wrap);
210 CPPUNIT_ASSERT_EQUAL_MESSAGE(
211 "GSSWrap did not retrive the GSS context correctly",
212 context.getContext(),
216 CPPUNIT_ASSERT_EQUAL_MESSAGE(
217 "GSSWrap did not parse the conf_req argument correctly",
222 CPPUNIT_ASSERT_EQUAL_MESSAGE(
223 "GSSWrap did not parse the qop_req argument correctly",
228 CPPUNIT_ASSERT_EQUAL_MESSAGE(
229 "GSSWrap did not parse the input message argument correctly",
230 std::string("mary had a little lamb"),
231 cmd.getInputMessage().toString()
240 /* Desired JSON output:
243 * "command": "gss_wrap",
248 * "conf_state": "TRUE",
249 * "output_message": "asdf"
253 void GSSWrapTest::testJSONMarshal()
256 std::string output("dns@google.com");
259 GSSWrap cmd = GSSWrap(&mock_wrap);
263 MockWrap::minor_status = 0;
264 MockWrap::retVal = 0;
265 MockWrap::outputMessageBuffer.setValue(output);
266 MockWrap::conf_state = confState;
270 result = cmd.toJSON();
271 // std::cout << "\nGSSWrap JSON: \n" << result->dump() << "\n";
274 CPPUNIT_ASSERT_EQUAL_MESSAGE(
275 "The return value was reported incorrectly",
276 (int)MockWrap::retVal,
277 (int)( (*result)["major_status"].integer() )
280 CPPUNIT_ASSERT_EQUAL_MESSAGE(
281 "The minor_status value was reported incorrectly",
282 (int)MockWrap::minor_status,
283 (int)( (*result)["minor_status"].integer() )
286 CPPUNIT_ASSERT_EQUAL_MESSAGE(
287 "The output message was reported incorrectly",
288 MockWrap::outputMessageBuffer.toString(),
289 std::string( (*result)["output_message"].string() )