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