Welcome to mirror list, hosted at ThFree Co, Russian Federation.

SignerLocationUnitTest.java « test « asn1 « bouncycastle « org « java « test « src « core - gitlab.com/quite/humla-spongycastle.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 8d2c2c062334a2e6c65e226025b632a94e4a23d3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
package org.bouncycastle.asn1.test;

import java.io.IOException;

import org.bouncycastle.asn1.ASN1EncodableVector;
import org.bouncycastle.asn1.ASN1InputStream;
import org.bouncycastle.asn1.ASN1Sequence;
import org.bouncycastle.asn1.DERSequence;
import org.bouncycastle.asn1.DERTaggedObject;
import org.bouncycastle.asn1.DERUTF8String;
import org.bouncycastle.asn1.esf.SignerLocation;
import org.bouncycastle.util.test.SimpleTest;

public class SignerLocationUnitTest 
    extends SimpleTest
{
    public String getName()
    {
        return "SignerLocation";
    }

    public void performTest() 
        throws Exception
    {
        DERUTF8String countryName = new DERUTF8String("Australia");
        
        SignerLocation sl = new SignerLocation(countryName, null, null);

        checkConstruction(sl, countryName, null, null);

        DERUTF8String localityName = new DERUTF8String("Melbourne");
        
        sl = new SignerLocation(null, localityName, null);

        checkConstruction(sl, null, localityName, null);
        
        sl = new SignerLocation(countryName, localityName, null);

        checkConstruction(sl, countryName, localityName, null);
        
        ASN1EncodableVector v = new ASN1EncodableVector();
        
        v.add(new DERUTF8String("line 1"));
        v.add(new DERUTF8String("line 2"));
        
        ASN1Sequence postalAddress = new DERSequence(v);
        
        sl = new SignerLocation(null, null, postalAddress);
        
        checkConstruction(sl, null, null, postalAddress);
        
        sl = new SignerLocation(countryName, null, postalAddress);
        
        checkConstruction(sl, countryName, null, postalAddress);
        
        sl = new SignerLocation(countryName, localityName, postalAddress);
        
        checkConstruction(sl, countryName, localityName, postalAddress);
        
        sl = SignerLocation.getInstance(null);
        
        if (sl != null)
        {
            fail("null getInstance() failed.");
        }
        
        try
        {
            SignerLocation.getInstance(new Object());
            
            fail("getInstance() failed to detect bad object.");
        }
        catch (IllegalArgumentException e)
        {
            // expected
        }
      
        //
        // out of range postal address
        //
        v = new ASN1EncodableVector();
        
        v.add(new DERUTF8String("line 1"));
        v.add(new DERUTF8String("line 2"));
        v.add(new DERUTF8String("line 3"));
        v.add(new DERUTF8String("line 4"));
        v.add(new DERUTF8String("line 5"));
        v.add(new DERUTF8String("line 6"));
        v.add(new DERUTF8String("line 7"));
        
        postalAddress = new DERSequence(v);
        
        try
        {
            new SignerLocation(null, null, postalAddress);
            
            fail("constructor failed to detect bad postalAddress.");
        }
        catch (IllegalArgumentException e)
        {
            // expected
        }
        
        try
        {
            SignerLocation.getInstance(new DERSequence(new DERTaggedObject(2, postalAddress)));
            
            fail("sequence constructor failed to detect bad postalAddress.");
        }
        catch (IllegalArgumentException e)
        {
            // expected
        }
        
        try
        {
            SignerLocation.getInstance(new DERSequence(new DERTaggedObject(5, postalAddress)));
            
            fail("sequence constructor failed to detect bad tag.");
        }
        catch (IllegalArgumentException e)
        {
            // expected
        }
    }

    private void checkConstruction(
        SignerLocation sl,
        DERUTF8String  countryName,
        DERUTF8String  localityName,
        ASN1Sequence   postalAddress) 
        throws IOException
    {
        checkValues(sl, countryName, localityName, postalAddress);
        
        sl = SignerLocation.getInstance(sl);
        
        checkValues(sl, countryName, localityName, postalAddress);
        
        ASN1InputStream aIn = new ASN1InputStream(sl.toASN1Object().getEncoded());

        ASN1Sequence seq = (ASN1Sequence)aIn.readObject();
        
        sl = SignerLocation.getInstance(seq);
        
        checkValues(sl, countryName, localityName, postalAddress);
    }
    
    private void checkValues(
        SignerLocation sl,
        DERUTF8String  countryName,
        DERUTF8String  localityName,
        ASN1Sequence   postalAddress)
    {
        if (countryName != null)
        {
            if (!countryName.equals(sl.getCountryName()))
            {
                fail("countryNames don't match.");
            }
        }
        else if (sl.getCountryName() != null)
        {
            fail("countryName found when none expected.");
        }
        
        if (localityName != null)
        {
            if (!localityName.equals(sl.getLocalityName()))
            {
                fail("localityNames don't match.");
            }
        }
        else if (sl.getLocalityName() != null)
        {
            fail("localityName found when none expected.");
        }
        
        if (postalAddress != null)
        {
            if (!postalAddress.equals(sl.getPostalAddress()))
            {
                fail("postalAddresses don't match.");
            }
        }
        else if (sl.getPostalAddress() != null)
        {
            fail("postalAddress found when none expected.");
        }
    }
    
    public static void main(
        String[]    args)
    {
        runTest(new SignerLocationUnitTest());
    }
}