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