Add copyright comment headers to appropriate files
[gssweb.git] / json_gssapi / test / GSSWrapTest.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 "GSSWrapTest.h"
36 #include "command_mocks/MockWrap.h"
37 #include "GSSWrap.h"
38 #include <datamodel/GSSContext.h>
39 #include <cache/GSSContextCache.h>
40 #include <gssapi/gssapi.h>
41
42 CPPUNIT_TEST_SUITE_REGISTRATION( GSSWrapTest );
43
44 /* 
45  * a mock of the gss_import_name call
46  * 
47  * Basically, just copy the arguments over to/from the
48  * MockWrap global object
49  */
50
51 static OM_uint32 KRB5_CALLCONV
52 mock_wrap(
53     OM_uint32    *minor_status,
54     gss_ctx_id_t  context_handle,
55     int           conf_req_flag,
56     gss_qop_t     qop_req,
57     gss_buffer_t  input_message_buffer,
58     int          *conf_state,
59     gss_buffer_t  output_message_buffer)
60 {
61   /* Error checking */
62   /* Variables */
63   
64   /* Setup */
65   
66   /* Main */
67   // Copy our input from the appropriate parameters to MockWrap
68   MockWrap::context_handle = context_handle;
69   MockWrap::conf_req_flag  = conf_req_flag;
70   MockWrap::qop_req        = qop_req;
71   MockWrap::inputMessageBuffer.setValue(input_message_buffer);
72   
73   
74   // copy our output to the appropriate parameters
75   *minor_status = MockWrap::minor_status;
76   *conf_state = MockWrap::conf_state;
77   *output_message_buffer = *MockWrap::outputMessageBuffer.toGss();
78   
79   /* Cleanup */
80   /* return */
81   return MockWrap::retVal;
82 }
83
84 void GSSWrapTest::setUp()
85 {
86   CppUnit::TestFixture::setUp();
87   MockWrap::reset();
88 }
89
90 void GSSWrapTest::testConstructor()
91 {
92   /* Variables */
93   GSSWrap cmd = GSSWrap();
94   
95   /* Error checking */
96   /* Setup */
97   /* Main */
98   CPPUNIT_ASSERT_EQUAL_MESSAGE(
99     "The GSSWrap object has the wrong GSS function",
100     (void *)&gss_wrap,
101     (void *)cmd.getGSSFunction()
102   );
103   
104   /* Cleanup */
105   /* Return */
106 }
107
108 void GSSWrapTest::testEmptyCall()
109 {
110   /* Variables */
111   GSSWrap cmd = GSSWrap(&mock_wrap);
112   GSSBuffer input((char *)"Input message");
113   gss_qop_t desiredQop = rand();
114   int desiredConf = rand();
115   gss_ctx_id_t desiredContext = (gss_ctx_id_t)( (long)0 | rand() );
116   
117   /* Error checking */
118   /* Setup */
119   cmd.setContextHandle(desiredContext);
120   cmd.setConfReq( desiredConf );
121   cmd.setQopReq( desiredQop );
122   cmd.setInputMessage(&input);
123   
124   MockWrap::minor_status = 0;
125   MockWrap::retVal = 0;
126   MockWrap::conf_state = rand();
127   MockWrap::outputMessageBuffer.setValue((char *)"Output message");
128   
129   
130   /* Main */
131   cmd.execute();
132   CPPUNIT_ASSERT_EQUAL_MESSAGE(
133     "The requested GSS context handle is not correct",
134     desiredContext,
135     MockWrap::context_handle
136   );
137   
138   CPPUNIT_ASSERT_EQUAL_MESSAGE(
139     "The conf_req flag was incorrect.",
140     desiredConf,
141     MockWrap::conf_req_flag
142   );
143   
144   CPPUNIT_ASSERT_EQUAL_MESSAGE(
145     "The qop_req flag was incorrect.",
146     desiredQop,
147     MockWrap::qop_req
148   );
149   
150   CPPUNIT_ASSERT_EQUAL_MESSAGE(
151     "The input message was incorrect.",
152     input.toString(),
153     MockWrap::inputMessageBuffer.toString()
154   );
155   
156   
157   
158   CPPUNIT_ASSERT_EQUAL_MESSAGE(
159     "The conf_state flag was incorrect.",
160     MockWrap::conf_state,
161     cmd.getConfState()
162   );
163   
164   CPPUNIT_ASSERT_EQUAL_MESSAGE(
165     "The input message was incorrect.",
166     cmd.getOutputMessage().toString(),
167     MockWrap::outputMessageBuffer.toString()
168   );
169   
170   
171   /* Cleanup */
172   /* Return */
173 }
174
175 /*
176  * Example JSON input:
177  * {
178  *   "method": "gss_wrap",
179  *   "arguments": {
180  *     "context_handle": "stuff",
181  *     "conf_req": "TRUE",
182  *     "qop_req": "GSS_C_QOP_DEFAULT",
183  *     "input_message": "mary had a little lamb"
184  *   }
185  * }
186  * 
187  */
188 void GSSWrapTest::testConstructorWithJSONObject()
189 {
190   /* Variables */
191   GSSContext context( (gss_ctx_id_t)( (long)0 | rand()),
192                       true );
193   std::string key = GSSContextCache::instance()->store(context);
194   
195   std::string input = "{ \
196          \"context_handle\": \"" + key + "\", \
197          \"conf_req\": \"TRUE\", \
198          \"qop_req\": \"GSS_C_QOP_DEFAULT\", \
199          \"input_message\": \"mary had a little lamb\" \
200     }";
201   json_error_t jsonErr;
202   JSONObject json = JSONObject::load(input.c_str(), 0, &jsonErr);
203   
204   GSSWrap cmd = GSSWrap(&json, &mock_wrap);
205   
206   /* Error checking */
207   /* Setup */
208   /* Main */
209   
210   CPPUNIT_ASSERT_EQUAL_MESSAGE(
211     "GSSWrap did not retrive the GSS context correctly",
212     context.getContext(),
213     cmd.getContext()
214   );
215   
216   CPPUNIT_ASSERT_EQUAL_MESSAGE(
217     "GSSWrap did not parse the conf_req argument correctly",
218     1,
219     cmd.getConfReq()
220   );
221   
222   CPPUNIT_ASSERT_EQUAL_MESSAGE(
223     "GSSWrap did not parse the qop_req argument correctly",
224     (gss_qop_t)0,
225     cmd.getQopReq()
226   );
227   
228   CPPUNIT_ASSERT_EQUAL_MESSAGE(
229     "GSSWrap did not parse the input message argument correctly",
230     std::string("mary had a little lamb"),
231     cmd.getInputMessage().toString()
232   );
233   
234   
235   
236   /* Cleanup */
237   /* Return */
238 }
239
240 /* Desired JSON output:
241  * 
242  * {
243  *  "command":       "gss_wrap",
244  *  "return_values": 
245  *  {
246  *      "major_status":   0,
247  *      "minor_status":   0,
248  *      "conf_state":     "TRUE",
249  *      "output_message": "asdf"
250  *  }
251  * }
252  */
253 void GSSWrapTest::testJSONMarshal()
254 {
255   /* Variables */
256   std::string output("dns@google.com");
257   int confState = 1;
258   JSONObject *result;
259   GSSWrap cmd = GSSWrap(&mock_wrap);
260   
261   /* Error checking */
262   /* Setup */
263   MockWrap::minor_status = 0;
264   MockWrap::retVal = 0;
265   MockWrap::outputMessageBuffer.setValue(output);
266   MockWrap::conf_state = confState;
267   
268   /* Main */
269   cmd.execute();
270   result = cmd.toJSON();
271 //   std::cout << "\nGSSWrap JSON: \n" << result->dump() << "\n";
272   
273   
274   CPPUNIT_ASSERT_EQUAL_MESSAGE(
275     "The return value was reported incorrectly",
276     (int)MockWrap::retVal,
277     (int)( (*result)["major_status"].integer() )
278   );
279   
280   CPPUNIT_ASSERT_EQUAL_MESSAGE(
281     "The minor_status value was reported incorrectly",
282     (int)MockWrap::minor_status,
283     (int)( (*result)["minor_status"].integer() )
284   );
285   
286   CPPUNIT_ASSERT_EQUAL_MESSAGE(
287     "The output message was reported incorrectly",
288     MockWrap::outputMessageBuffer.toString(),
289     std::string( (*result)["output_message"].string() )
290   );
291   
292   
293   /* Cleanup */
294   /* Return */
295 }