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