File: getInstance.java

package info (click to toggle)
mauve 20161030-1
  • links: PTS, VCS
  • area: main
  • in suites: buster, stretch
  • size: 44,628 kB
  • ctags: 35,425
  • sloc: java: 336,555; sh: 2,834; xml: 208; makefile: 72
file content (330 lines) | stat: -rw-r--r-- 9,578 bytes parent folder | download | duplicates (5)
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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
/* getInstance.java -- Ensure names with extra spaces are recognized
   Copyright (C) 2006 Free Software Foundation, Inc.
This file is part of Mauve.

Mauve is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2, or (at your option)
any later version.

Mauve is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
General Public License for more details.

You should have received a copy of the GNU General Public License
along with Mauve; see the file COPYING.  If not, write to the
Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301 USA.

*/

// Tags: JDK1.4
// Uses: ../MessageDigest/MauveDigest

package gnu.testlet.java.security.Engine;

import gnu.java.security.Engine;
import gnu.testlet.TestHarness;
import gnu.testlet.Testlet;

import java.security.NoSuchAlgorithmException;
import java.security.Provider;
import java.security.Security;

public class getInstance extends Provider implements Testlet
{
  
  private Provider provider;
    
  public getInstance()
  {
    super("FakeProvider", 1.0, "A Fake Provider Used Within the Mauve Test Suite");

    put("MessageDigest.foo",
        "gnu.testlet.java.security.MessageDigest.MauveDigest");
    put("Alg.Alias.MessageDigest.bar", "foo");
  }

  
  // Test case for the behaviour of
  // Engine.getInstance (service, algorithm, provider).
  // White space should be ignored.
  // The algorithm names should be case insensitive.
  public void test (TestHarness harness){
    setUp (harness);
    testWhiteSpace(harness);
    testAlgorithmCase (harness);
    testNameRedundancy(harness);
  }

  private void setUp (TestHarness harness){
    provider = this;
    Security.addProvider(provider);
  }
  
  // Tests the behaviour of 
  // Engine.getInstance (service, algorithm, provider).
  // The algorithms and service names should ignore any white space.
  private void testWhiteSpace (TestHarness harness)
  {
    harness.checkPoint ("Engine");
    String signature;

    signature = "getInstance(\"MessageDigest\", \"foo\", provider)";
    try
      {
        harness.check(
            Engine.getInstance("MessageDigest", "foo", provider) != null,
            signature);
      }
    catch (Exception x)
      {
        harness.fail(signature);
        harness.debug(x);
      }

    signature = "getInstance(\"  MessageDigest  \", \"foo\", provider)";
    try
      {
        harness.check(
            Engine.getInstance("  MessageDigest  ", "foo", provider) != null,
            signature);
      }
    catch (Exception x)
      {
        harness.fail(signature);
        harness.debug(x);
      }

    signature = "getInstance(\"MessageDigest\", \"  foo  \", provider)";
    try
      {
        harness.check(
            Engine.getInstance("MessageDigest", "  foo  ", provider) != null,
            signature);
      }
    catch (Exception x)
      {
        harness.fail(signature);
        harness.debug(x);
      }

    signature = "getInstance(\"  MessageDigest  \", \"  foo  \", provider)";
    try
      {
        harness.check(
            Engine.getInstance("  MessageDigest  ", "  foo  ", provider) != null,
            signature);
      }
    catch (Exception x)
      {
        harness.fail(signature);
        harness.debug(x);
      }
  }

  // Tests the behaviour of 
  // Engine.getInstance (service, algorithm, provider).
  // The algorithm names should be case insensitive.
  private void testAlgorithmCase(TestHarness harness)
  {
    try
      {

        // test to make sure the engine can be found using all lowercase
        // characters.

        try
          {
            Engine.getInstance("MessageDigest", "foo", provider);
          }
        catch (NoSuchAlgorithmException e)
          {
            harness.fail("Could not find engine when using all lowercase characters");
            harness.debug(e);
          }

        // test to make sure the engine can be found using all uppercase
        // characters
        try
          {
            Engine.getInstance("MessageDigest", "FOO", provider);
          }
        catch (NoSuchAlgorithmException e)
          {
            harness.fail("Could not find engine when using all uppercase characters");
            harness.debug(e);
          }

        // test to make sure the engine can be found using a random case for the
        // characters
        try
          {
            Engine.getInstance("MessageDigest", "FoO", provider);
          }
        catch (NoSuchAlgorithmException e)
          {
            harness.fail("Could not find engine when using random case characters");
            harness.debug(e);
          }

        // test to make sure the engine can be found using the exact same case
        // specified in the Provider
        try
          {
            Engine.getInstance("MessageDigest", "foo", provider);
          }
        catch (NoSuchAlgorithmException e)
          {
            harness.fail("Could not find engine using exact case characters");
            harness.debug(e);
          }

        // test to make sure the engine can be found usinga all lowercase
        // characters using the alias
        try
          {
            Engine.getInstance("MessageDigest", "bar", provider);
          }
        catch (NoSuchAlgorithmException e)
          {
            harness.fail("Could not find engine using alias and all lowercase characters");
            harness.debug(e);
          }

        // test to make sure the engine can be found using all uppercase
        // characters using the alias
        try
          {
            Engine.getInstance("MessageDigest", "BAR", provider);
          }
        catch (NoSuchAlgorithmException e)
          {
            harness.fail("Could not find engine using alias and all uppercase characters");
            harness.debug(e);
          }

        // test to make sure the engine can be found using a random case for the
        // characters using the alias
        try
          {
            Engine.getInstance("MessageDigest", "bAr", provider);
          }
        catch (NoSuchAlgorithmException e)
          {
            harness.fail("Could not find engine using alias and random case characters");
            harness.debug(e);
          }

        // test to make sure the engine can be found using the exact same case
        // specified in the Provider using the alias
        try
          {
            Engine.getInstance("MessageDigest", "bar", provider);
          }
        catch (NoSuchAlgorithmException e)
          {
            harness.fail("Could not find engine using alias and exact case characters");
            harness.debug(e);
          }
      }

    catch (Exception e)
      {
        harness.debug(e);
        harness.fail(String.valueOf(e));
      }

  }

  /**
   * Tests that the Provider class is immune against adding/removing the same
   * algorithm with different case names.
   * 
   * @param harness the test harness.
   */
  private void testNameRedundancy(TestHarness harness)
  {
    harness.checkPoint("Engine.testNameRedundancy()");
    try
      {
        mustFindName(harness, "foo");
        mustFindName(harness, "FOO");

        // add a new spelling of 'foo'
        provider.put("MessageDigest.Foo",
                     "gnu.testlet.java.security.MessageDigest.MauveDigest");
        harness.verbose("*** Added 'Foo'");

        mustFindName(harness, "Foo");

        // now remove 'foo'.  all 'foo' spellings should not be found
        provider.remove("MessageDigest.foo");
        harness.verbose("*** Removed 'foo'");

        mustNotFindName(harness, "foo");
        mustNotFindName(harness, "FOO");
        mustNotFindName(harness, "Foo");

        // put 'foo' back
        put("MessageDigest.foo",
            "gnu.testlet.java.security.MessageDigest.MauveDigest");
        harness.verbose("*** Re-added 'foo'");
        // add a new spelling of 'bar'
        put("Alg.Alias.MessageDigest.Bar", "Foo");
        harness.verbose("*** Added alias 'Bar'");

        mustFindName(harness, "bar");
        mustFindName(harness, "BAR");
        mustFindName(harness, "Bar");

        // now remove 'bar'.  all 'bar' spellings should not be found
        provider.remove("Alg.Alias.MessageDigest.bar");
        harness.verbose("*** Removed alias 'bar'");

        mustNotFindName(harness, "bar");
        mustNotFindName(harness, "BAR");
        mustNotFindName(harness, "Bar");
      }
    catch (Exception x)
      {
        harness.debug(x);
        harness.fail("Engine.testNameRedundancy(): " + x);
      }
  }

  private void mustFindName(TestHarness harness, String name)
  {
    String msg = "MUST find " + name;
    try
      {
        Object obj = Engine.getInstance("MessageDigest", name, provider);
        harness.check(obj != null, msg);
      }
    catch (Exception x)
      {
        harness.fail(msg);
        harness.debug(x);
      }
  }

  private void mustNotFindName(TestHarness harness, String name)
  {
    String msg = "MUST NOT find " + name;
    try
      {
        Object obj = Engine.getInstance("MessageDigest", name, provider);
        harness.check(obj == null, msg);
      }
    catch (NoSuchAlgorithmException x)
      {
        harness.check(true, msg);
      }
    catch (Exception x)
      {
        harness.fail(msg);
        harness.debug(x);
      }
  }
}