static void TestSessionManager() { // disk //VolatileDisk disk = new VolatileDisk(1); PersistentDisk disk = new PersistentDisk(1, "disk1"); // create and write our OS to disk if needed CreateOSOnDisk(disk); // set up file system and mount disk disk.TurnOn(); FileSystem filesystem = new SimpleFS(); filesystem.Mount(disk, "/"); // security system SecuritySystem security = new SimpleSecurity(filesystem, "passwd"); // add pete and his test data if needed CreatePete(security, filesystem); // session manager ShellFactory shells = new SimpleShellFactory(); SessionManager sessionmanager = new SimpleSessionManager(security, filesystem, shells); // terminal TerminalDriver driver = new DotNetConsoleTerminal(); Terminal term = new Terminal(driver); term.Connect(); // allow terminals to connect and establish new sessions for users try { while (true) { Session session = sessionmanager.NewSession(term); if (session == null) { throw new Exception("Failed to create new session!"); } session.Run(); // after session exits, time to log out! session.Logout(); } } catch (Exception ex) { Console.WriteLine(ex.Message); } term.Disconnect(); disk.TurnOff(); }
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); } }
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(); }
static void CreateOSOnDisk(DiskDriver disk) { // create the operating system on disk if not present disk.TurnOn(); // try to mount the disk, format if needed first FileSystem filesystem = new SimpleFS(); try { filesystem.Mount(disk, "/"); } catch (Exception) { filesystem.Format(disk); filesystem.Mount(disk, "/"); } Directory rootDir = filesystem.GetRootDirectory(); // password file for security subsystem if not present if (filesystem.Find("/passwd") == null) { rootDir.CreateFile("passwd"); } // user home directory and files if not present if (filesystem.Find("/home") == null) { rootDir.CreateDirectory("home"); } // root user if not present SecuritySystem security = new SimpleSecurity(filesystem, "passwd"); try { security.AddUser("root"); } catch (Exception) { } disk.TurnOff(); }
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)); }