Add copyright comment headers to appropriate files
[gssweb.git] / json_gssapi / test / GSSPseudoRandomTest.cpp
index 12693ad..65cd5c2 100644 (file)
@@ -1,19 +1,49 @@
 /*
- * 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 <algorithm>
+#include <string>
 
 //#include <cppunit/TestFixture.h>
 //#include <cppunit/extensions/HelperMacros.h>
 #include <openssl/rand.h>
 
+#include "cache/GSSContextCache.h"
+#include "datamodel/GSSContext.h"
+#include "command_mocks/MockPseudoRandom.h"
 #include "GSSPseudoRandom.h"
 #include "GSSPseudoRandomTest.h"
-#include "command_mocks/MockPseudoRandom.h"
 
 // Registers the fixture into the 'registry'
 CPPUNIT_TEST_SUITE_REGISTRATION( GSSPseudoRandomTest );
@@ -35,7 +65,6 @@ OM_uint32 mock_gss_pseudo_random(
     gss_buffer_t prf_out)
 {
   /* Variables */
-  std::string buffer;
   /* Error checking */
   /* Setup */
   /* Main */
@@ -44,9 +73,7 @@ OM_uint32 mock_gss_pseudo_random(
   MockPseudoRandom::inputMessageBuffer.setValue(prf_in);
   MockPseudoRandom::desiredOutputLength = desired_output_len;
   
-  buffer = MockPseudoRandom::outputMessageBuffer.toString();
-  prf_out->length = buffer.length();
-  prf_out->value = (void *)buffer.c_str();
+  *prf_out = *MockPseudoRandom::outputMessageBuffer.toGss();
   
   /* Cleanup */
   /* Return */
@@ -173,7 +200,7 @@ void GSSPseudoRandomTest::testEmptyCall()
  * {"method":    "gss_pseudo_random",
  *  "arguments": 
  *   {
- *     "context_handle":     "########",
+ *     "context_handle":     "context handle key",
  *     "prf_key":            ###,
  *     "prf_in":             "la la la input message",
  *     "desired_output_len": ####
@@ -183,17 +210,18 @@ void GSSPseudoRandomTest::testEmptyCall()
 void GSSPseudoRandomTest::testConstructorWithJSONObject()
 {
   /* Variables */
-  const char* input = "{\"method\": \"gss_pseudo_random\", \
-    \"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 + "\", \
          \"prf_key\": 1234567890, \
          \"prf_in\": \"mary had a little lamb\", \
          \"desired_output_len\": 256 \
-    }\
-  }";
+    }";
   json_error_t jsonErr;
-  JSONObject json = JSONObject::load(input, 0, &jsonErr);
+  JSONObject json = JSONObject::load(input.c_str(), 0, &jsonErr);
   
   GSSPseudoRandom cmd = GSSPseudoRandom(&json, &mock_gss_pseudo_random);
 
@@ -202,6 +230,12 @@ void GSSPseudoRandomTest::testConstructorWithJSONObject()
   /* Main */
   
   CPPUNIT_ASSERT_EQUAL_MESSAGE(
+    "GSSWrap did not retrive the GSS context correctly",
+    context.getContext(),
+    cmd.getContextHandle()
+  );
+  
+  CPPUNIT_ASSERT_EQUAL_MESSAGE(
     "GSSPseudoRandom did not parse the prf_key argument correctly.",
     1234567890,
     cmd.getKey()
@@ -255,27 +289,21 @@ void GSSPseudoRandomTest::testJSONMarshal()
 //   std::cout << "\nGSSWrap JSON: \n" << result->dump() << "\n";
   
   CPPUNIT_ASSERT_EQUAL_MESSAGE(
-    "The command name is incorrect",
-    std::string("gss_pseudo_random"),
-    std::string( (*result)["command"].string() )
-  );
-  
-  CPPUNIT_ASSERT_EQUAL_MESSAGE(
     "The return value was reported incorrectly",
     (int)MockPseudoRandom::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)MockPseudoRandom::minor_status,
-    (int)( (*result)["return_values"]["minor_status"].integer() )
+    (int)( (*result)["minor_status"].integer() )
   );
   
   CPPUNIT_ASSERT_EQUAL_MESSAGE(
     "The random bytes were reported incorrectly",
     MockPseudoRandom::outputMessageBuffer.toString(),
-    std::string( (*result)["return_values"]["random_bytes"].string() )
+    std::string( (*result)["random_bytes"].string() )
   );