a7c6cad74f982c73195d82da850d3be3e6054414
[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 = "{\"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
164
165 /* Expected output sample:
166  * 
167  * {
168  *   "command": "gss_import_name",
169  *   "return_values":
170  *   {
171  *     "major_status": 0,
172  *     "minor_status": 0,
173  *     "gss_name": "base64_encoded_string"
174  *   }
175  * }
176  */
177 void GSSImportNameTest::testJSONMarshal()
178 {
179   /* Variables */
180   std::string name("dns@google.com");
181   std::string type("{ 1 2 840 113554 1 2 1 4 }");
182   std::string key;
183   GSSName gssName;
184   JSONObject *result;
185   GSSImportName cmd;
186   
187   /* Error checking */
188   /* Setup */
189   cmd.setInputName(name);
190   cmd.setInputNameType(type);
191   
192   /* Main */
193   cmd.execute();
194   result = cmd.toJSON();
195   
196 //   std::cout << "JSON Output:" << std::endl << result->dump(4) << std::endl;
197   
198   
199   CPPUNIT_ASSERT_EQUAL_MESSAGE(
200     "The return value was reported incorrectly",
201     (int)MockImportName::retVal,
202     (int)( (*result)["major_status"].integer() )
203   );
204   
205   CPPUNIT_ASSERT_EQUAL_MESSAGE(
206     "The minor_status value was reported incorrectly",
207     (int)MockImportName::minor_status,
208     (int)( (*result)["minor_status"].integer() )
209   );
210   
211   key = (*result)["gss_name"].string();
212   gssName = GSSNameCache::instance()->retrieve(key);
213   
214   CPPUNIT_ASSERT_EQUAL_MESSAGE(
215     "The gss_name was reported incorrectly",
216     name,
217     gssName.toString()
218   );
219   
220   
221   /* Cleanup */
222   /* Return */
223 }
224