Пример #1
0
    public void CanSetContents()
    {
        FileSystem fs = new FileSystem();

        Assert.IsNotNull(fs);

        EditableFile f = fs.createFile("test.txt");

        Assert.AreEqual(f.getContents(), "");
        f.setContents("hello world");
        Assert.AreEqual(f.getContents(), "hello world");
    }
Пример #2
0
    public void CanNonadminUsersModifyRightPermissions()
    {
        FileSystem   fs = new FileSystem();
        EditableFile fi = fs.createFile("hello.txt");

        fs.setPermissions(fi, 000);

        // Make a nonadmin user
        fs.addUser("meow", "pass", SECURITY_LEVEL.NONADMIN);
        Assert.AreEqual(2, fs.allUsers.Count);
        fs.changeUser("meow", "pass");

        // Can't read
        Assert.Throws(typeof(InvalidUserException), delegate {
            fi.getContents();
        });

        // Can't write
        Assert.Throws(typeof(InvalidUserException), delegate {
            fi.setContents("hello world!");
        });

        // Can't set root permissions
        Assert.Throws(typeof(InvalidUserException), delegate {
            fs.setPermissions(fi, 700);
        });

        // Can't set admin permissions
        Assert.Throws(typeof(InvalidUserException), delegate {
            fs.setPermissions(fi, 070);
        });

        // Can set nonadmin permissions
        fs.setPermissions(fi, 007);

        fi.setContents("meowmeow");
        Assert.AreEqual("meowmeow", fi.getContents());
    }
Пример #3
0
    public void CanRootUsersModifyRightPermissions()
    {
        FileSystem fs = new FileSystem();

        // We are root user, so we can change all permissions.
        // Try making something we can't read or write to.
        EditableFile fi = fs.createFile("hello.txt");

        fs.setPermissions(fi, 000);
        Assert.AreEqual(000, fi.getPermissions());

        // Can't read
        Assert.Throws(typeof(InvalidUserException), delegate {
            fi.getContents();
        });

        // Can't write
        Assert.Throws(typeof(InvalidUserException), delegate {
            fi.setContents("hello world!");
        });

        // Can set any permissions
        fs.setPermissions(fi, 700);
        Assert.AreEqual(700, fi.getPermissions());
        fs.setPermissions(fi, 770);
        Assert.AreEqual(770, fi.getPermissions());
        fs.setPermissions(fi, 777);
        Assert.AreEqual(777, fi.getPermissions());
        fs.setPermissions(fi, 700);
        Assert.AreEqual(700, fi.getPermissions());

        // Can read + write now
        Assert.AreEqual("", fi.getContents());
        fi.setContents("hello!");
        Assert.AreEqual("hello!", fi.getContents());
    }
Пример #4
0
        public override string execute(params string[] args)
        {
            NetworkNode node = GameManager.currentHost;

            if (!(node is IFileSystem))
            {
                throw new ExecutionException("The current node does not support a file system.");
            }
            FileSystem currentFileSystem = (node as IFileSystem).fileSystem;

            string currentPath = GameManager.currentPath;
            string filename    = args [1];
            File   newFile     = currentFileSystem.getFile(currentPath + "/" + filename);

            if (newFile == null)
            {
                throw new ExecutionException("File \"" + filename + "\" does not exist.");
            }

            if (newFile is Directory)
            {
                throw new ExecutionException("Can't cat file: is directory.");
            }

            if (!(newFile is EditableFile))
            {
                throw new ExecutionException("Can't cat file: contents are unreadable.");
            }

            EditableFile newFileE = newFile as EditableFile;

            try {
                return("Contents of file \"" + newFileE.getFullName() + "\":\n" + newFileE.getContents());
            } catch (InvalidUserException iue) {
                throw new ExecutionException(iue.Message);
            }
        }