public static String getSize(AttachmentProxy a, ReadableDatabase database) {
    if (a.getFile() != null) {
      int size = (int) a.getFile().length();
      return getSizeInKb(size);
    }

    AttachmentPacket packet = a.getPendingPacket();
    if (packet != null) {
      return getSizeInKb(packet.getFileSize());
    }

    int size = 0;
    UniversalId id = a.getUniversalId();
    try {
      DatabaseKey key = DatabaseKey.createMutableKey(id);
      if (!database.doesRecordExist(key)) key = DatabaseKey.createImmutableKey(id);
      if (!database.doesRecordExist(key)) return "";
      size = database.getRecordSize(key);
    } catch (Exception e) {
      e.printStackTrace();
    }

    size -= 1024; // Public code & overhead
    size = size * 3 / 4; // Base64 overhead
    return getSizeInKb(size);
  }
  public boolean equals(Object otherObject) {
    if (this == otherObject) return true;

    if (otherObject instanceof DatabaseKey) {
      DatabaseKey otherKey = (DatabaseKey) otherObject;
      return getString().equals(otherKey.getString());
    }

    return false;
  }
  public void testGetBulletinUploadRecord() throws Exception {
    String burNotFound = server.getBulletinUploadRecord(bhp1.getAccountId(), bhp1.getLocalId());
    assertNull("found bur?", burNotFound);

    String expectedBur =
        BulletinUploadRecord.createBulletinUploadRecord(bhp1.getLocalId(), server.getSecurity());
    DatabaseKey headerKey = bhp1.createKeyWithHeaderStatus(bhp1.getUniversalId());
    String bulletinLocalId = headerKey.getLocalId();
    BulletinUploadRecord.writeSpecificBurToDatabase(
        coreServer.getWriteableDatabase(), bhp1, expectedBur);
    String bur1 = server.getBulletinUploadRecord(bhp1.getAccountId(), bulletinLocalId);
    assertNotNull("didn't find bur1?", bur1);
    assertEquals("wrong bur?", expectedBur, bur1);
  }
  public boolean doWeWantThis(BulletinMirroringInformation mirroringInfo) {
    // TODO handle delete requests when we are propagating deletes,
    DatabaseKey key = getDatabaseKey(mirroringInfo);
    if (store.isHidden(key)) return false;

    UniversalId uid = mirroringInfo.getUid();
    DatabaseKey sealedKey = DatabaseKey.createSealedKey(uid);
    if (store.doesBulletinRevisionExist(sealedKey)) return false;

    try {
      if (mirroringInfo.isSealed()) return (!store.doesBulletinRevisionExist(key));

      if (store.doesBulletinDelRecordExist(DeleteRequestRecord.getDelKey(uid))) {
        DeleteRequestRecord delRecord =
            new DeleteRequestRecord(store.getDatabase(), uid, store.getSignatureVerifier());
        if (delRecord.isBefore(mirroringInfo.mTime)) return true;
        return false;
      }

      if (!store.doesBulletinRevisionExist(key)) return true;

      long currentBulletinsmTime = store.getDatabase().getmTime(key);
      if (mirroringInfo.getmTime() > currentBulletinsmTime) return true;
      return false;
    } catch (Exception e) {
      logError(e.getMessage(), e);
    }
    return false;
  }
  public void testRunReport() throws Exception {
    MockMartusApp app = MockMartusApp.create();
    app.getLocalization().setCurrentLanguageCode(MiniLocalization.ENGLISH);
    app.loadSampleData();
    BulletinStore store = app.getStore();
    ReportFormat rf = new ReportFormat();
    rf.setDetailSection("$i. $bulletin.localId\n");
    ReportOutput result = new ReportOutput();
    Set leafUids = store.getAllBulletinLeafUids();
    SortableBulletinList list =
        new SortableBulletinList(app.getLocalization(), new MiniFieldSpec[0]);
    Iterator it = leafUids.iterator();
    while (it.hasNext()) {
      DatabaseKey key = DatabaseKey.createLegacyKey((UniversalId) it.next());
      list.add(BulletinLoader.loadFromDatabase(store.getDatabase(), key, app.getSecurity()));
    }

    RunReportOptions options = new RunReportOptions();
    rr.runReport(
        rf, store.getDatabase(), list, result, options, PoolOfReusableChoicesLists.EMPTY_POOL);
    result.close();
    StringBuffer expected = new StringBuffer();
    UniversalId[] uids = list.getSortedUniversalIds();
    for (int i = 0; i < uids.length; ++i) {
      expected.append(Integer.toString(i + 1));
      expected.append(". ");
      expected.append(uids[i].getLocalId());
      expected.append("\n");
    }
    assertEquals(new String(expected), result.getPageText(0));
  }
  Vector writeSampleAvailableIDPacket(BulletinHeaderPacket bhp) throws Exception {

    StringWriter writer = new StringWriter();
    byte[] sigBytes = bhp.writeXml(writer, authorSecurity);
    DatabaseKey key = null;
    if (bhp.getStatus().equals(BulletinConstants.STATUSDRAFT))
      key = DatabaseKey.createDraftKey(bhp.getUniversalId());
    else key = DatabaseKey.createSealedKey(bhp.getUniversalId());

    String sigString = StreamableBase64.encode(sigBytes);
    MockDatabase db = new MockServerDatabase();
    db.writeRecord(key, "Some text");
    supplier.addAvailableIdsToMirror(db, key, sigString);

    BulletinMirroringInformation bulletinInfo =
        new BulletinMirroringInformation(db, key, sigString);
    Vector info = bulletinInfo.getInfoWithLocalId();
    return info;
  }
  private void internalTestFindLegacyRecords(Database db) throws Exception {
    MartusCrypto security = MockMartusSecurity.createClient();

    UniversalId uid = UniversalIdForTesting.createDummyUniversalId();
    DatabaseKey legacyKey = DatabaseKey.createLegacyKey(uid);
    db.writeRecord(legacyKey, smallString);
    InputStream inLegacy = db.openInputStream(legacyKey, security);
    assertNotNull("legacy not found?", inLegacy);
    inLegacy.close();

    InputStream inDraft = db.openInputStream(legacyKey, security);
    assertNotNull("draft not found?", inDraft);
    inDraft.close();

    DatabaseKey sealedKey = DatabaseKey.createSealedKey(uid);
    InputStream inSealed = db.openInputStream(sealedKey, security);
    assertNotNull("sealed not found?", inSealed);
    inSealed.close();

    db.deleteAllData();
  }
  Vector writeSampleHeaderPacket(BulletinHeaderPacket bhp) throws Exception {
    StringWriter writer = new StringWriter();
    byte[] sigBytes = bhp.writeXml(writer, authorSecurity);
    DatabaseKey key = DatabaseKey.createSealedKey(bhp.getUniversalId());
    if (bhp.getStatus().equals(BulletinConstants.STATUSDRAFT)) return null;
    String sigString = StreamableBase64.encode(sigBytes);
    supplier.addBulletinToMirror(key, sigString);

    Vector info = new Vector();
    info.add(bhp.getLocalId());
    info.add(sigString);
    return info;
  }
  private void internalTestSealeds() {
    ReadableDatabase db = coreServer.getDatabase();
    MockServerDatabase mdb = (MockServerDatabase) db;
    assertEquals(6, mdb.getRecordCount());
    Set allKeys = mdb.getAllKeys();
    int drafts = 0;
    int sealeds = 0;
    for (Iterator iter = allKeys.iterator(); iter.hasNext(); ) {
      DatabaseKey key = (DatabaseKey) iter.next();
      if (key.isMutable()) ++drafts;
      else ++sealeds;
    }
    assertEquals(5, sealeds);
    assertEquals(1, drafts);

    String publicKeyString1 = clientSecurity1.getPublicKeyString();
    Vector result1 = new Vector(server.listAvailableIdsForMirroring(publicKeyString1));
    assertEquals(2, result1.size());
    Vector ids1 = new Vector();
    ids1.add(((Vector) result1.get(0)).get(0));
    ids1.add(((Vector) result1.get(1)).get(0));
    assertContains(bhp1.getLocalId(), ids1);
    assertContains(bhp2.getLocalId(), ids1);
  }
Beispiel #10
0
  public AttachmentProxy getAsFileProxy(
      AttachmentProxy ap, ReadableDatabase otherDatabase, String status)
      throws IOException, CryptoException, InvalidPacketException, SignatureVerificationException,
          WrongPacketTypeException, InvalidBase64Exception {
    if (ap.getFile() != null) return ap;
    if (otherDatabase == null) return ap;

    DatabaseKey key = DatabaseKey.createKey(ap.getUniversalId(), status);
    InputStreamWithSeek packetIn = otherDatabase.openInputStream(key, security);
    if (packetIn == null) return ap;

    try {
      return AttachmentProxy.createFileProxyFromAttachmentPacket(packetIn, ap, security);
    } finally {
      packetIn.close();
    }
  }
  protected void setUp() throws Exception {
    super.setUp();
    logger = new LoggerToNull();
    MockMartusSecurity serverSecurity = MockMartusSecurity.createServer();
    coreServer = new MockMartusServer(this);
    coreServer.setSecurity(serverSecurity);
    server = new ServerForMirroring(coreServer, logger);

    clientSecurity1 = MockMartusSecurity.createClient();
    clientSecurity2 = MockMartusSecurity.createOtherClient();

    Database db = coreServer.getWriteableDatabase();

    bhp1 = new BulletinHeaderPacket(clientSecurity1);
    bhp1.setStatus(BulletinConstants.STATUSIMMUTABLE);
    DatabaseKey key1 = bhp1.createKeyWithHeaderStatus(bhp1.getUniversalId());
    bhp1.writeXmlToDatabase(db, key1, false, clientSecurity1);

    bhp2 = new BulletinHeaderPacket(clientSecurity1);
    bhp2.setStatus(BulletinConstants.STATUSIMMUTABLE);
    DatabaseKey key2 = bhp2.createKeyWithHeaderStatus(bhp2.getUniversalId());
    bhp2.writeXmlToDatabase(db, key2, false, clientSecurity1);

    bhp3 = new BulletinHeaderPacket(clientSecurity2);
    bhp3.setStatus(BulletinConstants.STATUSIMMUTABLE);
    DatabaseKey key3 = bhp3.createKeyWithHeaderStatus(bhp3.getUniversalId());
    bhp3.writeXmlToDatabase(db, key3, false, clientSecurity2);

    bhp4 = new BulletinHeaderPacket(clientSecurity2);
    bhp4.setStatus(BulletinConstants.STATUSMUTABLE);
    DatabaseKey key4 = bhp4.createKeyWithHeaderStatus(bhp4.getUniversalId());
    bhp4.writeXmlToDatabase(db, key4, false, clientSecurity2);

    UniversalId fdpUid = FieldDataPacket.createUniversalId(clientSecurity1);
    FieldSpec[] tags = {LegacyCustomFields.createFromLegacy("whatever")};
    FieldDataPacket fdp1 = new FieldDataPacket(fdpUid, new FieldSpecCollection(tags));
    fdp1.writeXmlToClientDatabase(db, false, clientSecurity1);

    UniversalId otherPacketId =
        UniversalIdForTesting.createFromAccountAndPrefix(clientSecurity2.getPublicKeyString(), "X");
    DatabaseKey key = DatabaseKey.createImmutableKey(otherPacketId);
    db.writeRecord(key, "Not a valid packet");
  }
 private ReportOutput runReportOnAppData(
     ReportFormat rf, MockMartusApp app, RunReportOptions options, MiniFieldSpec[] sortSpecs)
     throws IOException, DamagedBulletinException, NoKeyPairException, Exception {
   BulletinStore store = app.getStore();
   MartusCrypto security = app.getSecurity();
   ReadableDatabase db = store.getDatabase();
   Set leafUids = store.getAllBulletinLeafUids();
   MiniLocalization localization = new MiniLocalization();
   localization.setCurrentLanguageCode(MiniLocalization.ENGLISH);
   SortableBulletinList list = new SortableBulletinList(localization, sortSpecs);
   Iterator it = leafUids.iterator();
   while (it.hasNext()) {
     DatabaseKey key = DatabaseKey.createLegacyKey((UniversalId) it.next());
     Bulletin b = BulletinLoader.loadFromDatabase(db, key, security);
     list.add(b);
   }
   ReportOutput result = new ReportOutput();
   rr.runReport(
       rf, store.getDatabase(), list, result, options, PoolOfReusableChoicesLists.EMPTY_POOL);
   result.close();
   return result;
 }
 public static DatabaseKey createLegacyKey(UniversalId uidToUse) {
   DatabaseKey key = new DatabaseKey(uidToUse);
   key.setImmutable();
   return key;
 }
 private DatabaseKey getDatabaseKey(BulletinMirroringInformation mirroringInfo) {
   DatabaseKey key = null;
   if (mirroringInfo.isSealed()) key = DatabaseKey.createSealedKey(mirroringInfo.getUid());
   else if (mirroringInfo.isDraft()) key = DatabaseKey.createDraftKey(mirroringInfo.getUid());
   return key;
 }