Update tests to deal with GSSCommand objects now taking only the 'argument' part...
[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 = "{ \
149          \"context_handle\": \"" + key + "\", \
150          \"input_message\": \"mary had a little lamb\" \
151     }";
152   json_error_t jsonErr;
153   JSONObject json = JSONObject::load(input.c_str(), 0, &jsonErr);
154   
155   GSSUnwrap cmd = GSSUnwrap(&json, &mock_unwrap);
156   
157   /* Error checking */
158   /* Setup */
159   /* Main */
160   
161   CPPUNIT_ASSERT_EQUAL_MESSAGE(
162     "GSSUnwrap did not retrieve the GSS context correctly",
163     context.getContext(),
164     cmd.getContextHandle()
165   );
166   
167   CPPUNIT_ASSERT_EQUAL_MESSAGE(
168     "GSSUnwrap did not parse the input message argument correctly",
169     std::string("mary had a little lamb"),
170     cmd.getInputMessage().toString()
171   );
172   
173   
174   
175   /* Cleanup */
176   /* Return */
177 }
178
179
180 void GSSUnwrapTest::testJSONMarshal()
181 {
182   /* Variables */
183   std::string output("dns@google.com");
184   int confState = 1;
185   gss_qop_t qopState = GSS_C_QOP_DEFAULT;
186   JSONObject *result;
187   GSSUnwrap cmd = GSSUnwrap(&mock_unwrap);
188   
189   /* Error checking */
190   /* Setup */
191   MockUnwrap::minor_status = 0;
192   MockUnwrap::retVal = 0;
193   MockUnwrap::outputMessageBuffer.setValue(output);
194   MockUnwrap::conf_state = confState;
195   MockUnwrap::qop_state = qopState;
196   
197   /* Main */
198   cmd.execute();
199   result = cmd.toJSON();
200 //   std::cout << "\nGSSUnwrap JSON: \n" << result->dump() << "\n";
201   
202   
203   CPPUNIT_ASSERT_EQUAL_MESSAGE(
204     "The return value was reported incorrectly",
205     (int)MockUnwrap::retVal,
206     (int)( (*result)["major_status"].integer() )
207   );
208   
209   CPPUNIT_ASSERT_EQUAL_MESSAGE(
210     "The minor_status value was reported incorrectly",
211     (int)MockUnwrap::minor_status,
212     (int)( (*result)["minor_status"].integer() )
213   );
214   
215   CPPUNIT_ASSERT_EQUAL_MESSAGE(
216     "The output message was reported incorrectly",
217     MockUnwrap::outputMessageBuffer.toString(),
218     std::string( (*result)["output_message"].string() )
219   );
220   
221   CPPUNIT_ASSERT_EQUAL_MESSAGE(
222     "The minor_status value was reported incorrectly",
223     (int)qopState,
224     (int)( (*result)["qop_state"].integer() )
225   );
226   
227   
228   /* Cleanup */
229   /* Return */
230 }