2 * Copyright (c) 2014 Painless Security LLC
4 * For license details, see the LICENSE file in the root of this project.
6 * GSSImportNameTest.h - Test the GSSImportName object.
9 * Testing basic object creation
10 * testConstructorWithJSONObject -
11 * Test object creation with a set of arguments
13 * Basic test of the system, with an empty call to
14 * a mocked out gss_import_name function, to test
15 * whether the object passes and records arguments
18 * test the JSON serialization of the object
21 #include "GSSImportNameTest.h"
22 #include "GSSImportName.h"
23 #include <cache/GSSNameCache.h>
24 #include "command_mocks/MockImportName.h"
26 CPPUNIT_TEST_SUITE_REGISTRATION( GSSImportNameTest );
30 * a mock of the gss_import_name call
32 * Basically, just copy the arguments over to/from the
33 * MockImportName global object
35 static OM_uint32 KRB5_CALLCONV
37 OM_uint32 *minor_status,
38 gss_buffer_t input_name_buffer,
39 gss_OID input_name_type,
40 gss_name_t *output_name)
47 // Copy our input from the appropriate parameters to MockImportName
48 MockImportName::input_name_buffer.setValue((char *)input_name_buffer->value, input_name_buffer->length);
49 MockImportName::input_name_type.setValue(input_name_type);
51 // copy our output to the appropriate parameters
52 *minor_status = MockImportName::minor_status;
53 *output_name = MockImportName::output_name.toGss();
58 return MockImportName::retVal;
61 void GSSImportNameTest::setUp()
63 CppUnit::TestFixture::setUp();
64 MockImportName::reset();
67 void GSSImportNameTest::tearDown()
69 CppUnit::TestFixture::tearDown();
72 void GSSImportNameTest::testConstructor()
75 GSSImportName cmd = GSSImportName();
80 CPPUNIT_ASSERT_EQUAL_MESSAGE(
81 "The GSSImportName object has the wrong GSS function",
82 (void *)&gss_import_name,
83 (void *)cmd.getGSSFunction()
89 void GSSImportNameTest::testEmptyCall()
92 GSSImportName cmd = GSSImportName(&mock_import_name);
93 std::string name = std::string("ssh@server");
94 std::string type = std::string("{ 1 2 840 113554 1 2 1 4 }");
98 cmd.setInputName(name);
99 cmd.setInputNameType(type);
100 MockImportName::minor_status = rand() % 1024;
101 MockImportName::retVal = rand() % 1024;
102 MockImportName::output_name.setValue(GSS_C_NO_NAME);
104 CPPUNIT_ASSERT_EQUAL_MESSAGE(
105 "Input name was not set correctly.",
107 cmd.getInputName().toString()
109 CPPUNIT_ASSERT_EQUAL_MESSAGE(
110 "Input name was not set correctly.",
112 cmd.getInputNameType().toString()
118 CPPUNIT_ASSERT_EQUAL_MESSAGE(
119 "The requested GSS name is not correct",
121 MockImportName::input_name_buffer.toString()
124 GSSOID retOID = GSSOID(MockImportName::input_name_type);
125 CPPUNIT_ASSERT_EQUAL_MESSAGE(
126 "The requested GSS name type is not correct",
134 void GSSImportNameTest::testConstructorWithJSONObject()
137 const char* input = "{\"input_name\": \"http@localhost\", \
138 \"input_name_type\": \"{ 1 2 840 113554 1 2 1 4 }\"}";
139 json_error_t jsonErr;
140 JSONObject json = JSONObject::load(input, 0, &jsonErr);
142 GSSImportName cmd = GSSImportName(&json, &mock_import_name);
148 CPPUNIT_ASSERT_EQUAL_MESSAGE(
149 "GSSImportName did not parse the input_name argument correctly",
150 std::string("http@localhost"),
151 cmd.getInputName().toString()
154 CPPUNIT_ASSERT_EQUAL_MESSAGE(
155 "GSSImportName did not parse the input_name_type argument correctly",
156 std::string("{ 1 2 840 113554 1 2 1 4 }"),
157 cmd.getInputNameType().toString()
165 /* Expected output sample:
168 * "command": "gss_import_name",
173 * "gss_name": "base64_encoded_string"
177 void GSSImportNameTest::testJSONMarshal()
180 std::string name("dns@google.com");
181 std::string type("{ 1 2 840 113554 1 2 1 4 }");
189 cmd.setInputName(name);
190 cmd.setInputNameType(type);
194 result = cmd.toJSON();
196 // std::cout << "JSON Output:" << std::endl << result->dump(4) << std::endl;
199 CPPUNIT_ASSERT_EQUAL_MESSAGE(
200 "The return value was reported incorrectly",
201 (int)MockImportName::retVal,
202 (int)( (*result)["major_status"].integer() )
205 CPPUNIT_ASSERT_EQUAL_MESSAGE(
206 "The minor_status value was reported incorrectly",
207 (int)MockImportName::minor_status,
208 (int)( (*result)["minor_status"].integer() )
211 key = (*result)["gss_name"].string();
212 gssName = GSSNameCache::instance()->retrieve(key);
214 CPPUNIT_ASSERT_EQUAL_MESSAGE(
215 "The gss_name was reported incorrectly",