1/*
2 *******************************************************************************
3 * Copyright (C) 1996-2014, International Business Machines Corporation and
4 * others. All Rights Reserved.
5 *******************************************************************************
6 */
7
8package com.ibm.icu.dev.test.util;
9
10import java.io.IOException;
11import java.nio.ByteBuffer;
12
13import com.ibm.icu.dev.test.TestFmwk;
14import com.ibm.icu.impl.ICUBinary;
15
16/**
17* Testing class for Trie. Tests here will be simple, since both CharTrie and
18* IntTrie are very similar and are heavily used in other parts of ICU4J.
19* Codes using Tries are expected to have detailed tests.
20* @author Syn Wee Quek
21* @since release 2.1 Jan 01 2002
22*/
23public final class ICUBinaryTest extends TestFmwk
24{
25    // constructor ---------------------------------------------------
26
27    /**
28    * Constructor
29    */
30    public ICUBinaryTest()
31    {
32    }
33
34    // public methods -----------------------------------------------
35
36    public static void main(String arg[])
37    {
38        ICUBinaryTest test = new ICUBinaryTest();
39        try {
40            test.run(arg);
41        } catch (Exception e) {
42            test.errln("Error testing icubinarytest");
43        }
44    }
45
46    /**
47     * Testing the constructors of the Tries
48     */
49    public void TestReadHeader()
50    {
51        int formatid = 0x01020304;
52        byte array[] = {
53            // header size
54            0, 0x18,
55            // magic numbers
56            (byte)0xda, 0x27,
57            // size
58            0, 0x14,
59            // reserved word
60            0, 0,
61            // bigendian
62            1,
63            // charset
64            0,
65            // charsize
66            2,
67            // reserved byte
68            0,
69            // data format id
70            1, 2, 3, 4,
71            // dataVersion
72            1, 2, 3, 4,
73            // unicodeVersion
74            3, 2, 0, 0
75        };
76        ByteBuffer bytes = ByteBuffer.wrap(array);
77        ICUBinary.Authenticate authenticate
78                = new ICUBinary.Authenticate() {
79                    public boolean isDataVersionAcceptable(byte version[])
80                    {
81                        return version[0] == 1;
82                    }
83                };
84        // check full data version
85        try {
86            ICUBinary.readHeader(bytes, formatid, authenticate);
87        } catch (IOException e) {
88            errln("Failed: Lenient authenticate object should pass ICUBinary.readHeader");
89        }
90        // no restriction to the data version
91        try {
92            bytes.rewind();
93            ICUBinary.readHeader(bytes, formatid, null);
94        } catch (IOException e) {
95            errln("Failed: Null authenticate object should pass ICUBinary.readHeader");
96        }
97        // lenient data version
98        array[17] = 9;
99        try {
100            bytes.rewind();
101            ICUBinary.readHeader(bytes, formatid, authenticate);
102        } catch (IOException e) {
103            errln("Failed: Lenient authenticate object should pass ICUBinary.readHeader");
104        }
105        // changing the version to an incorrect one, expecting failure
106        array[16] = 2;
107        try {
108            bytes.rewind();
109            ICUBinary.readHeader(bytes, formatid, authenticate);
110            errln("Failed: Invalid version number should not pass authenticate object");
111        } catch (IOException e) {
112            logln("PASS: ICUBinary.readHeader with invalid version number failed as expected");
113        }
114    }
115}
116