2 * Copyright (c) 2014 <copyright holder> <email>
4 * For license details, see the LICENSE file in the root of this project.
11 //#include <cppunit/TestFixture.h>
12 //#include <cppunit/extensions/HelperMacros.h>
13 #include <openssl/rand.h>
15 #include "cache/GSSContextCache.h"
16 #include "datamodel/GSSContext.h"
17 #include "command_mocks/MockPseudoRandom.h"
18 #include "GSSPseudoRandom.h"
19 #include "GSSPseudoRandomTest.h"
21 // Registers the fixture into the 'registry'
22 CPPUNIT_TEST_SUITE_REGISTRATION( GSSPseudoRandomTest );
25 // typedef OM_uint32 (*gss_pseudo_random_type) (
26 // OM_uint32 *, /* minor_status */
27 // gss_ctx_id_t, /* context */
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,
36 const gss_buffer_t prf_in,
37 ssize_t desired_output_len,
44 MockPseudoRandom::context_handle = context;
45 MockPseudoRandom::key = prf_key;
46 MockPseudoRandom::inputMessageBuffer.setValue(prf_in);
47 MockPseudoRandom::desiredOutputLength = desired_output_len;
49 *prf_out = *MockPseudoRandom::outputMessageBuffer.toGss();
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": "context handle key",
178 * "prf_in": "la la la input message",
179 * "desired_output_len": ####
183 void GSSPseudoRandomTest::testConstructorWithJSONObject()
186 GSSContext context( (gss_ctx_id_t)rand(), true );
187 std::string key = GSSContextCache::instance()->store(context);
189 std::string input = "{ \
190 \"context_handle\": \"" + key + "\", \
191 \"prf_key\": 1234567890, \
192 \"prf_in\": \"mary had a little lamb\", \
193 \"desired_output_len\": 256 \
195 json_error_t jsonErr;
196 JSONObject json = JSONObject::load(input.c_str(), 0, &jsonErr);
198 GSSPseudoRandom cmd = GSSPseudoRandom(&json, &mock_gss_pseudo_random);
204 CPPUNIT_ASSERT_EQUAL_MESSAGE(
205 "GSSWrap did not retrive the GSS context correctly",
206 context.getContext(),
207 cmd.getContextHandle()
210 CPPUNIT_ASSERT_EQUAL_MESSAGE(
211 "GSSPseudoRandom did not parse the prf_key argument correctly.",
216 CPPUNIT_ASSERT_EQUAL_MESSAGE(
217 "GSSPseudoRandom did not parse the prf_in argument correctly.",
218 std::string("mary had a little lamb"),
219 cmd.getInputMessage().toString()
222 CPPUNIT_ASSERT_EQUAL_MESSAGE(
223 "GSSPseudoRandom did not parse the desired_output_len argument correctly.",
225 cmd.getDesiredOutputLength()
232 /* Desired JSON output:
235 * "command": "gss_pseudo_random",
240 * "random_bytes": "asdf"
244 void GSSPseudoRandomTest::testJSONMarshal()
248 std::string output("dns@google.com");
250 GSSPseudoRandom cmd = GSSPseudoRandom(&mock_gss_pseudo_random);
254 MockPseudoRandom::minor_status = 0;
255 MockPseudoRandom::retVal = 0;
256 MockPseudoRandom::outputMessageBuffer.setValue(output);
260 result = cmd.toJSON();
261 // std::cout << "\nGSSWrap JSON: \n" << result->dump() << "\n";
263 CPPUNIT_ASSERT_EQUAL_MESSAGE(
264 "The return value was reported incorrectly",
265 (int)MockPseudoRandom::retVal,
266 (int)( (*result)["major_status"].integer() )
269 CPPUNIT_ASSERT_EQUAL_MESSAGE(
270 "The minor_status value was reported incorrectly",
271 (int)MockPseudoRandom::minor_status,
272 (int)( (*result)["minor_status"].integer() )
275 CPPUNIT_ASSERT_EQUAL_MESSAGE(
276 "The random bytes were reported incorrectly",
277 MockPseudoRandom::outputMessageBuffer.toString(),
278 std::string( (*result)["random_bytes"].string() )