1ed079d7c6077ad071971aeb65c95215957449b3
[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   
39   /* Setup */
40   
41   /* Main */
42   // Copy our input from the appropriate parameters to MockUnwrap
43   MockUnwrap::context_handle = context_handle;
44   MockUnwrap::inputMessageBuffer.setValue(input_message_buffer);
45   
46   
47   // copy our output to the appropriate parameters
48   *minor_status = MockUnwrap::minor_status;
49   *conf_state = MockUnwrap::conf_state;
50   *qop_state = MockUnwrap::qop_state;
51   *output_message_buffer = *MockUnwrap::outputMessageBuffer.toGss();
52   
53   /* Cleanup */
54   /* return */
55   return MockUnwrap::retVal;
56 }
57
58 void GSSUnwrapTest::setUp()
59 {
60   CppUnit::TestFixture::setUp();
61   MockUnwrap::reset();
62 }
63
64 void GSSUnwrapTest::testConstructor()
65 {
66   /* Variables */
67   GSSUnwrap cmd = GSSUnwrap();
68   
69   /* Error checking */
70   /* Setup */
71   /* Main */
72   CPPUNIT_ASSERT_EQUAL_MESSAGE(
73     "The GSSUnwrap object has the wrong GSS function",
74     (void *)&gss_unwrap,
75     (void *)cmd.getGSSFunction()
76   );
77   
78   /* Cleanup */
79   /* Return */
80 }
81
82 void GSSUnwrapTest::testEmptyCall()
83 {
84   /* Variables */
85   GSSUnwrap cmd = GSSUnwrap(&mock_unwrap);
86   GSSBuffer input((char *)"Input message");
87   gss_ctx_id_t desiredContext = (gss_ctx_id_t)rand();
88   
89   /* Error checking */
90   /* Setup */
91   cmd.setContextHandle(desiredContext);
92   cmd.setInputMessage(&input);
93   
94   MockUnwrap::minor_status = 0;
95   MockUnwrap::retVal = 0;
96   MockUnwrap::conf_state = rand();
97   MockUnwrap::qop_state = GSS_C_QOP_DEFAULT;
98   MockUnwrap::outputMessageBuffer.setValue((char *)"Output message");
99   
100   
101   /* Main */
102   cmd.execute();
103   CPPUNIT_ASSERT_EQUAL_MESSAGE(
104     "The requested GSS context handle is not correct",
105     desiredContext,
106     MockUnwrap::context_handle
107   );
108   
109   CPPUNIT_ASSERT_EQUAL_MESSAGE(
110     "The input message was incorrect.",
111     input.toString(),
112     MockUnwrap::inputMessageBuffer.toString()
113   );
114   
115   
116   
117   CPPUNIT_ASSERT_EQUAL_MESSAGE(
118     "The conf_state flag was incorrect.",
119     MockUnwrap::qop_state,
120     cmd.getQopState()
121   );
122   
123   CPPUNIT_ASSERT_EQUAL_MESSAGE(
124     "The conf_state flag was incorrect.",
125     MockUnwrap::conf_state,
126     cmd.getConfState()
127   );
128   
129   CPPUNIT_ASSERT_EQUAL_MESSAGE(
130     "The input message was incorrect.",
131     cmd.getOutputMessage().toString(),
132     MockUnwrap::outputMessageBuffer.toString()
133   );
134   
135   
136   /* Cleanup */
137   /* Return */
138 }
139
140 void GSSUnwrapTest::testConstructorWithJSONObject()
141 {
142   /* Variables */
143   GSSContext context((gss_ctx_id_t)rand(), true);
144   std::string key = GSSContextCache::instance()->store(context);
145   std::string input = "{ \
146          \"context_handle\": \"" + key + "\", \
147          \"input_message\": \"mary had a little lamb\" \
148     }";
149   json_error_t jsonErr;
150   JSONObject json = JSONObject::load(input.c_str(), 0, &jsonErr);
151   
152   GSSUnwrap cmd = GSSUnwrap(&json, &mock_unwrap);
153   
154   /* Error checking */
155   /* Setup */
156   /* Main */
157   
158   CPPUNIT_ASSERT_EQUAL_MESSAGE(
159     "GSSUnwrap did not retrieve the GSS context correctly",
160     context.getContext(),
161     cmd.getContextHandle()
162   );
163   
164   CPPUNIT_ASSERT_EQUAL_MESSAGE(
165     "GSSUnwrap did not parse the input message argument correctly",
166     std::string("mary had a little lamb"),
167     cmd.getInputMessage().toString()
168   );
169   
170   
171   
172   /* Cleanup */
173   /* Return */
174 }
175
176
177 void GSSUnwrapTest::testJSONMarshal()
178 {
179   /* Variables */
180   std::string output("dns@google.com");
181   int confState = 1;
182   gss_qop_t qopState = GSS_C_QOP_DEFAULT;
183   JSONObject *result;
184   GSSUnwrap cmd = GSSUnwrap(&mock_unwrap);
185   
186   /* Error checking */
187   /* Setup */
188   MockUnwrap::minor_status = 0;
189   MockUnwrap::retVal = 0;
190   MockUnwrap::outputMessageBuffer.setValue(output);
191   MockUnwrap::conf_state = confState;
192   MockUnwrap::qop_state = qopState;
193   
194   /* Main */
195   cmd.execute();
196   result = cmd.toJSON();
197 //   std::cout << "\nGSSUnwrap JSON: \n" << result->dump() << "\n";
198   
199   
200   CPPUNIT_ASSERT_EQUAL_MESSAGE(
201     "The return value was reported incorrectly",
202     (int)MockUnwrap::retVal,
203     (int)( (*result)["major_status"].integer() )
204   );
205   
206   CPPUNIT_ASSERT_EQUAL_MESSAGE(
207     "The minor_status value was reported incorrectly",
208     (int)MockUnwrap::minor_status,
209     (int)( (*result)["minor_status"].integer() )
210   );
211   
212   CPPUNIT_ASSERT_EQUAL_MESSAGE(
213     "The output message was reported incorrectly",
214     MockUnwrap::outputMessageBuffer.toString(),
215     std::string( (*result)["output_message"].string() )
216   );
217   
218   CPPUNIT_ASSERT_EQUAL_MESSAGE(
219     "The minor_status value was reported incorrectly",
220     (int)qopState,
221     (int)( (*result)["qop_state"].integer() )
222   );
223   
224   
225   /* Cleanup */
226   /* Return */
227 }