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