Fix the build broken by the previous commit
[gssweb.git] / json_gssapi / test / GSSImportNameTest.cpp
1 /*
2  * Copyright (c) 2014, 2015 JANET(UK)
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  *
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.
15  *
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.
19  *
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.
32  *
33  */
34
35 /*
36  * GSSImportNameTest.h - Test the GSSImportName object.
37  * Tests include:
38  *   testConstructor - 
39  *       Testing basic object creation
40  *   testConstructorWithJSONObject - 
41  *       Test object creation with a set of arguments
42  *   testEmptyCall - 
43  *       Basic test of the system, with an empty call to
44  *       a mocked out gss_import_name function, to test
45  *       whether the object passes and records arguments
46  *       correctly
47  *   testJSONMarshal - 
48  *       test the JSON serialization of the object
49  */
50
51 #include "GSSImportNameTest.h"
52 #include "GSSImportName.h"
53 #include <cache/GSSNameCache.h>
54 #include "command_mocks/MockImportName.h"
55
56 CPPUNIT_TEST_SUITE_REGISTRATION( GSSImportNameTest );
57
58
59 /* 
60  * a mock of the gss_import_name call
61  * 
62  * Basically, just copy the arguments over to/from the
63  * MockImportName global object
64  */
65 static OM_uint32 KRB5_CALLCONV
66 mock_import_name(
67     OM_uint32     *minor_status,
68     gss_buffer_t   input_name_buffer, 
69     gss_OID        input_name_type,
70     gss_name_t    *output_name)
71 {
72   /* Error checking */
73   /* Variables */
74   
75   /* Setup */
76   /* Main */
77   // Copy our input from the appropriate parameters to MockImportName
78   MockImportName::input_name_buffer.setValue((char *)input_name_buffer->value, input_name_buffer->length);
79   MockImportName::input_name_type.setValue(input_name_type);
80   
81   // copy our output to the appropriate parameters
82   *minor_status = MockImportName::minor_status;
83   *output_name = MockImportName::output_name.toGss();
84   
85   
86   /* Cleanup */
87   /* return */
88   return MockImportName::retVal;
89 }
90
91 void GSSImportNameTest::setUp()
92 {
93   CppUnit::TestFixture::setUp();
94   MockImportName::reset();
95 }
96
97 void GSSImportNameTest::tearDown()
98 {
99     CppUnit::TestFixture::tearDown();
100 }
101
102 void GSSImportNameTest::testConstructor()
103 {
104   /* Variables */
105   GSSImportName cmd = GSSImportName();
106   
107   /* Error checking */
108   /* Setup */
109   /* Main */
110   CPPUNIT_ASSERT_EQUAL_MESSAGE(
111     "The GSSImportName object has the wrong GSS function",
112     (void *)&gss_import_name,
113     (void *)cmd.getGSSFunction()
114   );
115   
116   /* Cleanup */
117   /* Return */
118 }
119 void GSSImportNameTest::testEmptyCall()
120 {
121   /* Variables */
122   GSSImportName cmd = GSSImportName(&mock_import_name);
123   std::string name = std::string("ssh@server");
124   std::string type = std::string("{ 1 2 840 113554 1 2 1 4 }");
125   
126   /* Error checking */
127   /* Setup */
128   cmd.setInputName(name);
129   cmd.setInputNameType(type);
130   MockImportName::minor_status = rand() % 1024;
131   MockImportName::retVal = rand() % 1024;
132   MockImportName::output_name.setValue(GSS_C_NO_NAME);
133   
134   CPPUNIT_ASSERT_EQUAL_MESSAGE(
135     "Input name was not set correctly.",
136     name,
137     cmd.getInputName().toString()
138   );
139   CPPUNIT_ASSERT_EQUAL_MESSAGE(
140     "Input name was not set correctly.",
141     type,
142     cmd.getInputNameType().toString()
143   );
144   
145   cmd.execute();
146   
147   /* Main */
148   CPPUNIT_ASSERT_EQUAL_MESSAGE(
149     "The requested GSS name is not correct",
150     name,
151     MockImportName::input_name_buffer.toString()
152   );
153   
154   GSSOID retOID = GSSOID(MockImportName::input_name_type);
155   CPPUNIT_ASSERT_EQUAL_MESSAGE(
156     "The requested GSS name type is not correct",
157     type,
158     retOID.toString()
159   );
160   
161   /* Cleanup */
162   /* Return */
163 }
164 void GSSImportNameTest::testConstructorWithJSONObject()
165 {
166   /* Variables */
167   const char* input = "{\"input_name\": \"http@localhost\", \
168     \"input_name_type\": \"{ 1 2 840 113554 1 2 1 4 }\"}";
169   json_error_t jsonErr;
170   JSONObject json = JSONObject::load(input, 0, &jsonErr);
171   
172   GSSImportName cmd = GSSImportName(&json, &mock_import_name);
173   
174   /* Error checking */
175   /* Setup */
176   /* Main */
177   
178   CPPUNIT_ASSERT_EQUAL_MESSAGE(
179     "GSSImportName did not parse the input_name argument correctly",
180     std::string("http@localhost"),
181     cmd.getInputName().toString()
182   );
183   
184   CPPUNIT_ASSERT_EQUAL_MESSAGE(
185     "GSSImportName did not parse the input_name_type argument correctly",
186     std::string("{ 1 2 840 113554 1 2 1 4 }"),
187     cmd.getInputNameType().toString()
188   );
189   
190   /* Cleanup */
191   /* Return */
192 }
193
194
195 /* Expected output sample:
196  * 
197  * {
198  *   "command": "gss_import_name",
199  *   "return_values":
200  *   {
201  *     "major_status": 0,
202  *     "minor_status": 0,
203  *     "gss_name": "base64_encoded_string"
204  *   }
205  * }
206  */
207 void GSSImportNameTest::testJSONMarshal()
208 {
209   /* Variables */
210   std::string name("dns@google.com");
211   std::string type("{ 1 2 840 113554 1 2 1 4 }");
212   std::string key;
213   GSSName gssName;
214   JSONObject *result;
215   GSSImportName cmd;
216   
217   /* Error checking */
218   /* Setup */
219   cmd.setInputName(name);
220   cmd.setInputNameType(type);
221   
222   /* Main */
223   cmd.execute();
224   result = cmd.toJSON();
225   
226 //   std::cout << "JSON Output:" << std::endl << result->dump(4) << std::endl;
227   
228   
229   CPPUNIT_ASSERT_EQUAL_MESSAGE(
230     "The return value was reported incorrectly",
231     (int)MockImportName::retVal,
232     (int)( (*result)["major_status"].integer() )
233   );
234   
235   CPPUNIT_ASSERT_EQUAL_MESSAGE(
236     "The minor_status value was reported incorrectly",
237     (int)MockImportName::minor_status,
238     (int)( (*result)["minor_status"].integer() )
239   );
240   
241   key = (*result)["gss_name"].string();
242   gssName = GSSNameCache::instance()->retrieve(key);
243   
244   CPPUNIT_ASSERT_EQUAL_MESSAGE(
245     "The gss_name was reported incorrectly",
246     name,
247     gssName.toString()
248   );
249   
250   
251   /* Cleanup */
252   /* Return */
253 }
254