Update tests to deal with GSSCommand objects now taking only the 'argument' part...
[gssweb.git] / json_gssapi / test / GSSPseudoRandomTest.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 <algorithm>
9 #include <string>
10
11 //#include <cppunit/TestFixture.h>
12 //#include <cppunit/extensions/HelperMacros.h>
13 #include <openssl/rand.h>
14
15 #include "cache/GSSContextCache.h"
16 #include "datamodel/GSSContext.h"
17 #include "command_mocks/MockPseudoRandom.h"
18 #include "GSSPseudoRandom.h"
19 #include "GSSPseudoRandomTest.h"
20
21 // Registers the fixture into the 'registry'
22 CPPUNIT_TEST_SUITE_REGISTRATION( GSSPseudoRandomTest );
23
24
25 // typedef OM_uint32 (*gss_pseudo_random_type) (
26 //     OM_uint32 *,        /* minor_status */
27 //     gss_ctx_id_t,       /* context */
28 //     int,                /* prf_key */
29 //     const gss_buffer_t, /* prf_in */
30 //     ssize_t,            /* desired_output_len */
31 //     gss_buffer_t);      /* prf_out */
32 OM_uint32 mock_gss_pseudo_random(
33     OM_uint32 *minor_status,
34     gss_ctx_id_t  context,
35     int prf_key,
36     const gss_buffer_t prf_in,
37     ssize_t desired_output_len,
38     gss_buffer_t prf_out)
39 {
40   /* Variables */
41   std::string buffer;
42   /* Error checking */
43   /* Setup */
44   /* Main */
45   MockPseudoRandom::context_handle = context;
46   MockPseudoRandom::key = prf_key;
47   MockPseudoRandom::inputMessageBuffer.setValue(prf_in);
48   MockPseudoRandom::desiredOutputLength = desired_output_len;
49   
50   buffer = MockPseudoRandom::outputMessageBuffer.toString();
51   prf_out->length = buffer.length();
52   prf_out->value = (void *)buffer.c_str();
53   
54   /* Cleanup */
55   /* Return */
56   return 0;
57 }
58
59
60 void GSSPseudoRandomTest::setUp()
61 {
62   CppUnit::TestFixture::setUp();
63   MockPseudoRandom::reset();
64 }
65
66
67 void GSSPseudoRandomTest::testConstructor()
68 {
69   /* Variables */
70   GSSPseudoRandom cmd = GSSPseudoRandom();
71   
72   /* Error checking */
73   /* Setup */
74   /* Main */
75   CPPUNIT_ASSERT_EQUAL_MESSAGE(
76     "The GSSPseudoRandom object has the wrong GSS function",
77     (void *)&gss_pseudo_random,
78     (void *)cmd.getGSSFunction()
79   );
80   
81   /* Cleanup */
82   /* Return */
83 }
84
85 /* 
86  * Test that the command calls into gss_pseudo_random with
87  * all of the correct values, and that the command reads
88  * all of the appropriate values back out of it.
89  */
90 void GSSPseudoRandomTest::testEmptyCall()
91 {
92   /* Variables */
93   GSSPseudoRandom cmd = GSSPseudoRandom(&mock_gss_pseudo_random);
94   gss_ctx_id_t desiredContext;
95   int prf_key;
96   ssize_t desired_output_len;
97   GSSBuffer prf_in((char *)"Input message");
98   
99   /* Error Checking */
100   /* Setup */
101   
102   // Populate cmd with what will be used to call into gss_pseudo_random
103     // taking the address of prf_key should be plenty random for testing the empty call.
104   desiredContext = (gss_ctx_id_t)&prf_key;
105   prf_key = rand();
106   desired_output_len = (ssize_t)(rand() + 50);  // Must be at least fifty bytes long
107   
108   cmd.setContextHandle(desiredContext);
109   cmd.setKey(prf_key);
110   cmd.setInputMessage( prf_in );
111   cmd.setDesiredOutputLength(desired_output_len);
112   
113   
114   // Populate the mock with what will be returned from gss_pseudo_random
115   MockPseudoRandom::minor_status = 0;
116   MockPseudoRandom::retVal = 0;
117   MockPseudoRandom::outputMessageBuffer.setValue((char *)"Output message");
118
119   
120   /* Main */
121   cmd.execute();
122   
123   // Do we populate the call to gss_pseudo_random correctly?
124   CPPUNIT_ASSERT_EQUAL_MESSAGE(
125     "The requested GSS context handle is not correct",
126     desiredContext,
127     MockPseudoRandom::context_handle
128   );
129   
130   CPPUNIT_ASSERT_EQUAL_MESSAGE(
131     "The requested pseudo random function key is not correct",
132     prf_key,
133     MockPseudoRandom::key
134   );
135   
136   CPPUNIT_ASSERT_EQUAL_MESSAGE(
137     "The requested desired output length is not correct",
138     desired_output_len,
139     MockPseudoRandom::desiredOutputLength
140   );
141   
142   CPPUNIT_ASSERT_EQUAL_MESSAGE(
143     "The requested input message is not correct",
144     prf_in.toString(),
145     MockPseudoRandom::inputMessageBuffer.toString()
146   );
147   
148   // Do we read the results of gss_pseudo_random correctly?
149   
150   CPPUNIT_ASSERT_EQUAL_MESSAGE(
151     "The requested output message is not correct",
152     MockPseudoRandom::outputMessageBuffer.toString(),
153     cmd.getOutputMessage().toString()
154   );
155   
156   CPPUNIT_ASSERT_EQUAL_MESSAGE(
157     "The return value is not correct",
158     MockPseudoRandom::retVal,
159     cmd.getRetVal()
160   );
161   
162   CPPUNIT_ASSERT_EQUAL_MESSAGE(
163     "The minor status value is not correct",
164     MockPseudoRandom::minor_status,
165     cmd.getMinorStatus()
166   );
167   
168   
169   
170   /* Cleanup */
171   /* Return */
172 }
173
174 /* JSON input
175  * 
176  * {"method":    "gss_pseudo_random",
177  *  "arguments": 
178  *   {
179  *     "context_handle":     "context handle key",
180  *     "prf_key":            ###,
181  *     "prf_in":             "la la la input message",
182  *     "desired_output_len": ####
183  *   }
184  * 
185  */
186 void GSSPseudoRandomTest::testConstructorWithJSONObject()
187 {
188   /* Variables */
189   GSSContext context( (gss_ctx_id_t)rand(), true );
190   std::string key = GSSContextCache::instance()->store(context);
191   
192   std::string input = "{ \
193          \"context_handle\": \"" + key + "\", \
194          \"prf_key\": 1234567890, \
195          \"prf_in\": \"mary had a little lamb\", \
196          \"desired_output_len\": 256 \
197     }";
198   json_error_t jsonErr;
199   JSONObject json = JSONObject::load(input.c_str(), 0, &jsonErr);
200   
201   GSSPseudoRandom cmd = GSSPseudoRandom(&json, &mock_gss_pseudo_random);
202
203   /* Error Checking */
204   /* Setup */
205   /* Main */
206   
207   CPPUNIT_ASSERT_EQUAL_MESSAGE(
208     "GSSWrap did not retrive the GSS context correctly",
209     context.getContext(),
210     cmd.getContextHandle()
211   );
212   
213   CPPUNIT_ASSERT_EQUAL_MESSAGE(
214     "GSSPseudoRandom did not parse the prf_key argument correctly.",
215     1234567890,
216     cmd.getKey()
217   );
218   
219   CPPUNIT_ASSERT_EQUAL_MESSAGE(
220     "GSSPseudoRandom did not parse the prf_in argument correctly.",
221     std::string("mary had a little lamb"),
222     cmd.getInputMessage().toString()
223   );
224   
225   CPPUNIT_ASSERT_EQUAL_MESSAGE(
226     "GSSPseudoRandom did not parse the desired_output_len argument correctly.",
227     256,
228     cmd.getDesiredOutputLength()
229   );
230   
231   /* Cleanup */
232   /* Return */
233 }
234
235 /* Desired JSON output:
236  * 
237  * {
238  *  "command":       "gss_pseudo_random",
239  *  "return_values": 
240  *  {
241  *      "major_status": 0,
242  *      "minor_status": 0,
243  *      "random_bytes": "asdf"
244  *  }
245  * }
246  */
247 void GSSPseudoRandomTest::testJSONMarshal()
248 {
249
250   /* Variables */
251   std::string output("dns@google.com");
252   JSONObject *result;
253   GSSPseudoRandom cmd = GSSPseudoRandom(&mock_gss_pseudo_random);
254   
255   /* Error checking */
256   /* Setup */
257   MockPseudoRandom::minor_status = 0;
258   MockPseudoRandom::retVal = 0;
259   MockPseudoRandom::outputMessageBuffer.setValue(output);
260   
261   /* Main */
262   cmd.execute();
263   result = cmd.toJSON();
264 //   std::cout << "\nGSSWrap JSON: \n" << result->dump() << "\n";
265   
266   CPPUNIT_ASSERT_EQUAL_MESSAGE(
267     "The return value was reported incorrectly",
268     (int)MockPseudoRandom::retVal,
269     (int)( (*result)["major_status"].integer() )
270   );
271   
272   CPPUNIT_ASSERT_EQUAL_MESSAGE(
273     "The minor_status value was reported incorrectly",
274     (int)MockPseudoRandom::minor_status,
275     (int)( (*result)["minor_status"].integer() )
276   );
277   
278   CPPUNIT_ASSERT_EQUAL_MESSAGE(
279     "The random bytes were reported incorrectly",
280     MockPseudoRandom::outputMessageBuffer.toString(),
281     std::string( (*result)["random_bytes"].string() )
282   );
283   
284   
285   /* Cleanup */
286   /* Return */
287 }
288
289   /* Variables */
290   /* Error Checking */
291   /* Setup */
292   /* Main */
293   /* Cleanup */
294   /* Return */