Search in sources :

Example 1 with UnregisteredObjectException

use of edu.uiuc.ncsa.security.core.exceptions.UnregisteredObjectException in project OA4MP by ncsa.

the class AssetStoreTest method testUpdate.

public void testUpdate(AssetStore store) throws Exception {
    if (store == null) {
        System.out.println("WARNING: no asset store configured, skipping test.");
        return;
    }
    SecureRandom secureRandom = new SecureRandom();
    String r1 = Long.toHexString(secureRandom.nextLong());
    KeyPair kp1 = KeyUtil.generateKeyPair();
    PrivateKey privateKey1 = kp1.getPrivate();
    MyPKCS10CertRequest cr1 = CertUtil.createCertRequest(kp1);
    String rawCR1 = CertUtil.fromCertReqToString(cr1);
    String username1 = "testUser-" + r1;
    URI redirect1 = URI.create("http://test.foo/test/" + r1 + "/" + System.currentTimeMillis());
    Identifier token1 = BasicIdentifier.newID("token:id:/" + r1 + "/" + System.currentTimeMillis());
    Identifier id1 = BasicIdentifier.newID("asset:id:/" + r1 + "/" + System.currentTimeMillis());
    Asset asset = store.create();
    assert asset != null : "Error: The store is not producing valid assets when requested. A null was returned";
    asset.setIdentifier(id1);
    asset.setUsername(username1);
    asset.setPrivateKey(privateKey1);
    asset.setRedirect(redirect1);
    asset.setToken(token1);
    asset.setCertReq(cr1);
    store.save(asset);
    // Now try and update the identifier -- that should fail.
    String r2 = Long.toHexString(secureRandom.nextLong());
    Identifier id2 = BasicIdentifier.newID("asset:id:/" + r2 + "/" + System.currentTimeMillis());
    asset.setIdentifier(id2);
    // identifier means the object needs to be registered first.
    try {
        store.update(asset);
        assert false : "Error: was able to update the identifier.";
    } catch (UnregisteredObjectException t) {
        assert true;
    }
    // ok, set the id back since that worked.
    asset.setIdentifier(id1);
    // now for everything else.
    KeyPair kp2 = KeyUtil.generateKeyPair();
    PrivateKey privateKey2 = kp2.getPrivate();
    MyPKCS10CertRequest cr2 = CertUtil.createCertRequest(kp2);
    String rawCR2 = CertUtil.fromCertReqToString(cr2);
    String username2 = "testUser-" + r2;
    URI redirect2 = URI.create("http://test.foo/test/" + r2 + "/" + System.currentTimeMillis());
    Identifier token2 = BasicIdentifier.newID("token:id:/" + r1 + "/" + System.currentTimeMillis());
    asset.setUsername(username2);
    asset.setPrivateKey(privateKey2);
    asset.setCertReq(cr2);
    asset.setRedirect(redirect2);
    asset.setToken(token2);
    store.update(asset);
    Asset asset2 = store.get(asset.getIdentifier());
    assert asset2.getUsername().equals(username2);
    assert asset2.getPrivateKey().equals(privateKey2);
    assert CertUtil.fromCertReqToString(asset2.getCertReq()).equals(rawCR2);
    assert asset2.getToken().equals(token2);
    assert asset2.getRedirect().equals(redirect2);
    store.remove(asset.getIdentifier());
}
Also used : KeyPair(java.security.KeyPair) PrivateKey(java.security.PrivateKey) BasicIdentifier(edu.uiuc.ncsa.security.core.util.BasicIdentifier) Identifier(edu.uiuc.ncsa.security.core.Identifier) UnregisteredObjectException(edu.uiuc.ncsa.security.core.exceptions.UnregisteredObjectException) SecureRandom(java.security.SecureRandom) Asset(edu.uiuc.ncsa.myproxy.oa4mp.client.Asset) URI(java.net.URI) MyPKCS10CertRequest(edu.uiuc.ncsa.security.util.pkcs.MyPKCS10CertRequest)

Aggregations

Asset (edu.uiuc.ncsa.myproxy.oa4mp.client.Asset)1 Identifier (edu.uiuc.ncsa.security.core.Identifier)1 UnregisteredObjectException (edu.uiuc.ncsa.security.core.exceptions.UnregisteredObjectException)1 BasicIdentifier (edu.uiuc.ncsa.security.core.util.BasicIdentifier)1 MyPKCS10CertRequest (edu.uiuc.ncsa.security.util.pkcs.MyPKCS10CertRequest)1 URI (java.net.URI)1 KeyPair (java.security.KeyPair)1 PrivateKey (java.security.PrivateKey)1 SecureRandom (java.security.SecureRandom)1