Example #1
0
        public void Should_Compile_And_Run_Source_In_Sandbox()
        {
            var isolator = new AppDomainIsolator();
            var environment = new DefaultEnvironment();

            var source = new CSharpSource("using System; using System.IO; namespace Foo { public class Bar { public int Greet() { return 42; } } }");
            environment.Register(source);

            using (var jail = Jail.Create(isolator, environment))
            {
                dynamic bar = jail.Resolve("Foo.Bar");
                int result = bar.Greet();

                Assert.AreEqual(42, result);
            }
        }
Example #2
0
            public void Loads_And_Calculates_With_Dynamic_Proxy_In_Separate_AppDomain()
            {
                var isolator = new AppDomainIsolator();

                var environment = new DefaultEnvironment();
                environment.Register("Ext/Calculator.dll");

                using (var jail = Jail.Create(isolator, environment))
                {
                    dynamic calculator = jail.Resolve("Calculator.SimpleCalculator");
                    int result = calculator.Sum(new[] {1, 2, 3, 4, 5});
                    calculator.Name = "simple calculator";

                    Assert.AreEqual(15, result);
                    Assert.AreEqual("simple calculator", calculator.Name);
                }

                Assert.IsFalse(AppDomain.CurrentDomain.GetAssemblies().Any(asm => asm.GetName().Name == "Calculator"));
            }
        private void Run()
        {
            var appOptions = new AppDomainOptions();
            appOptions.Permissions.AddPermission(new SecurityPermission(SecurityPermissionFlag.UnmanagedCode));

            var isolator = new AppDomainIsolator(appOptions);
            var environment = new DefaultEnvironment();
            environment.Register(sInfo.assemblyPath);

            InteractionObject accessApi = new InteractionObject();

            using (var jail = Jail.Create(isolator, environment)) {
                var watch = new Stopwatch();
                var localSInfo = sInfo;
                main.window.updateScriptInfo(localSInfo);
                IAbstractScript mainClass = jail.Resolve<IAbstractScript>(sInfo.mainScript.FullName);

                mainClass._setApi(null);

                new RunUntil(() => mainClass.init(), delegate () { return localSInfo.run; });

                new Thread(() => mainClass.start()).Start();

                while (localSInfo.run) {
                    watch.Start();

                    new RunUntil(() => mainClass.tick(), delegate () { return localSInfo.run; });

                    var time = watch.ElapsedMilliseconds;
                    watch.Reset();

                    if(debug) Console.WriteLine(time);

                    Thread.Sleep(100);
                    // This will fail since the isolator has not been given
                    // FileIO permissions to that location.
                    //byte[] data = bar.ReadFile("C:\\Temp\\File.txt");
                }
                new Thread(() => mainClass.dispose()).Start();
            }
        }