2 * Copyright (c) 2014 <copyright holder> <email>
4 * For license details, see the LICENSE file in the root of this project.
9 #include "GSSCreateSecContextTest.h"
10 #include "GSSCreateSecContextCommand.h"
11 #include "command_mocks/InitSecContextMock.h"
15 #include "util_json.h"
16 #include <cache/GSSContextCache.h>
17 #include <datamodel/GSSContext.h>
19 // Registers the fixture into the 'registry'
20 CPPUNIT_TEST_SUITE_REGISTRATION( GSSCreateSecContextTest );
24 static OM_uint32 KRB5_CALLCONV
26 OM_uint32 *minor_status,
27 gss_cred_id_t claimant_cred_handle,
28 gss_ctx_id_t *context_handle,
29 gss_name_t target_name,
33 gss_channel_bindings_t input_chan_bindings,
34 gss_buffer_t input_token,
35 gss_OID *actual_mech_type,
36 gss_buffer_t output_token,
40 gss_ctx_id_t tmpContext;
42 InitSecContextMock::visited = true;
44 /* Copy in the input to this function */
45 InitSecContextMock::claimant_cred_handle = claimant_cred_handle;
46 InitSecContextMock::target_name = target_name;
47 InitSecContextMock::mech_type = mech_type;
48 InitSecContextMock::req_flags = req_flags;
49 InitSecContextMock::time_req = time_req;
50 InitSecContextMock::input_chan_bindings = input_chan_bindings;
51 InitSecContextMock::input_token.length = input_token->length;
52 InitSecContextMock::input_token.value = input_token->value;
55 /* Copy out the output from this function */
56 *minor_status = InitSecContextMock::minor_status;
57 *actual_mech_type = InitSecContextMock::actual_mech_type;
58 output_token->length = InitSecContextMock::output_token.length;
59 output_token->value = InitSecContextMock::output_token.value;
60 *ret_flags = InitSecContextMock::ret_flags;
61 *time_rec = InitSecContextMock::time_rec;
63 /* Handle the one that's I/O */
64 tmpContext = *context_handle;
65 *context_handle = InitSecContextMock::context_handle;
66 InitSecContextMock::context_handle = tmpContext;
68 return InitSecContextMock::retVal;
73 GSSCreateSecContextTest::setUp()
75 InitSecContextMock::reset();
79 GSSCreateSecContextTest::tearDown()
84 GSSCreateSecContextTest::testConstructor()
86 GSSCreateSecContextCommand cmd = GSSCreateSecContextCommand();
90 cmdFn = cmd.getGSSFunction();
91 GSSFn = (void *)&gss_init_sec_context;
92 CPPUNIT_ASSERT_MESSAGE(
93 "The default constructor for GSSCreateSecContextCommand should assign the function gss_init_sec_context",
99 * "method": "gss_create_sec_context",
104 * "mech_type": "{ 1 2 840 113554 1 2 1 4 }",
105 * "target_name": "me@my.sha/DOW"
109 void GSSCreateSecContextTest::testConstructorWithJSONObject()
111 const char* input = "{\"method\": \"gss_create_sec_context\", \
112 \"arguments\": {\"req_flags\": \"1\", \
113 \"time_req\": \"2\", \
114 \"mech_type\": \"{ 1 2 840 113554 1 2 1 4 }\", \
115 \"target_name\": \"me@my.sha/DOW\"}}";
117 json_error_t jsonErr;
118 JSONObject json = JSONObject::load(input, 0, &jsonErr);
120 GSSCreateSecContextCommand cmd = GSSCreateSecContextCommand(
122 (void *)&mock_init_sec
126 const char *from_json = json["arguments"]["target_name"].string();
127 const char *from_cmd = cmd.getTargetDisplayName();
129 CPPUNIT_ASSERT_MESSAGE(
130 "The object does not have a target name.",
131 ( strcmp(from_json, from_cmd) == 0 )
134 CPPUNIT_ASSERT_EQUAL_MESSAGE(
135 "The context_handle values differ.",
136 json["arguments"]["context_handle"].integer(),
137 (json_int_t)cmd.getContextHandle()
140 CPPUNIT_ASSERT_MESSAGE(
141 "The mech_type values differ.",
142 ( strcmp(json["arguments"]["mech_type"].string(), cmd.getMechType()) == 0 )
145 CPPUNIT_ASSERT_EQUAL_MESSAGE(
146 "The req_flags differ.",
147 (int)json["arguments"]["req_flags"].integer(),
148 (int)cmd.getReqFlags()
151 CPPUNIT_ASSERT_EQUAL_MESSAGE(
152 "The req_flags differ.",
153 (int)json["arguments"]["time_req"].integer(),
154 (int)cmd.getTimeReq()
160 GSSCreateSecContextTest::testEmptyCall()
162 gss_ctx_id_t expectedResult, expectedArgument;
164 GSSCreateSecContextCommand cmd ((void *)&mock_init_sec);
166 /* Set expectations on what the GSS function will be called with */
167 cmd.time_req = rand() % 1024;
168 cmd.req_flags = rand() % 1024;
169 cmd.target_name = NULL;
170 cmd.context_handle = expectedArgument = (gss_ctx_id_t)rand();
172 CPPUNIT_ASSERT_MESSAGE(
173 "The mech_type values differ.",
174 ( strcmp("{ 1 2 840 113554 1 2 1 4 }", cmd.getMechType()) == 0 )
179 /* Set expectations on what the GSS function will produce */
180 InitSecContextMock::retVal = rand() % 1024;
181 InitSecContextMock::minor_status = rand() % 1024;
182 InitSecContextMock::context_handle = expectedResult = (gss_ctx_id_t)rand();
183 InitSecContextMock::actual_mech_type = NULL;
184 InitSecContextMock::output_token.value = (void *)"http@project-moonshot.org/PROJECT-MOONSHOT.ORG\0";
185 InitSecContextMock::output_token.length = strlen((char *)InitSecContextMock::output_token.value);
186 InitSecContextMock::ret_flags = rand() % 1024;
187 InitSecContextMock::time_req = rand() % 1024;
191 /* Check that init_sec_context's inputs are sent correctly */
192 CPPUNIT_ASSERT_MESSAGE(
193 "The GSS function was not invoked!",
194 InitSecContextMock::visited
196 CPPUNIT_ASSERT_EQUAL_MESSAGE(
197 "The time_req field was not used in the call to init_sec_context",
199 InitSecContextMock::time_req
201 CPPUNIT_ASSERT_EQUAL_MESSAGE(
202 "The req_flags field was not used in the call to init_sec_context",
204 InitSecContextMock::req_flags
206 CPPUNIT_ASSERT_EQUAL_MESSAGE(
207 "The mech_type field was not used in the call to init_sec_context",
209 InitSecContextMock::mech_type
211 CPPUNIT_ASSERT_EQUAL_MESSAGE(
212 "The target_name field was not used in the call to init_sec_context",
214 InitSecContextMock::target_name
218 /* Check that init_sec_context's outputs are captured correctly */
219 CPPUNIT_ASSERT_EQUAL_MESSAGE(
220 "Return value was not copied back to the command.",
221 InitSecContextMock::retVal,
224 CPPUNIT_ASSERT_EQUAL_MESSAGE(
225 "Status was not copied back to the command.",
226 InitSecContextMock::minor_status,
229 CPPUNIT_ASSERT_EQUAL_MESSAGE(
230 "context_handle was not copied back to the command.",
234 CPPUNIT_ASSERT_EQUAL_MESSAGE(
235 "context_handle was not copied back to the command.",
237 InitSecContextMock::context_handle
239 CPPUNIT_ASSERT_EQUAL_MESSAGE(
240 "actual_mech_type was not copied back to the command.",
241 InitSecContextMock::actual_mech_type,
244 CPPUNIT_ASSERT_EQUAL_MESSAGE(
245 "output_token was not copied back to the command.",
246 InitSecContextMock::output_token.value,
247 cmd.output_token.value
249 CPPUNIT_ASSERT_EQUAL_MESSAGE(
250 "ret_flags was not copied back to the command.",
251 InitSecContextMock::ret_flags,
254 CPPUNIT_ASSERT_EQUAL_MESSAGE(
255 "time_rec was not copied back to the command.",
256 InitSecContextMock::time_rec,
260 // Set this to no context, or cleanup attempts to free the not-a-real-pointer.
261 InitSecContextMock::context_handle = GSS_C_NO_CONTEXT;
265 /* Expected JSON output:
268 * "command": "gss_init_sec_context",
271 * "context_handle": "base64_encoded_string",
272 * "major_status": ##,
273 * "output_token": "http@project-moonshot.org/PROJECT-MOONSHOT.ORG",
274 * "actual_mech_type": "{ 1 3 6 1 5 5 13 4 }",
275 * "minor_status": ##,
282 void GSSCreateSecContextTest::testJSONMarshal()
285 GSSCreateSecContextCommand cmd ((void *)&mock_init_sec);
287 GSSContextCache *cache = GSSContextCache::instance();
289 gss_ctx_id_t expectedResult;
294 // Set expectations on what the GSS function will produce
295 InitSecContextMock::retVal = GSS_S_BAD_MECH;
296 InitSecContextMock::minor_status = 20;
297 InitSecContextMock::context_handle = expectedResult = (gss_ctx_id_t)rand();
298 InitSecContextMock::actual_mech_type = (gss_OID)GSS_C_MA_MECH_NEGO;
299 InitSecContextMock::output_token.value = (void *)"http@project-moonshot.org/PROJECT-MOONSHOT.ORG\0";
300 InitSecContextMock::output_token.length = strlen((char *)InitSecContextMock::output_token.value);
301 InitSecContextMock::ret_flags = GSS_C_MUTUAL_FLAG |
303 GSS_C_SEQUENCE_FLAG |
307 GSS_C_PROT_READY_FLAG;
308 InitSecContextMock::time_req = GSS_C_INDEFINITE;
312 result = cmd.toJSON();
314 std::cout << "create sec context json: " << result->dump() << "\n";*/
316 CPPUNIT_ASSERT_MESSAGE(
317 "The command name is incorrect",
318 ( strcmp("gss_init_sec_context",
319 (*result)["command"].string() ) == 0 )
323 CPPUNIT_ASSERT_EQUAL_MESSAGE(
324 "The return value was reported incorrectly",
325 (int)InitSecContextMock::retVal,
326 (int)( (*result)["return_values"]["major_status"].integer() )
329 CPPUNIT_ASSERT_EQUAL_MESSAGE(
330 "The minor_status value was reported incorrectly",
331 (int)InitSecContextMock::minor_status,
332 (int)( (*result)["return_values"]["minor_status"].integer() )
335 CPPUNIT_ASSERT_MESSAGE(
336 "The actual_mech_type value was reported incorrectly",
337 ( strcmp("{ 1 3 6 1 5 5 13 4 }",
338 (*result)["return_values"]["actual_mech_type"].string() ) == 0 )
341 CPPUNIT_ASSERT_MESSAGE(
342 "The output_token value was reported incorrectly",
343 ( strcmp((const char *)(InitSecContextMock::output_token.value),
344 (*result)["return_values"]["output_token"].string() ) == 0 )
347 CPPUNIT_ASSERT_EQUAL_MESSAGE(
348 "The minor_status value was reported incorrectly",
349 (int)InitSecContextMock::ret_flags,
350 (int)( (*result)["return_values"]["ret_flags"].integer() )
353 CPPUNIT_ASSERT_EQUAL_MESSAGE(
354 "The minor_status value was reported incorrectly",
355 (int)InitSecContextMock::time_rec,
356 (int)( (*result)["return_values"]["time_rec"].integer() )
359 context = cache->retrieve( (*result)["return_values"]["context_handle"].string() );
361 CPPUNIT_ASSERT_EQUAL_MESSAGE(
362 "The returned context was reported incorrectly",
363 (long)expectedResult,
364 (long)context.getContext()