/*****************************************************************/
/**                                                             **/
/**             (C) Copyright 1992 the Regents of the           **/
/**        University of California.  All Rights Reserved.      **/
/**                                                             **/
/**        This work was produced under the sponsorship of      **/
/**         the U.S. Department of Energy.  The Government      **/
/**                retains certain rights therein.              **/
/**                                                             **/
/**                         Written by                          **/
/**         Distributed Programming Environments Project        **/
/**           Computer Software and Networking Division,        **/
/**                   Livermore Computer Center,                **/
/**            Lawrence Livermore National Laboratory           **/
/**                                                             **/
/*****************************************************************/

Interface sample;

    Real(20,5) 			Real20_5type;
    BitString(17) 		BitString17type;
    ObjectIdentifier(5,2) 	ObjectIdentifier5_2type;
    Boolean(1) 			Boolean1type;
    Integer(4) 			Integer4type;
    OctetString(10)		OctetString10type;
    Enumerated(4) 		Enumerated4type;
    Null	 		Nulltype;
    IA5String(30) 		IA5String30type;

    Union 
    {
       Real(20,5) 			real20_5u1;
       BitString(17) 			bs17u2;
       ObjectIdentifier(5,2) 		oid5_2u3;
       Boolean(1) 			bool1u4;
       Integer(4) 			int4u5;
       OctetString(10)			os10u6;
       Enumerated(4) 			en4u7;
       Null				nulu8;
       IA5String(30) 			ia5_30u9;
    } untype;

   Invoke ike 52 Integer(2);

End;


main(argc,argv)

   int argc;
   char *argv[];
{
   untype un_s, un_r;
   Data (sample) inst;

   int i;
   int l1,l2;

   Real20_5type real20_5_s = 4.56;
   static BitString17type bs17_s = {17, {0xa8, 0xa8, 0x80}};
   static ObjectIdentifier5_2type oid5_2_s = {3,{2,5,6}};
   Boolean1type bool1_s = 1;
   Integer4type int4_s = 456;
   static OctetString10type os10_s = {10, {0x5a,0x5a,
					   0x5a,0x5a,
					   0x5a,0x5a,
					   0x5a,0x5a,
					   0x5a,0x5a}};
   Enumerated4type en4_s = 678;
   static IA5String30type ia5_30_s = {5,"hello"};

   if (argc != 2)
      exit(1);   

/***************** send it out ************************/

   Bind_STUB(inst,argv[1],"w",Asn_1_ber);
   Encode inst
   {
      for (i=0 ; i<9; i++) {
	 un_s.tag = i;
	 switch (i) {
	 case 0:
	    un_s.un.real20_5u1 = 4.56;
	    break;
	 case 1:
            un_s.un.bs17u2.len=17;
	    un_s.un.bs17u2.val[0] = 0xa8;
	    un_s.un.bs17u2.val[1] = 0xa8;
	    un_s.un.bs17u2.val[2] = 0x80;
	    break;
	 case 2:
	    un_s.un.oid5_2u3.size = 3;
	    un_s.un.oid5_2u3.comp[0] = 2;
	    un_s.un.oid5_2u3.comp[1] = 5;
	    un_s.un.oid5_2u3.comp[2] = 6;
	    break;
	 case 3:
	    un_s.un.bool1u4 = 1;
	    break;
	 case 4:
	    un_s.un.int4u5 = 456;
	    break;
	 case 5:
	    memset(un_s.un.os10u6.val,0x5A,10);
            un_s.un.os10u6.len=10;
	    break;
	 case 6:
	    un_s.un.en4u7= 678;
	    break;
	 case 7:
	    /* don't set un_s.un.nulu8 and won't be sent */
	    break;
	 case 8:
            un_s.un.ia5_30u9.len = 5;
	    memcpy(un_s.un.ia5_30u9.val,"hello",5);
	    break;
	 }
	 un_s => untype;
      }
   }
   Unbind(inst);

/***************** receive it back ********************/

   Bind_STUB(inst,argv[1],"r",Asn_1_ber);
   for (i=0; i<9; i++) {
      if (i == 7) continue;
      Decode inst
	 {
	    On untype => (un_r);
	 }

/***************** report results *********************/

      switch (un_r.tag) {
	 case 0:
	    if (un_r.un.real20_5u1 != real20_5_s) {
		printf("   un_r.un.real20_5u1 value was bad\n");
		exit(1);
	    }
	    break;
	 case 1:
	     if (!__ROr_cmpP_BitString(&un_r.un.bs17u2,
				       &bs17_s,
				       (l1=17,&l1) ,0)) {
		printf("   un_r.bs17u2 value was bad\n");
		exit(1);
	    }
	    break;
	 case 2:
	    if (!__ROr_cmpP_ObjectIdentifier(
				     &(un_r.un.oid5_2u3),
				     &(oid5_2_s),
				     (l1=5,&l1) ,(l2=2,&l2))) {
	       printf("   un_r.oid5_2u3 value was bad\n");
	       exit(1);
	    }
	    break;
	 case 3:
	    if (!__ROr_cmpP_Boolean(&un_r.un.bool1u4,
				    &(bool1_s),
				    (l1=1,&l1) ,0)) {
	       printf("   un_r.bool1u4 value was bad\n");
	       exit(1);
	    }
	    break;
	 case 4:
	    if (un_r.un.int4u5 != int4_s) {
	       printf("   un_r.un.int4u5 had bad value\n");
	       exit(1);
	    }
	    break;
	 case 5:
	    if (!__ROr_CMPP_OctetString(un_r.un.os10u6,
					os10_s,(l1=10,&l1) ,0)) {
	       printf("   un.os10u6 value was bad\n");
	       exit(1);
	    }
	    break;
	 case 6:
	    if (un_r.un.en4u7 != en4_s) {
	       printf("   un_r.un.en4u7 had bad value\n");
	       exit(1);
	    }
	    break;
	 case 7:
	    /* null was sent correctly */
	    break;
	 case 8:
	    if (!__ROr_CMPP_IA5String(un_r.un.ia5_30u9,ia5_30_s,(l1=5,&l1),0)) {
	       printf("   un_r.un.ia5_30u9 had bad value\n");
	       exit(1);
	    }
	    break;
      }
   }
   Unbind(inst);

   printf("test completed\n");
   return(0);
}
