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