/***********************************************************************
 * 
 *  Copyright (C) 2005-2006 Novell, Inc. All Rights Reserved.
 *
 *  This library is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU Lesser General Public
 *  License as published by the Free Software Foundation; version 2.1
 *  of the License.
 *
 *  This library is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 *  Library Lesser General Public License for more details.
 *
 *  You should have received a copy of the GNU Lesser General Public
 *  License along with this library; if not, Novell, Inc.
 * 
 *  To contact Novell about this file by physical or electronic mail, 
 *  you may find current contact information at www.novell.com.
 * 
 ***********************************************************************/

//===[ Include files ]=====================================================

#include <stdio.h>
#include <string.h>
#include <stdbool.h>
#include <getopt.h>
#include <errno.h>

#include <auth_token.h>

//===[ Type definitions ]==================================================

//
// DbgTrace macro define
//
#define DbgTrace(LEVEL, X, Y) {                          \
   if (LEVEL == 0)                                       \
      printf(X, Y);                                      \
   else if (DebugLevel >= LEVEL)                         \
         printf(X, Y);                                   \
}


//===[ Function prototypes ]===============================================

//===[ Global variables ]==================================================

// Usage string
char  usage[] = "\ntest: usage: [-p ConnectPort] [-D DebugLevel]\n";

// Debug Level
int   DebugLevel = 3;


//++=======================================================================
void
ExecuteTests(void)
//
//  Arguments: 
//
//  Returns:   
//
//  Abstract:  
//
//  Notes:
//
//  Environment:
//
//=======================================================================--
{
   CasaStatus        status;
   char              userName[100];
   int               userNameBufLen = sizeof(userName);
   char              token[1000];
   int               tokenBufLen = sizeof(token);

   DbgTrace(1, "ExecuteTests- Start\n", 0);

   status = GetAuthTokenCredentials("krb-test-service",
                                    userName,
                                    &userNameBufLen,
                                    token,
                                    &tokenBufLen);
   if (CASA_SUCCESS(status)
       && CasaStatusCode(status) != CASA_STATUS_OBJECT_NOT_FOUND)
   {
      // We obtained authentication token credentials to authenticate
      // to the service, now verify them.
      printf("userName = %s\n", userName);
      printf("userNameBufLen = %d\n", userNameBufLen);
      printf("tokenBufLen = %d\n", tokenBufLen);
      status = ValidateAuthTokenCredentials("krb-test-service",
                                            userName,
                                            strlen(userName),
                                            token,
                                            strlen(token));
      if (CASA_SUCCESS(status))
      {
         DbgTrace(1, "ExecuteTests- ValidateAuthTokenCredentials success\n", 0);
      }
      else
      {
         DbgTrace(0, "ExecuteTests- ValidateAuthTokenCredentials failure, status = %08X\n", status);
      }
   }
   else
   {
      DbgTrace(0, "ExecuteTests- GetAuthTokenCredentials failure, status = %08X\n", status);
   }

   DbgTrace(1, "ExecuteTests- End\n", 0);
}


//++=======================================================================
int
main(
   int argc,
   char* argv[])
//
//  Arguments: 
//
//  Returns:   
//
//  Abstract:  
//
//  Notes:
//
// L2
//=======================================================================--
{
   int         optionsSpecified = 0;
   bool        doneScanning = false;
   bool        invalidOption = false;
   int         option;

   printf("**** auth-token-test ****\n");

   // Scan through the options specified
   while (!doneScanning)
   {
      opterr = 0;
      option = getopt(argc, argv, "D");

      // Proceed based on the result
      switch (option)
      {
         case 'D':
            // Set the debug level
            DebugLevel = atoi(optarg);
            optionsSpecified++;
            break;

         case '?':
            // Invalid option detected
            doneScanning = true;
            invalidOption = true;
            break;

         default:
            // Done scanning
            doneScanning = true;
            break;
      }
   }

   // Do some sanity checking
   if (!invalidOption)
   {
      int   i;

      for (i = 0; i < 1; i++)
         ExecuteTests();
   }
   else
   {
      // Invalid option detected or the user failed to
      // specify the listening port number.
      printf(usage, argv[0]);
   }

   return 0;

}  /*-- main() --*/