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