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