Add copyright comment headers to appropriate files
[gssweb.git] / json_gssapi / test / GSSGetMicTest.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 "GSSGetMicTest.h"
36 #include "command_mocks/MockGetMic.h"
37 #include "GSSGetMic.h"
38 #include <datamodel/GSSContext.h>
39 #include <cache/GSSContextCache.h>
40 #include <gssapi/gssapi.h>
41
42 CPPUNIT_TEST_SUITE_REGISTRATION( GSSGetMicTest );
43
44 /* 
45  * a mock of the gss_import_name call
46  * 
47  * Basically, just copy the arguments over to/from the
48  * MockGetMic global object
49  */
50
51 static OM_uint32 KRB5_CALLCONV
52 mock_get_mic(
53     OM_uint32    *minor_status,
54     gss_ctx_id_t  context_handle,
55     gss_qop_t     qop_req,
56     gss_buffer_t  message_buffer,
57     gss_buffer_t  message_token)
58 {
59   /* Error checking */
60   /* Variables */
61   
62   /* Setup */
63   
64   /* Main */
65   // Copy our input from the appropriate parameters to MockGetMic
66   MockGetMic::context_handle = context_handle;
67   MockGetMic::qop_req        = qop_req;
68   MockGetMic::inputMessage.setValue((char *)message_buffer->value, message_buffer->length);
69   
70   
71   // copy our output to the appropriate parameters
72   *minor_status = MockGetMic::minor_status;
73   *message_token = *MockGetMic::outputToken.toGss();
74   
75   /* Cleanup */
76   /* return */
77   return MockGetMic::retVal;
78 }
79
80 void GSSGetMicTest::setUp()
81 {
82   CppUnit::TestFixture::setUp();
83   MockGetMic::reset();
84 }
85
86 void GSSGetMicTest::testConstructor()
87 {
88   /* Variables */
89   GSSGetMic cmd = GSSGetMic();
90   
91   /* Error checking */
92   /* Setup */
93   /* Main */
94   CPPUNIT_ASSERT_EQUAL_MESSAGE(
95     "The GSSGetMic object has the wrong GSS function",
96     (void *)&gss_get_mic,
97     (void *)cmd.getGSSFunction()
98   );
99   
100   /* Cleanup */
101   /* Return */
102 }
103
104 void GSSGetMicTest::testEmptyCall()
105 {
106   /* Variables */
107   GSSGetMic cmd = GSSGetMic(&mock_get_mic);
108   GSSBuffer input((char *)"Input message");
109   std::string out("Output Message");
110   gss_qop_t desiredQop = rand();
111   gss_ctx_id_t desiredContext = (gss_ctx_id_t)( (long)0 | rand() );
112   
113   /* Error checking */
114   /* Setup */
115   cmd.setContextHandle(desiredContext);
116   cmd.setQopReq( desiredQop );
117   cmd.setInputMessage(&input);
118   
119   MockGetMic::minor_status = 0;
120   MockGetMic::retVal = 0;
121   MockGetMic::outputToken.setValue(out);
122   
123   
124   /* Main */
125   cmd.execute();
126   CPPUNIT_ASSERT_EQUAL_MESSAGE(
127     "The requested GSS context handle is not correct",
128     desiredContext,
129     MockGetMic::context_handle
130   );
131   
132   CPPUNIT_ASSERT_EQUAL_MESSAGE(
133     "The qop_req flag was incorrect.",
134     desiredQop,
135     MockGetMic::qop_req
136   );
137   
138   CPPUNIT_ASSERT_EQUAL_MESSAGE(
139     "The input message was incorrect.",
140     input.toString(),
141     MockGetMic::inputMessage.toString()
142   );
143   
144   
145   CPPUNIT_ASSERT_EQUAL_MESSAGE(
146     "The output message token was incorrect.",
147     out,
148     cmd.getOutputToken().toString()
149   );
150   
151   
152   /* Cleanup */
153   /* Return */
154 }
155
156 void GSSGetMicTest::testConstructorWithJSONObject()
157 {
158   /* Variables */
159   GSSContext context((gss_ctx_id_t)( (long)0 | rand() ),
160                      true);
161   std::string key = GSSContextCache::instance()->store(context);
162
163   std::string input = "{ \
164          \"context_handle\": \"" + key + "\", \
165          \"qop_req\": \"GSS_C_QOP_DEFAULT\", \
166          \"input_message\": \"mary had a little lamb\" \
167     }";
168   json_error_t jsonErr;
169   JSONObject json = JSONObject::load(input.c_str(), 0, &jsonErr);
170   
171   GSSGetMic cmd = GSSGetMic(&json, &mock_get_mic);
172   
173   /* Error checking */
174   /* Setup */
175   /* Main */
176   
177   CPPUNIT_ASSERT_EQUAL_MESSAGE(
178     "GSSGetMic did not parse the qop_req argument correctly",
179     (gss_qop_t)0,
180     cmd.getQopReq()
181   );
182   
183   CPPUNIT_ASSERT_EQUAL_MESSAGE(
184     "GSSGetMic did not parse the input message argument correctly",
185     std::string("mary had a little lamb"),
186     cmd.getInputMessage().toString()
187   );
188   
189   CPPUNIT_ASSERT_EQUAL_MESSAGE(
190     "GSSGetMic did not retrieve the context handle correctly",
191     context.getContext(),
192     cmd.getContextHandle()
193   );
194   
195   /* Cleanup */
196   /* Return */
197 }
198
199
200 /* Desired JSON output:
201  * 
202  * {
203  *  "command":       "gss_get_mic",
204  *  "return_values": 
205  *  {
206  *      "major_status": 0,
207  *      "minor_status": 0,
208  *      "output_token": "asdf"
209  *  }
210  * }
211  */
212
213 void GSSGetMicTest::testJSONMarshal()
214 {
215   /* Variables */
216   std::string output("dns@google.com");
217   GSSBuffer input( (char*)"abc" );
218   JSONObject *result;
219   GSSGetMic cmd = GSSGetMic(&mock_get_mic);
220   
221   /* Error checking */
222   /* Setup */
223   MockGetMic::minor_status = 0;
224   MockGetMic::retVal = 0;
225   MockGetMic::outputToken.setValue(output);
226   
227   cmd.setContextHandle(GSS_C_NO_CONTEXT);
228   cmd.setInputMessage( &input );
229   cmd.setQopReq(GSS_C_QOP_DEFAULT);
230   
231   /* Main */
232   cmd.execute();
233   result = cmd.toJSON();
234 //   std::cout << "\nGSSGetMic JSON: \n" << result->dump() << "\n";
235   
236   CPPUNIT_ASSERT_EQUAL_MESSAGE(
237     "The return value was reported incorrectly",
238     (int)MockGetMic::retVal,
239     (int)( (*result)["major_status"].integer() )
240   );
241   
242   CPPUNIT_ASSERT_EQUAL_MESSAGE(
243     "The minor_status value was reported incorrectly",
244     (int)MockGetMic::minor_status,
245     (int)( (*result)["minor_status"].integer() )
246   );
247   
248   CPPUNIT_ASSERT_EQUAL_MESSAGE(
249     "The output message was reported incorrectly",
250     output,
251     std::string( (*result)["output_token"].string() )
252   );
253   
254   
255   /* Cleanup */
256   /* Return */
257 }