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