Utils { public static String generateDirectoryHash(String rootDir, String chaincodeDir, String hash) throws IOException { Path projectPath = null; if (rootDir == null) { projectPath = Paths.get(chaincodeDir); } else { projectPath = Paths.get(rootDir, chaincodeDir); } File dir = projectPath.toFile(); if (!dir.exists() || !dir.isDirectory()) { throw new IOException(String.format("The chaincode path \"%s\" is invalid", projectPath)); } StringBuilder hashBuilder = new StringBuilder(hash); Files.walk(projectPath) .sorted(Comparator.naturalOrder()) .filter(Files::isRegularFile) .map(Path::toFile) .forEach(file -> { try { byte[] buf = readFile(file); byte[] toHash = Arrays.concatenate(buf, hashBuilder.toString().getBytes(UTF_8)); hashBuilder.setLength(0); hashBuilder.append(Hex.toHexString(hash(toHash, new SHA3Digest()))); } catch (IOException ex) { throw new RuntimeException(String.format("Error while reading file %s", file.getAbsolutePath()), ex); } }); if (hashBuilder.toString().equals(hash)) { throw new IOException(String.format("The chaincode directory \"%s\" has no files", projectPath)); } return hashBuilder.toString(); } private  Utils(); static String generateParameterHash(String path, String func, List<String> args); static String generateDirectoryHash(String rootDir, String chaincodeDir, String hash); static byte[] generateTarGz(File sourceDirectory, String pathPrefix); static byte[] readFile(File input); static String generateUUID(); static Timestamp generateTimestamp(); static void deleteFileOrDirectory(File file); static byte[] hash(byte[] input, Digest digest); static String combinePaths(String first, String... other); static byte[] readFileFromClasspath(String fileName); static Properties parseGrpcUrl(String url); static Exception checkGrpcUrl(String url); static boolean isNullOrEmpty(String url); static String logString(final String string); static byte[] generateNonce(); static String toHexString(ByteString byteString); static String toHexString(byte[] bytes);  }
@Test public void testGenerateDirectoryHash() throws Exception { doGenerateDirectoryHash(false, false); }
@Test(expected = IOException.class) public void testGenerateDirectoryHashNoDirectory() throws Exception { File rootDir = tempFolder.getRoot().getAbsoluteFile(); File nonExistentDir = new File(rootDir, "temp"); Utils.generateDirectoryHash(null, nonExistentDir.getAbsolutePath(), ""); Assert.fail("Expected an IOException as the directory does not exist"); }
@Test(expected = IOException.class) public void testGenerateDirectoryHashEmptyDirectory() throws Exception { File emptyDir = tempFolder.newFolder("subfolder"); Utils.generateDirectoryHash(null, emptyDir.getAbsolutePath(), ""); Assert.fail("Expected an IOException as the directory is empty"); }
@Test(expected = IOException.class) public void testGenerateDirectoryHashWithFile() throws Exception { File tempFile = tempFolder.newFile("temp.txt"); Utils.generateDirectoryHash(null, tempFile.getAbsolutePath(), ""); Assert.fail("Expected an IOException as we passed it a file"); }