Ejemplo n.º 1
0
        static void TestSecuritySystem()
        {
            try
            {
                VolatileDisk disk = new VolatileDisk(0);
                disk.TurnOn();
                FileSystem fs = new SimpleFS();
                fs.Format(disk);
                fs.Mount(disk, "/");

                SecuritySystem security = new SimpleSecurity(fs, "passwd");
                security.AddUser("steven");
                if (security.NeedsPassword("steven"))
                {
                    security.SetPassword("steven", "foobar42");
                }
                int userID = security.Authenticate("steven", "foobar42");
                Console.WriteLine("UserID " + userID.ToString());
                Console.WriteLine("Username " + security.UserName(userID));
                Console.WriteLine("Home Directory " + security.UserHomeDirectory(userID));
                Console.WriteLine("Shell " + security.UserPreferredShell(userID));

                // Try failure scenarios

                // Add user "steven" again
                TestSecurityException(() => { security.AddUser("steven"); });
                // Password for no one
                TestSecurityException(() => { security.NeedsPassword("nope"); });
                // Set password for invalid user
                TestSecurityException(() => { security.SetPassword("nope", "12345678"); });
                // Set invalid password
                TestSecurityException(() => { security.SetPassword("steven", "hat"); });
                // Authenticate invalid user
                TestSecurityException(() => { security.Authenticate("nope", "12345678"); });
                // Authenticate invalid password
                TestSecurityException(() => { security.Authenticate("steven", "hat"); });
                // invalid user ID
                TestSecurityException(() => { security.UserName(42); });
                // invalid user ID
                TestSecurityException(() => { security.UserHomeDirectory(42); });
                // invalid user ID
                TestSecurityException(() => { security.UserPreferredShell(42); });

                // second security system
                SecuritySystem security2 = new SimpleSecurity(fs, "passwd");
                security2.Authenticate("steven", "foobar42");


                fs.Unmount("/");
                disk.TurnOff();
            }
            catch (Exception ex)
            {
                Console.WriteLine("Exception: " + ex.Message);
            }
        }
Ejemplo n.º 2
0
        static void TestSessionManager()
        {
            SlowDisk disk = new SlowDisk(42);

            disk.TurnOn();

            FileSystem filesystem = new SimpleFS();

            filesystem.Format(disk);
            filesystem.Mount(disk, "/");

            //make dir/files
            Directory root     = filesystem.GetRootDirectory();
            Directory usersDir = root.CreateDirectory("users");
            Directory johnDir  = usersDir.CreateDirectory("john");
            Directory subDir   = johnDir.CreateDirectory("subdir");

            subDir.CreateFile("file1");
            subDir.CreateFile("file2");
            // add an /etc/passwd file that contains users for security system.


            SecuritySystem security = new SimpleSecurity();

            security.AddUser("john");
            security.SetPassword("john", "foo");


            ShellFactory   shells         = new SimpleShellFactory();
            SessionManager sessionManager = new SimpleSessionManager(security, filesystem, shells);
            TerminalDriver driver         = new DotNetConsoleTerminal();
            Terminal       term           = new Terminal(driver);

            term.Connect();

            while (true)
            {
                Session session = sessionManager.NewSession(term);
                if (session == null)
                {
                    throw new Exception("Failed to get sessions!");
                }
                session.Run();

                // after session exits, logout
                session.Logout();
            }

            term.Disconnect();
        }
Ejemplo n.º 3
0
        static void TestSecuritySystem()
        {
            string         username = "******";
            SecuritySystem security = new SimpleSecurity();

            security.AddUser(username);

            if (security.NeedsPassword(username))
            {
                security.SetPassword(username, "test");
            }
            int userId = security.Authenticate(username, "test");

            Console.WriteLine("UserID " + userId.ToString());
            Console.WriteLine("Username " + security.UserName(userId));
            Console.WriteLine("Home Directory " + security.UserHomeDirectory(userId));
            Console.WriteLine("Shell " + security.UserPreferredShell(userId));
        }