<commit_before>package peergos.tests;

import org.junit.*;
import peergos.corenode.*;
import peergos.crypto.*;
import peergos.fuse.*;
import peergos.server.Start;
import peergos.user.*;
import peergos.util.*;

import java.io.*;
import java.lang.*;
import java.lang.reflect.*;
import java.net.*;
import java.nio.file.*;
import java.util.*;
import java.util.stream.*;

public class FuseTests {
    public static int WEB_PORT = 8888;
    public static int CORE_PORT = 7777;
    public static String username = "test02";
    public static String password = <PASSWORD>;
    public static Path mountPoint, home;
    public static FuseProcess fuseProcess;

    public static void setWebPort(int webPort) {
        WEB_PORT = webPort;
    }

    public static void setCorePort(int corePort) {
        CORE_PORT = corePort;
    }

    static void setFinalStatic(Field field, Object newValue) throws Exception {
        field.setAccessible(true);

        Field modifiersField = Field.class.getDeclaredField("modifiers");
        modifiersField.setAccessible(true);
        modifiersField.setInt(field, field.getModifiers() & ~Modifier.FINAL);

        field.set(null, newValue);
    }

    public static UserContext ensureSignedUp(String username, String password) throws IOException {
        DHTClient.HTTP dht = new DHTClient.HTTP(new URL("http://localhost:"+ WEB_PORT +"/"));
        Btree.HTTP btree = new Btree.HTTP(new URL("http://localhost:"+ WEB_PORT +"/"));
        HTTPCoreNode coreNode = new HTTPCoreNode(new URL("http://localhost:"+ WEB_PORT +"/"));
        UserContext userContext = UserContext.ensureSignedUp(username, password, dht, btree, coreNode);
        return userContext;
    }

    @BeforeClass
    public static void init() throws Exception {
        Random  random  = new Random();
        int offset = random.nextInt(100);
        setWebPort(8888 + offset);
        setCorePort(7777 + offset);

        System.out.println("Using web-port "+ WEB_PORT);
        System.out.flush();
        // use insecure random otherwise tests take ages
        setFinalStatic(TweetNaCl.class.getDeclaredField("prng"), new Random(1));

        Args.parse(new String[]{"useIPFS", "false",
                "-port", Integer.toString(WEB_PORT),
                "-corenodePort", Integer.toString(CORE_PORT)});

        Start.local();
        UserContext userContext = ensureSignedUp(username, password);

        String mountPath = Args.getArg("mountPoint", "/tmp/peergos/tmp");

        mountPoint = Paths.get(mountPath);
        mountPoint = mountPoint.resolve(UUID.randomUUID().toString());
        mountPoint.toFile().mkdirs();
        home = mountPoint.resolve(username);

        System.out.println("\n\nMountpoint "+ mountPoint +"\n\n");
        PeergosFS peergosFS = new PeergosFS(userContext);
        fuseProcess = new FuseProcess(peergosFS, mountPoint);

        Runtime.getRuntime().addShutdownHook(new Thread(()  -> fuseProcess.close()));

        fuseProcess.start();
    }

    public static String readStdout(Process p) throws IOException {
        return new String(Serialize.readFully(p.getInputStream())).trim();
    }

    @Test
    public void variousTests() throws IOException {
        boolean homeExists = Stream.of(mountPoint.toFile().listFiles())
                .map(f -> f.getName())
                .filter(n -> n.equals(username))
                .findAny()
                .isPresent();
        Assert.assertTrue("Correct home directory: " + homeExists, homeExists);

        Path home = mountPoint.resolve(username);

        // write a small file
        Path filename1 = home.resolve("data.txt");
        String data = "Hello Peergos!";
        FileOutputStream fout1 = new FileOutputStream(filename1.toFile());
        fout1.write(data.getBytes());
        fout1.flush();
        fout1.close();
        byte[] smallFileContents = Serialize.readFully(new FileInputStream(filename1.toFile()));
        Assert.assertTrue("Correct file contents: " + new String(smallFileContents), Arrays.equals(smallFileContents, data.getBytes()));

        // correct file size
        Assert.assertTrue("Correct file size", filename1.toFile().length() == data.getBytes().length);

        // rename a file
        Path newFileName = home.resolve("moredata.txt");
        boolean renamed = filename1.toFile().renameTo(newFileName.toFile());
        readStdout(Runtime.getRuntime().exec(new String[]{"/bin/sh", "-c", "mv " + home + "/data.txt " + home + "/" + newFileName}));
        byte[] movedSmallFileContents = Serialize.readFully(new FileInputStream(newFileName.toFile()));
        Assert.assertTrue("Correct moved file contents", renamed && Arrays.equals(movedSmallFileContents, data.getBytes()));

        // mkdir
        Path directory = home.resolve("adirectory");
        boolean mkdir = directory.toFile().mkdir();
        boolean dirPresent = Stream.of(home.toFile().listFiles())
                .map(f -> f.getName())
                .filter(n -> n.equals(directory.getFileName().toString()))
                .findAny()
                .isPresent();
        Assert.assertTrue("Mkdir exists", mkdir && dirPresent);

        //move a file to a different directory (calls rename)
        Path inDir = directory.resolve(newFileName.getFileName().toString());
        boolean moved = newFileName.toFile().renameTo(inDir.toFile());
        byte[] movedToDirSmallFileContents = Serialize.readFully(new FileInputStream(inDir.toFile()));
        Assert.assertTrue("Correct file contents after move to another directory", moved && Arrays.equals(movedToDirSmallFileContents, data.getBytes()));
    }

    private void fileTest(int length, Random random)  throws IOException {
        byte[] data = new byte[length];
        random.nextBytes(data);

        String filename = UserTests.randomString();
        Path path = home.resolve(filename);

        Files.write(path, data);

        byte[] contents = Files.readAllBytes(path);

        Assert.assertTrue("Correct file contents for length "+ length, Arrays.equals(data, contents));
    }

//    @Test
//    public void mediumFileTest() throws IOException {
//        int length = 5 * 1024 * 1024 + 256 * 1024;
//        fileTest(length, new Random());
//    }

    @Test
    public void readWriteTest() throws IOException {
        Random  random =  new Random(666); // repeatable with same seed
        for (int power = 5; power < 20; power++) {
            int length =  (int) Math.pow(2, power);
            length +=  random.nextInt(length);
            fileTest(length, random);
        }
    }


    private static void runForAWhile() {
        for (int i=0; i < 600; i++)
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {}
    }

    @AfterClass
    public static void shutdown() {
        fuseProcess.close();
    }
}
<commit_msg>Refactored FuseTests::variousTests to replace remaining  invocations of Runtime.exec  with java.nio operations.<commit_after>package peergos.tests;

import org.junit.*;
import peergos.corenode.*;
import peergos.crypto.*;
import peergos.fuse.*;
import peergos.server.Start;
import peergos.user.*;
import peergos.util.*;

import java.io.*;
import java.lang.*;
import java.lang.reflect.*;
import java.net.*;
import java.nio.file.*;
import java.util.*;
import java.util.function.Supplier;
import java.util.stream.*;

public class FuseTests {
    public static int WEB_PORT = 8888;
    public static int CORE_PORT = 7777;
    public static String username = "test02";
    public static String password = <PASSWORD>;
    public static Path mountPoint, home;
    public static FuseProcess fuseProcess;

    public static void setWebPort(int webPort) {
        WEB_PORT = webPort;
    }

    public static void setCorePort(int corePort) {
        CORE_PORT = corePort;
    }

    static void setFinalStatic(Field field, Object newValue) throws Exception {
        field.setAccessible(true);

        Field modifiersField = Field.class.getDeclaredField("modifiers");
        modifiersField.setAccessible(true);
        modifiersField.setInt(field, field.getModifiers() & ~Modifier.FINAL);

        field.set(null, newValue);
    }

    public static UserContext ensureSignedUp(String username, String password) throws IOException {
        DHTClient.HTTP dht = new DHTClient.HTTP(new URL("http://localhost:"+ WEB_PORT +"/"));
        Btree.HTTP btree = new Btree.HTTP(new URL("http://localhost:"+ WEB_PORT +"/"));
        HTTPCoreNode coreNode = new HTTPCoreNode(new URL("http://localhost:"+ WEB_PORT +"/"));
        UserContext userContext = UserContext.ensureSignedUp(username, password, dht, btree, coreNode);
        return userContext;
    }

    @BeforeClass
    public static void init() throws Exception {
        Random  random  = new Random();
        int offset = random.nextInt(100);
        setWebPort(8888 + offset);
        setCorePort(7777 + offset);

        System.out.println("Using web-port "+ WEB_PORT);
        System.out.flush();
        // use insecure random otherwise tests take ages
        setFinalStatic(TweetNaCl.class.getDeclaredField("prng"), new Random(1));

        Args.parse(new String[]{"useIPFS", "false",
                "-port", Integer.toString(WEB_PORT),
                "-corenodePort", Integer.toString(CORE_PORT)});

        Start.local();
        UserContext userContext = ensureSignedUp(username, password);

        String mountPath = Args.getArg("mountPoint", "/tmp/peergos/tmp");

        mountPoint = Paths.get(mountPath);
        mountPoint = mountPoint.resolve(UUID.randomUUID().toString());
        mountPoint.toFile().mkdirs();
        home = mountPoint.resolve(username);

        System.out.println("\n\nMountpoint "+ mountPoint +"\n\n");
        PeergosFS peergosFS = new PeergosFS(userContext);
        fuseProcess = new FuseProcess(peergosFS, mountPoint);

        Runtime.getRuntime().addShutdownHook(new Thread(()  -> fuseProcess.close()));

        fuseProcess.start();
    }

    public static String readStdout(Process p) throws IOException {
        return new String(Serialize.readFully(p.getInputStream())).trim();
    }

    @Test
    public void variousTests() throws IOException {
        boolean homeExists = Stream.of(mountPoint.toFile().listFiles())
                .map(f -> f.getName())
                .filter(n -> n.equals(username))
                .findAny()
                .isPresent();
        Assert.assertTrue("Correct home directory: " + homeExists, homeExists);

        Path home = mountPoint.resolve(username);

        // write a small file
        Path filename1 = home.resolve("data.txt");
        String msg = "Hello Peergos!";

        Files.write(filename1, msg.getBytes());

        byte[] smallFileContents = Files.readAllBytes(filename1);
        Assert.assertTrue("Correct file contents: " + msg, Arrays.equals(smallFileContents, msg.getBytes()));


        // rename a file
        Path newFileName = home.resolve("moredata.txt");
        boolean updatedFileAlreadyExists = newFileName.toFile().exists();
        Assert.assertFalse("updated file "+  newFileName+" doesn't already exist", updatedFileAlreadyExists);

        Files.move(filename1, newFileName);
        byte[] movedContents  = Files.readAllBytes(newFileName);

        Assert.assertTrue("Correct moved file contents", Arrays.equals(movedContents, msg.getBytes()));

        // mkdir
        Path directory = home.resolve("adirectory");

        Supplier<Boolean> directoryExists = () -> directory.toFile().exists();
        Assert.assertFalse("directory "+ directory +" doesn't already exist", directoryExists.get());

        directory.toFile().mkdir();

        Assert.assertTrue("Mkdir exists", directoryExists.get());

        //move a file to a different directory (calls rename)
        Path inDir = directory.resolve(newFileName.getFileName());
        Supplier<Boolean> inDirExists = () -> inDir.toFile().exists();
        Assert.assertFalse("new file in directory "+ inDir +" doesn't already exist", inDirExists.get());

        Files.move(newFileName, inDir);

        Assert.assertTrue("new file in directory "+ inDir +" exist", inDirExists.get());

        byte[] inDirContents =  Files.readAllBytes(inDir);

        Assert.assertTrue("Correct file contents after move to another directory", Arrays.equals(inDirContents, msg.getBytes()));
    }

    private void fileTest(int length, Random random)  throws IOException {
        byte[] data = new byte[length];
        random.nextBytes(data);

        String filename = UserTests.randomString();
        Path path = home.resolve(filename);

        Files.write(path, data);

        byte[] contents = Files.readAllBytes(path);

        Assert.assertTrue("Correct file contents for length "+ length, Arrays.equals(data, contents));
    }

    @Test
    public void readWriteTest() throws IOException {
        Random  random =  new Random(666); // repeatable with same seed
        for (int power = 5; power < 20; power++) {
            int length =  (int) Math.pow(2, power);
            length +=  random.nextInt(length);
            fileTest(length, random);
        }
    }


    private static void runForAWhile() {
        for (int i=0; i < 600; i++)
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {}
    }

    @AfterClass
    public static void shutdown() {
        fuseProcess.close();
    }
}

{'fraction_non_alphanumeric': 0.12539091586001488, 'fraction_numerical': 0.00766939687267312, 'mean_word_length': 2.836332476435304, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '950779', 'n_tokens_mistral': 3829, 'n_tokens_neox': 3659, 'n_words': 887}