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