0cf7ad5495950c0fad4566902fe65bb4862fb63c
[gssweb.git] / json_gssapi / test / GSSCreateSecContextTest.cpp
1 /*
2  * Copyright (c) 2014 <copyright holder> <email>
3  * 
4  * For license details, see the LICENSE file in the root of this project.
5  * 
6  */
7
8
9 #include "GSSCreateSecContextTest.h"
10 #include "GSSCreateSecContextCommand.h"
11 #include "InitSecContextMock.h"
12 #include <iostream>
13 #include <string.h>
14 #include <exception>
15 #include "util_json.h"
16
17 // Registers the fixture into the 'registry'
18 CPPUNIT_TEST_SUITE_REGISTRATION( GSSCreateSecContextTest );
19
20
21
22 OM_uint32 KRB5_CALLCONV
23 mock_init_sec(
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,
28     gss_OID                mech_type,
29     OM_uint32              req_flags,
30     OM_uint32              time_req,
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,
35     OM_uint32             *ret_flags,
36     OM_uint32             *time_rec)
37 {
38   InitSecContextMock::visited = true;
39   
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;
48   
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;
56   
57   /* Handle the one that's I/O */
58     if (*context_handle == GSS_C_NO_CONTEXT)
59   {
60     *context_handle = InitSecContextMock::context_handle;
61   } else if (*context_handle != InitSecContextMock::context_handle)
62   {
63     InitSecContextMock::invalidContextHandle = true;
64   }
65
66   return InitSecContextMock::retVal;
67 }
68
69
70 void
71 GSSCreateSecContextTest::setUp()
72 {
73   InitSecContextMock::reset();
74 }
75
76 void
77 GSSCreateSecContextTest::tearDown()
78 {
79 }
80
81 void
82 GSSCreateSecContextTest::testConstructor()
83 {
84   GSSCreateSecContextCommand cmd = GSSCreateSecContextCommand();
85   void *cmdFn;
86   void *GSSFn;
87   
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);
91 }
92
93
94 void GSSCreateSecContextTest::testConstructorWithJSONObject()
95 {
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\"}";
100
101   json_error_t jsonErr;
102   JSONObject json = JSONObject::load(input, 0, &jsonErr);
103   
104   GSSCreateSecContextCommand cmd = GSSCreateSecContextCommand(
105     &json, 
106     (void *)&mock_init_sec
107   );
108   
109   
110   const char *from_json = json["target_name"].string();
111   const char *from_cmd = cmd.getTargetDisplayName();
112   
113   CPPUNIT_ASSERT_MESSAGE(
114     "The object does not have a target name.",
115     ( strcmp(from_json, from_cmd) == 0 )
116   );
117   
118   CPPUNIT_ASSERT_EQUAL_MESSAGE(
119     "The context_handle values differ.",
120     json["context_handle"].integer(),
121     (json_int_t)cmd.getContextHandle()
122   );
123   
124   CPPUNIT_ASSERT_MESSAGE(
125     "The mech_type values differ.",
126     ( strcmp(json["mech_type"].string(), cmd.getMechType()) == 0 )
127   );
128   
129   CPPUNIT_ASSERT_EQUAL_MESSAGE(
130     "The req_flags differ.",
131     (int)json["req_flags"].integer(),
132     (int)cmd.getReqFlags()
133   );
134   
135   CPPUNIT_ASSERT_EQUAL_MESSAGE(
136     "The req_flags differ.",
137     (int)json["time_req"].integer(),
138     (int)cmd.getTimeReq()
139   );
140   
141   
142 }
143
144 void
145 GSSCreateSecContextTest::testEmptyCall()
146 {
147   GSSCreateSecContextCommand cmd ((void *)&mock_init_sec);
148   
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;
154   
155   CPPUNIT_ASSERT_MESSAGE(
156     "The mech_type values differ.",
157     ( strcmp("{ 1 2 840 113554 1 2 1 4 }", cmd.getMechType()) == 0 )
158   );
159   
160   
161   
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;
172   
173   cmd.execute();
174   
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
179   );
180   CPPUNIT_ASSERT_EQUAL_MESSAGE(
181     "The time_req field was not used in the call to init_sec_context",
182     cmd.time_req,
183     InitSecContextMock::time_req
184   );
185   CPPUNIT_ASSERT_EQUAL_MESSAGE(
186     "The req_flags field was not used in the call to init_sec_context",
187     cmd.req_flags,
188     InitSecContextMock::req_flags
189   );
190   CPPUNIT_ASSERT_EQUAL_MESSAGE(
191     "The mech_type field was not used in the call to init_sec_context",
192     cmd.mech_type,
193     InitSecContextMock::mech_type
194   );
195   CPPUNIT_ASSERT_EQUAL_MESSAGE(
196     "The target_name field was not used in the call to init_sec_context",
197     cmd.target_name,
198     InitSecContextMock::target_name
199   );
200   
201   
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,
206     cmd.retVal
207   );
208   CPPUNIT_ASSERT_EQUAL_MESSAGE(
209     "Status was not copied back to the command.",
210     InitSecContextMock::minor_status,
211     cmd.minor_status
212   );
213   CPPUNIT_ASSERT_EQUAL_MESSAGE(
214     "context_handle was not copied back to the command.",
215     InitSecContextMock::context_handle,
216     cmd.context_handle
217   );
218   CPPUNIT_ASSERT_EQUAL_MESSAGE(
219     "actual_mech_type was not copied back to the command.",
220     InitSecContextMock::actual_mech_type,
221     cmd.actual_mech_type
222   );
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
227   );
228   CPPUNIT_ASSERT_EQUAL_MESSAGE(
229     "ret_flags was not copied back to the command.",
230     InitSecContextMock::ret_flags,
231     cmd.ret_flags
232   );
233   CPPUNIT_ASSERT_EQUAL_MESSAGE(
234     "time_rec was not copied back to the command.",
235     InitSecContextMock::time_rec,
236     cmd.time_rec
237   );
238 }
239