2 * Copyright (c) 2014 <copyright holder> <email>
4 * For license details, see the LICENSE file in the root of this project.
10 //#include <cppunit/TestFixture.h>
11 //#include <cppunit/extensions/HelperMacros.h>
12 #include <openssl/rand.h>
14 #include "GSSPseudoRandom.h"
15 #include "GSSPseudoRandomTest.h"
16 #include "command_mocks/MockPseudoRandom.h"
18 // Registers the fixture into the 'registry'
19 CPPUNIT_TEST_SUITE_REGISTRATION( GSSPseudoRandomTest );
22 // typedef OM_uint32 (*gss_pseudo_random_type) (
23 // OM_uint32 *, /* minor_status */
24 // gss_ctx_id_t, /* context */
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,
33 const gss_buffer_t prf_in,
34 ssize_t desired_output_len,
42 MockPseudoRandom::context_handle = context;
43 MockPseudoRandom::key = prf_key;
44 MockPseudoRandom::inputMessageBuffer.setValue(prf_in);
45 MockPseudoRandom::desiredOutputLength = desired_output_len;
47 buffer = MockPseudoRandom::outputMessageBuffer.toString();
48 prf_out->length = buffer.length();
49 prf_out->value = (void *)buffer.c_str();
57 void GSSPseudoRandomTest::setUp()
59 CppUnit::TestFixture::setUp();
60 MockPseudoRandom::reset();
64 void GSSPseudoRandomTest::testConstructor()
67 GSSPseudoRandom cmd = GSSPseudoRandom();
72 CPPUNIT_ASSERT_EQUAL_MESSAGE(
73 "The GSSPseudoRandom object has the wrong GSS function",
74 (void *)&gss_pseudo_random,
75 (void *)cmd.getGSSFunction()
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.
87 void GSSPseudoRandomTest::testEmptyCall()
90 GSSPseudoRandom cmd = GSSPseudoRandom(&mock_gss_pseudo_random);
91 gss_ctx_id_t desiredContext;
93 ssize_t desired_output_len;
94 GSSBuffer prf_in((char *)"Input message");
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;
103 desired_output_len = (ssize_t)(rand() + 50); // Must be at least fifty bytes long
105 cmd.setContextHandle(desiredContext);
107 cmd.setInputMessage( prf_in );
108 cmd.setDesiredOutputLength(desired_output_len);
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");
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",
124 MockPseudoRandom::context_handle
127 CPPUNIT_ASSERT_EQUAL_MESSAGE(
128 "The requested pseudo random function key is not correct",
130 MockPseudoRandom::key
133 CPPUNIT_ASSERT_EQUAL_MESSAGE(
134 "The requested desired output length is not correct",
136 MockPseudoRandom::desiredOutputLength
139 CPPUNIT_ASSERT_EQUAL_MESSAGE(
140 "The requested input message is not correct",
142 MockPseudoRandom::inputMessageBuffer.toString()
145 // Do we read the results of gss_pseudo_random correctly?
147 CPPUNIT_ASSERT_EQUAL_MESSAGE(
148 "The requested output message is not correct",
149 MockPseudoRandom::outputMessageBuffer.toString(),
150 cmd.getOutputMessage().toString()
153 CPPUNIT_ASSERT_EQUAL_MESSAGE(
154 "The return value is not correct",
155 MockPseudoRandom::retVal,
159 CPPUNIT_ASSERT_EQUAL_MESSAGE(
160 "The minor status value is not correct",
161 MockPseudoRandom::minor_status,
173 * {"method": "gss_pseudo_random",
176 * "context_handle": "########",
178 * "prf_in": "la la la input message",
179 * "desired_output_len": ####
183 void GSSPseudoRandomTest::testConstructorWithJSONObject()
186 const char* input = "{\"method\": \"gss_pseudo_random\", \
189 \"context_handle\": \"#######\", \
190 \"prf_key\": 1234567890, \
191 \"prf_in\": \"mary had a little lamb\", \
192 \"desired_output_len\": 256 \
195 json_error_t jsonErr;
196 JSONObject json = JSONObject::load(input, 0, &jsonErr);
198 GSSPseudoRandom cmd = GSSPseudoRandom(&json, &mock_gss_pseudo_random);
204 CPPUNIT_ASSERT_EQUAL_MESSAGE(
205 "GSSPseudoRandom did not parse the prf_key argument correctly.",
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()
216 CPPUNIT_ASSERT_EQUAL_MESSAGE(
217 "GSSPseudoRandom did not parse the desired_output_len argument correctly.",
219 cmd.getDesiredOutputLength()
226 /* Desired JSON output:
229 * "command": "gss_pseudo_random",
234 * "random_bytes": "asdf"
238 void GSSPseudoRandomTest::testJSONMarshal()
242 std::string output("dns@google.com");
244 GSSPseudoRandom cmd = GSSPseudoRandom(&mock_gss_pseudo_random);
248 MockPseudoRandom::minor_status = 0;
249 MockPseudoRandom::retVal = 0;
250 MockPseudoRandom::outputMessageBuffer.setValue(output);
254 result = cmd.toJSON();
255 // std::cout << "\nGSSWrap JSON: \n" << result->dump() << "\n";
257 CPPUNIT_ASSERT_EQUAL_MESSAGE(
258 "The command name is incorrect",
259 std::string("gss_pseudo_random"),
260 std::string( (*result)["command"].string() )
263 CPPUNIT_ASSERT_EQUAL_MESSAGE(
264 "The return value was reported incorrectly",
265 (int)MockPseudoRandom::retVal,
266 (int)( (*result)["return_values"]["major_status"].integer() )
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() )
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() )