Add copyright comment headers to appropriate files
[gssweb.git] / json_gssapi / test / GSSAcquireCredTest.cpp
1 /*
2  * Copyright (c) 2014, 2015 JANET(UK)
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  *
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.
15  *
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.
19  *
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.
32  *
33  */
34
35 #include "GSSAcquireCredTest.h"
36 #include "GSSAcquireCred.h"
37 #include "command_mocks/MockAcquireCred.h"
38 #include <datamodel/GSSName.h>
39 #include <cache/GSSNameCache.h>
40 #include <gssapi.h>
41 #include <stdexcept>
42
43 CPPUNIT_TEST_SUITE_REGISTRATION( GSSAcquireCredTest );
44
45 OM_uint32 KRB5_CALLCONV
46 mock_acquire_cred(
47     OM_uint32 *minor_status,
48     gss_name_t desired_name,
49     OM_uint32 time_req,
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,
54     OM_uint32 *time_rec)
55 {
56   
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;
62   
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;
67   
68   *minor_status = MockAcquireCred::minor_status;
69   return(MockAcquireCred::retVal);
70 }
71
72 void GSSAcquireCredTest::setUp()
73 {
74
75 }
76
77 void GSSAcquireCredTest::tearDown()
78 {
79
80 }
81
82 void GSSAcquireCredTest::testConstructor()
83 {
84   /* Variables */
85   GSSAcquireCred cmd = GSSAcquireCred();
86   
87   /* Error checking */
88   /* Setup */
89   /* Main */
90   CPPUNIT_ASSERT_EQUAL_MESSAGE(
91     "The GSSImportName object has the wrong GSS function",
92     &gss_acquire_cred,
93     cmd.getGSSFunction()
94   );
95   
96   /* Cleanup */
97   /* Return */
98 }
99
100 void GSSAcquireCredTest::testConstructorWithJSONObject()
101 {
102   /* Variables */
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";
109   GSSName desired;
110   OM_uint32 major, minor;
111   gss_name_t des;
112   
113   
114   major = gss_import_name(&minor, GSSBuffer(desired_name).toGss(), GSS_C_NT_HOSTBASED_SERVICE, &des);
115   if (GSS_ERROR(major))
116   {
117     OM_uint32 min, context;
118     gss_buffer_desc buf;
119     
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;
123   }
124   CPPUNIT_ASSERT_MESSAGE(
125     "Could not generate a name to test GSSCreateSecContext JSON parsing.",
126     !GSS_ERROR(major)
127   );
128   desired.setValue(des);
129   std::string key = GSSNameCache::instance()->store(desired);
130
131   // The JSON string
132   input << 
133   "{\"desired_name\" : \"" << key << "\", \
134     \"time_req\"     : " << time_req << ", \
135     \"cred_usage\"   : " << cred_usage << ", \
136     \"desired_mechs\": [\"" << mech.toString() << "\"] \
137    }";
138   
139   // The JSON itself
140   json_error_t jsonErr;
141   JSONObject json = JSONObject::load(input.str().c_str(), 0, &jsonErr);
142   
143   GSSOIDSet desiredMechs;
144   
145   
146   GSSAcquireCred cmd = GSSAcquireCred(&json, &mock_acquire_cred);
147   
148   /* Error checking */
149   /* Setup */
150   /* Main */
151   CPPUNIT_ASSERT_EQUAL_MESSAGE(
152     "GSSAcquireCred's time_req was not loaded correctly",
153     time_req,
154     cmd.getTimeReq()
155   );
156   
157   CPPUNIT_ASSERT_EQUAL_MESSAGE(
158     "GSSAcquireCred's cred_usage was not loaded correctly",
159     cred_usage,
160     cmd.getCredUsage()
161   );
162   
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",
167     GSS_C_INITIATE,
168     (int)cmd.getCredUsage()
169   );
170   
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 
177   );
178   
179   desiredMechs = cmd.getDesiredMechs();
180   CPPUNIT_ASSERT_MESSAGE(
181     "The desired mechanisms were not set properly.",
182     desiredMechs.includes(mech)
183   );
184   
185   /* Cleanup */
186   /* Return */
187 }
188
189 void GSSAcquireCredTest::testEmptyCall()
190 {
191   /* Variables */
192   GSSAcquireCred cmd = GSSAcquireCred(&mock_acquire_cred);
193   OM_uint32 minor;
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}");
196   
197   /* Error checking */
198   /* Setup */
199   cmd.setDesiredName(steve);
200   cmd.setTimeReq(0);
201   cmd.addDesiredMech( moonshotOID );
202   cmd.setCredUsage(GSS_C_INITIATE);
203   
204   
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();
210   
211   cmd.execute();
212   
213   /* Main */
214   CPPUNIT_ASSERT_EQUAL_MESSAGE(
215     "The desired name was not passed in correctly",
216     steve.toGss(),
217     MockAcquireCred::desired_name
218   );
219   
220   CPPUNIT_ASSERT_EQUAL_MESSAGE(
221     "The time_req was not passed in correctly",
222     cmd.getTimeReq(),
223     MockAcquireCred::time_req
224   );
225   
226   CPPUNIT_ASSERT_EQUAL_MESSAGE(
227     "The desired mechs were not passed in correctly",
228     cmd.getDesiredMechs(),
229     MockAcquireCred::desired_mechs
230   );
231   
232   CPPUNIT_ASSERT_EQUAL_MESSAGE(
233     "The cred_usage was not passed in correctly",
234     cmd.getCredUsage(),
235     MockAcquireCred::cred_usage
236   );
237   
238   CPPUNIT_ASSERT_EQUAL_MESSAGE(
239     "The output credential handle was not passed in correctly",
240     MockAcquireCred::output_cred_handle,
241     cmd.getOutputCredHandle()
242   );
243   
244   CPPUNIT_ASSERT_EQUAL_MESSAGE(
245     "The actual mechanisms value was not passed in correctly",
246     MockAcquireCred::actual_mechs->count,
247     cmd.getActualMechs()->count
248   );
249   
250   CPPUNIT_ASSERT_EQUAL_MESSAGE(
251     "The time_rec value was not passed in correctly",
252     MockAcquireCred::time_rec,
253     cmd.getTimeRec()
254   );
255   
256   /* Cleanup */
257   /* Return */
258 }
259
260
261 /* Desired JSON output:
262  * 
263  * {
264  *   "command": "gss_acquire_cred",
265  *   "return_values": {
266  *     "major_status": 0,
267  *     "minor_status": 0,
268  *     "cred_handle": "###########",
269  *     "actual_mechs": [
270  *       "{ 1 2 3 4 }",
271  *       "{ 1 5 6 7 8 }"
272  *     ],
273  *     "time_rec": 0
274  *   }
275  * }
276  */
277 void GSSAcquireCredTest::testJSONMarshal()
278 {
279   /* Variables */
280   GSSOIDSet actualMechs;
281   JSONObject *result;
282   GSSAcquireCred cmd = GSSAcquireCred(&mock_acquire_cred);
283   
284   /* Error checking */
285   /* Setup */
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;
294   
295   /* Main */
296   cmd.execute();
297   result = cmd.toJSON();
298   
299 //   std::cout << "\n" << result->dump() << "\n";
300   
301   
302   CPPUNIT_ASSERT_EQUAL_MESSAGE(
303     "The return value was reported incorrectly",
304     (int)MockAcquireCred::retVal,
305     (int)( (*result)["major_status"].integer() )
306   );
307   
308   CPPUNIT_ASSERT_EQUAL_MESSAGE(
309     "The minor_status value was reported incorrectly",
310     (int)MockAcquireCred::minor_status,
311     (int)( (*result)["minor_status"].integer() )
312   );
313   
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() )
318   );
319   
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() )
324   );
325   
326   CPPUNIT_ASSERT_EQUAL_MESSAGE(
327     "The minor_status value was reported incorrectly",
328     (int)MockAcquireCred::time_rec,
329     (int)( (*result)["time_rec"].integer() )
330   );
331   
332   
333   /* Cleanup */
334   /* Return */
335 }
336