e38ab49270cff833c67ba2cc01cf2e50f08c92e1
[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 = "{\"method\": \"gss_pseudo_random\", \
193     \"arguments\": \
194     { \
195          \"context_handle\": \"" + key + "\", \
196          \"prf_key\": 1234567890, \
197          \"prf_in\": \"mary had a little lamb\", \
198          \"desired_output_len\": 256 \
199     }\
200   }";
201   json_error_t jsonErr;
202   JSONObject json = JSONObject::load(input.c_str(), 0, &jsonErr);
203   
204   GSSPseudoRandom cmd = GSSPseudoRandom(&json, &mock_gss_pseudo_random);
205
206   /* Error Checking */
207   /* Setup */
208   /* Main */
209   
210   CPPUNIT_ASSERT_EQUAL_MESSAGE(
211     "GSSWrap did not retrive the GSS context correctly",
212     context.getContext(),
213     cmd.getContextHandle()
214   );
215   
216   CPPUNIT_ASSERT_EQUAL_MESSAGE(
217     "GSSPseudoRandom did not parse the prf_key argument correctly.",
218     1234567890,
219     cmd.getKey()
220   );
221   
222   CPPUNIT_ASSERT_EQUAL_MESSAGE(
223     "GSSPseudoRandom did not parse the prf_in argument correctly.",
224     std::string("mary had a little lamb"),
225     cmd.getInputMessage().toString()
226   );
227   
228   CPPUNIT_ASSERT_EQUAL_MESSAGE(
229     "GSSPseudoRandom did not parse the desired_output_len argument correctly.",
230     256,
231     cmd.getDesiredOutputLength()
232   );
233   
234   /* Cleanup */
235   /* Return */
236 }
237
238 /* Desired JSON output:
239  * 
240  * {
241  *  "command":       "gss_pseudo_random",
242  *  "return_values": 
243  *  {
244  *      "major_status": 0,
245  *      "minor_status": 0,
246  *      "random_bytes": "asdf"
247  *  }
248  * }
249  */
250 void GSSPseudoRandomTest::testJSONMarshal()
251 {
252
253   /* Variables */
254   std::string output("dns@google.com");
255   JSONObject *result;
256   GSSPseudoRandom cmd = GSSPseudoRandom(&mock_gss_pseudo_random);
257   
258   /* Error checking */
259   /* Setup */
260   MockPseudoRandom::minor_status = 0;
261   MockPseudoRandom::retVal = 0;
262   MockPseudoRandom::outputMessageBuffer.setValue(output);
263   
264   /* Main */
265   cmd.execute();
266   result = cmd.toJSON();
267 //   std::cout << "\nGSSWrap JSON: \n" << result->dump() << "\n";
268   
269   CPPUNIT_ASSERT_EQUAL_MESSAGE(
270     "The command name is incorrect",
271     std::string("gss_pseudo_random"),
272     std::string( (*result)["command"].string() )
273   );
274   
275   CPPUNIT_ASSERT_EQUAL_MESSAGE(
276     "The return value was reported incorrectly",
277     (int)MockPseudoRandom::retVal,
278     (int)( (*result)["return_values"]["major_status"].integer() )
279   );
280   
281   CPPUNIT_ASSERT_EQUAL_MESSAGE(
282     "The minor_status value was reported incorrectly",
283     (int)MockPseudoRandom::minor_status,
284     (int)( (*result)["return_values"]["minor_status"].integer() )
285   );
286   
287   CPPUNIT_ASSERT_EQUAL_MESSAGE(
288     "The random bytes were reported incorrectly",
289     MockPseudoRandom::outputMessageBuffer.toString(),
290     std::string( (*result)["return_values"]["random_bytes"].string() )
291   );
292   
293   
294   /* Cleanup */
295   /* Return */
296 }
297
298   /* Variables */
299   /* Error Checking */
300   /* Setup */
301   /* Main */
302   /* Cleanup */
303   /* Return */