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