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.
37 #include "GSSUnwrapTest.h"
38 #include "command_mocks/MockUnwrap.h"
39 #include "GSSUnwrap.h"
40 #include <datamodel/GSSContext.h>
41 #include <cache/GSSContextCache.h>
42 #include <gssapi/gssapi.h>
45 CPPUNIT_TEST_SUITE_REGISTRATION( GSSUnwrapTest );
48 * a mock of the gss_import_name call
50 * Basically, just copy the arguments over to/from the
51 * MockUnwrap global object
54 static OM_uint32 KRB5_CALLCONV
56 OM_uint32 *minor_status,
57 gss_ctx_id_t context_handle,
58 gss_buffer_t input_message_buffer,
59 gss_buffer_t output_message_buffer,
69 // Copy our input from the appropriate parameters to MockUnwrap
70 MockUnwrap::context_handle = context_handle;
71 MockUnwrap::inputMessageBuffer.setValue(input_message_buffer);
74 // copy our output to the appropriate parameters
75 *minor_status = MockUnwrap::minor_status;
76 *conf_state = MockUnwrap::conf_state;
77 *qop_state = MockUnwrap::qop_state;
78 *output_message_buffer = *MockUnwrap::outputMessageBuffer.toGss();
82 return MockUnwrap::retVal;
85 void GSSUnwrapTest::setUp()
87 CppUnit::TestFixture::setUp();
91 void GSSUnwrapTest::testConstructor()
94 GSSUnwrap cmd = GSSUnwrap();
99 CPPUNIT_ASSERT_EQUAL_MESSAGE(
100 "The GSSUnwrap object has the wrong GSS function",
102 (void *)cmd.getGSSFunction()
109 void GSSUnwrapTest::testEmptyCall()
112 GSSUnwrap cmd = GSSUnwrap(&mock_unwrap);
113 GSSBuffer input((char *)"Input message");
114 gss_ctx_id_t desiredContext = (gss_ctx_id_t)( (long)0 | rand());
118 cmd.setContextHandle(desiredContext);
119 cmd.setInputMessage(&input);
121 MockUnwrap::minor_status = 0;
122 MockUnwrap::retVal = 0;
123 MockUnwrap::conf_state = rand();
124 MockUnwrap::qop_state = GSS_C_QOP_DEFAULT;
125 MockUnwrap::outputMessageBuffer.setValue((char *)"Output message");
130 CPPUNIT_ASSERT_EQUAL_MESSAGE(
131 "The requested GSS context handle is not correct",
133 MockUnwrap::context_handle
136 CPPUNIT_ASSERT_EQUAL_MESSAGE(
137 "The input message was incorrect.",
139 MockUnwrap::inputMessageBuffer.toString()
144 CPPUNIT_ASSERT_EQUAL_MESSAGE(
145 "The conf_state flag was incorrect.",
146 MockUnwrap::qop_state,
150 CPPUNIT_ASSERT_EQUAL_MESSAGE(
151 "The conf_state flag was incorrect.",
152 MockUnwrap::conf_state,
156 CPPUNIT_ASSERT_EQUAL_MESSAGE(
157 "The input message was incorrect.",
158 cmd.getOutputMessage().toString(),
159 MockUnwrap::outputMessageBuffer.toString()
167 void GSSUnwrapTest::testConstructorWithJSONObject()
170 GSSContext context((gss_ctx_id_t)( (long)0 | rand()),
172 std::string key = GSSContextCache::instance()->store(context);
173 std::string input = "{ \
174 \"context_handle\": \"" + key + "\", \
175 \"input_message\": \"mary had a little lamb\" \
177 json_error_t jsonErr;
178 JSONObject json = JSONObject::load(input.c_str(), 0, &jsonErr);
180 GSSUnwrap cmd = GSSUnwrap(&json, &mock_unwrap);
186 CPPUNIT_ASSERT_EQUAL_MESSAGE(
187 "GSSUnwrap did not retrieve the GSS context correctly",
188 context.getContext(),
189 cmd.getContextHandle()
192 CPPUNIT_ASSERT_EQUAL_MESSAGE(
193 "GSSUnwrap did not parse the input message argument correctly",
194 std::string("mary had a little lamb"),
195 cmd.getInputMessage().toString()
205 void GSSUnwrapTest::testJSONMarshal()
208 std::string output("dns@google.com");
210 gss_qop_t qopState = GSS_C_QOP_DEFAULT;
212 GSSUnwrap cmd = GSSUnwrap(&mock_unwrap);
216 MockUnwrap::minor_status = 0;
217 MockUnwrap::retVal = 0;
218 MockUnwrap::outputMessageBuffer.setValue(output);
219 MockUnwrap::conf_state = confState;
220 MockUnwrap::qop_state = qopState;
224 result = cmd.toJSON();
225 // std::cout << "\nGSSUnwrap JSON: \n" << result->dump() << "\n";
228 CPPUNIT_ASSERT_EQUAL_MESSAGE(
229 "The return value was reported incorrectly",
230 (int)MockUnwrap::retVal,
231 (int)( (*result)["major_status"].integer() )
234 CPPUNIT_ASSERT_EQUAL_MESSAGE(
235 "The minor_status value was reported incorrectly",
236 (int)MockUnwrap::minor_status,
237 (int)( (*result)["minor_status"].integer() )
240 CPPUNIT_ASSERT_EQUAL_MESSAGE(
241 "The output message was reported incorrectly",
242 MockUnwrap::outputMessageBuffer.toString(),
243 std::string( (*result)["output_message"].string() )
246 CPPUNIT_ASSERT_EQUAL_MESSAGE(
247 "The minor_status value was reported incorrectly",
249 (int)( (*result)["qop_state"].integer() )