a890af608bcd60e61ff4a062c7d2f1f63c356463
[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 <cache/GSSNameCache.h>
24 #include "command_mocks/MockImportName.h"
25
26 CPPUNIT_TEST_SUITE_REGISTRATION( GSSImportNameTest );
27
28
29 /* 
30  * a mock of the gss_import_name call
31  * 
32  * Basically, just copy the arguments over to/from the
33  * MockImportName global object
34  */
35 static OM_uint32 KRB5_CALLCONV
36 mock_import_name(
37     OM_uint32     *minor_status,
38     gss_buffer_t   input_name_buffer, 
39     gss_OID        input_name_type,
40     gss_name_t    *output_name)
41 {
42   /* Error checking */
43   /* Variables */
44   
45   /* Setup */
46   /* Main */
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);
50   
51   // copy our output to the appropriate parameters
52   *minor_status = MockImportName::minor_status;
53   *output_name = MockImportName::output_name.toGss();
54   
55   
56   /* Cleanup */
57   /* return */
58   return MockImportName::retVal;
59 }
60
61 void GSSImportNameTest::setUp()
62 {
63   CppUnit::TestFixture::setUp();
64   MockImportName::reset();
65 }
66
67 void GSSImportNameTest::tearDown()
68 {
69     CppUnit::TestFixture::tearDown();
70 }
71
72 void GSSImportNameTest::testConstructor()
73 {
74   /* Variables */
75   GSSImportName cmd = GSSImportName();
76   
77   /* Error checking */
78   /* Setup */
79   /* Main */
80   CPPUNIT_ASSERT_EQUAL_MESSAGE(
81     "The GSSImportName object has the wrong GSS function",
82     (void *)&gss_import_name,
83     (void *)cmd.getGSSFunction()
84   );
85   
86   /* Cleanup */
87   /* Return */
88 }
89 void GSSImportNameTest::testEmptyCall()
90 {
91   /* Variables */
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 }");
95   
96   /* Error checking */
97   /* Setup */
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);
103   
104   CPPUNIT_ASSERT_EQUAL_MESSAGE(
105     "Input name was not set correctly.",
106     name,
107     cmd.getInputName().toString()
108   );
109   CPPUNIT_ASSERT_EQUAL_MESSAGE(
110     "Input name was not set correctly.",
111     type,
112     cmd.getInputNameType().toString()
113   );
114   
115   cmd.execute();
116   
117   /* Main */
118   CPPUNIT_ASSERT_EQUAL_MESSAGE(
119     "The requested GSS name is not correct",
120     name,
121     MockImportName::input_name_buffer.toString()
122   );
123   
124   GSSOID retOID = GSSOID(MockImportName::input_name_type);
125   CPPUNIT_ASSERT_EQUAL_MESSAGE(
126     "The requested GSS name type is not correct",
127     type,
128     retOID.toString()
129   );
130   
131   /* Cleanup */
132   /* Return */
133 }
134 void GSSImportNameTest::testConstructorWithJSONObject()
135 {
136   /* Variables */
137   const char* input = "{\"method\": \"gss_import_name\", \
138     \"arguments\": {\"input_name\": \"http@localhost\", \
139     \"input_name_type\": \"{ 1 2 840 113554 1 2 1 4 }\"}}";
140   json_error_t jsonErr;
141   JSONObject json = JSONObject::load(input, 0, &jsonErr);
142   
143   GSSImportName cmd = GSSImportName(&json, &mock_import_name);
144   
145   /* Error checking */
146   /* Setup */
147   /* Main */
148   
149   CPPUNIT_ASSERT_EQUAL_MESSAGE(
150     "GSSImportName did not parse the input_name argument correctly",
151     std::string("http@localhost"),
152     cmd.getInputName().toString()
153   );
154   
155   CPPUNIT_ASSERT_EQUAL_MESSAGE(
156     "GSSImportName did not parse the input_name_type argument correctly",
157     std::string("{ 1 2 840 113554 1 2 1 4 }"),
158     cmd.getInputNameType().toString()
159   );
160   
161   /* Cleanup */
162   /* Return */
163 }
164
165
166 /* Expected output sample:
167  * 
168  * {
169  *   "command": "gss_import_name",
170  *   "return_values":
171  *   {
172  *     "major_status": 0,
173  *     "minor_status": 0,
174  *     "gss_name": "base64_encoded_string"
175  *   }
176  * }
177  */
178 void GSSImportNameTest::testJSONMarshal()
179 {
180   /* Variables */
181   std::string name("dns@google.com");
182   std::string type("{ 1 2 840 113554 1 2 1 4 }");
183   std::string key;
184   GSSName gssName;
185   JSONObject *result;
186   GSSImportName cmd;
187   
188   /* Error checking */
189   /* Setup */
190   cmd.setInputName(name);
191   cmd.setInputNameType(type);
192   
193   /* Main */
194   cmd.execute();
195   result = cmd.toJSON();
196   
197 //   std::cout << "JSON Output:" << std::endl << result->dump(4) << std::endl;
198   
199   CPPUNIT_ASSERT_EQUAL_MESSAGE(
200     "The command name is incorrect",
201     std::string("gss_import_name"),
202     std::string( (*result)["command"].string() )
203   );
204   
205   
206   CPPUNIT_ASSERT_EQUAL_MESSAGE(
207     "The return value was reported incorrectly",
208     (int)MockImportName::retVal,
209     (int)( (*result)["return_values"]["major_status"].integer() )
210   );
211   
212   CPPUNIT_ASSERT_EQUAL_MESSAGE(
213     "The minor_status value was reported incorrectly",
214     (int)MockImportName::minor_status,
215     (int)( (*result)["return_values"]["minor_status"].integer() )
216   );
217   
218   key = (*result)["return_values"]["gss_name"].string();
219   gssName = GSSNameCache::instance()->retrieve(key);
220   
221   CPPUNIT_ASSERT_EQUAL_MESSAGE(
222     "The gss_name was reported incorrectly",
223     name,
224     gssName.toString()
225   );
226   
227   
228   /* Cleanup */
229   /* Return */
230 }
231