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