2 * Copyright (c) 2014, 2015 JANET(UK)
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * 3. Neither the name of JANET(UK) nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
23 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
24 * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
25 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
26 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
27 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
29 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
31 * OF THE POSSIBILITY OF SUCH DAMAGE.
36 * GSSImportNameTest.h - Test the GSSImportName object.
39 * Testing basic object creation
40 * testConstructorWithJSONObject -
41 * Test object creation with a set of arguments
43 * Basic test of the system, with an empty call to
44 * a mocked out gss_import_name function, to test
45 * whether the object passes and records arguments
48 * test the JSON serialization of the object
51 #include "GSSImportNameTest.h"
52 #include "GSSImportName.h"
53 #include <cache/GSSNameCache.h>
54 #include "command_mocks/MockImportName.h"
56 CPPUNIT_TEST_SUITE_REGISTRATION( GSSImportNameTest );
60 * a mock of the gss_import_name call
62 * Basically, just copy the arguments over to/from the
63 * MockImportName global object
65 static OM_uint32 KRB5_CALLCONV
67 OM_uint32 *minor_status,
68 gss_buffer_t input_name_buffer,
69 gss_OID input_name_type,
70 gss_name_t *output_name)
77 // Copy our input from the appropriate parameters to MockImportName
78 MockImportName::input_name_buffer.setValue((char *)input_name_buffer->value, input_name_buffer->length);
79 MockImportName::input_name_type.setValue(input_name_type);
81 // copy our output to the appropriate parameters
82 *minor_status = MockImportName::minor_status;
83 *output_name = MockImportName::output_name.toGss();
88 return MockImportName::retVal;
91 void GSSImportNameTest::setUp()
93 CppUnit::TestFixture::setUp();
94 MockImportName::reset();
97 void GSSImportNameTest::tearDown()
99 CppUnit::TestFixture::tearDown();
102 void GSSImportNameTest::testConstructor()
105 GSSImportName cmd = GSSImportName();
110 CPPUNIT_ASSERT_EQUAL_MESSAGE(
111 "The GSSImportName object has the wrong GSS function",
112 (void *)&gss_import_name,
113 (void *)cmd.getGSSFunction()
119 void GSSImportNameTest::testEmptyCall()
122 GSSImportName cmd = GSSImportName(&mock_import_name);
123 std::string name = std::string("ssh@server");
124 std::string type = std::string("{ 1 2 840 113554 1 2 1 4 }");
128 cmd.setInputName(name);
129 cmd.setInputNameType(type);
130 MockImportName::minor_status = rand() % 1024;
131 MockImportName::retVal = rand() % 1024;
132 MockImportName::output_name.setValue(GSS_C_NO_NAME);
134 CPPUNIT_ASSERT_EQUAL_MESSAGE(
135 "Input name was not set correctly.",
137 cmd.getInputName().toString()
139 CPPUNIT_ASSERT_EQUAL_MESSAGE(
140 "Input name was not set correctly.",
142 cmd.getInputNameType().toString()
148 CPPUNIT_ASSERT_EQUAL_MESSAGE(
149 "The requested GSS name is not correct",
151 MockImportName::input_name_buffer.toString()
154 GSSOID retOID = GSSOID(MockImportName::input_name_type);
155 CPPUNIT_ASSERT_EQUAL_MESSAGE(
156 "The requested GSS name type is not correct",
164 void GSSImportNameTest::testConstructorWithJSONObject()
167 const char* input = "{\"input_name\": \"http@localhost\", \
168 \"input_name_type\": \"{ 1 2 840 113554 1 2 1 4 }\"}";
169 json_error_t jsonErr;
170 JSONObject json = JSONObject::load(input, 0, &jsonErr);
172 GSSImportName cmd = GSSImportName(&json, &mock_import_name);
178 CPPUNIT_ASSERT_EQUAL_MESSAGE(
179 "GSSImportName did not parse the input_name argument correctly",
180 std::string("http@localhost"),
181 cmd.getInputName().toString()
184 CPPUNIT_ASSERT_EQUAL_MESSAGE(
185 "GSSImportName did not parse the input_name_type argument correctly",
186 std::string("{ 1 2 840 113554 1 2 1 4 }"),
187 cmd.getInputNameType().toString()
195 /* Expected output sample:
198 * "command": "gss_import_name",
203 * "gss_name": "base64_encoded_string"
207 void GSSImportNameTest::testJSONMarshal()
210 std::string name("dns@google.com");
211 std::string type("{ 1 2 840 113554 1 2 1 4 }");
219 cmd.setInputName(name);
220 cmd.setInputNameType(type);
224 result = cmd.toJSON();
226 // std::cout << "JSON Output:" << std::endl << result->dump(4) << std::endl;
229 CPPUNIT_ASSERT_EQUAL_MESSAGE(
230 "The return value was reported incorrectly",
231 (int)MockImportName::retVal,
232 (int)( (*result)["major_status"].integer() )
235 CPPUNIT_ASSERT_EQUAL_MESSAGE(
236 "The minor_status value was reported incorrectly",
237 (int)MockImportName::minor_status,
238 (int)( (*result)["minor_status"].integer() )
241 key = (*result)["gss_name"].string();
242 gssName = GSSNameCache::instance()->retrieve(key);
244 CPPUNIT_ASSERT_EQUAL_MESSAGE(
245 "The gss_name was reported incorrectly",