GSS Acquire Cred calling out correctly; slight reorginzation
[gssweb.git] / json_gssapi / test / GSSImportNameTest.cpp
1 /*
2  * Copyright (c) 2014 Painless Security LLC
3  * 
4  * For license details, see the LICENSE file in the root of this project.
5  * 
6  * GSSImportNameTest.h - Test the GSSImportName object.
7  * Tests include:
8  *   testConstructor - 
9  *       Testing basic object creation
10  *   testConstructorWithJSONObject - 
11  *       Test object creation with a set of arguments
12  *   testEmptyCall - 
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
16  *       correctly
17  *   testJSONMarshal - 
18  *       test the JSON serialization of the object
19  */
20
21 #include "GSSImportNameTest.h"
22 #include "GSSImportName.h"
23 #include "command_mocks/MockImportName.h"
24
25 CPPUNIT_TEST_SUITE_REGISTRATION( GSSImportNameTest );
26
27
28 /* 
29  * a mock of the gss_import_name call
30  * 
31  * Basically, just copy the arguments over to/from the
32  * MockImportName global object
33  */
34 static OM_uint32 KRB5_CALLCONV
35 mock_import_name(
36     OM_uint32     *minor_status,
37     gss_buffer_t   input_name_buffer, 
38     gss_OID        input_name_type,
39     gss_name_t    *output_name)
40 {
41   /* Error checking */
42   /* Variables */
43   
44   /* Setup */
45   /* Main */
46   // Copy our input from the appropriate parameters to MockImportName
47   MockImportName::input_name_buffer.setValue((char *)input_name_buffer->value, input_name_buffer->length);
48   MockImportName::input_name_type.setValue(input_name_type);
49   
50   // copy our output to the appropriate parameters
51   *minor_status = MockImportName::minor_status;
52   *output_name = MockImportName::output_name.toGss();
53   
54   
55   /* Cleanup */
56   /* return */
57   return MockImportName::retVal;
58 }
59
60 void GSSImportNameTest::setUp()
61 {
62   CppUnit::TestFixture::setUp();
63   MockImportName::reset();
64 }
65
66 void GSSImportNameTest::tearDown()
67 {
68     CppUnit::TestFixture::tearDown();
69 }
70
71 void GSSImportNameTest::testConstructor()
72 {
73   /* Variables */
74   GSSImportName cmd = GSSImportName();
75   
76   /* Error checking */
77   /* Setup */
78   /* Main */
79   CPPUNIT_ASSERT_EQUAL_MESSAGE(
80     "The GSSImportName object has the wrong GSS function",
81     (void *)&gss_import_name,
82     (void *)cmd.getGSSFunction()
83   );
84   
85   /* Cleanup */
86   /* Return */
87 }
88 void GSSImportNameTest::testEmptyCall()
89 {
90   /* Variables */
91   GSSImportName cmd = GSSImportName(&mock_import_name);
92   std::string name = std::string("ssh@server");
93   std::string type = std::string("{ 1 2 840 113554 1 2 1 4 }");
94   
95   /* Error checking */
96   /* Setup */
97   cmd.setInputName(name);
98   cmd.setInputNameType(type);
99   MockImportName::minor_status = rand() % 1024;
100   MockImportName::retVal = rand() % 1024;
101   MockImportName::output_name.setValue(GSS_C_NO_NAME);
102   
103   CPPUNIT_ASSERT_EQUAL_MESSAGE(
104     "Input name was not set correctly.",
105     name,
106     cmd.getInputName().toString()
107   );
108   CPPUNIT_ASSERT_EQUAL_MESSAGE(
109     "Input name was not set correctly.",
110     type,
111     cmd.getInputNameType().toString()
112   );
113   
114   cmd.execute();
115   
116   /* Main */
117   CPPUNIT_ASSERT_EQUAL_MESSAGE(
118     "The requested GSS name is not correct",
119     name,
120     MockImportName::input_name_buffer.toString()
121   );
122   
123   GSSOID retOID = GSSOID(MockImportName::input_name_type);
124   CPPUNIT_ASSERT_EQUAL_MESSAGE(
125     "The requested GSS name type is not correct",
126     type,
127     retOID.toString()
128   );
129   
130   /* Cleanup */
131   /* Return */
132 }
133 void GSSImportNameTest::testConstructorWithJSONObject()
134 {
135   /* Variables */
136   const char* input = "{\"method\": \"gss_import_name\", \
137     \"arguments\": {\"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);
141   
142   GSSImportName cmd = GSSImportName(&json, &mock_import_name);
143   
144   /* Error checking */
145   /* Setup */
146   /* Main */
147   
148   CPPUNIT_ASSERT_EQUAL_MESSAGE(
149     "GSSImportName did not parse the input_name argument correctly",
150     std::string("http@localhost"),
151     cmd.getInputName().toString()
152   );
153   
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()
158   );
159   
160   /* Cleanup */
161   /* Return */
162 }
163 void GSSImportNameTest::testJSONMarshal()
164 {
165   /* Variables */
166   std::string name("dns@google.com");
167   std::string type("{ 1 2 840 113554 1 2 1 4 }");
168   JSONObject *result;
169   GSSImportName cmd = GSSImportName(&mock_import_name);
170   
171   /* Error checking */
172   /* Setup */
173   cmd.setInputName(name);
174   cmd.setInputNameType(type);
175   MockImportName::minor_status = 0;
176   MockImportName::retVal = 0;
177   MockImportName::output_name.setValue(GSS_C_NO_NAME);
178   
179   /* Main */
180   cmd.execute();
181   result = cmd.toJSON();
182   
183   CPPUNIT_ASSERT_EQUAL_MESSAGE(
184     "The command name is incorrect",
185     std::string("gss_import_name"),
186     std::string( (*result)["command"].string() )
187   );
188   
189   
190   CPPUNIT_ASSERT_EQUAL_MESSAGE(
191     "The return value was reported incorrectly",
192     (int)MockImportName::retVal,
193     (int)( (*result)["return_values"]["major_status"].integer() )
194   );
195   
196   CPPUNIT_ASSERT_EQUAL_MESSAGE(
197     "The minor_status value was reported incorrectly",
198     (int)MockImportName::minor_status,
199     (int)( (*result)["return_values"]["minor_status"].integer() )
200   );
201   
202   CPPUNIT_ASSERT_EQUAL_MESSAGE(
203     "The gss_name was reported incorrectly",
204     std::string("constant for now"),
205     std::string( (*result)["return_values"]["gss_name"].string() )
206   );
207   
208   
209   /* Cleanup */
210   /* Return */
211 }
212