/* ***************************************************************** *
 * Copyright 1998 International Business Machines Corporation. All   *
 * Rights Reserved.                                                  *
 *                                                                   *
 * Please read this carefully.  Your use of this reference           *
 * implementation of certain of the IETF public-key infrastructure   *
 * specifications ("Software") indicates your acceptance of the      *
 * following.  If you do not agree to the following, do not install  *
 * or use any of the Software.                                       *
 *                                                                   *
 * Permission to use, reproduce, distribute and create derivative    *
 * works from the Software ("Software Derivative Works"), and to     *
 * distribute such Software Derivative Works is hereby granted to    *
 * you by International Business Machines Corporation ("IBM").  This *
 * permission includes a license under the patents of IBM that are   *
 * necessarily infringed by your use of the Software as provided by  *
 * IBM.                                                              *
 *                                                                   *
 * IBM licenses the Software to you on an "AS IS" basis, without     *
 * warranty of any kind.  IBM HEREBY EXPRESSLY DISCLAIMS ALL         *
 * WARRANTIES OR CONDITIONS, EITHER EXPRESS OR IMPLIED, INCLUDING,   *
 * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OR CONDITIONS OF       *
 * MERCHANTABILITY, NON INFRINGEMENT AND FITNESS FOR A PARTICULAR    *
 * PURPOSE.  You are solely responsible for determining the          *
 * appropriateness of using this Software and assume all risks       *
 * associated with the use of this Software, including but not       *
 * limited to the risks of program errors, damage to or loss of      *
 * data, programs or equipment, and unavailability or interruption   *
 * of operations.                                                    *
 *                                                                   *
 * IBM WILL NOT BE LIABLE FOR ANY DIRECT DAMAGES OR FOR ANY SPECIAL, *
 * INCIDENTAL, OR  INDIRECT DAMAGES OR FOR ANY ECONOMIC              *
 * CONSEQUENTIAL DAMAGES (INCLUDING LOST PROFITS OR SAVINGS), EVEN   *
 * IF IBM HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.  IBM  *
 * will not be liable for the loss of, or damage to, your records or *
 * data, or any damages claimed by you based on a third party claim. *
 *                                                                   *
 * IBM wishes to obtain your feedback to assist in improving the     *
 * Software.  You grant IBM a world-wide, royalty-free right to use, *
 * copy, distribute, sublicense and prepare derivative works based   *
 * upon any feedback, including materials, error corrections,        *
 * Software Derivatives, enhancements, suggestions and the like that *
 * you provide to IBM relating to the Software (this does not        *
 * include products for which you charge a royalty and distribute to *
 * IBM under other terms and conditions).                            *
 *                                                                   *
 * You agree to distribute the Software and any Software Derivatives *
 * under a license agreement that: 1) is sufficient to notify all    *
 * licensees of the Software and Software Derivatives that IBM       *
 * assumes no liability for any claim that may arise regarding the   *
 * Software or Software Derivatives, and 2) that disclaims all       *
 * warranties, both express and implied, from IBM regarding the      *
 * Software and Software Derivatives.  (If you include this          *
 * Agreement with any distribution of the Software or Software       *
 * Derivatives you will have met this requirement.)  You agree that  *
 * you will not delete any copyright notices in the Software.        *
 *                                                                   *
 * This Agreement is the exclusive statement of your rights in the   *
 * Software as provided by IBM.   Except for the rights granted to   *
 * you in the second paragraph above, You are not granted any other  *
 * patent rights, including but not limited to the right to make     *
 * combinations of the Software with products that infringe IBM      *
 * patents. You agree to comply with all applicable laws and         *
 * regulations, including all export and import laws and regulation. *
 * This Agreement is governed by the laws of the State of New York.  *
 * This Agreement supersedes all other communications,               *
 * understandings or agreements we may have had prior to this        *
 * Agreement.                                                        *
 * ***************************************************************** */

#include "asnbase.h"
#include "asnnames.h"
#include "Init.h"
#include <stdio.h>
#include <stdlib.h>


int test(void) {
  int result;
  asn_oid cn_oid;
  buffer_t buf;
  asn_x500name name;
  asn_x500name name2;
  r_buffer_t r_buf;

  asn_integer aint;
  asn_integer bint;
  long l;

  unsigned long cn_val[4] = {2,5,4,3};

  Init();

  if ((result = cn_oid.set_value(cn_val, 4)) != 0) {
    fprintf(stderr, "OID::set_value returned %d\n", result);
    return EXIT_FAILURE;
  };

  if ((result = cn_oid.display_name(buf)) != 0) {
    fprintf(stderr, "OID::display_name returned %d\n", result);
    if (result != ASN_CANT_CONVERT) return EXIT_FAILURE;
    fprintf(stderr, "ASN_CANT_CONVERT: check inifile \\Jonah.INI contains a OID line for 2.5.4.3\n");
  } else {
    fprintf(stdout, "2.5.4.3::display_name returned %.*s\n", buf.data_len, buf.data);
  };
 
  if ((result = cn_oid.set_value("OU")) != 0) {
    fprintf(stderr, "OID::set_value(OU) returned %d\n", result);
    if (result != ASN_CANT_CONVERT) return EXIT_FAILURE;
    fprintf(stderr, "ASN_CANT_CONVERT: check inifile \\Jonah.INI contains a OID line for 2.5.4.3\n");
  } else {
    buf.clear();
    if ((result = cn_oid.display(buf)) != 0) {
      fprintf(stderr, "OID::display returned %d\n", result);
      return EXIT_FAILURE;
    };
    
    fprintf(stdout, "Imported OU as %.*s\n", buf.data_len, buf.data);

    buf.clear();
    if ((result = cn_oid.display_name(buf)) != 0) {
      fprintf(stderr, "OID::display_name returned %d\n", result);
      if (result != ASN_CANT_CONVERT) return EXIT_FAILURE;
      fprintf(stderr, "ASN_CANT_CONVERT: check inifile \\Jonah.INI contains a OID line for 2.5.4.3\n");
    } else {
      fprintf(stdout, "OU::display_name returned %.*s\n", buf.data_len, buf.data);
    };

  };

  fprintf(stdout, "Testing names.");


  r_buf.data = (unsigned char *)"/C=us/O=Dis Organization/OU=Dat Organizational Unit/CN=Foo";
  r_buf.data_len = strlen((char *)r_buf.data);
  if ((result = name.set_value_UTF8(r_buf)) != 0) {
    fprintf(stderr, "name::set_value_utf8 returned %d\n", result);
    return EXIT_FAILURE;
  };

  fprintf(stdout, "Set value with name: \"%.*s\"\n", r_buf.data_len, r_buf.data);
  fflush(stdout);
  buf.clear();
  result = name.get_value_IA5(buf);
  if (result != 0) {
    fprintf(stderr, "Name.get_value_IA5 returned %d\n", result);
    return EXIT_FAILURE;
  };

  fprintf(stdout, "Get value returned:  \"%.*s\"\n", buf.data_len, buf.data);

  buf.clear();
  result = name.write(buf);
  if (result != 0) {
    fprintf(stderr, "name::write returned %d\n", result);
    return EXIT_FAILURE;
  };


  result = name2.read(buf);
  if (result != 0) {
    fprintf(stderr, "name::read returned %d\n", result);
    return EXIT_FAILURE;
  };

  buf.clear();

  result = name2.get_value_IA5(buf);
  if (result != 0) {
    fprintf(stderr, "Name.get_value_IA5 returned %d\n", result);
    return EXIT_FAILURE;
  };

  fprintf(stdout, "Get value returned:  \"%.*s\"\n", buf.data_len, buf.data);



  if ((result = aint.set_value(42)) != 0) {
    fprintf(stderr, "int::set_value(42) returned %d\n", result);
    return EXIT_FAILURE;
  };
  if ((result = aint.get_value(l)) != 0) {
    fprintf(stderr, "int::write() returned %d\n", result);
    return EXIT_FAILURE;
  };
  if (l != 42) {
    fprintf(stderr, "Error re-reading value from int.  Expected 42, got %ld\n", l);
    return EXIT_FAILURE;
  };
  buf.clear();
  if ((result = aint.write(buf)) != 0) {
    fprintf(stderr, "int::write() returned %d\n", result);
    return EXIT_FAILURE;
  };
  if ((result = bint.read(buf)) != 0) {
    fprintf(stderr, "int::read() returned %d\n", result);
    return EXIT_FAILURE;
  };
  if ((result = bint.get_value(l)) != 0) {
    fprintf(stderr, "int::write() returned %d\n", result);
    return EXIT_FAILURE;
  };
  if (l != 42) {
    fprintf(stderr, "Error reading value from int.  Expected 42, got %ld\n", l);
    return EXIT_FAILURE;
  };

  if ((result = aint.set_value(-1)) != 0) {
    fprintf(stderr, "int::set_value(-1) returned %d\n", result);
    return EXIT_FAILURE;
  };
  if ((result = aint.get_value(l)) != 0) {
    fprintf(stderr, "int::write() returned %d\n", result);
    return EXIT_FAILURE;
  };
  if (l != -1) {
    fprintf(stderr, "Error re-reading value from int.  Expected -1, got %ld\n", l);
    return EXIT_FAILURE;
  };
  buf.clear();
  if ((result = aint.write(buf)) != 0) {
    fprintf(stderr, "int::write() returned %d\n", result);
    return EXIT_FAILURE;
  };
  if ((result = bint.read(buf)) != 0) {
    fprintf(stderr, "int::read() returned %d\n", result);
    return EXIT_FAILURE;
  };
  if ((result = bint.get_value(l)) != 0) {
    fprintf(stderr, "int::write() returned %d\n", result);
    return EXIT_FAILURE;
  };
  if (l != -1) {
    fprintf(stderr, "Error reading value from int.  Expected -1, got %ld\n", l);
    return EXIT_FAILURE;
  };

  if ((result = aint.set_value(0)) != 0) {
    fprintf(stderr, "int::set_value(0) returned %d\n", result);
    return EXIT_FAILURE;
  };
  if ((result = aint.get_value(l)) != 0) {
    fprintf(stderr, "int::write() returned %d\n", result);
    return EXIT_FAILURE;
  };
  if (l != 0) {
    fprintf(stderr, "Error re-reading value from int.  Expected 0, got %ld\n", l);
    return EXIT_FAILURE;
  };
  buf.clear();
  if ((result = aint.write(buf)) != 0) {
    fprintf(stderr, "int::write() returned %d\n", result);
    return EXIT_FAILURE;
  };
  if ((result = bint.read(buf)) != 0) {
    fprintf(stderr, "int::read() returned %d\n", result);
    return EXIT_FAILURE;
  };
  if ((result = bint.get_value(l)) != 0) {
    fprintf(stderr, "int::write() returned %d\n", result);
    return EXIT_FAILURE;
  };
  if (l != 0) {
    fprintf(stderr, "Error reading value from int.  Expected 0, got %ld\n", l);
    return EXIT_FAILURE;
  };
  
  
  return EXIT_SUCCESS;

}


int main(int argc, char * argv[]) {
  return test();
}
  
