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 = "{ \
193 \"context_handle\": \"" + key + "\", \
194 \"prf_key\": 1234567890, \
195 \"prf_in\": \"mary had a little lamb\", \
196 \"desired_output_len\": 256 \
198 json_error_t jsonErr;
199 JSONObject json = JSONObject::load(input.c_str(), 0, &jsonErr);
201 GSSPseudoRandom cmd = GSSPseudoRandom(&json, &mock_gss_pseudo_random);
207 CPPUNIT_ASSERT_EQUAL_MESSAGE(
208 "GSSWrap did not retrive the GSS context correctly",
209 context.getContext(),
210 cmd.getContextHandle()
213 CPPUNIT_ASSERT_EQUAL_MESSAGE(
214 "GSSPseudoRandom did not parse the prf_key argument correctly.",
219 CPPUNIT_ASSERT_EQUAL_MESSAGE(
220 "GSSPseudoRandom did not parse the prf_in argument correctly.",
221 std::string("mary had a little lamb"),
222 cmd.getInputMessage().toString()
225 CPPUNIT_ASSERT_EQUAL_MESSAGE(
226 "GSSPseudoRandom did not parse the desired_output_len argument correctly.",
228 cmd.getDesiredOutputLength()
235 /* Desired JSON output:
238 * "command": "gss_pseudo_random",
243 * "random_bytes": "asdf"
247 void GSSPseudoRandomTest::testJSONMarshal()
251 std::string output("dns@google.com");
253 GSSPseudoRandom cmd = GSSPseudoRandom(&mock_gss_pseudo_random);
257 MockPseudoRandom::minor_status = 0;
258 MockPseudoRandom::retVal = 0;
259 MockPseudoRandom::outputMessageBuffer.setValue(output);
263 result = cmd.toJSON();
264 // std::cout << "\nGSSWrap JSON: \n" << result->dump() << "\n";
266 CPPUNIT_ASSERT_EQUAL_MESSAGE(
267 "The return value was reported incorrectly",
268 (int)MockPseudoRandom::retVal,
269 (int)( (*result)["major_status"].integer() )
272 CPPUNIT_ASSERT_EQUAL_MESSAGE(
273 "The minor_status value was reported incorrectly",
274 (int)MockPseudoRandom::minor_status,
275 (int)( (*result)["minor_status"].integer() )
278 CPPUNIT_ASSERT_EQUAL_MESSAGE(
279 "The random bytes were reported incorrectly",
280 MockPseudoRandom::outputMessageBuffer.toString(),
281 std::string( (*result)["random_bytes"].string() )