📄 tasn.cpp
字号:
// tasn.cpp
//
#include <iostream.h>
#include <stdio.h>
#include "tasn.h"
//
// CTestAsn实现
void CTestAsn::WaitForNextTest( LPCSTR pszHint )
{
cout << pszHint << endl << endl;
getchar();
}
void CTestAsn::Test()
{
TestAsnBitString();
TestAsnBoolean();
TestAsnEnumerated();
TestAsnExternal();
TestAsnGeneralizedTime();
TestAsnGeneralString();
TestAsnGraphicString();
TestAsnIa5String();
TestAsnInteger();
TestAsnNull();
TestAsnNumericString();
TestAsnObjectDescriptor();
TestAsnObjectIdentifier();
TestAsnOctetString();
TestPrintableString();
TestAsnReal();
TestTeletexString();
TestUtcTime();
TestAsnVideotexString();
TestVisibleString();
TestAsnChoice();
TestAsnSequence();
TestAsnSequenceOf();
TestAsnSet();
TestAsnSetOf();
}
void CTestAsn::TestAsnBitString()
{
cout << "Test CAsnBitString *************************************" << endl;
BYTE bits[] = { 0x21, 0x43, 0x65, 0x87, 0xA9, 0xCB, 0xED, 0x2F };
INT len = 15*4;
CHAR str[15+1];
CHAR strtemp[3];
INT i;
cout << "bits is : " << endl;
for ( i = 0; i < len/8; i ++ )
{
sprintf( strtemp, "%02X ", bits[i] );
memcpy( &str[i*2], strtemp, 2 );
}
str[i*2] = '\0';
cout << str << endl;
CAsnBitString cBitString1;
cBitString1.Set( bits, len );
cout << "Bit String value : " << endl;
for ( i = 0; i < cBitString1.GetSize(); i ++ )
{
if ( ( i % 8 ) == 0 )
cout << ' ';
cout << (INT)cBitString1.GetAt( i );
}
cout << endl;
len = cBitString1.GetSize();
cout << "bits size is : " << len << endl;
cBitString1.Get( bits, len );
cout << "bits is : " << endl;
for ( i = 0; i < len/8; i ++ )
{
sprintf( strtemp, "%02X ", bits[i] );
memcpy( &str[i*2], strtemp, 2 );
}
str[i*2] = '\0';
cout << str << endl;
CAsnBitString cBitString2( cBitString1 );
ASSERT( cBitString1 == cBitString2 );
cout << "CAsnBitString Encode : " << endl;
CAsnBuffer b;
if ( cBitString2.Serialize( b, TRUE ) )
{
cout << "Encode successful, value is ... " << endl;
b.Dump( g_OmcDump );
cout << endl;
}
else
cout << "Encode fail! " << endl;
CAsnBitString cBitString3;
cout << "CAsnBitString Decode : " << endl;
if ( cBitString3.Serialize( b, FALSE ) )
{
cout << "Decode successful, value is ... " << endl;
for ( INT i = 0; i < cBitString1.GetSize(); i ++ )
{
if ( ( i % 8 ) == 0 )
cout << ' ';
cout << (INT)cBitString3[i];
}
cout << endl;
cBitString3.Dump( g_OmcDump );
ASSERT( cBitString2 == cBitString3 );
}
else
cout << "Encode fail! " << endl;
cout << endl;
WaitForNextTest( "CAsnBitString has been tested ! " );
}
void CTestAsn::TestAsnBoolean()
{
cout << "Test CAsnBoolean *************************************" << endl;
CAsnBoolean cAsnBoolean1;
cAsnBoolean1.Set( FALSE );
cout << "value1 is : " << (BOOL)cAsnBoolean1 << endl;
cAsnBoolean1 = TRUE;
cout << "value1 is : " << cAsnBoolean1.Get() << endl;
CAsnBoolean cAsnBoolean2( cAsnBoolean1 );
cout << "value2 is : " << (BOOL)cAsnBoolean2 << endl;
cout << "value1 == value2 : " << ( cAsnBoolean1 == cAsnBoolean2 ) << endl;
CAsnBoolean cAsnBoolean3;
cAsnBoolean3 = !(BOOL)cAsnBoolean2;
cout << "value3 is : " << (BOOL)cAsnBoolean3 << endl;
cout << "value2 != value3 : "<< ( cAsnBoolean2 != cAsnBoolean3 ) << endl;
CAsnBuffer b;
cout << " value2 Encode : " << endl;
if ( ! cAsnBoolean2.Serialize( b, TRUE ) )
cout << "Encode fail! " << endl;
else
{
cout << "Encode successful, value is ... " << endl;
b.Dump( g_OmcDump );
cout << endl;
}
cout << " value2 Decode : " << endl;
if ( ! cAsnBoolean1.Serialize( b, FALSE ) )
cout << "Decode fail! " << endl;
else
{
cout << "Decode successful, value is : " << (BOOL)cAsnBoolean1 << endl;
cAsnBoolean1.Dump( g_OmcDump );
}
cout << " value3 Encode : " << endl;
if ( ! cAsnBoolean3.Serialize( b, TRUE ) )
cout << "Encode fail! " << endl;
else
{
cout << "Encode successful, value is ... " << endl;
b.Dump( g_OmcDump );
cout << endl;
}
cout << " value3 Decode : " << endl;
if ( ! cAsnBoolean1.Serialize( b, FALSE ) )
cout << "Decode fail! " << endl;
else
{
cout << "Decode successful, value is : " << (BOOL)cAsnBoolean1 << endl;
cAsnBoolean1.Dump( g_OmcDump );
}
WaitForNextTest( "CAsnBoolean has been tested ! " );
}
void CTestAsn::TestAsnEnumerated()
{
cout << "Test CAsnEnumerated *************************************" << endl;
CAsnEnumerated cAsnEnum1( 4, 2, 3, 9, 13 );
cout << "value1 rang is : 2, 3, 9, 13 " << endl;
cAsnEnum1 = 2;
cout << "value1 is : " << (ASNINTEGERTYPE)cAsnEnum1 << endl;
CAsnEnumerated cAsnEnum2 = cAsnEnum1;
ASNINTEGERTYPE tAsnEnum;
cAsnEnum2.Get( tAsnEnum );
cout << "value2 is : " << tAsnEnum << endl;
cAsnEnum2.Set( 9 );
cout << "value2 is : " << cAsnEnum2.Get() << endl;
cout << "value1 == value2 : " << ( cAsnEnum1 == cAsnEnum2 ) << endl;
CAsnBuffer b;
cout << "value1 Encode : " << endl;
if ( cAsnEnum1.Serialize( b, TRUE ) )
{
b.Dump( g_OmcDump );
}
cout << "value2 Decode : " << endl;
if ( cAsnEnum2.Serialize( b, FALSE ) )
{
cout << "vlaue2 is : " << (ASNINTEGERTYPE)cAsnEnum2 << endl;
cAsnEnum2.Dump( g_OmcDump );
}
WaitForNextTest( "CAsnEnumerated has been tested ! " );
}
void CTestAsn::TestAsnExternal()
{
cout << "Test CAsnExternal *************************************" << endl;
WaitForNextTest( "CAsnExternal has been tested ! " );
}
void CTestAsn::TestAsnGeneralizedTime()
{
cout << "Test CAsnGeneralizedTime*************************************" << endl;
CAsnGeneralizedTime cTime1;
cTime1.Set( 2000, 4, 8, 16, 41, 03, 0 );
cout << "time1 is : " << cTime1.GetYear() << cTime1.GetMonth() << cTime1.GetDay()
<< cTime1.GetHour() << cTime1.GetMinute() << cTime1.GetSecond()
<< '.' << cTime1.GetDeciSecond() << endl;
CAsnGeneralizedTime cTime2 = cTime1;
cout << "time1 is : " << cTime2.GetYear() << cTime2.GetMonth() << cTime2.GetDay()
<< cTime2.GetHour() << cTime2.GetMinute() << cTime2.GetSecond()
<< '.' << cTime2.GetDeciSecond() << endl;
ASNCHARTYPE time[20] = "19990903212249.1";
cout << "time string is : " << time << endl;
cTime1.Set( time );
cout << "time1 is : " << cTime1.GetYear() << cTime1.GetMonth() << cTime1.GetDay()
<< cTime1.GetHour() << cTime1.GetMinute() << cTime1.GetSecond()
<< '.' << cTime1.GetDeciSecond() << endl;
cTime1.Set( time, strlen( (LPSTR)time ) );
cout << "time1 is : " << cTime1.GetYear() << cTime1.GetMonth() << cTime1.GetDay()
<< cTime1.GetHour() << cTime1.GetMinute() << cTime1.GetSecond()
<< '.' << cTime1.GetDeciSecond() << endl;
//INT nSize = cTime2.GetSize();
//cTime2.Get( time, nSize );
//cout << "time2 string length is : " << nSize << endl;
//cout << "time2 string format is : " << time << endl;
CAsnGeneralizedTime cTime3;
cout << "time3 = time " << endl;
cTime3 = time;
cout << "time3 == time : " << ( cTime3 == time ) << endl;
cout << "time3 == time1 : " << ( cTime3 == cTime1 ) << endl;
cTime1.Set( 2037, 12, 28, 16, 41, 03, 3, 's' );
INT nYear, nMonth, nDay, nHour, nMinute, nSecond, nDeciSecond, nDifference;
ASNCHARTYPE cTimeZone;
cTime1.Get ( nYear, nMonth, nDay, nHour, nMinute, nSecond, nDeciSecond, cTimeZone );
cout << "time1 is : " << nYear << nMonth << nDay
<< nHour << nMinute << nSecond << '.' << nDeciSecond << cTimeZone << endl;
cTime2.Set( 2000, 8, 1, 1, 1, 1, 1, (INT)-400 );
cTime2.Get ( nYear, nMonth, nDay, nHour, nMinute, nSecond, nDeciSecond, nDifference );
cout << "time2 is : " << nYear << nMonth << nDay
<< nHour << nMinute << nSecond << '.' << nDeciSecond << nDifference << endl;
cTime1.SetYear( 2010 );
CAsnGeneralizedTime cTime4 = cTime1;
cout << "time4 is : " << cTime4.GetYear() << cTime4.GetMonth() << cTime4.GetDay()
<< cTime4.GetHour() << cTime4.GetMinute() << cTime4.GetSecond()
<< '.' << cTime4.GetDeciSecond() << endl;
CAsnBuffer b;
cout << "time1 Encode : " << endl;
if ( cTime1.Serialize( b, TRUE ) )
{
b.Dump( g_OmcDump );
}
cout << "time2 Decode : " << endl;
if ( cTime2.Serialize( b, FALSE ) )
{
switch ( cTime2.GetTimeType() )
{
case ASN_TIME_LOCAL :
cTime2.Get ( nYear, nMonth, nDay, nHour, nMinute, nSecond, nDeciSecond );
cout << "time2 is : " << nYear << nMonth << nDay << nHour << nMinute
<< nSecond << '.' << nDeciSecond << endl;
break;
case ASN_TIME_UTC :
cTime2.Get ( nYear, nMonth, nDay, nHour, nMinute, nSecond, nDeciSecond, cTimeZone );
cout << "time2 is : " << nYear << nMonth << nDay << nHour << nMinute
<< nSecond << '.' << nDeciSecond << cTimeZone << endl;
break;
case ASN_TIME_DIFFERENCE :
cTime2.Get ( nYear, nMonth, nDay, nHour, nMinute, nSecond, nDeciSecond, nDifference );
cout << "time2 is : " << nYear << nMonth << nDay << nHour << nMinute
<< nSecond << '.' << nDeciSecond << nDifference << endl;
break;
}
cout << "time2 == time4 : " << ( cTime2 == cTime4 ) << endl;
cTime2.Dump( g_OmcDump );
}
WaitForNextTest( "CAsnGeneralizedTime has been tested ! " );
}
void CTestAsn::TestAsnGeneralString()
{
cout << "Test CAsnGeneralString *************************************" << endl;
ASNCHARTYPE str[50] = "asdfasdfasdfasdfasdfasdfasdfasdf";
cout << "str is : " << str << endl;
ASNCHARTYPE strtemp[50];
CAsnGeneralString cString;
cString.Set( str );
//cString.Get( strtemp, cString.GetSize() );
//strtemp[cString.GetSize()] = '\0';
cout << "string1 is : " << (LPASNCHARTYPE)cString << endl;
cout << "string1 == str : " << ( cString == str ) << endl;
memcpy( str, "lkjlkjlkjlkjlkjlkjljklkjlkj", 50 );
cString = str;
cString.Get( strtemp, cString.GetSize() );
strtemp[cString.GetSize()] = '\0';
cout << "string1 is : " << strtemp << endl;
cString.Add( 'A' );
cout << "string1 Add 'A', now value is : " << (LPASNCHARTYPE)cString << endl;
cString.SetAt( 1, 'B' );
cout << "string1 SetAt( 1, 'B') , now value is : " << (LPASNCHARTYPE)cString << endl;
cout << "string1 GetSize() is : " << cString.GetSize() << endl;
cString.SetAtGrow( cString.GetSize(), 'C' );
cout << "string1 SetAtGrow( cString.GetSize(), 'C' ), now value is : " << (LPASNCHARTYPE)cString << endl;
cout << "string1 GetAt( cString.GetSize()-1 ) is : " << cString.GetAt( cString.GetSize()-1 ) << endl;
CAsnGeneralString cString2( "asdfs" );
cString2.Get( str, cString2.GetSize() );
str[cString2.GetSize()] = '\0';
cout << "string2 is : " << str << endl;
CAsnBuffer b;
if ( cString.Serialize( b, TRUE ) )
{
cout << "string1 Encode : " << endl;
b.Dump( g_OmcDump );
}
if ( cString2.Serialize( b, FALSE ) )
{
cout << "string2 Decode : " << (LPASNCHARTYPE)cString2 << endl;
cString2.Dump( g_OmcDump );
}
WaitForNextTest( "CAsnGeneralString has been tested ! " );
}
void CTestAsn::TestAsnGraphicString()
{
cout << "Test CAsnGraphicString *************************************" << endl;
CAsnGraphicString cString("asdfasdfasdf");
cout << "string is : " << (LPASNCHARTYPE)cString << endl;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -