Exemplo 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);
            }
        }
Exemplo n.º 2
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));
        }