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 "InitSecContextMock.h"
15 #include "util_json.h"
17 // Registers the fixture into the 'registry'
18 CPPUNIT_TEST_SUITE_REGISTRATION( GSSCreateSecContextTest );
22 OM_uint32 KRB5_CALLCONV
24 OM_uint32 *minor_status,
25 gss_cred_id_t claimant_cred_handle,
26 gss_ctx_id_t *context_handle,
27 gss_name_t target_name,
31 gss_channel_bindings_t input_chan_bindings,
32 gss_buffer_t input_token,
33 gss_OID *actual_mech_type,
34 gss_buffer_t output_token,
38 InitSecContextMock::visited = true;
40 /* Copy in the input to this function */
41 InitSecContextMock::claimant_cred_handle = claimant_cred_handle;
42 InitSecContextMock::target_name = target_name;
43 InitSecContextMock::mech_type = mech_type;
44 InitSecContextMock::req_flags = req_flags;
45 InitSecContextMock::time_req = time_req;
46 InitSecContextMock::input_chan_bindings = input_chan_bindings;
47 InitSecContextMock::input_token = input_token;
49 /* Copy out the output from this function */
50 *minor_status = InitSecContextMock::minor_status;
51 *actual_mech_type = InitSecContextMock::actual_mech_type;
52 output_token->length = InitSecContextMock::output_token->length;
53 output_token->value = InitSecContextMock::output_token->value;
54 *ret_flags = InitSecContextMock::ret_flags;
55 *time_rec = InitSecContextMock::time_rec;
57 /* Handle the one that's I/O */
58 if (*context_handle == GSS_C_NO_CONTEXT)
60 *context_handle = InitSecContextMock::context_handle;
61 } else if (*context_handle != InitSecContextMock::context_handle)
63 InitSecContextMock::invalidContextHandle = true;
66 return InitSecContextMock::retVal;
71 GSSCreateSecContextTest::setUp()
73 InitSecContextMock::reset();
77 GSSCreateSecContextTest::tearDown()
82 GSSCreateSecContextTest::testConstructor()
84 GSSCreateSecContextCommand cmd = GSSCreateSecContextCommand();
88 cmdFn = cmd.getGSSFunction();
89 GSSFn = (void *)&gss_init_sec_context;
90 CPPUNIT_ASSERT_MESSAGE("The default constructor for GSSCreateSecContextCommand should assign the function gss_init_sec_context", cmdFn == GSSFn);
94 void GSSCreateSecContextTest::testConstructorWithJSONObject()
96 const char* input = "{\"req_flags\": \"1\", \
97 \"time_req\": \"2\", \
98 \"mech_type\": \"{ 1 2 840 113554 1 2 1 4 }\", \
99 \"target_name\": \"me@my.sha/DOW\"}";
101 json_error_t jsonErr;
102 JSONObject json = JSONObject::load(input, 0, &jsonErr);
104 GSSCreateSecContextCommand cmd = GSSCreateSecContextCommand(
106 (void *)&mock_init_sec
110 const char *from_json = json["target_name"].string();
111 const char *from_cmd = cmd.getTargetDisplayName();
113 CPPUNIT_ASSERT_MESSAGE(
114 "The object does not have a target name.",
115 ( strcmp(from_json, from_cmd) == 0 )
118 CPPUNIT_ASSERT_EQUAL_MESSAGE(
119 "The context_handle values differ.",
120 json["context_handle"].integer(),
121 (json_int_t)cmd.getContextHandle()
124 CPPUNIT_ASSERT_MESSAGE(
125 "The mech_type values differ.",
126 ( strcmp(json["mech_type"].string(), cmd.getMechType()) == 0 )
129 CPPUNIT_ASSERT_EQUAL_MESSAGE(
130 "The req_flags differ.",
131 (int)json["req_flags"].integer(),
132 (int)cmd.getReqFlags()
135 CPPUNIT_ASSERT_EQUAL_MESSAGE(
136 "The req_flags differ.",
137 (int)json["time_req"].integer(),
138 (int)cmd.getTimeReq()
145 GSSCreateSecContextTest::testEmptyCall()
147 GSSCreateSecContextCommand cmd ((void *)&mock_init_sec);
149 /* Set expectations on what the GSS function will be called with */
150 cmd.time_req = rand() % 1024;
151 cmd.req_flags = rand() % 1024;
152 cmd.target_name = NULL;
153 cmd.context_handle = GSS_C_NO_CONTEXT;
155 CPPUNIT_ASSERT_MESSAGE(
156 "The mech_type values differ.",
157 ( strcmp("{ 1 2 840 113554 1 2 1 4 }", cmd.getMechType()) == 0 )
162 /* Set expectations on what the GSS function will produce */
163 InitSecContextMock::retVal = rand() % 1024;
164 InitSecContextMock::minor_status = rand() % 1024;
165 InitSecContextMock::context_handle = GSS_C_NO_CONTEXT;
166 InitSecContextMock::actual_mech_type = NULL;
167 InitSecContextMock::output_token = (gss_buffer_t)malloc(sizeof(gss_buffer_desc));
168 InitSecContextMock::output_token->value = (void *)"http@project-moonshot.org/PROJECT-MOONSHOT.ORG\0";
169 InitSecContextMock::output_token->length = strlen((char *)InitSecContextMock::output_token->value);
170 InitSecContextMock::ret_flags = rand() % 1024;
171 InitSecContextMock::time_req = rand() % 1024;
175 /* Check that init_sec_context's inputs are sent correctly */
176 CPPUNIT_ASSERT_MESSAGE(
177 "The GSS function was not invoked!",
178 InitSecContextMock::visited
180 CPPUNIT_ASSERT_EQUAL_MESSAGE(
181 "The time_req field was not used in the call to init_sec_context",
183 InitSecContextMock::time_req
185 CPPUNIT_ASSERT_EQUAL_MESSAGE(
186 "The req_flags field was not used in the call to init_sec_context",
188 InitSecContextMock::req_flags
190 CPPUNIT_ASSERT_EQUAL_MESSAGE(
191 "The mech_type field was not used in the call to init_sec_context",
193 InitSecContextMock::mech_type
195 CPPUNIT_ASSERT_EQUAL_MESSAGE(
196 "The target_name field was not used in the call to init_sec_context",
198 InitSecContextMock::target_name
202 /* Check that init_sec_context's outputs are captured correctly */
203 CPPUNIT_ASSERT_EQUAL_MESSAGE(
204 "Return value was not copied back to the command.",
205 InitSecContextMock::retVal,
208 CPPUNIT_ASSERT_EQUAL_MESSAGE(
209 "Status was not copied back to the command.",
210 InitSecContextMock::minor_status,
213 CPPUNIT_ASSERT_EQUAL_MESSAGE(
214 "context_handle was not copied back to the command.",
215 InitSecContextMock::context_handle,
218 CPPUNIT_ASSERT_EQUAL_MESSAGE(
219 "actual_mech_type was not copied back to the command.",
220 InitSecContextMock::actual_mech_type,
223 CPPUNIT_ASSERT_EQUAL_MESSAGE(
224 "output_token was not copied back to the command.",
225 InitSecContextMock::output_token->value,
226 cmd.output_token->value
228 CPPUNIT_ASSERT_EQUAL_MESSAGE(
229 "ret_flags was not copied back to the command.",
230 InitSecContextMock::ret_flags,
233 CPPUNIT_ASSERT_EQUAL_MESSAGE(
234 "time_rec was not copied back to the command.",
235 InitSecContextMock::time_rec,