Tuesday, 10 April 2018

ASN.1: Data types

ASN.1 support three kinds of data types.
a.   Simple types like INTEGER, BOOLEAN
b.   Composite types like SEQUENCE, SET
c.   User-defined types

Below table summarizes the data types supported by ASN.1.
Data Type
Description
Example
Boolean is used to represent TRUE, FALSE states. It is similar to boolean type in programming languages.
isPassedInExam BOOLEAN
canIReadTheFile BOOLEAN
It is used to represent integer values. It do not have any upper and lower limits, it can be of any magnitude.
id  INTEGER
age INTEGER (15..60)

In the second example, age is of type integer and the valid values are from 15 to 60.
Used to represent bit arrays. Specific bits can be identified by parenthesized integers and assigned names. It is used to model the binary data of arbitrary length.

Values can be represented in 3 possible forms:
binary - '01'B,
hex - '9'H, and
named - {windowOpen, engineOn}
Ex
Occupation ::=  BIT STRING {
         clerk        (0),
    watchman     (1),
         engineer     (2),
    manager      (3)
}
                   
Hobbies ::= BIT STRING {
         chess     (0),
         cricket   (1),
         hockey    (2),
         football  (3),
         others    (4)
}
This is used to represent binary data that is multiple of 8 bits. Octet string can be of any length.

It can be represented in two forms.
a.   Binary – '1000100'B
b.   Hexa - '44'H
Ex
id OCTET STRING
Used to represent date. It should be of the form "YYYY-MM-DD".
dateOfBirth DATE
Used to represent time of the day. It is of the form "HH:MM:SS"
startTime TIME-OF-DAY,
endTime TIME-OF-DAY
It is used to represent date and time together. "YYYY-MM-DDTHH:MM:SS"
startTime "2018-02-06T06:30:23"
Represent decimal point numbers.

You can represent real numbers in 2 ways.
a.   Decimal format like 546789.239
b.   Sequence format like {mantissa 546789239, base 10, exponent -3}
salary REAL
ENUMERATED is used to model constants. If you have list of items, you can model them using ENUMERATED type.
FavoriteColors ::= ENUMERATED {black, green, red, yellow, white}
It is used to uniquely identify an entity like algorithm, certificate, (or) organization. It is a sequence of integer components, and components usually have non-negative value.
organizationCode OBJECT IDENTIFIER
Used to define collection of items.
Employee ::= SEQUENCE {
    name VisibleString,
    id VisibleString,
         salary REAL
}
Used to define collection of items of same type. It is similar to arrays in programming languages.
favoriteColors SEQUENCE OF VisibleString
Used to select one item from collection of items
Motorcar ::= SEQUENCE {
    carRegNumber VisibleString
}

Bicycle ::= SEQUENCE {
   bicycleColor VisibleString,
   bicycleModel VisibleString
}

Boat ::= SEQUENCE {
    boatRegNumber VisibleString
}

PreferredVehicle ::= CHOICE { car Motorcar, bike Bicycle, boat Boat }
It is an arbitrary string of IA5 (ASCII) characters. IA5 stands for International Alphabet 5.
email IA5String
Use this type, when you want to use ASCII characters but not control characters
name VisibleString
Use this when you want a string with only numeric characters.
salary NumericString
Use this to create string with uni code characters.
name UTF8String
NULL
Use this to represent a variable with no value. It indicates the absence of an element.

It is similar to SEQUENCE except the type of each identifier must be different from other identifier in the set and order of the elements doesn’t matter here.
Person ::= SET {
  name VisibleString,
  id INTEGER,
  salary REAL
}
It is similar to SEQUENCE OF type, only difference, order of the items may not be same at the receiver end. Suppose you sent the items like 1, 2, 3, but at the receiver end you may receive like 2, 1, 3.
Person ::= SEQUENCE {
    name VisibleString,
         favoriteColors SET OF VisibleString
}
By using ANY, you can supply any valid ASN.1 type while defining identifiers.



Every type in ASN.1 has given a universal tag number to identify them uniquely.

Universal Tag number
Type
0
reserved for BER
1
BOOLEAN
2
INTEGER
3
BIT STRING
4
OCTET STRING
5
NULL
6
OBJECT IDENTIFIER
7
ObjectDescriptor
8
INSTANCE OF, EXTERNAL
9
REAL
10
ENUMERATED
11
EMBEDDED PDV
12
UTF8String
13
RELATIVE-OID
14
Reserved for future use
15
Reserved for future use
16
SEQUENCE, SEQUENCE OF
17
SET, SET OF
18
NumericString
19
PrintableString
20
TeletexString, T61String
21
VideotexString
22
IA5String
23
UTCTime
24
GeneralizedTime
25
GraphicString
26
VisibleString, ISO646String
27
GeneralString
28
UniversalString
29
CHARACTER STRING
30
BMPString
31
Reserved for future use



Previous                                                 Next                                                 Home

Monday, 9 April 2018

ASN.1 Defining types and identifiers

As per convention, a type in ASN.1 is defined using capital letter. Whereas identifiers must begin with lowercase letter.

Example
Employee ::= SEQUENCE {
 firstName VisibleString,
 lastName VisibleString,
 address Address
}

Address ::= SEQUENCE {
 street VisibleString,
 city VisibleString,
 state VisibleString,
 country VisibleString,
 pin INTEGER
}

In the above example,
a.   Employee and Address are types.
b.   firstName, lastName, address, street, city, state, country and pin are identifiers.

You can create an Employee object like below.

employee1 Employee ::= {
 firstName "Hari Krishna",
 lastName "Gurram",
 address {
  street "chowdesrai temple street",
  city "Bangalore",
  state "Karnantaka",
  country "India",
  pin 523145
 }
}

Encoding of above data in XML scheme looks like below.

<?xml version="1.0" encoding="UTF-8"?>
<Employee>
  <firstName>Hari Krishna</firstName>
  <lastName>Gurram</lastName>
  <address>
    <street>chowdesrai temple street</street>
    <city>Bangalore</city>
    <state>Karnantaka</state>
    <country>India</country>
    <pin>523145</pin>
  </address>
</Employee>



Previous                                                 Next                                                 Home

Friday, 6 April 2018

Introduction to ASN.1

ASN.1 stands for 'Abstract Syntax Notation.One', It is an interface description language used to define structured information that can be serialized and deserialized in cross-platform way. By using ASN.1, you can describe the information and its structure at high level and encoding of such information. you no need to worry about, how it is represented while transmitting between one ASN.1 application to other ASN.1 application.

For example, below statements define a structure to define person objects
Person ::= SEQUENCE {
 name VisibleString,
 phone NumericString,
 email VisibleString
}


Below statements define a person instance by using above structure.
person1 Person ::= 
{  
    name "Hari Krishna", 
    phone "1234567890",
    email "krishna@abcdef.com"
}

Now you can encode above data using any one of ASN.1 supported encoding schemes like BER, DER, PER, UPER, XML, OER.


When you encode above data using XML encoding, it looks like below.
<?xml version="1.0" encoding="UTF-8"?>
<Person>
  <name>Hari Krishna</name>
  <phone>1234567890</phone>
  <email>krishna@abcdef.com</email>
</Person>

You can experiment with ASN.1 online at ‘http://asn1-playground.oss.com/’.

ASN.1 is mainly used in cryptography. ‘ASN.1’ standard used to define the format of x509 certificates.

Where else ASN.1 standard is used?
a.   Telecommunications
b.   Computer Networking
c.   LDAP
d.   SNMP (Simple Network Management Protocol)
e.   Kerberos etc.,

Why ASN.1?
It is internationally accepted standard for data communication, it is vendor, language and platform independent. At one end, you can implement data transfer using Java, and at other end you can consume the data transfer using other language like C++, C#, python etc.,


How the applications communicate using ASN.1?

As shown in the above figure, ASN.1 applications agreed on specific formatting rules. Once the rules are defined, one ASN.1 application can able to communicate with other ASN.1 application.




Previous                                                 Next                                                 Home

ASN.1 tutorial


      Introduction to ASN.1
      Defining types and identifiers
      Data types
            BOOLEAN type: Represent TRUE/FALSE states
            INTEGER : Represent integer values
            BIT STRING
            OCTET STRING
            DATE: Working with dates
            TIME-OF-DAY: Represent time of the day
            DATE-TIME: Represent date and time together
            REAL: Working with decimal numbers
            ENUMERATED: Model list of items
            OBJECT IDENTIFIER
            SEQUENCE: Define collection of items
            SEQUENCE OF : Define collection of items of same type
            CHOICE: Choose an item among multiple items
            IA5String: Represent International Alphabet 5
            VisibleString: Create a string variable
            NumericString: string with numbers
            UTF8String: Create a string of Unicode characters
            SET: Define composite type
            SET OF: Collection of items of same type
            ANY: Refer any valid ASN type
      Applying constraints on types
      Comments
      Specifying OPTIONAL fields
Previous                                                 Next                                                 Home

Thursday, 5 April 2018

Hamcrest: closeTo: Working with numbers

Suppose if the salary of the employee is 12345.6, you would like to match all the employees with some error + (or) – 1.2. then you can use these matchers.

Hamcrest provides closeTo matcher to match double and bigDecimals with given error.

For example, below statements match bigdecimals with delta + (or) - 0.05
assertThat(new BigDecimal("1.05"), closeTo(new BigDecimal("1.0"), new BigDecimal("0.05")));
assertThat(new BigDecimal("1.05"), closeTo(new BigDecimal("1.10"), new BigDecimal("0.05")));

Below statements match decimals with delta + (or) – 0.06
assertThat(1.05, closeTo(1.0, 0.06));
assertThat(1.05, closeTo(1.10, 0.06));

TestApp.java
package com.sample.tests;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.closeTo;

import java.math.BigDecimal;

import org.junit.Test;

public class TestApp {

 @Test
 public void testmethod() {
  assertThat(new BigDecimal("1.05"), closeTo(new BigDecimal("1.0"), new BigDecimal("0.05")));
  assertThat(new BigDecimal("1.05"), closeTo(new BigDecimal("1.10"), new BigDecimal("0.05")));

  assertThat(1.05, closeTo(1.0, 0.06));
  assertThat(1.05, closeTo(1.10, 0.06));
 }
}




Previous                                                 Next                                                 Home

Hamcrest: any: Match an object that is instance of class

By using ‘any’ method, you can create a matcher that is used to match any object that is of given type.

Ex
Employee emp1 = new Employee(1, "Krishna", 12345.6);
assertThat(emp1, any(Employee.class));

Employee.java
package com.sample.model;

public class Employee {
 private int id;
 private String name;
 private Double experience;

 public Employee(int id, String name, Double experience) {
  this.id = id;
  this.name = name;
  this.experience = experience;
 }

 public int getId() {
  return id;
 }

 public void setId(int id) {
  this.id = id;
 }

 public String getName() {
  return name;
 }

 public void setName(String name) {
  this.name = name;
 }

 public double getExperience() {
  return experience;
 }

 public void setExperience(double experience) {
  this.experience = experience;
 }

 @Override
 public int hashCode() {
  final int prime = 31;
  int result = 1;
  result = prime * result + id;
  return result;
 }

 @Override
 public boolean equals(Object obj) {
  if (this == obj)
   return true;
  if (obj == null)
   return false;
  if (getClass() != obj.getClass())
   return false;
  Employee other = (Employee) obj;
  if (id != other.id)
   return false;
  return true;
 }

}

TestApp.java
package com.sample.tests;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.any;

import org.junit.Test;

import com.sample.model.Employee;

public class TestApp {

 @Test
 public void testmethod() {
  Employee emp1 = new Employee(1, "Krishna", 12345.6);
  assertThat(emp1, any(Employee.class));
 }
}

Previous                                                 Next                                                 Home

Hamcrest: samePropertyValuesAs: Check two beans properties

By using 'samePropertyValuesAs' method, you can create a matcher that matches when the examined object has values for all of its JavaBean properties that are equal to the corresponding values of the specified bean.

Ex
Employee emp1 = new Employee(1, "Krishna", 12345.6);
Employee emp2 = new Employee(1, "Krishna", 12345.6);
assertThat("emp1 and emp2 must have same property values", emp1, samePropertyValuesAs(emp2));

Find the below working application.

TestApp.java
package com.sample.tests;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.samePropertyValuesAs;

import org.junit.Test;

import com.sample.model.Employee;

public class TestApp {

 @Test
 public void testmethod() {
  Employee emp1 = new Employee(1, "Krishna", 12345.6);
  Employee emp2 = new Employee(1, "Krishna", 12345.6);
  assertThat("emp1 and emp2 must have same property values", emp1, samePropertyValuesAs(emp2));
 }
}




Previous                                                 Next                                                 Home