1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.asn1.ber.digester.rules ;
18
19
20 import junit.framework.TestCase ;
21
22 import org.apache.asn1.ber.digester.BERDigester ;
23 import org.apache.asn1.ber.primitives.UniversalTag;
24 import org.apache.asn1.ber.Length;
25 import org.apache.asn1.ber.primitives.UniversalTag;
26 import org.apache.asn1.ber.digester.rules.PrimitiveOctetStringRule;
27 import org.apache.asn1.ber.digester.BERDigester;
28
29 import java.nio.ByteBuffer;
30
31
32 /***
33 * Tests the operation of the PrimitiveOctetStringRule.
34 *
35 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
36 * @version $Rev: 157644 $
37 */
38 public class PrimitiveOctetStringRuleTest extends TestCase
39 {
40 private BERDigester digester ;
41 private PrimitiveOctetStringRule rule ;
42
43
44 protected void setUp() throws Exception
45 {
46 super.setUp() ;
47
48 rule = new PrimitiveOctetStringRule() ;
49 digester = new BERDigester() ;
50 rule.setDigester( digester ) ;
51 }
52
53
54 protected void tearDown() throws Exception
55 {
56 super.tearDown() ;
57
58 rule = null ;
59 digester = null ;
60 }
61
62
63 /***
64 * Tests for correct behavior when the OCTET STRING is constructed.
65 */
66 public void testConstructedOctetString()
67 {
68 rule.tag( UniversalTag.OCTET_STRING.getTagId(), false, null ) ;
69 rule.length( 2 ) ;
70 byte[] bites = { 0x07, 0x1 } ;
71 ByteBuffer buf = ByteBuffer.wrap( bites ) ;
72 rule.value( buf ) ;
73 rule.finish() ;
74
75
76 assertEquals( 0, digester.getCount() ) ;
77 }
78
79
80 /***
81 * Tests for correct behavior when the OCTET STRING is primitive.
82 */
83 public void testPrimitiveOctetString()
84 {
85 rule.tag( UniversalTag.OCTET_STRING.getTagId(), true, null ) ;
86 rule.length( 2 ) ;
87 byte[] bites = { 0x07, 0x1 } ;
88 ByteBuffer buf = ByteBuffer.wrap( bites ) ;
89 rule.value( buf ) ;
90 rule.finish() ;
91
92
93 assertEquals( 1, digester.getCount() ) ;
94 ByteBuffer pushed = ( ByteBuffer ) digester.pop() ;
95 assertEquals( bites[0], pushed.get() ) ;
96 assertEquals( bites[1], pushed.get() ) ;
97 assertEquals( 0, digester.getCount() ) ;
98 }
99
100
101 /***
102 * Tests when the length is indefinite.
103 */
104 public void testIndefiniteLength()
105 {
106 rule.tag( UniversalTag.OCTET_STRING.getTagId(), true, null ) ;
107 rule.length( Length.INDEFINITE ) ;
108 rule.finish() ;
109 ByteBuffer buf = ( ByteBuffer ) digester.pop() ;
110 assertFalse( buf.hasRemaining() ) ;
111 }
112
113
114 /***
115 * Tests when the length is indefinite.
116 */
117 public void testNullValue()
118 {
119 rule.tag( UniversalTag.OCTET_STRING.getTagId(), true, null ) ;
120 rule.length( Length.INDEFINITE ) ;
121 rule.value( null ) ;
122 rule.value( ByteBuffer.allocate( 0 ) ) ;
123 rule.finish() ;
124 ByteBuffer buf = ( ByteBuffer ) digester.pop() ;
125 assertFalse( buf.hasRemaining() ) ;
126 }
127
128
129 /***
130 * Tests when the wrong tag is supplied.
131 */
132 public void testWrongTag()
133 {
134 try
135 {
136 rule.tag( UniversalTag.OBJECT_IDENTIFIER.getTagId(), true, null ) ;
137 fail( "should never get here due to an exception" ) ;
138 }
139 catch ( IllegalArgumentException e )
140 {
141 }
142 }
143 }