Set correct permissions for moved certificates, added missing test changes.

This commit is contained in:
Felix Ableitner 2014-07-22 18:01:13 +02:00
parent f7952aa9ce
commit 387202d92d
5 changed files with 47 additions and 12 deletions

View file

@ -4,8 +4,8 @@ import android.test.AndroidTestCase;
import android.test.suitebuilder.annotation.SmallTest; import android.test.suitebuilder.annotation.SmallTest;
import android.widget.TextView; import android.widget.TextView;
import com.nutomic.zertman.Certificate; import com.nutomic.zertman.*;
import com.nutomic.zertman.CertificateAdapter; import com.nutomic.zertman.R;
import java.util.ArrayList; import java.util.ArrayList;
@ -19,7 +19,9 @@ public class CertificateAdapterTest extends AndroidTestCase {
protected void setUp() throws Exception { protected void setUp() throws Exception {
super.setUp(); super.setUp();
mCertificateAdapter = new CertificateAdapter(getContext()); CertificateManager cm = new CertificateManager();
MovedCertificatesStorage mv = new MovedCertificatesStorage(getContext());
mCertificateAdapter = new CertificateAdapter(getContext(), cm, mv);
mTestCertificates.add(new Certificate("first", false)); mTestCertificates.add(new Certificate("first", false));
mTestCertificates.add(new Certificate("second", true)); mTestCertificates.add(new Certificate("second", true));
mCertificateAdapter.addAll(mTestCertificates); mCertificateAdapter.addAll(mTestCertificates);
@ -29,7 +31,7 @@ public class CertificateAdapterTest extends AndroidTestCase {
public void testAdapter() { public void testAdapter() {
for (int i = 0; i < mTestCertificates.size(); i++) { for (int i = 0; i < mTestCertificates.size(); i++) {
TextView tv1 = (TextView) mCertificateAdapter.getView(i, null, null) TextView tv1 = (TextView) mCertificateAdapter.getView(i, null, null)
.findViewById(android.R.id.text1); .findViewById(R.id.title);
assertTrue(tv1.getText().equals(mTestCertificates.get(i).getFile().getName())); assertTrue(tv1.getText().equals(mTestCertificates.get(i).getFile().getName()));
} }
} }

View file

@ -3,15 +3,20 @@ package com.nutomic.zertman.test;
import android.test.AndroidTestCase; import android.test.AndroidTestCase;
import android.test.suitebuilder.annotation.MediumTest; import android.test.suitebuilder.annotation.MediumTest;
import android.test.suitebuilder.annotation.SmallTest; import android.test.suitebuilder.annotation.SmallTest;
import android.util.Log;
import com.nutomic.zertman.Certificate; import com.nutomic.zertman.Certificate;
import com.nutomic.zertman.CertificateManager; import com.nutomic.zertman.CertificateManager;
import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.File; import java.io.File;
import java.io.FileNotFoundException; import java.io.FileNotFoundException;
import java.io.FileOutputStream; import java.io.FileOutputStream;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.List; import java.util.List;
import eu.chainfire.libsuperuser.Shell; import eu.chainfire.libsuperuser.Shell;
@ -51,12 +56,46 @@ public class CertificateManagerTest extends AndroidTestCase {
assertNotNull(newCertificate); assertNotNull(newCertificate);
assertEquals(newCertificate.getFile().getName(), cert.getFile().getName()); assertEquals(newCertificate.getFile().getName(), cert.getFile().getName());
assertNotSame(newCertificate.getFile(), cert.getFile()); assertNotSame(newCertificate.getFile(), cert.getFile());
assertPermissions(newCertificate.getFile(), "-rw-r--r--");
assertFalse(mCertificateManager.getCertificates(false).contains(cert)); assertFalse(mCertificateManager.getCertificates(false).contains(cert));
assertTrue(mCertificateManager.getCertificates(true).contains(newCertificate)); assertTrue(mCertificateManager.getCertificates(true).contains(newCertificate));
assertTrue(mCertificateManager.deleteCertificate(newCertificate)); assertTrue(mCertificateManager.deleteCertificate(newCertificate));
assertReadOnly(); assertReadOnly();
} }
/**
* Asserts that file has the given permissions.
*/
private void assertPermissions(File file, String expectedPermissions) {
Process process;
DataOutputStream dos = null;
InputStreamReader isr = null;
BufferedReader br = null;
try {
process = Runtime.getRuntime().exec("sh");
dos = new DataOutputStream(process.getOutputStream());
isr = new InputStreamReader(process.getInputStream());
dos.writeBytes("ls -l " + file.getAbsolutePath() + "\n");
dos.writeBytes("exit\n");
br = new BufferedReader(isr);
assertTrue(br.readLine().startsWith(expectedPermissions));
dos.flush();
}
catch (IOException e) {
fail();
}
finally {
try {
dos.close();
br.close();
isr.close();
}
catch (IOException e) {
fail();
}
}
}
@SmallTest @SmallTest
public void testSystemCertificates() { public void testSystemCertificates() {
Certificate cert = copyCertificate(true); Certificate cert = copyCertificate(true);

View file

@ -31,11 +31,6 @@ public class MovedCertificatesStorageTest extends AndroidTestCase {
mMovedCertificatesStorage.close(); mMovedCertificatesStorage.close();
} }
@SmallTest
public void testInvalidInsert() {
assertFalse(mMovedCertificatesStorage.insert(new Certificate("user", false)));
}
@SmallTest @SmallTest
public void testInvalidDelete() { public void testInvalidDelete() {
assertFalse(mMovedCertificatesStorage.delete(new Certificate("cert", true))); assertFalse(mMovedCertificatesStorage.delete(new Certificate("cert", true)));

View file

@ -53,6 +53,7 @@ public class CertificateManager {
// NOTE: Using mv gives error: "failed on *file* - Cross-device link". // NOTE: Using mv gives error: "failed on *file* - Cross-device link".
run("cp " + USER_CERTIFICATES_DIR + "/" + certificate.getFile().getName() + run("cp " + USER_CERTIFICATES_DIR + "/" + certificate.getFile().getName() +
" " + SYSTEM_CERTIFICATES_DIR + "/" + certificate.getFile().getName()); " " + SYSTEM_CERTIFICATES_DIR + "/" + certificate.getFile().getName());
run("chmod 644 " + SYSTEM_CERTIFICATES_DIR + "/" + certificate.getFile().getName());
remountSystem(Mode.ReadOnly); remountSystem(Mode.ReadOnly);
deleteCertificate(certificate); deleteCertificate(certificate);
return new Certificate(certificate.getFile().getName(), true); return new Certificate(certificate.getFile().getName(), true);

View file

@ -32,8 +32,6 @@ public class MainActivity extends ListActivity {
new CertificateAdapter(this, mCertificateManager, mMovedCertificatesStorage); new CertificateAdapter(this, mCertificateManager, mMovedCertificatesStorage);
mCertificateAdapter.addAll(mCertificateManager.getCertificates(false)); mCertificateAdapter.addAll(mCertificateManager.getCertificates(false));
// FIXME: nothing listed
// -> db table is empty
mCertificateAdapter.addAll(mMovedCertificatesStorage.list()); mCertificateAdapter.addAll(mMovedCertificatesStorage.list());
mListView.setAdapter(mCertificateAdapter); mListView.setAdapter(mCertificateAdapter);
} }