Update tests to deal with GSSCommand objects now taking only the 'argument' part...
[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 = "{ \
171          \"context_handle\": \"" + key + "\", \
172          \"conf_req\": \"TRUE\", \
173          \"qop_req\": \"GSS_C_QOP_DEFAULT\", \
174          \"input_message\": \"mary had a little lamb\" \
175     }";
176   json_error_t jsonErr;
177   JSONObject json = JSONObject::load(input.c_str(), 0, &jsonErr);
178   
179   GSSWrap cmd = GSSWrap(&json, &mock_wrap);
180   
181   /* Error checking */
182   /* Setup */
183   /* Main */
184   
185   CPPUNIT_ASSERT_EQUAL_MESSAGE(
186     "GSSWrap did not retrive the GSS context correctly",
187     context.getContext(),
188     cmd.getContext()
189   );
190   
191   CPPUNIT_ASSERT_EQUAL_MESSAGE(
192     "GSSWrap did not parse the conf_req argument correctly",
193     1,
194     cmd.getConfReq()
195   );
196   
197   CPPUNIT_ASSERT_EQUAL_MESSAGE(
198     "GSSWrap did not parse the qop_req argument correctly",
199     (gss_qop_t)0,
200     cmd.getQopReq()
201   );
202   
203   CPPUNIT_ASSERT_EQUAL_MESSAGE(
204     "GSSWrap did not parse the input message argument correctly",
205     std::string("mary had a little lamb"),
206     cmd.getInputMessage().toString()
207   );
208   
209   
210   
211   /* Cleanup */
212   /* Return */
213 }
214
215 /* Desired JSON output:
216  * 
217  * {
218  *  "command":       "gss_wrap",
219  *  "return_values": 
220  *  {
221  *      "major_status":   0,
222  *      "minor_status":   0,
223  *      "conf_state":     "TRUE",
224  *      "output_message": "asdf"
225  *  }
226  * }
227  */
228 void GSSWrapTest::testJSONMarshal()
229 {
230   /* Variables */
231   std::string output("dns@google.com");
232   int confState = 1;
233   JSONObject *result;
234   GSSWrap cmd = GSSWrap(&mock_wrap);
235   
236   /* Error checking */
237   /* Setup */
238   MockWrap::minor_status = 0;
239   MockWrap::retVal = 0;
240   MockWrap::outputMessageBuffer.setValue(output);
241   MockWrap::conf_state = confState;
242   
243   /* Main */
244   cmd.execute();
245   result = cmd.toJSON();
246 //   std::cout << "\nGSSWrap JSON: \n" << result->dump() << "\n";
247   
248   
249   CPPUNIT_ASSERT_EQUAL_MESSAGE(
250     "The return value was reported incorrectly",
251     (int)MockWrap::retVal,
252     (int)( (*result)["major_status"].integer() )
253   );
254   
255   CPPUNIT_ASSERT_EQUAL_MESSAGE(
256     "The minor_status value was reported incorrectly",
257     (int)MockWrap::minor_status,
258     (int)( (*result)["minor_status"].integer() )
259   );
260   
261   CPPUNIT_ASSERT_EQUAL_MESSAGE(
262     "The output message was reported incorrectly",
263     MockWrap::outputMessageBuffer.toString(),
264     std::string( (*result)["output_message"].string() )
265   );
266   
267   
268   /* Cleanup */
269   /* Return */
270 }