CASA/c_test/c_sharp_sample/cSharpSample.cs
2006-02-01 17:48:29 +00:00

263 lines
6.6 KiB
C#

/***********************************************************************
*
* 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.
*
***********************************************************************/
using System;
using Novell.CASA;
namespace cSharpTest
{
/// <summary>
/// Summary description for Class1.
/// </summary>
class Class1
{
private static string APPID = "cSharpAppID";
//private static string SHARED_ID = "cSharp_SharedID";
private static string USERNAME = "Admin.nov€ll";
private static string PASSWORD = "PasswordFor€Admin";
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main(string[] args)
{
Console.WriteLine("**************************************************");
Console.WriteLine("********** miCASA Sample written in C# ***********");
Console.WriteLine("**************************************************");
ShowMenu();
}
static private void ShowMenu()
{
while (true)
{
Console.WriteLine();
Console.WriteLine("********** Menu ***********");
Console.WriteLine("* 1. Add secret *");
Console.WriteLine("* 2. Display secret *");
Console.WriteLine("* 3. Remove secret *");
Console.WriteLine("* 4. Run automated test *");
Console.WriteLine("* 5. Quit *");
Console.WriteLine("***************************");
Console.WriteLine("Select option and Press enter");
String line = Console.ReadLine();
if (line.Length > 0)
{
char[] c = line.Substring(0, 1).ToCharArray();
if (c.Length > 0)
{
if (c[0].Equals('5'))
break;
if (c[0].Equals('1'))
AddSecret();
else if (c[0].Equals('2'))
DisplaySecret();
else if (c[0].Equals('3'))
RemoveSecret();
else if (c[0].Equals('4'))
RunTest();
}
}
}
}
static private void AddSecret()
{
while (true)
{
Console.Write("Enter SecretID: ");
String sID = Console.ReadLine();
Console.Write("Enter Username: ");
String sUsername = Console.ReadLine();
Console.Write("Enter Password: ");
String sPassword = Console.ReadLine();
if (sID != null && sUsername != null && sPassword != null)
{
try
{
//miCASA.SetBasicCredential(sID, null, sUsername, sPassword);
miCASA.SetCredential(0, sID, null, miCASA.USERNAME_TYPE_CN_F, sUsername, sPassword);
}
catch (miCasaException e)
{
Console.WriteLine(e.getMessage());
}
return;
}
else
{
Console.WriteLine("Invalid input, try again");
}
}
}
static private void RemoveSecret()
{
Console.Write("Enter SecretID to remove: ");
String sID = Console.ReadLine();
if (sID != null)
{
try
{
miCASA.RemoveBasicCredential(sID, null);
}
catch (miCasaException e)
{
Console.WriteLine(e.getMessage());
}
}
}
static private void DisplaySecret()
{
Console.Write("Enter SecretID to display: ");
String sID = Console.ReadLine();
if (sID != null)
{
try
{
BasicCredential bc;
//bc = miCASA.GetBasicCredential(sID, null);
bc = miCASA.GetCredential(0, sID, null, miCASA.USERNAME_TYPE_CN_F);
if (bc != null)
{
Console.WriteLine("Data for "+sID);
Console.WriteLine("------------------------");
Console.WriteLine("Username:" + bc.GetUsername());
Console.WriteLine("Password:" + bc.GetPassword());
Console.WriteLine("------------------------");
Console.WriteLine("Press the Enter key to continue");
Console.ReadLine();
}
else
Console.WriteLine(sID + " not found");
}
catch (miCasaException e)
{
Console.WriteLine(e.getMessage());
}
}
}
static private void RunTest()
{
int count = 1;
Console.Write("Enter times to run: ");
String sCount = Console.ReadLine();
try
{
int icount = int.Parse(sCount);
count = icount;
}
catch (Exception)
{
}
for (int i=0; i<count; i++)
{
Console.Write("Setting Credential .....");
try
{
miCASA.SetBasicCredential(APPID, null, USERNAME, PASSWORD);
Console.WriteLine("Succeeded");
}
catch (Exception e)
{
Console.WriteLine("Failed");
Console.WriteLine(e.ToString());
Console.WriteLine(e.StackTrace.ToString());
return;
}
Console.WriteLine("");
// did we get it the credential back
Console.WriteLine("Getting Credential .....");
try
{
BasicCredential bc = miCASA.GetBasicCredential(APPID, null);
if (bc != null)
{
if (bc.GetUsername().Equals(USERNAME))
Console.WriteLine(" Username matched : "+bc.GetUsername());
if (bc.GetPassword().Equals(PASSWORD))
Console.WriteLine(" Password matched : "+bc.GetPassword());
}
else
{
Console.WriteLine("Failed");
}
}
catch (Exception e)
{
Console.WriteLine(e.ToString());
}
try
{
Console.WriteLine("\r\nRemoving Credential");
miCASA.RemoveBasicCredential(APPID, null);
}
catch (Exception e)
{
Console.WriteLine(e.ToString());
}
try
{
BasicCredential bc = miCASA.GetBasicCredential(APPID, null);
if (bc != null)
{
Console.WriteLine("\r\nCredential exists and should not - FAILED!");
}
else
Console.WriteLine("\r\nCredential not found as expected - SUCCESS!");
}
catch (Exception)
{
Console.WriteLine("\r\n Credential not found as expected - SUCCESS!");
}
Console.WriteLine("Test completed ");
}
}
}
}