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