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 * Testing basic object creation
37 * testConstructorWithJSONObject -
38 * Test object creation with a set of arguments
40 * Basic test of the system, with an empty call to
41 * a mocked out gss_import_name function, to test
42 * whether the object passes and records arguments
45 * test the JSON serialization of the object
48 #include "GSSImportNameTest.h"
49 #include "GSSImportName.h"
50 #include <cache/GSSNameCache.h>
51 #include "command_mocks/MockImportName.h"
53 CPPUNIT_TEST_SUITE_REGISTRATION( GSSImportNameTest );
57 * a mock of the gss_import_name call
59 * Basically, just copy the arguments over to/from the
60 * MockImportName global object
62 static OM_uint32 KRB5_CALLCONV
64 OM_uint32 *minor_status,
65 gss_buffer_t input_name_buffer,
66 gss_OID input_name_type,
67 gss_name_t *output_name)
74 // Copy our input from the appropriate parameters to MockImportName
75 MockImportName::input_name_buffer.setValue((char *)input_name_buffer->value, input_name_buffer->length);
76 MockImportName::input_name_type.setValue(input_name_type);
78 // copy our output to the appropriate parameters
79 *minor_status = MockImportName::minor_status;
80 *output_name = MockImportName::output_name.toGss();
85 return MockImportName::retVal;
88 void GSSImportNameTest::setUp()
90 CppUnit::TestFixture::setUp();
91 MockImportName::reset();
94 void GSSImportNameTest::tearDown()
96 CppUnit::TestFixture::tearDown();
99 void GSSImportNameTest::testConstructor()
102 GSSImportName cmd = GSSImportName();
107 CPPUNIT_ASSERT_EQUAL_MESSAGE(
108 "The GSSImportName object has the wrong GSS function",
109 (void *)&gss_import_name,
110 (void *)cmd.getGSSFunction()
116 void GSSImportNameTest::testEmptyCall()
119 GSSImportName cmd = GSSImportName(&mock_import_name);
120 std::string name = std::string("ssh@server");
121 std::string type = std::string("{ 1 2 840 113554 1 2 1 4 }");
125 cmd.setInputName(name);
126 cmd.setInputNameType(type);
127 MockImportName::minor_status = rand() % 1024;
128 MockImportName::retVal = rand() % 1024;
129 MockImportName::output_name.setValue(GSS_C_NO_NAME);
131 CPPUNIT_ASSERT_EQUAL_MESSAGE(
132 "Input name was not set correctly.",
134 cmd.getInputName().toString()
136 CPPUNIT_ASSERT_EQUAL_MESSAGE(
137 "Input name was not set correctly.",
139 cmd.getInputNameType().toString()
145 CPPUNIT_ASSERT_EQUAL_MESSAGE(
146 "The requested GSS name is not correct",
148 MockImportName::input_name_buffer.toString()
151 GSSOID retOID = GSSOID(MockImportName::input_name_type);
152 CPPUNIT_ASSERT_EQUAL_MESSAGE(
153 "The requested GSS name type is not correct",
161 void GSSImportNameTest::testConstructorWithJSONObject()
164 const char* input = "{\"input_name\": \"http@localhost\", \
165 \"input_name_type\": \"{ 1 2 840 113554 1 2 1 4 }\"}";
166 json_error_t jsonErr;
167 JSONObject json = JSONObject::load(input, 0, &jsonErr);
169 GSSImportName cmd = GSSImportName(&json, &mock_import_name);
175 CPPUNIT_ASSERT_EQUAL_MESSAGE(
176 "GSSImportName did not parse the input_name argument correctly",
177 std::string("http@localhost"),
178 cmd.getInputName().toString()
181 CPPUNIT_ASSERT_EQUAL_MESSAGE(
182 "GSSImportName did not parse the input_name_type argument correctly",
183 std::string("{ 1 2 840 113554 1 2 1 4 }"),
184 cmd.getInputNameType().toString()
192 /* Expected output sample:
195 * "command": "gss_import_name",
200 * "gss_name": "base64_encoded_string"
204 void GSSImportNameTest::testJSONMarshal()
207 std::string name("dns@google.com");
208 std::string type("{ 1 2 840 113554 1 2 1 4 }");
216 cmd.setInputName(name);
217 cmd.setInputNameType(type);
221 result = cmd.toJSON();
223 // std::cout << "JSON Output:" << std::endl << result->dump(4) << std::endl;
226 CPPUNIT_ASSERT_EQUAL_MESSAGE(
227 "The return value was reported incorrectly",
228 (int)MockImportName::retVal,
229 (int)( (*result)["major_status"].integer() )
232 CPPUNIT_ASSERT_EQUAL_MESSAGE(
233 "The minor_status value was reported incorrectly",
234 (int)MockImportName::minor_status,
235 (int)( (*result)["minor_status"].integer() )
238 key = (*result)["gss_name"].string();
239 gssName = GSSNameCache::instance()->retrieve(key);
241 CPPUNIT_ASSERT_EQUAL_MESSAGE(
242 "The gss_name was reported incorrectly",