Add copyright comment headers to appropriate files
[gssweb.git] / json_gssapi / test / GSSUnwrapTest.cpp
1 /*
2  * Copyright (c) 2014, 2015 JANET(UK)
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  *
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * 3. Neither the name of JANET(UK) nor the names of its contributors
17  *    may be used to endorse or promote products derived from this software
18  *    without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
23  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
24  * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
25  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
26  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
27  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
29  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
31  * OF THE POSSIBILITY OF SUCH DAMAGE.
32  *
33  */
34
35 #include <string>
36
37 #include "GSSUnwrapTest.h"
38 #include "command_mocks/MockUnwrap.h"
39 #include "GSSUnwrap.h"
40 #include <datamodel/GSSContext.h>
41 #include <cache/GSSContextCache.h>
42 #include <gssapi/gssapi.h>
43
44
45 CPPUNIT_TEST_SUITE_REGISTRATION( GSSUnwrapTest );
46
47 /* 
48  * a mock of the gss_import_name call
49  * 
50  * Basically, just copy the arguments over to/from the
51  * MockUnwrap global object
52  */
53
54 static OM_uint32 KRB5_CALLCONV
55 mock_unwrap(
56     OM_uint32    *minor_status,
57     gss_ctx_id_t  context_handle,
58     gss_buffer_t  input_message_buffer,
59     gss_buffer_t  output_message_buffer,
60     int          *conf_state,
61     gss_qop_t    *qop_state)
62 {
63   /* Error checking */
64   /* Variables */
65   
66   /* Setup */
67   
68   /* Main */
69   // Copy our input from the appropriate parameters to MockUnwrap
70   MockUnwrap::context_handle = context_handle;
71   MockUnwrap::inputMessageBuffer.setValue(input_message_buffer);
72   
73   
74   // copy our output to the appropriate parameters
75   *minor_status = MockUnwrap::minor_status;
76   *conf_state = MockUnwrap::conf_state;
77   *qop_state = MockUnwrap::qop_state;
78   *output_message_buffer = *MockUnwrap::outputMessageBuffer.toGss();
79   
80   /* Cleanup */
81   /* return */
82   return MockUnwrap::retVal;
83 }
84
85 void GSSUnwrapTest::setUp()
86 {
87   CppUnit::TestFixture::setUp();
88   MockUnwrap::reset();
89 }
90
91 void GSSUnwrapTest::testConstructor()
92 {
93   /* Variables */
94   GSSUnwrap cmd = GSSUnwrap();
95   
96   /* Error checking */
97   /* Setup */
98   /* Main */
99   CPPUNIT_ASSERT_EQUAL_MESSAGE(
100     "The GSSUnwrap object has the wrong GSS function",
101     (void *)&gss_unwrap,
102     (void *)cmd.getGSSFunction()
103   );
104   
105   /* Cleanup */
106   /* Return */
107 }
108
109 void GSSUnwrapTest::testEmptyCall()
110 {
111   /* Variables */
112   GSSUnwrap cmd = GSSUnwrap(&mock_unwrap);
113   GSSBuffer input((char *)"Input message");
114   gss_ctx_id_t desiredContext = (gss_ctx_id_t)( (long)0 | rand());
115   
116   /* Error checking */
117   /* Setup */
118   cmd.setContextHandle(desiredContext);
119   cmd.setInputMessage(&input);
120   
121   MockUnwrap::minor_status = 0;
122   MockUnwrap::retVal = 0;
123   MockUnwrap::conf_state = rand();
124   MockUnwrap::qop_state = GSS_C_QOP_DEFAULT;
125   MockUnwrap::outputMessageBuffer.setValue((char *)"Output message");
126   
127   
128   /* Main */
129   cmd.execute();
130   CPPUNIT_ASSERT_EQUAL_MESSAGE(
131     "The requested GSS context handle is not correct",
132     desiredContext,
133     MockUnwrap::context_handle
134   );
135   
136   CPPUNIT_ASSERT_EQUAL_MESSAGE(
137     "The input message was incorrect.",
138     input.toString(),
139     MockUnwrap::inputMessageBuffer.toString()
140   );
141   
142   
143   
144   CPPUNIT_ASSERT_EQUAL_MESSAGE(
145     "The conf_state flag was incorrect.",
146     MockUnwrap::qop_state,
147     cmd.getQopState()
148   );
149   
150   CPPUNIT_ASSERT_EQUAL_MESSAGE(
151     "The conf_state flag was incorrect.",
152     MockUnwrap::conf_state,
153     cmd.getConfState()
154   );
155   
156   CPPUNIT_ASSERT_EQUAL_MESSAGE(
157     "The input message was incorrect.",
158     cmd.getOutputMessage().toString(),
159     MockUnwrap::outputMessageBuffer.toString()
160   );
161   
162   
163   /* Cleanup */
164   /* Return */
165 }
166
167 void GSSUnwrapTest::testConstructorWithJSONObject()
168 {
169   /* Variables */
170   GSSContext context((gss_ctx_id_t)( (long)0 | rand()),
171                      true);
172   std::string key = GSSContextCache::instance()->store(context);
173   std::string input = "{ \
174          \"context_handle\": \"" + key + "\", \
175          \"input_message\": \"mary had a little lamb\" \
176     }";
177   json_error_t jsonErr;
178   JSONObject json = JSONObject::load(input.c_str(), 0, &jsonErr);
179   
180   GSSUnwrap cmd = GSSUnwrap(&json, &mock_unwrap);
181   
182   /* Error checking */
183   /* Setup */
184   /* Main */
185   
186   CPPUNIT_ASSERT_EQUAL_MESSAGE(
187     "GSSUnwrap did not retrieve the GSS context correctly",
188     context.getContext(),
189     cmd.getContextHandle()
190   );
191   
192   CPPUNIT_ASSERT_EQUAL_MESSAGE(
193     "GSSUnwrap did not parse the input message argument correctly",
194     std::string("mary had a little lamb"),
195     cmd.getInputMessage().toString()
196   );
197   
198   
199   
200   /* Cleanup */
201   /* Return */
202 }
203
204
205 void GSSUnwrapTest::testJSONMarshal()
206 {
207   /* Variables */
208   std::string output("dns@google.com");
209   int confState = 1;
210   gss_qop_t qopState = GSS_C_QOP_DEFAULT;
211   JSONObject *result;
212   GSSUnwrap cmd = GSSUnwrap(&mock_unwrap);
213   
214   /* Error checking */
215   /* Setup */
216   MockUnwrap::minor_status = 0;
217   MockUnwrap::retVal = 0;
218   MockUnwrap::outputMessageBuffer.setValue(output);
219   MockUnwrap::conf_state = confState;
220   MockUnwrap::qop_state = qopState;
221   
222   /* Main */
223   cmd.execute();
224   result = cmd.toJSON();
225 //   std::cout << "\nGSSUnwrap JSON: \n" << result->dump() << "\n";
226   
227   
228   CPPUNIT_ASSERT_EQUAL_MESSAGE(
229     "The return value was reported incorrectly",
230     (int)MockUnwrap::retVal,
231     (int)( (*result)["major_status"].integer() )
232   );
233   
234   CPPUNIT_ASSERT_EQUAL_MESSAGE(
235     "The minor_status value was reported incorrectly",
236     (int)MockUnwrap::minor_status,
237     (int)( (*result)["minor_status"].integer() )
238   );
239   
240   CPPUNIT_ASSERT_EQUAL_MESSAGE(
241     "The output message was reported incorrectly",
242     MockUnwrap::outputMessageBuffer.toString(),
243     std::string( (*result)["output_message"].string() )
244   );
245   
246   CPPUNIT_ASSERT_EQUAL_MESSAGE(
247     "The minor_status value was reported incorrectly",
248     (int)qopState,
249     (int)( (*result)["qop_state"].integer() )
250   );
251   
252   
253   /* Cleanup */
254   /* Return */
255 }