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 "GSSGetMicTest.h"
36 #include "command_mocks/MockGetMic.h"
37 #include "GSSGetMic.h"
38 #include <datamodel/GSSContext.h>
39 #include <cache/GSSContextCache.h>
40 #include <gssapi/gssapi.h>
42 CPPUNIT_TEST_SUITE_REGISTRATION( GSSGetMicTest );
45 * a mock of the gss_import_name call
47 * Basically, just copy the arguments over to/from the
48 * MockGetMic global object
51 static OM_uint32 KRB5_CALLCONV
53 OM_uint32 *minor_status,
54 gss_ctx_id_t context_handle,
56 gss_buffer_t message_buffer,
57 gss_buffer_t message_token)
65 // Copy our input from the appropriate parameters to MockGetMic
66 MockGetMic::context_handle = context_handle;
67 MockGetMic::qop_req = qop_req;
68 MockGetMic::inputMessage.setValue((char *)message_buffer->value, message_buffer->length);
71 // copy our output to the appropriate parameters
72 *minor_status = MockGetMic::minor_status;
73 *message_token = *MockGetMic::outputToken.toGss();
77 return MockGetMic::retVal;
80 void GSSGetMicTest::setUp()
82 CppUnit::TestFixture::setUp();
86 void GSSGetMicTest::testConstructor()
89 GSSGetMic cmd = GSSGetMic();
94 CPPUNIT_ASSERT_EQUAL_MESSAGE(
95 "The GSSGetMic object has the wrong GSS function",
97 (void *)cmd.getGSSFunction()
104 void GSSGetMicTest::testEmptyCall()
107 GSSGetMic cmd = GSSGetMic(&mock_get_mic);
108 GSSBuffer input((char *)"Input message");
109 std::string out("Output Message");
110 gss_qop_t desiredQop = rand();
111 gss_ctx_id_t desiredContext = (gss_ctx_id_t)( (long)0 | rand() );
115 cmd.setContextHandle(desiredContext);
116 cmd.setQopReq( desiredQop );
117 cmd.setInputMessage(&input);
119 MockGetMic::minor_status = 0;
120 MockGetMic::retVal = 0;
121 MockGetMic::outputToken.setValue(out);
126 CPPUNIT_ASSERT_EQUAL_MESSAGE(
127 "The requested GSS context handle is not correct",
129 MockGetMic::context_handle
132 CPPUNIT_ASSERT_EQUAL_MESSAGE(
133 "The qop_req flag was incorrect.",
138 CPPUNIT_ASSERT_EQUAL_MESSAGE(
139 "The input message was incorrect.",
141 MockGetMic::inputMessage.toString()
145 CPPUNIT_ASSERT_EQUAL_MESSAGE(
146 "The output message token was incorrect.",
148 cmd.getOutputToken().toString()
156 void GSSGetMicTest::testConstructorWithJSONObject()
159 GSSContext context((gss_ctx_id_t)( (long)0 | rand() ),
161 std::string key = GSSContextCache::instance()->store(context);
163 std::string input = "{ \
164 \"context_handle\": \"" + key + "\", \
165 \"qop_req\": \"GSS_C_QOP_DEFAULT\", \
166 \"input_message\": \"mary had a little lamb\" \
168 json_error_t jsonErr;
169 JSONObject json = JSONObject::load(input.c_str(), 0, &jsonErr);
171 GSSGetMic cmd = GSSGetMic(&json, &mock_get_mic);
177 CPPUNIT_ASSERT_EQUAL_MESSAGE(
178 "GSSGetMic did not parse the qop_req argument correctly",
183 CPPUNIT_ASSERT_EQUAL_MESSAGE(
184 "GSSGetMic did not parse the input message argument correctly",
185 std::string("mary had a little lamb"),
186 cmd.getInputMessage().toString()
189 CPPUNIT_ASSERT_EQUAL_MESSAGE(
190 "GSSGetMic did not retrieve the context handle correctly",
191 context.getContext(),
192 cmd.getContextHandle()
200 /* Desired JSON output:
203 * "command": "gss_get_mic",
208 * "output_token": "asdf"
213 void GSSGetMicTest::testJSONMarshal()
216 std::string output("dns@google.com");
217 GSSBuffer input( (char*)"abc" );
219 GSSGetMic cmd = GSSGetMic(&mock_get_mic);
223 MockGetMic::minor_status = 0;
224 MockGetMic::retVal = 0;
225 MockGetMic::outputToken.setValue(output);
227 cmd.setContextHandle(GSS_C_NO_CONTEXT);
228 cmd.setInputMessage( &input );
229 cmd.setQopReq(GSS_C_QOP_DEFAULT);
233 result = cmd.toJSON();
234 // std::cout << "\nGSSGetMic JSON: \n" << result->dump() << "\n";
236 CPPUNIT_ASSERT_EQUAL_MESSAGE(
237 "The return value was reported incorrectly",
238 (int)MockGetMic::retVal,
239 (int)( (*result)["major_status"].integer() )
242 CPPUNIT_ASSERT_EQUAL_MESSAGE(
243 "The minor_status value was reported incorrectly",
244 (int)MockGetMic::minor_status,
245 (int)( (*result)["minor_status"].integer() )
248 CPPUNIT_ASSERT_EQUAL_MESSAGE(
249 "The output message was reported incorrectly",
251 std::string( (*result)["output_token"].string() )