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 "GSSAcquireCredTest.h"
36 #include "GSSAcquireCred.h"
37 #include "command_mocks/MockAcquireCred.h"
38 #include <datamodel/GSSName.h>
39 #include <cache/GSSNameCache.h>
43 CPPUNIT_TEST_SUITE_REGISTRATION( GSSAcquireCredTest );
45 OM_uint32 KRB5_CALLCONV
47 OM_uint32 *minor_status,
48 gss_name_t desired_name,
50 gss_OID_set desired_mechs,
51 gss_cred_usage_t cred_usage,
52 gss_cred_id_t * output_cred_handle,
53 gss_OID_set * actual_mechs,
57 // Set MockAcquireCred attributes from our in-parameters
58 MockAcquireCred::desired_name = desired_name;
59 MockAcquireCred::time_req = time_req;
60 MockAcquireCred::desired_mechs = desired_mechs;
61 MockAcquireCred::cred_usage = cred_usage;
63 // Set our out-parameters from MockAcquireCred
64 *output_cred_handle = MockAcquireCred::output_cred_handle;
65 *actual_mechs = MockAcquireCred::actual_mechs;
66 *time_rec = MockAcquireCred::time_rec;
68 *minor_status = MockAcquireCred::minor_status;
69 return(MockAcquireCred::retVal);
72 void GSSAcquireCredTest::setUp()
77 void GSSAcquireCredTest::tearDown()
82 void GSSAcquireCredTest::testConstructor()
85 GSSAcquireCred cmd = GSSAcquireCred();
90 CPPUNIT_ASSERT_EQUAL_MESSAGE(
91 "The GSSImportName object has the wrong GSS function",
100 void GSSAcquireCredTest::testConstructorWithJSONObject()
103 // To feed into the JSON
104 OM_uint32 time_req = rand();
105 gss_cred_usage_t cred_usage = 2;
106 GSSOID mech( (char *)"{ 1 2 840 113554 1 2 1 4 }" );
107 std::stringstream input;
108 char *desired_name = (char *)"HTTP@localhost\0";
110 OM_uint32 major, minor;
114 major = gss_import_name(&minor, GSSBuffer(desired_name).toGss(), GSS_C_NT_HOSTBASED_SERVICE, &des);
115 if (GSS_ERROR(major))
117 OM_uint32 min, context;
120 std::cout << "Error in importing name." << std::endl;
121 gss_display_status(&min, major, GSS_C_GSS_CODE, GSS_C_NT_HOSTBASED_SERVICE, &context, &buf);
122 std::cout << " message: " << (char *)buf.value << std::endl;
124 CPPUNIT_ASSERT_MESSAGE(
125 "Could not generate a name to test GSSCreateSecContext JSON parsing.",
128 desired.setValue(des);
129 std::string key = GSSNameCache::instance()->store(desired);
133 "{\"desired_name\" : \"" << key << "\", \
134 \"time_req\" : " << time_req << ", \
135 \"cred_usage\" : " << cred_usage << ", \
136 \"desired_mechs\": [\"" << mech.toString() << "\"] \
140 json_error_t jsonErr;
141 JSONObject json = JSONObject::load(input.str().c_str(), 0, &jsonErr);
143 GSSOIDSet desiredMechs;
146 GSSAcquireCred cmd = GSSAcquireCred(&json, &mock_acquire_cred);
151 CPPUNIT_ASSERT_EQUAL_MESSAGE(
152 "GSSAcquireCred's time_req was not loaded correctly",
157 CPPUNIT_ASSERT_EQUAL_MESSAGE(
158 "GSSAcquireCred's cred_usage was not loaded correctly",
163 json.set( "cred_usage", "GSS_C_INITIATE" );
164 cmd.loadParameters(&json);
165 CPPUNIT_ASSERT_EQUAL_MESSAGE(
166 "GSSAcquireCred's cred_usage was not loaded correctly",
168 (int)cmd.getCredUsage()
171 // CPPUNIT_ASSERT_THROW(expression, exception);
172 json.set( "cred_usage", "GSS_C_INITIATOR" );
173 CPPUNIT_ASSERT_THROW_MESSAGE(
174 "GSSAcquireCred's JSON parsing is admitting invalid strings.",
175 cmd.loadParameters(&json),
176 std::invalid_argument
179 desiredMechs = cmd.getDesiredMechs();
180 CPPUNIT_ASSERT_MESSAGE(
181 "The desired mechanisms were not set properly.",
182 desiredMechs.includes(mech)
189 void GSSAcquireCredTest::testEmptyCall()
192 GSSAcquireCred cmd = GSSAcquireCred(&mock_acquire_cred);
194 GSSName steve; // ((char *)"steve@local", (gss_OID)GSS_C_NT_USER_NAME);
195 GSSOID moonshotOID((char *)"{1 3 6 1 5 5 15 1 1 18}");
199 cmd.setDesiredName(steve);
201 cmd.addDesiredMech( moonshotOID );
202 cmd.setCredUsage(GSS_C_INITIATE);
205 MockAcquireCred::retVal = 0;
206 MockAcquireCred::minor_status = 0;
207 MockAcquireCred::output_cred_handle = GSS_C_NO_CREDENTIAL;
208 gss_create_empty_oid_set(&minor, &(MockAcquireCred::actual_mechs));
209 MockAcquireCred::time_rec = rand();
214 CPPUNIT_ASSERT_EQUAL_MESSAGE(
215 "The desired name was not passed in correctly",
217 MockAcquireCred::desired_name
220 CPPUNIT_ASSERT_EQUAL_MESSAGE(
221 "The time_req was not passed in correctly",
223 MockAcquireCred::time_req
226 CPPUNIT_ASSERT_EQUAL_MESSAGE(
227 "The desired mechs were not passed in correctly",
228 cmd.getDesiredMechs(),
229 MockAcquireCred::desired_mechs
232 CPPUNIT_ASSERT_EQUAL_MESSAGE(
233 "The cred_usage was not passed in correctly",
235 MockAcquireCred::cred_usage
238 CPPUNIT_ASSERT_EQUAL_MESSAGE(
239 "The output credential handle was not passed in correctly",
240 MockAcquireCred::output_cred_handle,
241 cmd.getOutputCredHandle()
244 CPPUNIT_ASSERT_EQUAL_MESSAGE(
245 "The actual mechanisms value was not passed in correctly",
246 MockAcquireCred::actual_mechs->count,
247 cmd.getActualMechs()->count
250 CPPUNIT_ASSERT_EQUAL_MESSAGE(
251 "The time_rec value was not passed in correctly",
252 MockAcquireCred::time_rec,
261 /* Desired JSON output:
264 * "command": "gss_acquire_cred",
268 * "cred_handle": "###########",
277 void GSSAcquireCredTest::testJSONMarshal()
280 GSSOIDSet actualMechs;
282 GSSAcquireCred cmd = GSSAcquireCred(&mock_acquire_cred);
286 actualMechs.addOID( GSSOID( (char *)"{ 1 2 3 4 }" ) );
287 actualMechs.addOID( GSSOID( (char *)"{ 1 5 6 7 8 }" ) );
288 MockAcquireCred::reset();
289 MockAcquireCred::retVal = 0;
290 MockAcquireCred::minor_status = 0;
291 MockAcquireCred::output_cred_handle = GSS_C_NO_CREDENTIAL;
292 MockAcquireCred::actual_mechs = actualMechs.toGss();
293 MockAcquireCred::time_rec = 0;
297 result = cmd.toJSON();
299 // std::cout << "\n" << result->dump() << "\n";
302 CPPUNIT_ASSERT_EQUAL_MESSAGE(
303 "The return value was reported incorrectly",
304 (int)MockAcquireCred::retVal,
305 (int)( (*result)["major_status"].integer() )
308 CPPUNIT_ASSERT_EQUAL_MESSAGE(
309 "The minor_status value was reported incorrectly",
310 (int)MockAcquireCred::minor_status,
311 (int)( (*result)["minor_status"].integer() )
314 CPPUNIT_ASSERT_EQUAL_MESSAGE(
315 "The gss_name was reported incorrectly",
316 std::string("{ 1 2 3 4 }"),
317 std::string( (*result)["actual_mechs"][(size_t)0].string() )
320 CPPUNIT_ASSERT_EQUAL_MESSAGE(
321 "The gss_name was reported incorrectly",
322 std::string("{ 1 5 6 7 8 }"),
323 std::string( (*result)["actual_mechs"][(size_t)1].string() )
326 CPPUNIT_ASSERT_EQUAL_MESSAGE(
327 "The minor_status value was reported incorrectly",
328 (int)MockAcquireCred::time_rec,
329 (int)( (*result)["time_rec"].integer() )