﻿// Copyright 2014 The Noda Time Authors. All rights reserved.
// Use of this source code is governed by the Apache License 2.0,
// as found in the LICENSE.txt file.

using NUnit.Framework;
using System.Collections.Generic;
using System.Linq;
using System.Security;

namespace NodaTime.Test.Annotations
{
    [TestFixture]
    public class SecurityTest
    {
        [Test]
        public void SecurityAttributesOnInterfaceImplementations()
        {
            var violations = new List<string>();
            foreach (var type in typeof(Instant).Assembly.GetTypes().Where(type => !type.IsInterface))
            {
                foreach (var iface in type.GetInterfaces())
                {
                    var map = type.GetInterfaceMap(iface);
                    var methodCount = map.InterfaceMethods.Length;
                    for (int i = 0; i < methodCount; i++)
                    {
                        if (map.TargetMethods[i].ReflectedType == type &&
                            map.InterfaceMethods[i].IsDefined(typeof(SecurityCriticalAttribute), false) &&
                            !map.TargetMethods[i].IsDefined(typeof(SecurityCriticalAttribute), false))
                        {
                            violations.Add(type.FullName + "/" + map.TargetMethods[i].Name);
                        }
                    }
                }
            }
            Assert.IsEmpty(violations);
        }
    }
}

﻿// Copyright 2014 The Noda Time Authors. All rights reserved.
// Use of this source code is governed by the Apache License 2.0,
// as found in the LICENSE.txt file.

using NUnit.Framework;
using System.Collections.Generic;
using System.Linq;
using System.Security;

namespace NodaTime.Test.Annotations
{
    [TestFixture]
    public class SecurityTest
    {
        [Test]
        public void SecurityAttributesOnInterfaceImplementations()
        {
            var violations = new List<string>();
            foreach (var type in typeof(Instant).Assembly.GetTypes().Where(type => !type.IsInterface))
            {
                foreach (var iface in type.GetInterfaces())
                {
                    var map = type.GetInterfaceMap(iface);
                    var methodCount = map.InterfaceMethods.Length;
                    for (int i = 0; i < methodCount; i++)
                    {
                        if (map.TargetMethods[i].DeclaringType == type &&
                            map.InterfaceMethods[i].IsDefined(typeof(SecurityCriticalAttribute), false) &&
                            !map.TargetMethods[i].IsDefined(typeof(SecurityCriticalAttribute), false))
                        {
                            violations.Add(type.FullName + "/" + map.TargetMethods[i].Name);
                        }
                    }
                }
            }
            Assert.IsEmpty(violations);
        }
    }
}

Use DeclaringType instead of ReflectedType to find interface implementations within the NodaTime assembly. (Not sure why the mapping code behaves differently in .NET to in Mono anyway, but...)
Use DeclaringType instead of ReflectedType to find interface implementations within the NodaTime assembly.
(Not sure why the mapping code behaves differently in .NET to in Mono anyway, but...)
