commit 2f9c942ca1a325d11b3ba063b8c5711e47cab85e Author: Karsten Loesing karsten.loesing@gmx.net Date: Sat Dec 3 20:49:22 2016 +0100
Add tests for descriptor reader. --- .../descriptor/impl/DescriptorReaderImplTest.java | 151 +++++++++++++++++++++ .../fafaa9366f010db805de13a4b7348aba2acb6f17 | 30 ++++ .../ffe08f10c0ca5198f7cfa8787651bee538f4f7e0 | 50 +++++++ 3 files changed, 231 insertions(+)
diff --git a/src/test/java/org/torproject/descriptor/impl/DescriptorReaderImplTest.java b/src/test/java/org/torproject/descriptor/impl/DescriptorReaderImplTest.java new file mode 100644 index 0000000..d5b1572 --- /dev/null +++ b/src/test/java/org/torproject/descriptor/impl/DescriptorReaderImplTest.java @@ -0,0 +1,151 @@ +/* Copyright 2016 The Tor Project + * See LICENSE for licensing information */ +package org.torproject.descriptor.impl; + +import static org.junit.Assert.assertEquals; + +import org.torproject.descriptor.DescriptorFile; +import org.torproject.descriptor.DescriptorReader; + +import org.junit.Before; +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.TemporaryFolder; + +import java.io.File; +import java.io.IOException; +import java.nio.charset.StandardCharsets; +import java.nio.file.Files; +import java.util.Iterator; +import java.util.SortedMap; +import java.util.TreeMap; + +/** Tests the descriptor reader by preparing a temporary folder with two input + * descriptor files and a parse history file, running the reader with different + * configurations, and verifying the reader's state afterwards. */ +public class DescriptorReaderImplTest { + + /** Temporary folder containing all files for this test. */ + @Rule + public TemporaryFolder temporaryFolder = new TemporaryFolder(); + + /** Directory containing two input descriptor files. */ + private File inputDirectory; + + /** Parse history map. */ + private SortedMap<String, Long> historyMap; + + /** Parse history file. */ + private File historyFile; + + /** Descriptor reader used in this test. */ + private DescriptorReader descriptorReader = new DescriptorReaderImpl(); + + /** Prepares the temporary folder and writes files to it for this test. */ + @Before + public void createTemporaryFolderAndContents() throws IOException { + this.inputDirectory = this.temporaryFolder.newFolder("in"); + File fafaFile = new File(this.inputDirectory, "fafa"); + File ffe0File = new File(this.inputDirectory, "ffe0"); + Files.copy(getClass().getClassLoader().getResource( + "fafaa9366f010db805de13a4b7348aba2acb6f17").openStream(), + fafaFile.toPath()); + Files.copy(getClass().getClassLoader().getResource( + "ffe08f10c0ca5198f7cfa8787651bee538f4f7e0").openStream(), + ffe0File.toPath()); + this.historyMap = new TreeMap<>(); + this.historyMap.put(fafaFile.getAbsolutePath(), fafaFile.lastModified()); + String parseHistoryContents = String.format("%d %s%n", + fafaFile.lastModified(), fafaFile.getAbsolutePath()); + this.historyFile = this.temporaryFolder.newFile("history"); + Files.write(this.historyFile.toPath(), + parseHistoryContents.getBytes(StandardCharsets.UTF_8)); + } + + private void readAllDescriptors() { + Iterator<DescriptorFile> descriptorFiles = + this.descriptorReader.readDescriptors(); + while (descriptorFiles.hasNext()) { + descriptorFiles.next(); + } + } + + private void assertExcludedFilesParsedFilesAndHistoryFileLines( + int expectedExcludedFiles, int expectedParsedFiles, + int expectedHistoryFileLines) throws IOException { + assertEquals(expectedExcludedFiles, + this.descriptorReader.getExcludedFiles().size()); + assertEquals(expectedParsedFiles, + this.descriptorReader.getParsedFiles().size()); + assertEquals(expectedHistoryFileLines, + Files.readAllLines(this.historyFile.toPath(), + StandardCharsets.UTF_8).size()); + } + + @Test + public void testDescriptors() throws IOException { + this.descriptorReader.addDirectory(this.inputDirectory); + this.readAllDescriptors(); + this.assertExcludedFilesParsedFilesAndHistoryFileLines(0, 2, 1); + } + + @Test + public void testNoDescriptors() throws IOException { + this.readAllDescriptors(); + this.assertExcludedFilesParsedFilesAndHistoryFileLines(0, 0, 1); + } + + @Test + @SuppressWarnings("deprecation") + public void testSetExcludeFilesDescriptors() throws InterruptedException, + IOException { + this.descriptorReader.setExcludeFiles(this.historyFile); + this.descriptorReader.addDirectory(this.inputDirectory); + this.readAllDescriptors(); + Thread.sleep(100L); /* It may take a moment to write the history file. */ + this.assertExcludedFilesParsedFilesAndHistoryFileLines(1, 1, 2); + } + + @Test + @SuppressWarnings("deprecation") + public void testSetExcludeFilesNoDescriptors() throws InterruptedException, + IOException { + this.descriptorReader.setExcludeFiles(this.historyFile); + this.readAllDescriptors(); + Thread.sleep(100L); /* It may take a moment to write the history file. */ + this.assertExcludedFilesParsedFilesAndHistoryFileLines(0, 0, 0); + } + + @Test + public void testSetHistoryFileDescriptors() throws IOException { + this.descriptorReader.setHistoryFile(this.historyFile); + this.descriptorReader.addDirectory(this.inputDirectory); + this.readAllDescriptors(); + descriptorReader.saveHistoryFile(this.historyFile); + this.assertExcludedFilesParsedFilesAndHistoryFileLines(1, 1, 2); + } + + @Test + public void testSetHistoryFileNoDescriptors() throws IOException { + this.descriptorReader.setHistoryFile(this.historyFile); + this.readAllDescriptors(); + this.descriptorReader.saveHistoryFile(this.historyFile); + this.assertExcludedFilesParsedFilesAndHistoryFileLines(0, 0, 0); + } + + @Test + public void testSetExcludedFilesDescriptors() throws IOException { + this.descriptorReader.setExcludedFiles(this.historyMap); + this.descriptorReader.addDirectory(this.inputDirectory); + this.readAllDescriptors(); + this.assertExcludedFilesParsedFilesAndHistoryFileLines(1, 1, 1); + } + + @Test + public void testSetExcludedFilesNoDescriptors() throws IOException { + this.descriptorReader.setExcludedFiles(this.historyMap); + this.readAllDescriptors(); + this.assertExcludedFilesParsedFilesAndHistoryFileLines(0, 0, 1); + } +} + diff --git a/src/test/resources/fafaa9366f010db805de13a4b7348aba2acb6f17 b/src/test/resources/fafaa9366f010db805de13a4b7348aba2acb6f17 new file mode 100644 index 0000000..143c348 --- /dev/null +++ b/src/test/resources/fafaa9366f010db805de13a4b7348aba2acb6f17 @@ -0,0 +1,30 @@ +@type server-descriptor 1.0 +router kouettng 163.172.27.62 9001 0 9030 +platform Tor 0.2.4.27 on Linux +protocols Link 1 2 Circuit 1 +published 2016-12-01 05:34:06 +fingerprint 1B95 9E07 DF0C 33C1 E956 C8D8 FA41 5AB5 2F07 6FB7 +uptime 21297741 +bandwidth 5120000 10240000 6494197 +extra-info-digest EC116B121B2E59095003761F49004CACA0C30692 +onion-key +-----BEGIN RSA PUBLIC KEY----- +MIGJAoGBALlqdCYEaNqaCtZgExgbsEaUPpcbSxVKFHg0GQQyX///WxzpXNliaIWq +npKXhsTyW2Bn1gcilvYXY/kLx8LV8c5QnolrJUClL3IQp0Mbt+VPk+rG8QfxqltV +bcXKh2sgRCtS1oWlnuaVlbjz+y/xpjNGxI3ejKc1ttvJ+RMH0fAHAgMBAAE= +-----END RSA PUBLIC KEY----- +signing-key +-----BEGIN RSA PUBLIC KEY----- +MIGJAoGBAK7E60fMuLIln5nJtDV2ldKw+bulhV4MrD634RpleWc6gMFgEulZmEy1 +8aCEtFk8OpbCHjFb6+ppsNmvZGmQ15lvYe3B7n92cYDe4y+ChVs3MGOkT7k+PjVu +pujkT428JsG1T5yfiagLxWJkUqP/pG5SHNpgKEViGaPhY1WG5gW7AgMBAAE= +-----END RSA PUBLIC KEY----- +hidden-service-dir +ntor-onion-key 9LojUfBId8wzCfFwgWrTKB1l3m3hwCJ1MJFD/sugM2U= +reject *:* +router-signature +-----BEGIN SIGNATURE----- +Z4I3KQDvcAhJsnhSiAj/Y0Mf7AJgYDtPnKR7ToPqsRxG2HqtOmK9LdJPK1oeJO3F +k1fNdhXaEsaRYkzwrdchyRUgiY2GBOuWpCsz00NDwVPpuv8FFGwVXwNhiTOmU6uW +f/5FMuIDdB8PqMJ69UvGPRg2EXBmuPIUqEsISw6ut7I= +-----END SIGNATURE----- diff --git a/src/test/resources/ffe08f10c0ca5198f7cfa8787651bee538f4f7e0 b/src/test/resources/ffe08f10c0ca5198f7cfa8787651bee538f4f7e0 new file mode 100644 index 0000000..fa5f0cd --- /dev/null +++ b/src/test/resources/ffe08f10c0ca5198f7cfa8787651bee538f4f7e0 @@ -0,0 +1,50 @@ +@type server-descriptor 1.0 +router furrygame 94.242.222.217 13092 0 30290 +identity-ed25519 +-----BEGIN ED25519 CERT----- +AQQABkkTAR0R1n6VRb7QkovMZ+OusX4bef/HnXdt0H81hkBRAxKiAQAgBADX6pg7 +pPLS47bkSTGXPwz+jUQZvJrzjMPZSz7dmHB2sihGOwFr5/gZ7nvRbV+qbdmQfUVI +tNfUeAdnTKhjFEziSN6PbEmlWNJBFJKrOJVN5+ERu+8tFM505XZ4S5WmlgQ= +-----END ED25519 CERT----- +master-key-ed25519 1+qYO6Ty0uO25Ekxlz8M/o1EGbya84zD2Us+3ZhwdrI +platform Tor 0.2.7.6 on Linux +protocols Link 1 2 Circuit 1 +published 2016-12-01 05:00:12 +fingerprint 1531 9513 EEF1 F22E E864 BD65 02BC 3570 0912 6586 +uptime 237671 +bandwidth 1024000 2048000 1219736 +extra-info-digest 686607A7AAAD4114A79D98DB5565AB87F63370DC kr42DOkso/qzzOGNGjHG2YWBaJ9xHZR6Uw0LxH54aQA +onion-key +-----BEGIN RSA PUBLIC KEY----- +MIGJAoGBAKRdqE8SW8dH9yDyvwLzNOFoCbzh32q+hkuaUDE6DEq7vdjMcIw0zk6K +sRnJ3nshGyrg3hNR+szL8aej4UsKW3NLMXmEekaI714+Moh8GxDuS9IyHSoQd4fZ +muLD/J9xZzNaEaGDumH9HP2ehIYnzHXGA4c75+lcyaK1PSa0vCetAgMBAAE= +-----END RSA PUBLIC KEY----- +signing-key +-----BEGIN RSA PUBLIC KEY----- +MIGJAoGBAL3guVgxitCW1ruuUlMLk4MCc8vxTniyvHjbBM9ovmzkYglXc/YiHss+ +5h/ovHbF37A5inKvKP9eA5yhS1AuNOssf8wjQHxfZIkiXW8px+7ULcuDacsJA/TH +dPHciCWO/K8iFIJr7g16sHciXz4tmaQ2HYWCHefV6WSYahWt2KgZAgMBAAE= +-----END RSA PUBLIC KEY----- +onion-key-crosscert +-----BEGIN CROSSCERT----- +mqzk1B7XIf5NggmpGzCnOBmuRxNc4RnMRpKSSvOjG+X4OoBbUXiaLDBvO+llD0PK +lGrMec7KGbCbbRGfjVwWOail9x/O6rpldS1JFRTaDUfBNfP0WTWwt/lncpX1LqeN +wFBoAySa/yOPfO9ZAuDFArKoOM1Ft2yXzLm1HP8kf5Q= +-----END CROSSCERT----- +ntor-onion-key-crosscert 0 +-----BEGIN ED25519 CERT----- +AQoABkcuAdfqmDuk8tLjtuRJMZc/DP6NRBm8mvOMw9lLPt2YcHayAAIwXxeXxgCc +mcrTnpPccnuiRK222AyWNaGXT7/xcspVt4rR1iyo44P0Z/gS0BDwefaHQjLWXUTq +pOZUVj94ygM= +-----END ED25519 CERT----- +hidden-service-dir +ntor-onion-key EJIyF9uU6SqdqLJOTU9XoP4H/vKCFbr9xCigdoQAOQY= +reject *:* +router-sig-ed25519 CkfG+ZTC6GfVNdeew765sqcWu8zmWuXgMlGxP/f6Ihd9jnJPMzVNuE8Im13QALNAP8Sh0tpAABa6WKmxycXyDg +router-signature +-----BEGIN SIGNATURE----- +I4cNr+Ax6/IIvOUbewQgj07L9b6YoOkO23LaLqaX67bQNYNUgMNp21M0QogI7zkz +zwMdgbMN6PIgn9tRIO+jfDNr6j768si8cAilwiuSz9Qkz8mlExoNIW6uGsh8NI2l +73f91vRyzFBDJpUXNCrwcEE6uyaOgq+iLgpoHSguHEQ= +-----END SIGNATURE-----