Exemplo n.º 1
0
    public static void RequestEventStorePermission(EKEntityType entityType, bool assert_granted = false)
    {
        TestRuntime.AssertMacSystemVersion(10, 9, throwIfOtherPlatform: false);

        var status = EKEventStore.GetAuthorizationStatus(entityType);

        Console.WriteLine("EKEventStore.GetAuthorizationStatus ({1}): {0}", status, entityType);
        switch (status)
        {
        case EKAuthorizationStatus.Authorized:
        case EKAuthorizationStatus.Restricted:
            return;

        case EKAuthorizationStatus.NotDetermined:
            // There's an instance method on EKEventStore to request permission,
            // but creating the instance can end up blocking the app showing a permission dialog...
            // (on Mavericks at least)
            if (TestRuntime.CheckMacSystemVersion(10, 10))
            {
                return;                 // Crossing fingers that this won't hang.
            }
            NUnit.Framework.Assert.Ignore("This test requires permission to access events, but there's no API to request access without potentially showing dialogs.");
            break;

        case EKAuthorizationStatus.Denied:
            if (assert_granted)
            {
                NUnit.Framework.Assert.Ignore("This test requires permission to access events.");
            }
            break;
        }
    }
Exemplo n.º 2
0
        public void RoundtripRSA1024OAEP()
        {
            SecKey private_key;
            SecKey public_key;

            using (var record = new SecRecord(SecKind.Key)) {
                record.KeyType       = SecKeyType.RSA;
                record.KeySizeInBits = 1024;                 // it's not a performance test :)

                Assert.That(SecKey.GenerateKeyPair(record.ToDictionary(), out public_key, out private_key), Is.EqualTo(SecStatusCode.Success), "GenerateKeyPair");

                byte [] plain = new byte [0];
                byte [] cipher;
                if (TestRuntime.CheckXcodeVersion(8, 0))
                {
                    Assert.True(public_key.IsAlgorithmSupported(SecKeyOperationType.Encrypt, SecKeyAlgorithm.RsaEncryptionOaepSha1), "public/IsAlgorithmSupported/Encrypt");
                    // I would have expect false
#if MONOMAC
                    Assert.That(public_key.IsAlgorithmSupported(SecKeyOperationType.Decrypt, SecKeyAlgorithm.RsaEncryptionOaepSha1), Is.EqualTo(TestRuntime.CheckMacSystemVersion(10, 13)), "public/IsAlgorithmSupported/Decrypt");
#else
                    Assert.True(public_key.IsAlgorithmSupported(SecKeyOperationType.Decrypt, SecKeyAlgorithm.RsaEncryptionOaepSha1), "public/IsAlgorithmSupported/Decrypt");
#endif
                }
                Assert.That(public_key.Encrypt(SecPadding.OAEP, plain, out cipher), Is.EqualTo(SecStatusCode.Success), "Encrypt");
                public_key.Dispose();

                byte[] result;
                if (TestRuntime.CheckXcodeVersion(8, 0))
                {
                    Assert.False(private_key.IsAlgorithmSupported(SecKeyOperationType.Encrypt, SecKeyAlgorithm.RsaEncryptionOaepSha1), "private/IsAlgorithmSupported/Encrypt");
                    Assert.True(private_key.IsAlgorithmSupported(SecKeyOperationType.Decrypt, SecKeyAlgorithm.RsaEncryptionOaepSha1), "private/IsAlgorithmSupported/Decrypt");
                }
                Assert.That(private_key.Decrypt(SecPadding.OAEP, cipher, out result), Is.EqualTo(SecStatusCode.Success), "Decrypt");
                Assert.That(plain, Is.EqualTo(result), "match");
                private_key.Dispose();
            }
        }
Exemplo n.º 3
0
        public void RoundtripRSAMinPKCS1()
        {
            NSError error;
            SecKey  private_key;
            SecKey  public_key;

            using (var record = new SecRecord(SecKind.Key)) {
                record.KeyType       = SecKeyType.RSA;
                record.KeySizeInBits = MinRsaKeySize;                 // it's not a performance test :)

                Assert.That(SecKey.GenerateKeyPair(record.ToDictionary(), out public_key, out private_key), Is.EqualTo(SecStatusCode.Success), "GenerateKeyPair");

                byte [] plain = new byte [20] {
                    1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0
                };
                byte [] cipher;
                if (TestRuntime.CheckXcodeVersion(8, 0))
                {
                    Assert.True(public_key.IsAlgorithmSupported(SecKeyOperationType.Encrypt, SecKeyAlgorithm.RsaEncryptionPkcs1), "public/IsAlgorithmSupported/Encrypt");

#if MONOMAC
                    Assert.That(public_key.IsAlgorithmSupported(SecKeyOperationType.Decrypt, SecKeyAlgorithm.RsaEncryptionPkcs1), Is.EqualTo(TestRuntime.CheckMacSystemVersion(10, 13)), "public/IsAlgorithmSupported/Decrypt");

                    using (var pub = public_key.GetPublicKey()) {
                        // a new native instance of the key is returned (so having a new managed SecKey is fine)
                        Assert.True(pub.Handle == public_key.Handle, "public/GetPublicKey");
                    }
#else
                    Assert.True(public_key.IsAlgorithmSupported(SecKeyOperationType.Decrypt, SecKeyAlgorithm.RsaEncryptionPkcs1), "public/IsAlgorithmSupported/Decrypt");

                    using (var pub = public_key.GetPublicKey())
                    {
                        // a new native instance of the key is returned (so having a new managed SecKey is fine)
                        Assert.False(pub.Handle == public_key.Handle, "public/GetPublicKey");
                    }
#endif

                    using (var attrs = public_key.GetAttributes()) {
                        Assert.That(attrs.Count, Is.GreaterThan(0), "public/GetAttributes");
                    }
                    using (var data = public_key.GetExternalRepresentation(out error)) {
                        Assert.Null(error, "public/error-1");
                        Assert.NotNull(data, "public/GetExternalRepresentation");

                        using (var key = SecKey.Create(data, SecKeyType.RSA, SecKeyClass.Public, MinRsaKeySize, null, out error)) {
                            Assert.Null(error, "public/Create/error-1");
                        }
                    }
                }
                Assert.That(public_key.Encrypt(SecPadding.PKCS1, plain, out cipher), Is.EqualTo(SecStatusCode.Success), "Encrypt");

                byte[] result;
                if (TestRuntime.CheckXcodeVersion(8, 0))
                {
                    Assert.False(private_key.IsAlgorithmSupported(SecKeyOperationType.Encrypt, SecKeyAlgorithm.RsaEncryptionPkcs1), "private/IsAlgorithmSupported/Encrypt");
                    Assert.True(private_key.IsAlgorithmSupported(SecKeyOperationType.Decrypt, SecKeyAlgorithm.RsaEncryptionPkcs1), "private/IsAlgorithmSupported/Decrypt");

#if MONOMAC
                    using (var pub2 = private_key.GetPublicKey()) {
                        Assert.That(pub2.Handle, Is.EqualTo(public_key.Handle), "private/GetPublicKey");
                    }
#else
                    using (var pub2 = private_key.GetPublicKey()) {
                        // a new native instance of the key is returned (so having a new managed SecKey is fine)
                        Assert.That(pub2.Handle, Is.Not.EqualTo(public_key.Handle), "private/GetPublicKey");
                    }
#endif
                    using (var attrs = private_key.GetAttributes()) {
                        Assert.That(attrs.Count, Is.GreaterThan(0), "private/GetAttributes");
                    }
                    using (var data2 = private_key.GetExternalRepresentation(out error)) {
                        Assert.Null(error, "private/error-1");
                        Assert.NotNull(data2, "private/GetExternalRepresentation");

                        using (var key = SecKey.Create(data2, SecKeyType.RSA, SecKeyClass.Private, MinRsaKeySize, null, out error)) {
                            Assert.Null(error, "private/Create/error-1");
                        }
                    }
                }
                public_key.Dispose();
                Assert.That(private_key.Decrypt(SecPadding.PKCS1, cipher, out result), Is.EqualTo(SecStatusCode.Success), "Decrypt");
                Assert.That(plain, Is.EqualTo(result), "match");
                private_key.Dispose();
            }
        }