Skip to content

Commit

Permalink
Merge pull request #1593 from dcrewi/feature/test-html-exports
Browse files Browse the repository at this point in the history
add test for html exports
  • Loading branch information
alexbakker authored Jan 24, 2025
2 parents ad2dc80 + 919e685 commit 6bbb42f
Showing 1 changed file with 33 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -61,13 +61,20 @@
import org.junit.rules.RuleChain;
import org.junit.rules.TestRule;
import org.junit.runner.RunWith;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;
import org.xmlpull.v1.XmlPullParserFactory;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.nio.charset.StandardCharsets;
import java.util.Collection;
import java.util.List;
import java.util.Locale;

import javax.crypto.Cipher;
import javax.crypto.SecretKey;
Expand Down Expand Up @@ -183,7 +190,9 @@ public void testPlainVaultExportHtml() {
onView(withText(R.string.export_format_html)).inRoot(RootMatchers.isPlatformPopup()).perform(click());
onView(withId(android.R.id.button1)).perform(click());
onView(withId(R.id.checkbox_accept)).perform(click());
doExport();
File file = doExport();

checkHtmlExport(file);
}

@Test
Expand All @@ -196,7 +205,9 @@ public void testEncryptedVaultExportHtml() {
onView(withText(R.string.export_format_html)).inRoot(RootMatchers.isPlatformPopup()).perform(click());
onView(withId(android.R.id.button1)).perform(click());
onView(withId(R.id.checkbox_accept)).perform(click());
doExport();
File file = doExport();

checkHtmlExport(file);
}

@Test
Expand Down Expand Up @@ -380,6 +391,26 @@ private void readTxtExport(File file) {
checkReadEntries(entries);
}

private void checkHtmlExport(File file) {
try (InputStream inStream = new FileInputStream(file)) {
Reader inReader = new InputStreamReader(inStream, StandardCharsets.UTF_8);
XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
XmlPullParser parser = factory.newPullParser();
parser.setInput(inReader);
while (parser.getEventType() != XmlPullParser.START_TAG) {
parser.next();
}
if (!parser.getName().toLowerCase(Locale.ROOT).equals("html")) {
throw new RuntimeException("not an html document!");
}
while (parser.getEventType() != XmlPullParser.END_DOCUMENT) {
parser.next();
}
} catch (IOException | XmlPullParserException e) {
throw new RuntimeException("Unable to read html export file", e);
}
}

private void checkReadEntries(Collection<VaultEntry> entries) {
List<VaultEntry> vectors = VaultEntries.get();
assertEquals(vectors.size(), entries.size());
Expand Down

0 comments on commit 6bbb42f

Please sign in to comment.