Add copyright comment headers to appropriate files
[gssweb.git] / json_gssapi / test / GSSWrapTest.cpp
index 7582722..06b7a53 100644 (file)
@@ -1,13 +1,42 @@
 /*
- * Copyright (c) 2014 <copyright holder> <email>
+ * Copyright (c) 2014, 2015 JANET(UK)
+ * All rights reserved.
  *
- * For license details, see the LICENSE file in the root of this project.
+ * 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 "GSSWrapTest.h"
 #include "command_mocks/MockWrap.h"
 #include "GSSWrap.h"
+#include <datamodel/GSSContext.h>
+#include <cache/GSSContextCache.h>
 #include <gssapi/gssapi.h>
 
 CPPUNIT_TEST_SUITE_REGISTRATION( GSSWrapTest );
@@ -31,10 +60,8 @@ mock_wrap(
 {
   /* Error checking */
   /* Variables */
-  std::string buffer;
   
   /* Setup */
-  buffer = MockWrap::outputMessageBuffer.toString();
   
   /* Main */
   // Copy our input from the appropriate parameters to MockWrap
@@ -47,8 +74,7 @@ mock_wrap(
   // copy our output to the appropriate parameters
   *minor_status = MockWrap::minor_status;
   *conf_state = MockWrap::conf_state;
-  output_message_buffer->length = buffer.length();
-  output_message_buffer->value  = (void *)buffer.c_str();
+  *output_message_buffer = *MockWrap::outputMessageBuffer.toGss();
   
   /* Cleanup */
   /* return */
@@ -86,7 +112,7 @@ void GSSWrapTest::testEmptyCall()
   GSSBuffer input((char *)"Input message");
   gss_qop_t desiredQop = rand();
   int desiredConf = rand();
-  gss_ctx_id_t desiredContext = (gss_ctx_id_t)rand();
+  gss_ctx_id_t desiredContext = (gss_ctx_id_t)( (long)0 | rand() );
   
   /* Error checking */
   /* Setup */
@@ -146,20 +172,34 @@ void GSSWrapTest::testEmptyCall()
   /* Return */
 }
 
+/*
+ * Example JSON input:
+ * {
+ *   "method": "gss_wrap",
+ *   "arguments": {
+ *     "context_handle": "stuff",
+ *     "conf_req": "TRUE",
+ *     "qop_req": "GSS_C_QOP_DEFAULT",
+ *     "input_message": "mary had a little lamb"
+ *   }
+ * }
+ * 
+ */
 void GSSWrapTest::testConstructorWithJSONObject()
 {
   /* Variables */
-  const char* input = "{\"method\": \"gss_wrap\", \
-    \"arguments\": \
-    { \
-         \"context_handle\": \"#######\", \
+  GSSContext context( (gss_ctx_id_t)( (long)0 | rand()),
+                     true );
+  std::string key = GSSContextCache::instance()->store(context);
+  
+  std::string input = "{ \
+         \"context_handle\": \"" + key + "\", \
          \"conf_req\": \"TRUE\", \
          \"qop_req\": \"GSS_C_QOP_DEFAULT\", \
          \"input_message\": \"mary had a little lamb\" \
-    }\
-  }";
+    }";
   json_error_t jsonErr;
-  JSONObject json = JSONObject::load(input, 0, &jsonErr);
+  JSONObject json = JSONObject::load(input.c_str(), 0, &jsonErr);
   
   GSSWrap cmd = GSSWrap(&json, &mock_wrap);
   
@@ -168,6 +208,12 @@ void GSSWrapTest::testConstructorWithJSONObject()
   /* Main */
   
   CPPUNIT_ASSERT_EQUAL_MESSAGE(
+    "GSSWrap did not retrive the GSS context correctly",
+    context.getContext(),
+    cmd.getContext()
+  );
+  
+  CPPUNIT_ASSERT_EQUAL_MESSAGE(
     "GSSWrap did not parse the conf_req argument correctly",
     1,
     cmd.getConfReq()
@@ -191,7 +237,19 @@ void GSSWrapTest::testConstructorWithJSONObject()
   /* Return */
 }
 
-
+/* Desired JSON output:
+ * 
+ * {
+ *  "command":       "gss_wrap",
+ *  "return_values": 
+ *  {
+ *      "major_status":   0,
+ *      "minor_status":   0,
+ *      "conf_state":     "TRUE",
+ *      "output_message": "asdf"
+ *  }
+ * }
+ */
 void GSSWrapTest::testJSONMarshal()
 {
   /* Variables */
@@ -212,29 +270,23 @@ void GSSWrapTest::testJSONMarshal()
   result = cmd.toJSON();
 //   std::cout << "\nGSSWrap JSON: \n" << result->dump() << "\n";
   
-  CPPUNIT_ASSERT_EQUAL_MESSAGE(
-    "The command name is incorrect",
-    std::string("gss_wrap"),
-    std::string( (*result)["command"].string() )
-  );
-  
   
   CPPUNIT_ASSERT_EQUAL_MESSAGE(
     "The return value was reported incorrectly",
     (int)MockWrap::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)MockWrap::minor_status,
-    (int)( (*result)["return_values"]["minor_status"].integer() )
+    (int)( (*result)["minor_status"].integer() )
   );
   
   CPPUNIT_ASSERT_EQUAL_MESSAGE(
     "The output message was reported incorrectly",
     MockWrap::outputMessageBuffer.toString(),
-    std::string( (*result)["return_values"]["output_message"].string() )
+    std::string( (*result)["output_message"].string() )
   );