#include "./include/asn1.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int test1();
int test2();
int test3();
int test4();
int test5();
int test6();
int test7();
int test8();
int test9();
int test10();
int test11();
int test12();
int showtlv(const char *tag_seq,ASN_BUF *asnbuf);
int main(int argc,char *argv[])
{
	if(argc==2)
	{
		switch(atoi(argv[1]))
		{
			case 1:
				test1();
				break;
			case 2:
				test2();
				break;
			case 3:
				test3();
				break;
			case 4:
				test4();
				break;
			case 5:
				test5();
				break;
			case 6:
				test6();
				break;
			case 7:
				test7();
				break;
			case 8:
				test8();
				break;
			case 9:
				test9();
				break;
			case 10:
				test10();
				break;
			case 11:
				test11();
				break;
			case 12:
				test12();
				break;
			default:
				printf("Out of range!\n");
		}
	}
	else
		printf("Usage:asntest number\n");
	return 0;
}

int test1()		//Tag encoding/decoding test
{
	u_char	msg_buf[50];
	int 	len;
	ASN_BUF	asnbuf;
	
	asn_encode(msg_buf,&asnbuf);
	
	len=add_null("2",0,&asnbuf);
	showbuf(msg_buf,len);
	asn_decode(msg_buf,1,&asnbuf);
	if(get_null("2",&asnbuf)==1)
		printf("decoding correctly\n");
	
	asn_encode(msg_buf,&asnbuf);
	
	len=add_null("32",0,&asnbuf);
	showbuf(msg_buf,len);
	asn_decode(msg_buf,1,&asnbuf);
	if(get_null("32",&asnbuf)==1)
		printf("decoding correctly\n");
	
	asn_encode(msg_buf,&asnbuf);
	
	len=add_null("129",0,&asnbuf);
	showbuf(msg_buf,len);
	asn_decode(msg_buf,1,&asnbuf);
	if(get_null("129",&asnbuf)==1)
		printf("decoding correctly\n");
	
	asn_encode(msg_buf,&asnbuf);
	
	len=add_null("2",0x80,&asnbuf);
	showbuf(msg_buf,len);
	asn_decode(msg_buf,1,&asnbuf);
	if(get_null("2",&asnbuf)==1)
		printf("decoding correctly\n");
	
	asn_encode(msg_buf,&asnbuf);
	
	len=add_null("32",0xC0,&asnbuf);
	showbuf(msg_buf,len);
	asn_decode(msg_buf,1,&asnbuf);
	if(get_null("32",&asnbuf)==1)
		printf("decoding correctly\n");
	
	return 1;
}

int test2()
{
	u_char msg_buf[1024],pvalue[1024];
	ASN_BUF	asnbuf;
	
	memset(pvalue,0xFF,1024);
	
	asn_encode(msg_buf,&asnbuf);
	add_tlv("1",0,pvalue,0,&asnbuf);
	showbuf(msg_buf,5);
	asn_decode(msg_buf,1,&asnbuf);
	printf("len=%d\n",get_tlv("1",pvalue,&asnbuf));
	
	asn_encode(msg_buf,&asnbuf);
	add_tlv("1",2,pvalue,0,&asnbuf);
	showbuf(msg_buf,5);
	asn_decode(msg_buf,1,&asnbuf);
	printf("len=%d\n",get_tlv("1",pvalue,&asnbuf));
	
	asn_encode(msg_buf,&asnbuf);
	add_tlv("1",128,pvalue,0,&asnbuf);
	showbuf(msg_buf,5);
	asn_decode(msg_buf,1,&asnbuf);
	printf("len=%d\n",get_tlv("1",pvalue,&asnbuf));
	
	asn_encode(msg_buf,&asnbuf);
	add_tlv("1",129,pvalue,0,&asnbuf);
	showbuf(msg_buf,5);
	asn_decode(msg_buf,1,&asnbuf);
	printf("len=%d\n",get_tlv("1",pvalue,&asnbuf));
	
	asn_encode(msg_buf,&asnbuf);
	add_tlv("1",257,pvalue,0,&asnbuf);
	showbuf(msg_buf,5);
	asn_decode(msg_buf,1,&asnbuf);
	printf("len=%d\n",get_tlv("1",pvalue,&asnbuf));
	return 1;
}
int test3()
{
	u_char 	msg_buf[256];
	ASN_BUF	asnbuf;
	char	oid[20];
	int		len;
	
	//Boolean----->
	asn_encode(msg_buf,&asnbuf);
	len=add_bool("1",0,0,&asnbuf);
	showbuf(msg_buf,len);
	asn_decode(msg_buf,1,&asnbuf);
	printf("decoded value:%d\n",get_bool("1",&asnbuf));
	
	asn_encode(msg_buf,&asnbuf);
	len=add_bool("1",1,0,&asnbuf);
	showbuf(msg_buf,len);
	asn_decode(msg_buf,1,&asnbuf);
	printf("decoded value:%d\n",get_bool("1",&asnbuf));
	//Integer----->
	asn_encode(msg_buf,&asnbuf);
	len=add_int("1",2,0,&asnbuf);
	showbuf(msg_buf,len);
	asn_decode(msg_buf,1,&asnbuf);
	printf("decoded value:%d\n",get_int("1",&asnbuf));
	
	asn_encode(msg_buf,&asnbuf);
	len=add_int("1",257,0,&asnbuf);
	showbuf(msg_buf,len);
	asn_decode(msg_buf,1,&asnbuf);
	printf("decoded value:%d\n",get_int("1",&asnbuf));
	
	asn_encode(msg_buf,&asnbuf);
	len=add_int("1",-2,0,&asnbuf);
	showbuf(msg_buf,len);
	asn_decode(msg_buf,1,&asnbuf);
	printf("decoded value:%d\n",get_int("1",&asnbuf));
	
	asn_encode(msg_buf,&asnbuf);
	len=add_int("1",-259,0,&asnbuf);
	showbuf(msg_buf,len);
	asn_decode(msg_buf,1,&asnbuf);
	printf("decoded value:%d\n",get_int("1",&asnbuf));
	
	//Object Identifier----->
	asn_encode(msg_buf,&asnbuf);
	len=add_oid("1","2.100.6",0,&asnbuf);
	showbuf(msg_buf,len);
	asn_decode(msg_buf,1,&asnbuf);
	get_oid("1",oid,&asnbuf);
	printf("decoded value:%s\n",oid);
	
	asn_encode(msg_buf,&asnbuf);
	len=add_oid("1","0.39.6",0,&asnbuf);
	showbuf(msg_buf,len);
	asn_decode(msg_buf,1,&asnbuf);
	get_oid("1",oid,&asnbuf);
	printf("decoded value:%s\n",oid);
	return 1;
}
int test4()		//encoding test
{
	u_char 	msg_buf[256];
	int		c1=0,c2=1;
	u_char	d1[2]={0x22,0x22};
	u_char	d2[2]={0x33,0x33};
	u_char	b3[2]={0x44,0x44};
	u_char	a2[2]={0x55,0x55};
	u_char	a3[2]={0x66,0x66};
	int 	len;
	ASN_BUF	asnbuf;
	
	asn_encode_v3(msg_buf,128,&asnbuf);
	len=add_int("1.0.2-1",c1,0,&asnbuf);
	len=add_int("1.0.2-2",c2,0,&asnbuf);
	len=add_tlv("1.0.1-3.0",2,d1,0x80,&asnbuf);
	len=add_tlv("1.0.1-3.1",2,d2,0x80,&asnbuf);
	len=add_tlv("1.1",2,b3,0x80,&asnbuf);
	len=add_tlv("2",2,a2,0x80,&asnbuf);
	len=add_tlv("3",2,a3,0x80,&asnbuf);
	showbuf(msg_buf,len);
	printf("decode......\n");
	asn_decode(msg_buf,3,&asnbuf);
	showtlv("1.0.2-1",&asnbuf);
	showtlv("1.0.2-2",&asnbuf);
	showtlv("1.0.1-3.0",&asnbuf);
	showtlv("1.0.1-3.1",&asnbuf);
	showtlv("1.1",&asnbuf);
	showtlv("2",&asnbuf);
	showtlv("3",&asnbuf);
	return 1;
}

int test5()			//decoding test(long length)
{
	u_char 	msg_buf[256]={0xA5,0x80,
							0x82,0x80,
								0x83,0x02,0x00,0x00,
							0x00,0x00,
						0x00,0x00,
						0xA6,0x80,0x82,0x02,0x11,0x11,0x00,0x00};
	ASN_BUF	asnbuf;
	asn_decode(msg_buf,2,&asnbuf);
	showtlv("5.2.3",&asnbuf);
	showtlv("6.2",&asnbuf);
	showtlv("5.2",&asnbuf);
	showtlv("6",&asnbuf);
	return 1;
}

int test6()			//decoding test(indefinite length)
{
	u_char 	msg_buf[256]={0xA5,0x80,
							0xA2,0x81,0x82,
								0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x0A,
								0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x0A,
								0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x0A,
								0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x0A,
								0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x0A,
								0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x0A,
								0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x0A,
								0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x0A,
								0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x0A,
								0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x0A,
								0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x0A,
								0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x0A,
								0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x0A,
							0xA3,0x02,0x01,0x01,
						0x00,0x00,
						0xA6,0x80,0x82,0x02,0x11,0x11,0x00,0x00};
	ASN_BUF	asnbuf;
	int			temp;
	asn_decode_v3(msg_buf,256,&temp,&asnbuf);

	showtlv("5.2",&asnbuf);
	showtlv("5.3",&asnbuf);
	showtlv("5",&asnbuf);
	showtlv("6.2",&asnbuf);
	return 1;
}



int showtlv(const char *tag_seq,ASN_BUF *asnbuf)
{
	u_char 	tlvbuf[256];
	int		len;
	len=get_tlv(tag_seq,tlvbuf,asnbuf);
	showbuf(tlvbuf,len);
	return 1;
}

int test7()			//decoding test(long length)
{
	u_char 	msg_buf[256]={0x60,0x80,
					0x80,0x02,0x07,0x80,
					0xa1,0x80,
						0x06,0x07,0x04,0x00,0x00,0x01,0x00,0x01,0x02,
					0x00,0x00,
			      0x00,0x00};
	ASN_BUF	asnbuf;
	asn_decode(msg_buf,1,&asnbuf);
	showtlv("0",&asnbuf);
	showtlv("0.0",&asnbuf);
	showtlv("0.1",&asnbuf);
	showtlv("0.1.6",&asnbuf);
	return 1;
}

int test8()			//decoding test(long length)
{
	u_char 	msg_buf[256]={0x10,0x03,0xaa,0xbb,0xcc};
	ASN_BUF	asnbuf;
	asn_decode(msg_buf,1,&asnbuf);
	printf("tlvcount=%d\n",asnbuf.tlvcount);
	showtlv("16",&asnbuf);
	return 1;
}

int test9()
{
	u_char 	msg_buf[256]={0x02,0x01,0xaa,
							0x04,0x02,0xbb,0xbb,
							0x04,0x02,0xcc,0xcc,
							0x04,0x02,0xdd,0xdd,
							0x02,0x01,0xaa,
							0x05,0x02,0x55,0xbb,
							0x05,0x02,0x55,0xcc,
							0x05,0x02,0x55,0xdd};
//	u_char	mybuf[64];
//	int		len;
	int		temp;
	ASN_BUF	asnbuf;
	asn_decode_v3(msg_buf,30,&temp,&asnbuf);
	
	showtlv("2",&asnbuf);
	showtlv("4-2",&asnbuf);
	showtlv("4-3",&asnbuf);
	showtlv("4-4",&asnbuf);
	return 1;
}

int test10()
{
	u_char 	msg_buf[256]={0xA0,0x80,
							0x04,0x01,0x21,
							0x30,0x80,
								0x30,0x80,
									0x83,0x01,0x11,
									0x84,0x01,0x07,
									0x85,0x05,0x81,0x21,0x84,0x00,0x94,
									0x86,0x01,0x00,
								0x00,0x00,
							0x00,0x00,
						0x00,0x00};
	u_char	mybuf[64];
	int		len;
	ASN_BUF	asnbuf;
	if(asn_decode(msg_buf,1,&asnbuf)==-1)
		printf("decode error\n");

	len=get_tlv("0",mybuf,&asnbuf);
	printf("0:");
	showbuf(mybuf,len);

	len=get_tlv("0.4",mybuf,&asnbuf);
	printf("0.4:");
	showbuf(mybuf,len);

	len=get_tlv("0.16",mybuf,&asnbuf);
	printf("0.16:");
	showbuf(mybuf,len);

	len=get_tlv("0.16.16.5",mybuf,&asnbuf);
	printf("0.16.5:");
	showbuf(mybuf,len);
	return 1;
}

int test11()
{
	u_char msg_buf[256]={0x64,0x6A,0x49
	,0x04,0x01,0x62,0x00,0x60,0x6B,0x80,0x28,0x80,0x06,0x07,0x00,0x11
	,0x86,0x05,0x01,0x01,0x01,0xA0,0x80,0x61,0x80,0x80,0x02,0x07,0x80
	,0xA1,0x80,0x06,0x07,0x04,0x00,0x00,0x01,0x00,0x05,0x02,0x00,0x00
	,0xA2,0x80,0x02,0x01,0x00,0x00,0x00,0xA3,0x80,0xA1,0x80,0x02,0x01
	,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
	,0x6C,0x26,0xA2,0x80,0x02,0x01,0x00,0x30,0x80,0x02,0x01,0x16,0x30
	,0x80,0x04,0x08,0x64,0x00,0x92,0x00,0x00,0x00,0x00,0xF1,0x04,0x08
	,0x91,0x44,0x57,0x25,0x31,0x06,0x20,0xF0,0x00,0x00,0x00,0x00,0x00,0x00
	};
//	u_char	mybuf[64];
//	int		len;
	ASN_BUF	asnbuf;
	if(asn_decode(msg_buf,1,&asnbuf)==-1)
		printf("decode error\n");
	return 1;
}

int test12()
{
	u_char msg_buf[256]={0xA1,0x0E,
		0x01,0x01,0x01,
		0x02,0x80,0x02,0x01,0x01,0x00,0x00,
		0x02,0x02,0x01,0x02
	};
//	u_char	mybuf[64];
//	int		len;
	ASN_BUF	asnbuf;
	if(asn_decode(msg_buf,1,&asnbuf)==-1)
		printf("decode error\n");
	return 1;
}

