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,
45 MockPseudoRandom::context_handle = context;
46 MockPseudoRandom::key = prf_key;
47 MockPseudoRandom::inputMessageBuffer.setValue(prf_in);
48 MockPseudoRandom::desiredOutputLength = desired_output_len;
50 buffer = MockPseudoRandom::outputMessageBuffer.toString();
51 prf_out->length = buffer.length();
52 prf_out->value = (void *)buffer.c_str();
60 void GSSPseudoRandomTest::setUp()
62 CppUnit::TestFixture::setUp();
63 MockPseudoRandom::reset();
67 void GSSPseudoRandomTest::testConstructor()
70 GSSPseudoRandom cmd = GSSPseudoRandom();
75 CPPUNIT_ASSERT_EQUAL_MESSAGE(
76 "The GSSPseudoRandom object has the wrong GSS function",
77 (void *)&gss_pseudo_random,
78 (void *)cmd.getGSSFunction()
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.
90 void GSSPseudoRandomTest::testEmptyCall()
93 GSSPseudoRandom cmd = GSSPseudoRandom(&mock_gss_pseudo_random);
94 gss_ctx_id_t desiredContext;
96 ssize_t desired_output_len;
97 GSSBuffer prf_in((char *)"Input message");
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;
106 desired_output_len = (ssize_t)(rand() + 50); // Must be at least fifty bytes long
108 cmd.setContextHandle(desiredContext);
110 cmd.setInputMessage( prf_in );
111 cmd.setDesiredOutputLength(desired_output_len);
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");
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",
127 MockPseudoRandom::context_handle
130 CPPUNIT_ASSERT_EQUAL_MESSAGE(
131 "The requested pseudo random function key is not correct",
133 MockPseudoRandom::key
136 CPPUNIT_ASSERT_EQUAL_MESSAGE(
137 "The requested desired output length is not correct",
139 MockPseudoRandom::desiredOutputLength
142 CPPUNIT_ASSERT_EQUAL_MESSAGE(
143 "The requested input message is not correct",
145 MockPseudoRandom::inputMessageBuffer.toString()
148 // Do we read the results of gss_pseudo_random correctly?
150 CPPUNIT_ASSERT_EQUAL_MESSAGE(
151 "The requested output message is not correct",
152 MockPseudoRandom::outputMessageBuffer.toString(),
153 cmd.getOutputMessage().toString()
156 CPPUNIT_ASSERT_EQUAL_MESSAGE(
157 "The return value is not correct",
158 MockPseudoRandom::retVal,
162 CPPUNIT_ASSERT_EQUAL_MESSAGE(
163 "The minor status value is not correct",
164 MockPseudoRandom::minor_status,
176 * {"method": "gss_pseudo_random",
179 * "context_handle": "context handle key",
181 * "prf_in": "la la la input message",
182 * "desired_output_len": ####
186 void GSSPseudoRandomTest::testConstructorWithJSONObject()
189 GSSContext context( (gss_ctx_id_t)rand(), true );
190 std::string key = GSSContextCache::instance()->store(context);
192 std::string input = "{\"method\": \"gss_pseudo_random\", \
195 \"context_handle\": \"" + key + "\", \
196 \"prf_key\": 1234567890, \
197 \"prf_in\": \"mary had a little lamb\", \
198 \"desired_output_len\": 256 \
201 json_error_t jsonErr;
202 JSONObject json = JSONObject::load(input.c_str(), 0, &jsonErr);
204 GSSPseudoRandom cmd = GSSPseudoRandom(&json, &mock_gss_pseudo_random);
210 CPPUNIT_ASSERT_EQUAL_MESSAGE(
211 "GSSWrap did not retrive the GSS context correctly",
212 context.getContext(),
213 cmd.getContextHandle()
216 CPPUNIT_ASSERT_EQUAL_MESSAGE(
217 "GSSPseudoRandom did not parse the prf_key argument correctly.",
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()
228 CPPUNIT_ASSERT_EQUAL_MESSAGE(
229 "GSSPseudoRandom did not parse the desired_output_len argument correctly.",
231 cmd.getDesiredOutputLength()
238 /* Desired JSON output:
241 * "command": "gss_pseudo_random",
246 * "random_bytes": "asdf"
250 void GSSPseudoRandomTest::testJSONMarshal()
254 std::string output("dns@google.com");
256 GSSPseudoRandom cmd = GSSPseudoRandom(&mock_gss_pseudo_random);
260 MockPseudoRandom::minor_status = 0;
261 MockPseudoRandom::retVal = 0;
262 MockPseudoRandom::outputMessageBuffer.setValue(output);
266 result = cmd.toJSON();
267 // std::cout << "\nGSSWrap JSON: \n" << result->dump() << "\n";
269 CPPUNIT_ASSERT_EQUAL_MESSAGE(
270 "The command name is incorrect",
271 std::string("gss_pseudo_random"),
272 std::string( (*result)["command"].string() )
275 CPPUNIT_ASSERT_EQUAL_MESSAGE(
276 "The return value was reported incorrectly",
277 (int)MockPseudoRandom::retVal,
278 (int)( (*result)["return_values"]["major_status"].integer() )
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() )
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() )