Add copyright comment headers to appropriate files
[gssweb.git] / json_gssapi / test / GSSCreateSecContextTest.cpp
index 572a939..7edc79c 100644 (file)
@@ -1,25 +1,54 @@
 /*
- * Copyright (c) 2014 <copyright holder> <email>
- * 
- * For license details, see the LICENSE file in the root of this project.
- * 
+ * Copyright (c) 2014, 2015 JANET(UK)
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ *
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * 3. Neither the name of JANET(UK) nor the names of its contributors
+ *    may be used to endorse or promote products derived from this software
+ *    without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
+ * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
+ * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
+ * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
+ * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
+ * OF THE POSSIBILITY OF SUCH DAMAGE.
+ *
  */
 
 
 #include "GSSCreateSecContextTest.h"
-#include "GSSCreateSecContextCommand.h"
+#include "GSSInitSecContext.h"
 #include "command_mocks/InitSecContextMock.h"
 #include <iostream>
 #include <string.h>
 #include <exception>
-#include "util_json.h"
+#include "utils/util_json.h"
 #include <cache/GSSContextCache.h>
+#include <cache/GSSNameCache.h>
 #include <datamodel/GSSContext.h>
+#include <utils/util_base64.h>
 
 // Registers the fixture into the 'registry'
 CPPUNIT_TEST_SUITE_REGISTRATION( GSSCreateSecContextTest );
 
-
+using std::cout;
 
 static OM_uint32 KRB5_CALLCONV
 mock_init_sec(
@@ -83,15 +112,22 @@ GSSCreateSecContextTest::tearDown()
 void
 GSSCreateSecContextTest::testConstructor()
 {
-  GSSCreateSecContextCommand cmd = GSSCreateSecContextCommand();
-  void *cmdFn;
-  void *GSSFn;
+  GSSInitSecContext cmd = GSSInitSecContext();
+  init_sec_context_type cmdFn;
+  init_sec_context_type GSSFn;
   
   cmdFn = cmd.getGSSFunction();
-  GSSFn = (void *)&gss_init_sec_context;
+  GSSFn = &gss_init_sec_context;
   CPPUNIT_ASSERT_MESSAGE(
     "The default constructor for GSSCreateSecContextCommand should assign the function gss_init_sec_context", 
     cmdFn == GSSFn);
+  
+  // Check that we defaut to the moonshot OID
+  CPPUNIT_ASSERT_EQUAL_MESSAGE(
+    "The mech_type default value is unexpected.",
+    std::string("{ 1 3 6 1 5 5 15 1 1 18 }"),
+    cmd.getMechType().toString()
+  );
 }
 
 /* JSON Input:
@@ -108,49 +144,72 @@ GSSCreateSecContextTest::testConstructor()
  */
 void GSSCreateSecContextTest::testConstructorWithJSONObject()
 {
-  const char* input = "{\"method\": \"gss_create_sec_context\", \
-    \"arguments\": {\"req_flags\": \"1\", \
+  OM_uint32 major, minor;
+  gss_name_t src;
+  GSSName source;
+  char *source_name = (char *)"HTTP@localhost\0";
+  
+  
+  major = gss_import_name(&minor, GSSBuffer(source_name).toGss(), GSS_C_NT_HOSTBASED_SERVICE, &src);
+  if (GSS_ERROR(major))
+  {
+    OM_uint32 min, context;
+    gss_buffer_desc buf;
+    
+    std::cout << "Error in importing name." << std::endl;
+    gss_display_status(&min, major, GSS_C_GSS_CODE, GSS_C_NT_HOSTBASED_SERVICE, &context, &buf);
+    std::cout << "  message: " << (char *)buf.value << std::endl;
+  }
+  CPPUNIT_ASSERT_MESSAGE(
+    "Could not generate a name to test GSSCreateSecContext JSON parsing.",
+    !GSS_ERROR(major)
+  );
+  source.setValue(src);
+  std::string key = GSSNameCache::instance()->store(source);
+
+  std::string input = "{\"req_flags\": \"1\", \
     \"time_req\": \"2\", \
     \"mech_type\": \"{ 1 2 840 113554 1 2 1 4 }\", \
-    \"target_name\": \"me@my.sha/DOW\"}}";
+    \"target_name\": \"";
+  input = input + key + "\"}";
 
   json_error_t jsonErr;
-  JSONObject json = JSONObject::load(input, 0, &jsonErr);
+  const char *in = input.c_str();
+  JSONObject json = JSONObject::load(in, 0, &jsonErr);
   
-  GSSCreateSecContextCommand cmd = GSSCreateSecContextCommand(
+  GSSInitSecContext cmd = GSSInitSecContext(
     &json, 
-    (void *)&mock_init_sec
+    &mock_init_sec
   );
-  
-  
-  const char *from_json = json["arguments"]["target_name"].string();
+
   const char *from_cmd = cmd.getTargetDisplayName();
   
   CPPUNIT_ASSERT_MESSAGE(
     "The object does not have a target name.",
-    ( strcmp(from_json, from_cmd) == 0 )
+    ( strcmp(source_name, from_cmd) == 0 )
   );
   
   CPPUNIT_ASSERT_EQUAL_MESSAGE(
     "The context_handle values differ.",
-    json["arguments"]["context_handle"].integer(),
+    json["context_handle"].integer(),
     (json_int_t)cmd.getContextHandle()
   );
   
-  CPPUNIT_ASSERT_MESSAGE(
+  CPPUNIT_ASSERT_EQUAL_MESSAGE(
     "The mech_type values differ.",
-    ( strcmp(json["arguments"]["mech_type"].string(), cmd.getMechType()) == 0 )
+    std::string(json["mech_type"].string()), 
+    cmd.getMechType().toString()
   );
   
   CPPUNIT_ASSERT_EQUAL_MESSAGE(
     "The req_flags differ.",
-    (int)json["arguments"]["req_flags"].integer(),
+    (int)json["req_flags"].integer(),
     (int)cmd.getReqFlags()
   );
   
   CPPUNIT_ASSERT_EQUAL_MESSAGE(
     "The req_flags differ.",
-    (int)json["arguments"]["time_req"].integer(),
+    (int)json["time_req"].integer(),
     (int)cmd.getTimeReq()
   );
   
@@ -161,25 +220,20 @@ GSSCreateSecContextTest::testEmptyCall()
 {
   gss_ctx_id_t expectedResult, expectedArgument;
   
-  GSSCreateSecContextCommand cmd ((void *)&mock_init_sec);
+  GSSInitSecContext cmd (&mock_init_sec);
   
   /* Set expectations on what the GSS function will be called with */
   cmd.time_req = rand() % 1024;
   cmd.req_flags = rand() % 1024;
   cmd.target_name = NULL;
-  cmd.context_handle = expectedArgument = (gss_ctx_id_t)rand();
-  
-  CPPUNIT_ASSERT_MESSAGE(
-    "The mech_type values differ.",
-    ( strcmp("{ 1 2 840 113554 1 2 1 4 }", cmd.getMechType()) == 0 )
-  );
+  cmd.context_handle = expectedArgument = (gss_ctx_id_t)(long)rand();
   
   
   
   /* Set expectations on what the GSS function will produce */
   InitSecContextMock::retVal = rand() % 1024;
   InitSecContextMock::minor_status = rand() % 1024;
-  InitSecContextMock::context_handle = expectedResult = (gss_ctx_id_t)rand();
+  InitSecContextMock::context_handle = expectedResult = (gss_ctx_id_t)(long)rand();
   InitSecContextMock::actual_mech_type = NULL;
   InitSecContextMock::output_token.value = (void *)"http@project-moonshot.org/PROJECT-MOONSHOT.ORG\0";
   InitSecContextMock::output_token.length = strlen((char *)InitSecContextMock::output_token.value);
@@ -205,7 +259,7 @@ GSSCreateSecContextTest::testEmptyCall()
   );
   CPPUNIT_ASSERT_EQUAL_MESSAGE(
     "The mech_type field was not used in the call to init_sec_context",
-    cmd.mech_type,
+    cmd.getMechType().toGss(),
     InitSecContextMock::mech_type
   );
   CPPUNIT_ASSERT_EQUAL_MESSAGE(
@@ -239,7 +293,7 @@ GSSCreateSecContextTest::testEmptyCall()
   CPPUNIT_ASSERT_EQUAL_MESSAGE(
     "actual_mech_type was not copied back to the command.",
     InitSecContextMock::actual_mech_type,
-    cmd.actual_mech_type
+    cmd.getActualMechType().toGss()
   );
   CPPUNIT_ASSERT_EQUAL_MESSAGE(
     "output_token was not copied back to the command.",
@@ -282,7 +336,7 @@ GSSCreateSecContextTest::testEmptyCall()
 void GSSCreateSecContextTest::testJSONMarshal()
 {
   /* Variables */
-  GSSCreateSecContextCommand cmd ((void *)&mock_init_sec);
+  GSSInitSecContext cmd (&mock_init_sec);
   JSONObject *result;
   GSSContextCache *cache = GSSContextCache::instance();
   GSSContext context;
@@ -292,9 +346,9 @@ void GSSCreateSecContextTest::testJSONMarshal()
   
   /* Setup */
   // Set expectations on what the GSS function will produce
-  InitSecContextMock::retVal = GSS_S_BAD_MECH;
-  InitSecContextMock::minor_status = 20;
-  InitSecContextMock::context_handle = expectedResult = (gss_ctx_id_t)rand();
+  InitSecContextMock::retVal = GSS_S_CONTINUE_NEEDED;
+  InitSecContextMock::minor_status = 0;
+  InitSecContextMock::context_handle = expectedResult = (gss_ctx_id_t)(long)rand();
   InitSecContextMock::actual_mech_type = (gss_OID)GSS_C_MA_MECH_NEGO;
   InitSecContextMock::output_token.value = (void *)"http@project-moonshot.org/PROJECT-MOONSHOT.ORG\0";
   InitSecContextMock::output_token.length = strlen((char *)InitSecContextMock::output_token.value);
@@ -313,50 +367,53 @@ void GSSCreateSecContextTest::testJSONMarshal()
 /*  
   std::cout << "create sec context json: " << result->dump() << "\n";*/
   
-  CPPUNIT_ASSERT_MESSAGE(
-    "The command name is incorrect",
-    ( strcmp("gss_init_sec_context", 
-            (*result)["command"].string() ) == 0 )
-  );
-  
-  
   CPPUNIT_ASSERT_EQUAL_MESSAGE(
     "The return value was reported incorrectly",
     (int)InitSecContextMock::retVal,
-    (int)( (*result)["return_values"]["major_status"].integer() )
+    (int)( (*result)["major_status"].integer() )
   );
   
   CPPUNIT_ASSERT_EQUAL_MESSAGE(
     "The minor_status value was reported incorrectly",
     (int)InitSecContextMock::minor_status,
-    (int)( (*result)["return_values"]["minor_status"].integer() )
+    (int)( (*result)["minor_status"].integer() )
   );
   
   CPPUNIT_ASSERT_MESSAGE(
     "The actual_mech_type value was reported incorrectly",
     ( strcmp("{ 1 3 6 1 5 5 13 4 }", 
-            (*result)["return_values"]["actual_mech_type"].string() ) == 0 )
+            (*result)["actual_mech_type"].string() ) == 0 )
   );
+
   
+  
+  std::string str = (*result)["output_token"].string();
+  size_t len;
+  void *decoded = base64Decode(str.c_str(), &len);
+  CPPUNIT_ASSERT_MESSAGE(
+    "The decoded token size is incorrect",
+    ( len == InitSecContextMock::output_token.length )
+  );
   CPPUNIT_ASSERT_MESSAGE(
     "The output_token value was reported incorrectly",
-    ( strcmp((const char *)(InitSecContextMock::output_token.value), 
-            (*result)["return_values"]["output_token"].string() ) == 0 )
+    ( memcmp(InitSecContextMock::output_token.value,
+             decoded, len ) == 0 )
   );
+  base64Free(decoded);
   
   CPPUNIT_ASSERT_EQUAL_MESSAGE(
     "The minor_status value was reported incorrectly",
     (int)InitSecContextMock::ret_flags,
-    (int)( (*result)["return_values"]["ret_flags"].integer() )
+    (int)( (*result)["ret_flags"].integer() )
   );
   
   CPPUNIT_ASSERT_EQUAL_MESSAGE(
     "The minor_status value was reported incorrectly",
     (int)InitSecContextMock::time_rec,
-    (int)( (*result)["return_values"]["time_rec"].integer() )
+    (int)( (*result)["time_rec"].integer() )
   );
   
-  context = cache->retrieve( (*result)["return_values"]["context_handle"].string() );
+  context = cache->retrieve( (*result)["context_handle"].string() );
   
   CPPUNIT_ASSERT_EQUAL_MESSAGE(
     "The returned context was reported incorrectly",