use of edu.umass.cs.gnsclient.client.util.GuidEntry in project GNS by MobilityFirst.
the class ByteificationComparisonFail method test_201_FromCommandPacket_128B_Signed.
/**
*
* @param byteificationComparison
* @throws UnsupportedEncodingException
* @throws JSONException
* @throws ClientException
* @throws NoSuchAlgorithmException
* @throws RequestParseException
*/
// FIXME: THIS TEST IS FAILING at new CommandPacket(bytes)
@Test
public void test_201_FromCommandPacket_128B_Signed(ByteificationComparisonFail byteificationComparison) throws UnsupportedEncodingException, JSONException, ClientException, NoSuchAlgorithmException, RequestParseException {
KeyPair keyPair = KeyPairGenerator.getInstance(GNSProtocol.RSA_ALGORITHM.toString()).generateKeyPair();
String guid = SharedGuidUtils.createGuidStringFromPublicKey(keyPair.getPublic().getEncoded());
// Squirrel this away now just in case the call below times out.
KeyPairUtils.saveKeyPair("gnsname", "alias", guid, keyPair);
GuidEntry querier = new GuidEntry("alias", guid, keyPair.getPublic(), keyPair.getPrivate());
CommandPacket packet = GNSCommand.fieldUpdate(querier, new String(Util.getRandomAlphanumericBytes(64)), new String(Util.getRandomAlphanumericBytes(64)));
String jsonBefore = packet.toJSONObject().toString();
byte[] bytes = packet.toBytes();
assert (jsonBefore.equals(packet.toJSONObject().toString()));
long startTime = System.nanoTime();
for (int i = 0; i < TEST_RUNS; i++) {
new CommandPacket(bytes);
}
long endTime = System.nanoTime();
double avg = (endTime - startTime) / (TEST_RUNS);
CommandPacket outputPacket = new CommandPacket(bytes);
System.out.println("Average time CommandPacket from bytes 128B Signed was " + avg + " nanoseconds.");
assert (Arrays.equals(bytes, outputPacket.toBytes()));
String canonicalJSON = CanonicalJSON.getCanonicalForm(jsonBefore);
String canonicalJSONOutput = CanonicalJSON.getCanonicalForm(outputPacket.toJSONObject());
//System.out.println(canonicalJSONOutput);
assert (canonicalJSON.equals(canonicalJSONOutput));
//CommandPacket outputPacket = CommandPacket.fromBytes(bytes);
//assert(packet.toJSONObject().toString().equals(outputPacket.toJSONObject().toString()));
}
use of edu.umass.cs.gnsclient.client.util.GuidEntry in project GNS by MobilityFirst.
the class HighConcurrencyReadsTest method setupClientsAndGuids.
private static void setupClientsAndGuids() throws Exception {
clients = new GNSClientCommands[numClients];
executor = (ScheduledThreadPoolExecutor) Executors.newScheduledThreadPool(numClients);
for (int i = 0; i < numClients; i++) {
clients[i] = new GNSClientCommands();
}
String gnsInstance = GNSClientCommands.getGNSProvider();
accountGuidEntries = new GuidEntry[numAccountGuids];
int numPreExisting = 0;
for (int i = 0; i < numAccountGuids; i++) {
LOGGER.log(Level.FINE, "Creating account GUID {0}", new Object[] { ACCOUNT_GUID_PREFIX + i });
try {
accountGuidEntries[i] = GuidUtils.lookupOrCreateAccountGuid(clients[0], ACCOUNT_GUID_PREFIX + i, PASSWORD);
LOGGER.log(Level.FINE, "Created account {0}", new Object[] { accountGuidEntries[i] });
assert (accountGuidEntries[i].getGuid().equals(KeyPairUtils.getGuidEntry(gnsInstance, ACCOUNT_GUID_PREFIX + i).getGuid()));
} catch (DuplicateNameException e) {
numPreExisting++;
accountGuidEntries[i] = KeyPairUtils.getGuidEntry(gnsInstance, ACCOUNT_GUID_PREFIX + i);
LOGGER.log(Level.INFO, "Found that account {0} already exists", new Object[] { accountGuidEntries[i] });
}
// any other exceptions should be thrown up
}
System.out.println("Created (" + (numAccountGuids - numPreExisting) + ") or found pre-existing (" + numPreExisting + ") a total of " + numAccountGuids + " account GUIDs: " + Arrays.asList(accountGuidEntries));
if (accountGuidsOnly) {
for (int i = 0; i < accountGuidEntries.length; i++) {
guidEntries[i] = accountGuidEntries[i];
}
return;
}
guidEntries = new GuidEntry[numGuids];
Set<String> subGuids = new HashSet<String>();
for (int i = 0, j = 0; i < numGuids; i++) {
subGuids.add(Config.getGlobalString(TC.TEST_GUID_PREFIX) + i);
if (subGuids.size() == numGuidsPerAccount || i == numGuids - 1) {
// because batch creation seems buggy
if (subGuids.size() == 1) {
String subGuid = subGuids.iterator().next();
try {
GuidEntry created = GuidUtils.lookupOrCreateGuid(clients[0], accountGuidEntries[i / numGuidsPerAccount], subGuid);
assert (created.getGuid().equals(KeyPairUtils.getGuidEntry(gnsInstance, subGuid).getGuid()));
} catch (DuplicateNameException de) {
// ignore, will retrieve it locally below
}
// any other exceptions should be thrown up
} else {
try {
// batch create
clients[0].guidBatchCreate(accountGuidEntries[i / numGuidsPerAccount], subGuids);
} catch (ClientException | IOException e) {
for (String subGuid : subGuids) {
try {
clients[0].guidCreate(accountGuidEntries[i / numGuidsPerAccount], subGuid);
} catch (DuplicateNameException de) {
// ignore, will retrieve it locally below
}
// any other exception should be throw up
}
}
}
for (String subGuid : subGuids) {
guidEntries[j++] = KeyPairUtils.getGuidEntry(gnsInstance, subGuid);
}
LOGGER.log(Level.FINE, "Created sub-guid(s) {0}", new Object[] { subGuids });
subGuids.clear();
}
}
for (GuidEntry guidEntry : accountGuidEntries) {
assert (guidEntry != null);
}
for (GuidEntry guidEntry : guidEntries) {
assert (guidEntry != null);
}
System.out.println("Created or found " + guidEntries.length + " pre-existing sub-guids " + Arrays.asList(guidEntries));
}
use of edu.umass.cs.gnsclient.client.util.GuidEntry in project GNS by MobilityFirst.
the class SelectTest method test_053_QuerySelectWorldReadable.
/**
* Check a query select with world readable fields
*/
@Test
public void test_053_QuerySelectWorldReadable() {
String fieldName = "testQueryWorldReadable";
try {
for (int cnt = 0; cnt < 5; cnt++) {
String queryTestName = "queryTest-" + RandomString.randomString(12);
client.execute(GNSCommand.createGUID(masterGuid, queryTestName));
GuidEntry testEntry = GuidUtils.getGUIDKeys(queryTestName);
// save them so we can delete them later
CREATED_GUIDS.add(testEntry);
JSONArray array = new JSONArray(Arrays.asList(25));
client.execute(GNSCommand.fieldReplaceOrCreateList(testEntry.getGuid(), fieldName, array, testEntry));
}
waitSettle(WAIT_SETTLE);
} catch (ClientException | IOException e) {
Utils.failWithStackTrace("Exception while trying to create the guids: " + e);
}
try {
String query = "~" + fieldName + " : ($gt: 0)";
JSONArray result = client.execute(GNSCommand.selectQuery(query)).getResultJSONArray();
for (int i = 0; i < result.length(); i++) {
System.out.println(result.get(i).toString());
}
// best we can do should be at least 5, but possibly more objects in results
Assert.assertThat(result.length(), Matchers.greaterThanOrEqualTo(5));
} catch (ClientException | IOException | JSONException e) {
Utils.failWithStackTrace("Exception executing selectQuery: " + e);
}
}
use of edu.umass.cs.gnsclient.client.util.GuidEntry in project GNS by MobilityFirst.
the class RemoveGuidTest method test_10_RemoveGuidUsingAccount.
/**
*
*/
@Test
public void test_10_RemoveGuidUsingAccount() {
String guidName = "testGUID" + RandomString.randomString(12);
GuidEntry guid = null;
try {
guid = clientCommands.guidCreate(masterGuid, guidName);
} catch (ClientException | IOException e) {
Utils.failWithStackTrace("Exception while creating testGuid: " + e);
}
if (guid != null) {
System.out.println("testGuid is " + guid.toString());
try {
clientCommands.guidRemove(masterGuid, guid.getGuid());
} catch (ClientException | IOException e) {
Utils.failWithStackTrace("Exception while removing testGuid (" + guid.toString() + "): " + e);
}
try {
clientCommands.lookupGuidRecord(guid.getGuid());
Utils.failWithStackTrace("Lookup testGuid should have throw an exception.");
} catch (ClientException e) {
} catch (IOException e) {
Utils.failWithStackTrace("Exception while doing Lookup testGuid: " + e);
}
}
}
use of edu.umass.cs.gnsclient.client.util.GuidEntry in project GNS by MobilityFirst.
the class SelectTest method test_059_QuerySelectwithProjectionSomeFieldsNotAccessible.
/**
* Check a query select with a projection
*/
@Test
public void test_059_QuerySelectwithProjectionSomeFieldsNotAccessible() {
String fieldName = "testQueryProjectionSomeFieldsNotAccessible";
try {
for (int cnt = 0; cnt < 5; cnt++) {
String queryTestName = "queryTest-" + RandomString.randomString(12);
client.execute(GNSCommand.createGUID(masterGuid, queryTestName));
GuidEntry testEntry = GuidUtils.getGUIDKeys(queryTestName);
// Remove default all fields / all guids ACL;
client.execute(GNSCommand.aclRemove(AclAccessType.READ_WHITELIST, testEntry, GNSProtocol.ENTIRE_RECORD.toString(), GNSProtocol.ALL_GUIDS.toString()));
// Also remove masterGuid access to entire record
client.execute(GNSCommand.aclRemove(AclAccessType.READ_WHITELIST, testEntry, GNSProtocol.ENTIRE_RECORD.toString(), masterGuid.getGuid()));
// save them so we can delete them later
CREATED_GUIDS.add(testEntry);
JSONObject json = new JSONObject();
json.put(fieldName, Arrays.asList(25));
json.put("field1", "value1");
json.put("inaccessableField", "someValue");
client.execute(GNSCommand.update(testEntry.getGuid(), json, testEntry));
// Add masterguid access to fieldName so that the query will work
client.execute(GNSCommand.aclAdd(AclAccessType.READ_WHITELIST, testEntry, fieldName, masterGuid.getGuid()));
// Add masterguid access to field1
client.execute(GNSCommand.aclAdd(AclAccessType.READ_WHITELIST, testEntry, "field1", masterGuid.getGuid()));
// Set up the ACL so the masterGuid can't read this field
client.execute(GNSCommand.fieldCreateAcl(AclAccessType.READ_WHITELIST, testEntry, "inaccessableField", testEntry.getGuid()));
}
waitSettle(WAIT_SETTLE);
} catch (ClientException | IOException | JSONException e) {
Utils.failWithStackTrace("Exception while trying to create the guids: " + e);
}
try {
String query = "~" + fieldName + " : ($gt: 0)";
//JSONArray result = client.execute(GNSCommand.selectRecords(masterGuid, query, null)).getResultJSONArray();
JSONArray result = waitForQueryResults(query);
for (int i = 0; i < result.length(); i++) {
System.out.println(result.get(i).toString());
}
// best we can do should be at least 5, but possibly more objects in results
Assert.assertThat(result.length(), Matchers.greaterThanOrEqualTo(5));
// Make sure that one of them has the field we want and
Assert.assertTrue(result.getJSONObject(0).has("field1"));
// doesn't have the field we can't see
Assert.assertFalse(result.getJSONObject(0).has("inaccessableField"));
} catch (JSONException e) {
//} catch (ClientException | IOException | JSONException e) {
Utils.failWithStackTrace("Exception executing selectRecords: " + e);
}
}
Aggregations