Exemplo n.º 1
0
        public void TestAddAssembly1()
        {
            using (var packer = CreatePacker(_fname))
            {
                var builder = new PluginBuilder("Simon", "Foo", "Plugin");
                builder.AssemblyVersion              = "4.0.3.1";
                builder.AssemblyFileVersion          = "1.2.3.42";
                builder.AssemblyInformationalVersion = "4.0.0.0-beta";
                builder.Save();
                packer.AddPluginAssembly(builder.FileName);
            }

            using (var reader = PluginArchive.OpenRead(_fname))
            {
                var index = reader.Index;
                index.Should().NotBeNull();
                index.Name.Should().Be("Plugin");
                index.Assemblies.Should().NotBeNull();
                index.Assemblies.Should().HaveCount(1);
                index.Assemblies[0].EntryName.Should().Be("Plugin.dll");
                index.Assemblies[0].AssemblyName.Should().Be("Plugin");
                index.Assemblies[0].AssemblyVersion.Should().Be(new Version(4, 0, 3, 1));
                index.Assemblies[0].AssemblyFileVersion.Should().Be(new Version(1, 2, 3, 42));
                index.Assemblies[0].AssemblyInformationalVersion.Should().Be("4.0.0.0-beta");
                index.NativeImages.Should().NotBeNull();
                index.NativeImages.Should().HaveCount(0);

                var actualAssembly = reader.LoadAssembly("Plugin.dll");
                actualAssembly.Should().NotBeNull();
            }
        }
        public void TestReflectTwoPluginImplementations()
        {
            var stream = new MemoryStream();

            using (var packer = PluginPacker.Create(stream, true))
            {
                var builder = new PluginBuilder("Kittyfisto", "UniquePluginId", "MyAwesomePlugin");
                builder.ImplementInterface <ILogEntryParserPlugin>("A");
                builder.ImplementInterface <ILogEntryParserPlugin>("B");
                builder.Save();

                packer.AddPluginAssembly(builder.FileName);
            }

            stream.Position = 0;
            _filesystem.Write(Path.Combine(Constants.PluginPath, "Kittyfisto.UniquePluginId.2.0.tvp"), stream);

            using (var loader = new PluginArchiveLoader(_filesystem, Constants.PluginPath))
            {
                loader.Plugins.Should().HaveCount(1, "because one plugin should've been loaded");
                var description = loader.Plugins.First();
                description.PluginImplementations.Should().HaveCount(2, "because we've implemented the IFileFormatPlugin twice");
                description.PluginImplementations[0].InterfaceType.Should().Be <ILogEntryParserPlugin>();
                description.PluginImplementations[0].FullTypeName.Should().Be("A");

                description.PluginImplementations[1].InterfaceType.Should().Be <ILogEntryParserPlugin>();
                description.PluginImplementations[1].FullTypeName.Should().Be("B");
            }
        }
        public void TestReflectPlugin2()
        {
            var plugin = "sql.dll";

            if (File.Exists(plugin))
            {
                File.Delete(plugin);
            }

            var builder = new PluginBuilder("Simon", "sql", "sql", "SMI", "none of your business", "go away");

            builder.ImplementInterface <ILogEntryParserPlugin>("sql.LogFilePlugin");
            builder.Save();

            var scanner     = new PluginAssemblyLoader();
            var description = scanner.ReflectPlugin(plugin);

            description.Author.Should().Be("SMI");
            description.Website.Should().Be(new Uri("none of your business", UriKind.RelativeOrAbsolute));
            description.Description.Should().Be("go away");
            description.PluginImplementations.Should().HaveCount(1);
            description.PluginImplementations.Should().Contain(x => x.InterfaceType == typeof(ILogEntryParserPlugin));
            var implementationDescription = description.PluginImplementations[0];

            implementationDescription.FullTypeName.Should().Be("sql.LogFilePlugin");
            implementationDescription
            .Version.Should().Be(PluginInterfaceVersionAttribute.GetInterfaceVersion(typeof(ILogEntryParserPlugin)));
        }
        public void TestLoadAllOfType1()
        {
            using (var stream = new MemoryStream())
            {
                using (var packer = PluginPacker.Create(stream, true))
                {
                    var builder = new PluginBuilder("Kittyfisto", "SomePlugin", "none of your business", "get of my lawn");
                    builder.ImplementInterface <ILogEntryParserPlugin>("Plugin.FileFormatPlugin");
                    builder.Save();

                    packer.AddPluginAssembly(builder.FileName);
                }

                stream.Position = 0;
                _filesystem.Write(Path.Combine(Constants.PluginPath, "Kittyfisto.SomePlugin.1.tvp"), stream);

                using (var loader = new PluginArchiveLoader(_filesystem, Constants.PluginPath))
                {
                    var plugins = loader.LoadAllOfType <ILogEntryParserPlugin>()?.ToList();
                    plugins.Should().NotBeNull();
                    plugins.Should().HaveCount(1);
                    plugins[0].Should().NotBeNull();
                    plugins[0].GetType().FullName.Should().Be("Plugin.FileFormatPlugin");
                }
            }
        }
        public void TestLoad2()
        {
            var assemblyFileName = "Foo2.dll";

            if (File.Exists(assemblyFileName))
            {
                File.Delete(assemblyFileName);
            }

            var builder = new PluginBuilder("Simon", "Foo2", "Foo2", "Simon", "None of your business", "Get of my lawn");

            builder.ImplementInterface <ILogEntryParserPlugin>("Foo2.MyAwesomePlugin");
            builder.Save();
            var description = new PluginDescription
            {
                FilePath = assemblyFileName,
                PluginImplementations = new List <IPluginImplementationDescription>
                {
                    new PluginImplementationDescription("Foo2.MyAwesomePlugin", typeof(ILogEntryParserPlugin))
                }
            };

            using (var scanner = new PluginAssemblyLoader())
            {
                var plugin = scanner.Load <ILogEntryParserPlugin>(description, description.PluginImplementations[0]);
                plugin.Should().NotBeNull();
                plugin.GetType().FullName.Should().Be("Foo2.MyAwesomePlugin");
            }
        }
        public void TestLoadAllOfTypeWithDescription()
        {
            using (var stream = new MemoryStream())
            {
                using (var packer = PluginPacker.Create(stream, true))
                {
                    var builder = new PluginBuilder("Kittyfisto", "Simon", "none of your business", "get of my lawn");
                    builder.ImplementInterface <ILogEntryParserPlugin>("Plugin.FileFormatPlugin");
                    builder.Save();

                    packer.AddPluginAssembly(builder.FileName);
                }

                stream.Position = 0;
                var path = Path.Combine(Constants.PluginPath, "Kittyfisto.Simon.1.0.tvp");
                _filesystem.WriteAllBytes(path, stream.ToArray());

                using (var loader = new PluginArchiveLoader(_filesystem, Constants.PluginPath))
                {
                    var pluginsWithDescription = loader.LoadAllOfTypeWithDescription <ILogEntryParserPlugin>();
                    pluginsWithDescription.Should().HaveCount(1, "because we've added one plugin which implements the IFileFormatPlugin interface");
                    var description = pluginsWithDescription[0].Description;
                    description.Should().NotBeNull();
                    description.Id.Should().Be(new PluginId("Kittyfisto.Simon"));
                    description.Author.Should().Be("get of my lawn");
                }
            }
        }
Exemplo n.º 7
0
        public void TestAddNativeImage1()
        {
            using (var packer = CreatePacker(_fname))
            {
                var builder = new PluginBuilder("Simon", "Foo", "Plugin");
                builder.Save();
                packer.AddPluginAssembly(builder.FileName);

                var fname = Path.Combine(_testData, "Native", "x86", "NativeImage.dll");
                packer.AddFile("NativeImage.dll", fname);
            }

            using (var reader = PluginArchive.OpenRead(_fname))
            {
                var index = reader.Index;
                index.Should().NotBeNull();
                index.Assemblies.Should().NotBeNull();
                index.Assemblies.Should().HaveCount(1);

                index.NativeImages.Should().NotBeNull();
                index.NativeImages.Count.Should().Be(1);
                index.NativeImages[0].EntryName.Should().Be("NativeImage.dll");
                index.NativeImages[0].ImageName.Should().Be("NativeImage");
            }
        }
Exemplo n.º 8
0
        public void TestPackPrivatePluginImplementation()
        {
            using (var packer = CreatePacker(_fname))
            {
                var builder = new PluginBuilder("Kittyfisto", "MyPlugin", "My First Plugin");
                builder.PluginVersion = new Version(1, 4, 12034);
                builder.ImplementInterface <ILogEntryParserPlugin>("Plugin.FileFormatPlugin", TypeAttributes.NotPublic | TypeAttributes.Sealed);
                builder.Save();

                new Action(() => packer.AddPluginAssembly(builder.FileName))
                .Should()
                .Throw <PackException>()
                .WithMessage("Plugin implementations must publicly visible, but 'Plugin.FileFormatPlugin' is not!");
            }
        }
Exemplo n.º 9
0
        protected static void CreatePlugin(string assemblyFileName,
                                           string author      = null,
                                           string website     = null,
                                           string description = null)
        {
            var pluginName = Path.GetFileNameWithoutExtension(assemblyFileName);
            var idParts    = pluginName.Split('.');

            idParts.Should().HaveCount(2);
            var @namespace = idParts[0];
            var name       = idParts[1];

            var builder = new PluginBuilder(@namespace, name, pluginName, author, website, description);

            builder.Save();
        }
Exemplo n.º 10
0
        private static PluginId CreatePlugin(MemoryStream stream)
        {
            var @namespace = "Kittyfisto";
            var name       = "UniquePluginId";

            using (var packer = PluginPacker.Create(stream, true))
            {
                var builder = new PluginBuilder(@namespace, name, "My very own plugin", "Simon", "http://google.com",
                                                "get of my lawn");
                builder.ImplementInterface <ILogEntryParserPlugin>("Plugin.FileFormatPlugin");
                builder.Save();

                packer.AddPluginAssembly(builder.FileName);
            }

            stream.Position = 0;
            return(new PluginId($"{@namespace}.{name}"));
        }
Exemplo n.º 11
0
        public void TestReflectTwoPluginImplementations()
        {
            var builder = new PluginBuilder("Kittyfisto", "TestReflectTwoPluginImplementations", "TestReflectTwoPluginImplementations");

            builder.ImplementInterface <ILogEntryParserPlugin>("A");
            builder.ImplementInterface <ILogEntryParserPlugin>("B");
            builder.Save();

            var assemblyLoader = new PluginAssemblyLoader();
            var description    = assemblyLoader.ReflectPlugin(builder.FileName);

            description.PluginImplementations.Should().HaveCount(2, "because we've implemented the IFileFormatPlugin twice");
            description.PluginImplementations[0].InterfaceType.Should().Be <ILogEntryParserPlugin>();
            description.PluginImplementations[0].FullTypeName.Should().Be("A");

            description.PluginImplementations[1].InterfaceType.Should().Be <ILogEntryParserPlugin>();
            description.PluginImplementations[1].FullTypeName.Should().Be("B");
        }
Exemplo n.º 12
0
        public void TestReflectPluginNonPublicSerializableType()
        {
            var pluginBuilder = new PluginBuilder("Kittyfisto", "Test", "TestReflectPluginNonPublicSerializableType");
            var type          = pluginBuilder.DefineType("SomeSerializableType", TypeAttributes.Class | TypeAttributes.NotPublic);

            type.AddInterfaceImplementation(typeof(ISerializableType));
            var attribute = pluginBuilder.BuildCustomAttribute(new DataContractAttribute());

            type.SetCustomAttribute(attribute);
            var ctorBuilder = type.DefineConstructor(MethodAttributes.Public, CallingConventions.HasThis, new Type[0]);
            var gen         = ctorBuilder.GetILGenerator();

            gen.Emit(OpCodes.Ret);

            var serialize = type.DefineMethod(nameof(ISerializableType.Serialize), MethodAttributes.Public | MethodAttributes.Virtual,
                                              CallingConventions.HasThis,
                                              typeof(void),
                                              new [] { typeof(IWriter) });

            serialize.GetILGenerator().Emit(OpCodes.Ret);

            var deserialize = type.DefineMethod(nameof(ISerializableType.Deserialize), MethodAttributes.Public | MethodAttributes.Virtual,
                                                CallingConventions.HasThis,
                                                typeof(void),
                                                new [] { typeof(IReader) });

            deserialize.GetILGenerator().Emit(OpCodes.Ret);

            type.CreateType();
            pluginBuilder.Save();

            var scanner = new PluginAssemblyLoader();

            var appender = Appender.CaptureEvents("Tailviewer.Archiver.Plugins.PluginAssemblyLoader", Level.Error);

            scanner.ReflectPlugin(pluginBuilder.FileName);
            appender.Events.Should().HaveCount(1, "because the serializable type's parameterless constructor is not publicly visible and this should have provoked an error");
            var error = appender.Events.First();

            error.RenderedMessage.Should().Contain(type.FullName);
            error.RenderedMessage.Should().Contain("must be set to public!");
        }
Exemplo n.º 13
0
        public void TestScanAndLoad()
        {
            using (var scanner = new PluginAssemblyLoader())
            {
                var assemblyFileName = "Foo3.dll";
                if (File.Exists(assemblyFileName))
                {
                    File.Delete(assemblyFileName);
                }

                var builder = new PluginBuilder("Simon", "Foo3", "Foo3", "Simon", "None of your business", "Get of my lawn");
                builder.ImplementInterface <ILogEntryParserPlugin>("Foo3.MyAwesomePlugin");
                builder.Save();

                var description = scanner.ReflectPlugin(assemblyFileName);
                var plugin      = scanner.Load <ILogEntryParserPlugin>(description, description.PluginImplementations[0]);
                plugin.Should().NotBeNull();
                plugin.GetType().FullName.Should().Be("Foo3.MyAwesomePlugin");
            }
        }
Exemplo n.º 14
0
        private void CreatePlugin(string @namespace, string name, Version version)
        {
            using (var stream = new MemoryStream())
            {
                using (var packer = PluginPacker.Create(stream, leaveOpen: true))
                {
                    var builder = new PluginBuilder(@namespace, name, "dawawdwdaaw")
                    {
                        PluginVersion = version
                    };
                    builder.ImplementInterface <ILogEntryParserPlugin>("dwwwddwawa");
                    builder.Save();
                    packer.AddPluginAssembly(builder.FileName);
                }

                stream.Position = 0;
                var fileName = Path.Combine(Constants.PluginPath, string.Format("{0}.{1}.{2}.tvp", @namespace, name, version));
                _filesystem.Write(fileName, stream);
            }
        }
Exemplo n.º 15
0
        public void TestAddAssembly5()
        {
            using (var packer = CreatePacker(_fname))
            {
                var builder = new PluginBuilder("Kittyfisto", "MyPlugin", "My First Plugin");
                builder.PluginVersion = new Version(1, 4, 12034);
                builder.ImplementInterface <ILogEntryParserPlugin>("Plugin.FileFormatPlugin");
                builder.Save();

                packer.AddPluginAssembly(builder.FileName);
            }

            using (var reader = PluginArchive.OpenRead(_fname))
            {
                var index = reader.Index;
                index.Version.Should().NotBeNull();
                index.Id.Should().Be("Kittyfisto.MyPlugin");
                index.Name.Should().Be("My First Plugin");
                index.Version.Should().Be(new Version(1, 4, 12034));
            }
        }
Exemplo n.º 16
0
        public void TestLoadAssembly1()
        {
            using (var stream = new MemoryStream())
            {
                using (var packer = PluginPacker.Create(stream, true))
                {
                    var builder = new PluginBuilder("Simon", "Foo1", "TestLoadAssembly1", "Simon", "None of your business", "Get of my lawn");
                    builder.ImplementInterface <ILogEntryParserPlugin>("Foo1.MyAwesomePlugin");
                    builder.Save();
                    packer.AddPluginAssembly(builder.FileName);
                }

                stream.Position = 0;

                using (var reader = PluginArchive.OpenRead(stream))
                {
                    var assembly = reader.LoadAssembly("Plugin.dll");
                    assembly.Should().NotBeNull();
                    reader.LoadAssembly("Plugin.dll").Should().BeSameAs(assembly, "because LoadAssembly should return the very same assembly every time");
                }
            }
        }
Exemplo n.º 17
0
        private void CreatePlugin <T>(string pluginFolder, PluginId actualId, Version actualVersion, PluginId fakeId, Version fakeVersion) where T : IPlugin
        {
            using (var stream = new MemoryStream())
            {
                using (var packer = PluginPacker.Create(stream, true))
                {
                    var idx             = actualId.Value.LastIndexOf(".");
                    var actualNamespace = actualId.Value.Substring(0, idx);
                    var actualName      = actualId.Value.Substring(idx + 1);
                    var builder         = new PluginBuilder(actualNamespace, actualName, "none of your business", "get of my lawn", version: actualVersion);
                    builder.ImplementInterface <T>("Plugin.FileFormatPlugin");
                    builder.Save();

                    packer.AddPluginAssembly(builder.FileName);
                }
                stream.Position = 0;

                var path = Path.Combine(pluginFolder, $"{fakeId.Value}.{fakeVersion}.tvp");
                _filesystem.CreateDirectory(pluginFolder);
                _filesystem.WriteAllBytes(path, stream.ToArray());
            }
        }
Exemplo n.º 18
0
        public void TestAddSameAssemblyTwice()
        {
            var directory = Directory.GetCurrentDirectory();
            var aFileName = "A.dll";
            var aFilePath = Path.Combine(directory, aFileName);
            var bFileName = "B.dll";
            var bFilePath = Path.Combine(directory, bFileName);

            using (var packer = CreatePacker(_fname))
            {
                var aBuilder = AssemblyBuilder.DefineDynamicAssembly(new AssemblyName("A"), AssemblyBuilderAccess.Save);
                var aModule  = aBuilder.DefineDynamicModule(aFileName);
                var aType    = aModule.DefineType("A_Dummy", TypeAttributes.Class | TypeAttributes.Public);
                aType.CreateType();
                aBuilder.Save(aFileName);

                var bBuilder = AssemblyBuilder.DefineDynamicAssembly(new AssemblyName("B"), AssemblyBuilderAccess.Save);
                var bModule  = bBuilder.DefineDynamicModule(bFileName);
                var bType    = bModule.DefineType("B_Dummy", TypeAttributes.Class | TypeAttributes.Public, Assembly.LoadFile(aFilePath).ExportedTypes.First());
                bType.CreateType();
                bBuilder.Save(bFileName);

                var builder = new PluginBuilder("Simon", "Foo", "Plugin");
                builder.AddDependency(aFilePath);
                builder.AddDependency(bFilePath);
                builder.Save();
                packer.AddPluginAssembly(builder.FileName);
            }

            using (var reader = PluginArchive.OpenRead(_fname))
            {
                var assemblies = reader.Index.Assemblies;
                assemblies.Should().ContainSingle(x => x.AssemblyName == "A");
                assemblies.Should().ContainSingle(x => x.AssemblyName == "B");
                assemblies.Should().ContainSingle(x => x.AssemblyName == "Plugin");
                assemblies.Should().HaveCount(3, "because we expect a total of 3 assemblies to have been saved");
            }
        }