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

Oid.cs « tests « System.Security.Cryptography.Encoding « src - github.com/mono/corefx.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: baedd579a05449ea6d7ff624d1582b36b8d42425 (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
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
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using System.Collections.Generic;
using System.Runtime.InteropServices;
using Xunit;

namespace System.Security.Cryptography.Encoding.Tests
{
    public static class OidTests
    {
        [Fact]
        public static void EmptyOid()
        {
            Oid oid = new Oid("");
            Assert.Equal("", oid.Value);
            Assert.Null(oid.FriendlyName);

            oid = new Oid();
            Assert.Null(oid.Value);
            Assert.Null(oid.FriendlyName);
        }

        [Theory]
        [MemberData(nameof(ValidOidFriendlyNamePairs))]
        public static void LookupOidByValue_Ctor(string oidValue, string friendlyName)
        {
            Oid oid = new Oid(oidValue);

            Assert.Equal(oidValue, oid.Value);
            Assert.Equal(friendlyName, oid.FriendlyName);
        }

        [Theory]
        [MemberData(nameof(ValidOidFriendlyNamePairs))]
        public static void LookupOidByFriendlyName_Ctor(string oidValue, string friendlyName)
        {
            Oid oid = new Oid(friendlyName);

            Assert.Equal(oidValue, oid.Value);
            Assert.Equal(friendlyName, oid.FriendlyName);
        }

        [Fact]
        public static void LookupNullOid()
        {
            Assert.Throws<ArgumentNullException>(() => new Oid((string)null));
        }

        [Fact]
        public static void LookupUnknownOid()
        {
            Oid oid = new Oid(Bogus_Name);

            Assert.Equal(Bogus_Name, oid.Value);
            Assert.Null(oid.FriendlyName);
        }

        [Fact]
        public static void Oid_StringString_BothNull()
        {
            // No validation at all.
            Oid oid = new Oid(null, null);

            Assert.Null(oid.Value);
            Assert.Null(oid.FriendlyName);
        }

        [Theory]
        [MemberData(nameof(ValidOidFriendlyNamePairs))]
        public static void Oid_StringString_NullFriendlyName(string oidValue, string expectedFriendlyName)
        {
            // Can omit friendly-name - FriendlyName property demand-computes it.
            Oid oid = new Oid(oidValue, null);

            Assert.Equal(oidValue, oid.Value);
            Assert.Equal(expectedFriendlyName, oid.FriendlyName);
        }

        [Theory]
        [InlineData(SHA1_Name)]
        [InlineData(SHA256_Name)]
        [InlineData(Bogus_Name)]
        public static void Oid_StringString_NullValue(string friendlyName)
        {
            // Can omit oid, Value property does no on-demand conversion.
            Oid oid = new Oid(null, friendlyName);

            Assert.Null(oid.Value);
            Assert.Equal(friendlyName, oid.FriendlyName);
        }

        [Theory]
        [InlineData(SHA1_Oid, SHA256_Name)]
        [InlineData(SHA256_Oid, SHA1_Name)]
        [InlineData(SHA256_Name, SHA1_Name)]
        [InlineData(SHA256_Name, Bogus_Name)]
        [InlineData(Bogus_Name, SHA256_Oid)]
        public static void Oid_StringString_BothSpecified(string oidValue, string friendlyName)
        {
            // The values are taken as true, not verified at all.
            // The data for this test series should be mismatched OID-FriendlyName pairs, and
            // sometimes the OID isn't a legal OID.
            Oid oid = new Oid(oidValue, friendlyName);

            Assert.Equal(oidValue, oid.Value);
            Assert.Equal(friendlyName, oid.FriendlyName);
        }

        [Fact]
        public static void TestValueProperty()
        {
            Oid oid = new Oid(null, null);

            // Value property is just a field exposed as a property - no extra policy at all.

            oid.Value = "BOGUS";
            Assert.Equal("BOGUS", oid.Value);

            oid.Value = null;
            Assert.Equal(null, oid.Value);
        }

        [Fact]
        public static void TestFriendlyNameProperty()
        {
            Oid oid;

            oid = new Oid(null, null);

            // Friendly name property can initialize itself from the Value (but only
            // if it was originally null.)

            oid.Value = SHA1_Oid;
            Assert.Equal(SHA1_Name, oid.FriendlyName);

            oid.Value = SHA256_Oid;
            Assert.Equal(SHA1_Name, oid.FriendlyName);

            oid.Value = null;
            Assert.Equal(SHA1_Name, oid.FriendlyName);

            oid.Value = Bogus_Name;
            Assert.Equal(SHA1_Name, oid.FriendlyName);

            // Setting the FriendlyName can also updates the value if there a valid OID for the new name.
            oid.FriendlyName = Bogus_Name;
            Assert.Equal(Bogus_Name, oid.FriendlyName);
            Assert.Equal(Bogus_Name, oid.Value);

            oid.FriendlyName = SHA1_Name;
            Assert.Equal(SHA1_Name, oid.FriendlyName);
            Assert.Equal(SHA1_Oid, oid.Value);

            oid.FriendlyName = SHA256_Name;
            Assert.Equal(SHA256_Name, oid.FriendlyName);
            Assert.Equal(SHA256_Oid, oid.Value);
        }
        
        [Theory]
        [MemberData(nameof(ValidOidFriendlyNameHashAlgorithmPairs))]
        public static void LookupOidByValue_Method_HashAlgorithm(string oidValue, string friendlyName)
        {
            Oid oid = Oid.FromOidValue(oidValue, OidGroup.HashAlgorithm);

            Assert.Equal(oidValue, oid.Value);
            Assert.Equal(friendlyName, oid.FriendlyName);
        }

        [Theory]
        [MemberData(nameof(ValidOidFriendlyNameEncryptionAlgorithmPairs))]
        public static void LookupOidByValue_Method_EncryptionAlgorithm(string oidValue, string friendlyName)
        {
            Oid oid = Oid.FromOidValue(oidValue, OidGroup.EncryptionAlgorithm);

            Assert.Equal(oidValue, oid.Value);
            Assert.Equal(friendlyName, oid.FriendlyName);
        }

        [Theory]
        [MemberData(nameof(ValidOidFriendlyNameHashAlgorithmPairs))]
        [PlatformSpecific(TestPlatforms.Windows)]  // Uses P/Invokes to get the  Oid lookup table
        public static void LookupOidByValue_Method_WrongGroup(string oidValue, string friendlyName)
        {
            // Oid group is implemented strictly - no fallback to OidGroup.All as with many other parts of Crypto.
            Assert.Throws<CryptographicException>(() => Oid.FromOidValue(oidValue, OidGroup.EncryptionAlgorithm));
        }

        [Fact]
        public static void LookupOidByValue_Method_NullInput()
        {
            Assert.Throws<ArgumentNullException>(() => Oid.FromOidValue(null, OidGroup.HashAlgorithm));
        }

        [Theory]
        [InlineData(SHA1_Name)] // Friendly names are not coerced into OID values from the method.
        [InlineData(Bogus_Name)]
        public static void LookupOidByValue_Method_BadInput(string badInput)
        {
            Assert.Throws<CryptographicException>(() => Oid.FromOidValue(badInput, OidGroup.HashAlgorithm));
        }

        [Theory]
        [MemberData(nameof(ValidOidFriendlyNameHashAlgorithmPairs))]
        public static void LookupOidByFriendlyName_Method_HashAlgorithm(string oidValue, string friendlyName)
        {
            Oid oid = Oid.FromFriendlyName(friendlyName, OidGroup.HashAlgorithm);

            Assert.Equal(oidValue, oid.Value);
            Assert.Equal(friendlyName, oid.FriendlyName);
        }

        [Theory]
        [MemberData(nameof(ValidOidFriendlyNameEncryptionAlgorithmPairs))]
        public static void LookupOidByFriendlyName_Method_EncryptionAlgorithm(string oidValue, string friendlyName)
        {
            Oid oid = Oid.FromFriendlyName(friendlyName, OidGroup.EncryptionAlgorithm);

            Assert.Equal(oidValue, oid.Value);
            Assert.Equal(friendlyName, oid.FriendlyName);
        }

        [Theory]
        [MemberData(nameof(ValidOidFriendlyNamePairs))]
        public static void LookupOidByFriendlyName_Method_InverseCase(string oidValue, string friendlyName)
        {
            // Note that oid lookup is case-insensitive, and we store the name in the form it was
            // input to the constructor (rather than "normalizing" it to the official casing.)
            string inverseCasedName = InvertCase(friendlyName);
            Oid oid = Oid.FromFriendlyName(inverseCasedName, OidGroup.All);

            Assert.Equal(oidValue, oid.Value);
            Assert.Equal(inverseCasedName, oid.FriendlyName);
        }

        [Theory]
        [MemberData(nameof(ValidOidFriendlyNameHashAlgorithmPairs))]
        [PlatformSpecific(TestPlatforms.Windows)]  // Uses P/Invokes to get the  Oid lookup table
        public static void LookupOidByFriendlyName_Method_WrongGroup(string oidValue, string friendlyName)
        {
            // Oid group is implemented strictly - no fallback to OidGroup.All as with many other parts of Crypto.
            Assert.Throws<CryptographicException>(() => Oid.FromFriendlyName(friendlyName, OidGroup.EncryptionAlgorithm));
        }

        [Fact]
        public static void LookupOidByFriendlyName_Method_NullInput()
        {
            Assert.Throws<ArgumentNullException>(() => Oid.FromFriendlyName(null, OidGroup.HashAlgorithm));
        }

        [Theory]
        [InlineData(SHA1_Oid)] // OIDs are not coerced into friendly names values from the method.
        [InlineData(Bogus_Name)]
        public static void LookupOidByFriendlyName_Method_BadInput(string badInput)
        {
            Assert.Throws<CryptographicException>(() => Oid.FromFriendlyName(badInput, OidGroup.HashAlgorithm));
        }

        [Fact]
        [PlatformSpecific(TestPlatforms.AnyUnix)]  // Uses P/Invokes to search Oid in the lookup table
        public static void LookupOidByValue_Method_UnixOnly()
        {
            // This needs to be an OID not in the static lookup table.  The purpose is to verify the
            // NativeOidToFriendlyName fallback for Unix.  For Windows this is accomplished by
            // using FromOidValue with an OidGroup other than OidGroup.All.

            Oid oid;

            try
            {
                oid = Oid.FromOidValue(ObsoleteSmime3desWrap_Oid, OidGroup.All);
            }
            catch (CryptographicException)
            {
                bool isMac = RuntimeInformation.IsOSPlatform(OSPlatform.OSX);

                Assert.True(isMac, "Exception is only raised on macOS");

                if (isMac)
                {
                    return;
                }
                else
                {
                    throw;
                }
            }

            Assert.Equal(ObsoleteSmime3desWrap_Oid, oid.Value);
            Assert.Equal(ObsoleteSmime3desWrap_Name, oid.FriendlyName);
        }

        [Fact]
        [PlatformSpecific(TestPlatforms.AnyUnix)]  // Uses P/Invokes to search Oid in the lookup table
        public static void LookupOidByFriendlyName_Method_UnixOnly()
        {
            // This needs to be a name not in the static lookup table.  The purpose is to verify the
            // NativeFriendlyNameToOid fallback for Unix.  For Windows this is accomplished by
            // using FromOidValue with an OidGroup other than OidGroup.All.
            Oid oid;

            try
            {
                oid = Oid.FromFriendlyName(ObsoleteSmime3desWrap_Name, OidGroup.All);
            }
            catch (CryptographicException)
            {
                bool isMac = RuntimeInformation.IsOSPlatform(OSPlatform.OSX);

                Assert.True(isMac, "Exception is only raised on macOS");

                if (isMac)
                {
                    return;
                }
                else
                {
                    throw;
                }
            }

            Assert.Equal(ObsoleteSmime3desWrap_Oid, oid.Value);
            Assert.Equal(ObsoleteSmime3desWrap_Name, oid.FriendlyName);
        }

        public static IEnumerable<string[]> ValidOidFriendlyNamePairs
        {
            get
            {
                List<string[]> data = new List<string[]>(ValidOidFriendlyNameHashAlgorithmPairs);
                data.AddRange(ValidOidFriendlyNameEncryptionAlgorithmPairs);

                return data;
            }
        }

        public static IEnumerable<string[]> ValidOidFriendlyNameHashAlgorithmPairs
        {
            get
            {
                return new[]
                {
                    new[] { SHA1_Oid, SHA1_Name },
                    new[] { SHA256_Oid, SHA256_Name },
                    new[] { "1.2.840.113549.2.5", "md5" },
                    new[] { "2.16.840.1.101.3.4.2.2", "sha384" },
                    new[] { "2.16.840.1.101.3.4.2.3", "sha512" },
                };
            }
        }

        public static IEnumerable<string[]> ValidOidFriendlyNameEncryptionAlgorithmPairs
        {
            get
            {
                return new[]
                {
                    new[] { "1.2.840.113549.3.7", "3des" },
                };
            }
        }

        private static string InvertCase(string existing)
        {
            char[] chars = existing.ToCharArray();

            for (int i = 0; i < chars.Length; i++)
            {
                if (char.IsUpper(chars[i]))
                {
                    chars[i] = char.ToLowerInvariant(chars[i]);
                }
                else if (char.IsLower(chars[i]))
                {
                    chars[i] = char.ToUpperInvariant(chars[i]);
                }
            }

            return new string(chars);
        }

        private const string SHA1_Name = "sha1";
        private const string SHA1_Oid = "1.3.14.3.2.26";

        private const string SHA256_Name = "sha256";
        private const string SHA256_Oid = "2.16.840.1.101.3.4.2.1";

        private const string Bogus_Name = "BOGUS_BOGUS_BOGUS_BOGUS";

        private const string ObsoleteSmime3desWrap_Oid = "1.2.840.113549.1.9.16.3.3";
        private const string ObsoleteSmime3desWrap_Name = "id-smime-alg-3DESwrap";
    }
}