use of org.mobicents.slee.resource.diameter.base.events.avp.DiameterAvpImpl in project jain-slee.diameter by RestComm.
the class ExpectedAvp method testOperationsAddWithValidatorOnAndRemovalNotAllowed.
/* ammendonca: removal allowed is gone...
@Test
public void testOperationsAddWithValidatorOnAndRemovalAllowed() {
instance.configure(this.getClass().getClassLoader().getResourceAsStream(validatorOnFile));
instance.setEnabled(true);
// It has session id
AccountingRequestImpl request = (AccountingRequestImpl) baseFactory.createAccountingRequest();
// <avp name="Session-Id" code="263" vendor="0" multiplicity="1" index="0"/>
AvpUtilities.setAvpAsUTF8String(request.getGenericData(), 263, request.getGenericData().getAvps(), "1346ferg5y");
// <avp name="Origin-Host" code="264" vendor="0" multiplicity="1" index="-1"/>
AvpUtilities.setAvpAsOctetString(request.getGenericData(), 264, request.getGenericData().getAvps(), clientURI);
// <avp name="Origin-Realm" code="296" vendor="0" multiplicity="1" index="-1"/>
AvpUtilities.setAvpAsOctetString(request.getGenericData(), 296, request.getGenericData().getAvps(), realmName);
AvpUtilities.setAvpAsOctetString(request.getGenericData(), 296, request.getGenericData().getAvps(), realmName);
// <avp name="Destination-Realm" code="283" vendor="0" multiplicity="1" index="-1"/>
AvpUtilities.setAvpAsOctetString(request.getGenericData(), 283, request.getGenericData().getAvps(), realmName);
// <avp name="Destination-Host" code="293" vendor="0" multiplicity="0-1" index="-1"/>
// AvpUtilities.setAvpAsOctetString(request.getGenericData(), 293, request.getGenericData().getAvps(), serverURI);
AvpUtilities.setAvpAsOctetString(request.getGenericData(), 293, request.getGenericData().getAvps(), serverHost);
// <avp name="Accounting-Record-Type" code="480" vendor="0" multiplicity="1" index="-1"/>
AvpUtilities.setAvpAsUnsigned32(request.getGenericData(), 480, request.getGenericData().getAvps(), 1);
String sessionId = AvpUtilities.getAvpAsUTF8String(263, request.getGenericData().getAvps());
Session localSession = null;
try {
localSession = stack.getSessionFactory().getNewSession(sessionId);
sendMessage(localSession, request);
System.out.println(request);
fail("Should not send this message. Message MUST contain Accounting-Record-Number AVP (485), and it is not present.");
}
catch (org.jdiameter.api.validation.AvpNotAllowedException e) {
if (e.getAvpCode() != 485 && e.getVendorId() != 0) {
fail("Message Validation failed with wrong AVP Code/Vendor-Id in Exception. Expected (485:0), Received (" + e.getAvpCode() + ":" + e.getVendorId()
+ ").");
}
}
catch (Exception e) {
Throwable cause = e;
boolean wasAvpNotAllowed = false;
while ((cause = cause.getCause()) != null) {
if (cause instanceof org.jdiameter.api.validation.AvpNotAllowedException) {
wasAvpNotAllowed = true;
org.jdiameter.api.validation.AvpNotAllowedException exc = (org.jdiameter.api.validation.AvpNotAllowedException) cause;
if (exc.getAvpCode() != 485 && exc.getVendorId() != 0) {
fail("Message Validation failed with wrong AVP Code/Vendor-Id in Exception. Expected (485:0), Received (" + exc.getAvpCode() + ":"
+ exc.getVendorId() + ").");
}
}
}
if (!wasAvpNotAllowed) {
fail("Message failed to be sent for wrong reason. Expected AvpNotAllowedException, Received " + e);
}
}
// <avp name="Accounting-Record-Number" code="485" vendor="0" multiplicity="1" index="-1"/>
AvpUtilities.setAvpAsUnsigned32(request.getGenericData(), 485, request.getGenericData().getAvps(), 1);
// In here we send the message actually. It is OK!
try {
sendMessage(localSession, request);
}
catch (Exception e) {
fail("Failed to send message when it should succeed! Exception: " + e);
}
// <avp name="Acct-Application-Id" code="259" vendor="0" multiplicity="0-1" index="-1"/>
AvpUtilities.setAvpAsUnsigned32(request.getGenericData(), 259, request.getGenericData().getAvps(), 1);
// Again, in here we send the message actually. It is OK!
try {
sendMessage(localSession, request);
}
catch (Exception e) {
fail("Failed to send message when it should succeed! Exception: " + e);
}
// <!-- FORBBIDEN -->
// <avp name="Auth-Application-Id" code="258" vendor="0" multiplicity="0" index="-1"/>
try {
AvpUtilities.setAvpAsUnsigned32(request.getGenericData(), 258, request.getGenericData().getAvps(), 1);
// Now send should fail. Message has AVP "Auth-Application-Id", not valid in ACR.
sendMessage(localSession, request);
fail("Should not send this message. Message MUST NOT contain Auth-Application-Id AVP (258), and it is present.");
}
catch (AvpNotAllowedException e) {
if (e.getAvpCode() != 258 && e.getVendorId() != 0) {
fail("Message Validation failed with wrong AVP Code/Vendor-Id in Exception. Expected (258:0), Received (" + e.getAvpCode() + ":" + e.getVendorId()
+ ").");
}
}
catch (Exception e) {
fail("Message failed to be sent for wrong reason. Expected AvpNotAllowedException, Received " + e);
}
// This is just in case
Map<ExpectedAvp, ExpectedAvp> expectedAvps = new HashMap<ExpectedAvp, ExpectedAvp>();
ExpectedAvp a = new ExpectedAvp();
a.code = 263;
a.count = 1;
expectedAvps.put(a, a);
a = new ExpectedAvp();
a.code = 264;
a.count = 1;
expectedAvps.put(a, a);
a = new ExpectedAvp();
a.code = 296;
a.count = 1;
expectedAvps.put(a, a);
a = new ExpectedAvp();
a.code = 283;
a.count = 1;
expectedAvps.put(a, a);
a = new ExpectedAvp();
a.code = 480;
a.count = 1;
expectedAvps.put(a, a);
a = new ExpectedAvp();
a.code = 485;
a.count = 1;
expectedAvps.put(a, a);
a = new ExpectedAvp();
a.code = 259;
a.count = 1;
expectedAvps.put(a, a);
a = new ExpectedAvp();
a.code = 293;
a.count = 1;
expectedAvps.put(a, a);
testPresentAvps(request.getGenericData().getAvps(), expectedAvps);
}
*/
@Test
public void testOperationsAddWithValidatorOnAndRemovalNotAllowed() {
instance.configure(this.getClass().getClassLoader().getResourceAsStream(validatorOnFile));
instance.setEnabled(true);
// It has session id
AccountingRequestImpl request = (AccountingRequestImpl) baseFactory.createAccountingRequest(new DiameterAvpImpl[] { new DiameterAvpImpl(263, 0L, 0, 1, "xxx".getBytes(), DiameterAvpType.UTF8_STRING) });
// <avp name="Session-Id" code="263" vendor="0" multiplicity="1" index="0"/>
try {
AvpUtilities.setAvpAsUTF8String(request.getGenericData(), 263, request.getGenericData().getAvps(), "1346ferg5y");
fail("Session-Id can not be set twice.");
} catch (AvpNotAllowedException e) {
if (e.getAvpCode() != 258 && e.getVendorId() != 0) {
fail("Message Validation failed with wrong AVP Code/Vendor-Id in Exception. Expected (258:0), Received (" + e.getAvpCode() + ":" + e.getVendorId() + ").");
}
} catch (Exception e) {
fail("Message failed to be sent for wrong reason. Expected AvpNotAllowedException, Received " + e);
}
// <avp name="Origin-Host" code="264" vendor="0" multiplicity="1" index="-1"/>
if (!request.hasOriginHost())
AvpUtilities.setAvpAsOctetString(request.getGenericData(), 264, request.getGenericData().getAvps(), clientURI);
// <avp name="Origin-Realm" code="296" vendor="0" multiplicity="1" index="-1"/>
if (!request.hasOriginRealm())
AvpUtilities.setAvpAsOctetString(request.getGenericData(), 296, request.getGenericData().getAvps(), realmName);
// <avp name="Destination-Host" code="293" vendor="0" multiplicity="0-1" index="-1"/>
AvpUtilities.setAvpAsOctetString(request.getGenericData(), 293, request.getGenericData().getAvps(), serverURI);
// <avp name="Destination-Realm" code="283" vendor="0" multiplicity="1" index="-1"/>
AvpUtilities.setAvpAsOctetString(request.getGenericData(), 283, request.getGenericData().getAvps(), realmName);
// <avp name="Accounting-Record-Type" code="480" vendor="0" multiplicity="1" index="-1"/>
AvpUtilities.setAvpAsUnsigned32(request.getGenericData(), 480, request.getGenericData().getAvps(), 1);
String sessionId = AvpUtilities.getAvpAsUTF8String(263, request.getGenericData().getAvps());
Session localSession = null;
try {
localSession = stack.getSessionFactory().getNewSession(sessionId);
sendMessage(localSession, request);
fail("Should not send this message. Message MUST contain Accounting-Record-Number AVP (485), and it is not present.");
} catch (org.jdiameter.api.validation.AvpNotAllowedException e) {
if (e.getAvpCode() != 485 && e.getVendorId() != 0) {
fail("Message Validation failed with wrong AVP Code/Vendor-Id in Exception. Expected (485:0), Received (" + e.getAvpCode() + ":" + e.getVendorId() + ").");
}
} catch (Exception e) {
Throwable cause = e;
boolean wasAvpNotAllowed = false;
while ((cause = cause.getCause()) != null) {
if (cause instanceof org.jdiameter.api.validation.AvpNotAllowedException) {
wasAvpNotAllowed = true;
org.jdiameter.api.validation.AvpNotAllowedException exc = (org.jdiameter.api.validation.AvpNotAllowedException) cause;
if (exc.getAvpCode() != 485 && exc.getVendorId() != 0) {
fail("Message Validation failed with wrong AVP Code/Vendor-Id in Exception. Expected (485:0), Received (" + exc.getAvpCode() + ":" + exc.getVendorId() + ").");
}
}
}
if (!wasAvpNotAllowed) {
fail("Message failed to be sent for wrong reason. Expected AvpNotAllowedException, Received " + e);
}
}
// <avp name="Accounting-Record-Number" code="485" vendor="0" multiplicity="1" index="-1"/>
AvpUtilities.setAvpAsUnsigned32(request.getGenericData(), 485, request.getGenericData().getAvps(), 1);
// In here we send the message actually. It is OK!
try {
sendMessage(localSession, request);
} catch (Exception e) {
fail("Failed to send message when it should succeed! Exception: " + e);
}
// Message should already come with Acct-Application-Id.. but still..
try {
// <avp name="Acct-Application-Id" code="259" vendor="0" multiplicity="0-1" index="-1"/>
AvpUtilities.setAvpAsUnsigned32(request.getGenericData(), 259, request.getGenericData().getAvps(), 1);
} catch (AvpNotAllowedException e) {
if (e.getAvpCode() != 259 && e.getVendorId() != 0) {
fail("Message Validation failed with wrong AVP Code/Vendor-Id in Exception. Expected (259:0), Received (" + e.getAvpCode() + ":" + e.getVendorId() + ").");
}
}
// Again, in here we send the message actually. It is OK!
try {
sendMessage(localSession, request);
} catch (Exception e) {
fail("Failed to send message when it should succeed! Exception: " + e);
}
// <avp name="Auth-Application-Id" code="258" vendor="0" multiplicity="0" index="-1"/>
try {
AvpUtilities.setAvpAsUnsigned32(request.getGenericData(), 258, request.getGenericData().getAvps(), 1);
// Now send should fail. Message has AVP "Auth-Application-Id", not valid in ACR.
sendMessage(localSession, request);
fail("Should not send this message. Message MUST NOT contain Auth-Application-Id AVP (258), and it is present.");
} catch (AvpNotAllowedException e) {
if (e.getAvpCode() != 258 && e.getVendorId() != 0) {
fail("Message Validation failed with wrong AVP Code/Vendor-Id in Exception. Expected (258:0), Received (" + e.getAvpCode() + ":" + e.getVendorId() + ").");
}
} catch (Exception e) {
fail("Message failed to be sent for wrong reason. Expected AvpNotAllowedException, Received " + e);
}
// Just in case.
Map<ExpectedAvp, ExpectedAvp> expectedAvps = new HashMap<ExpectedAvp, ExpectedAvp>();
ExpectedAvp a = new ExpectedAvp();
a.code = 263;
a.count = 1;
expectedAvps.put(a, a);
a = new ExpectedAvp();
a.code = 264;
a.count = 1;
expectedAvps.put(a, a);
a = new ExpectedAvp();
a.code = 296;
a.count = 1;
expectedAvps.put(a, a);
a = new ExpectedAvp();
a.code = 283;
a.count = 1;
expectedAvps.put(a, a);
a = new ExpectedAvp();
a.code = 480;
a.count = 1;
expectedAvps.put(a, a);
a = new ExpectedAvp();
a.code = 485;
a.count = 1;
expectedAvps.put(a, a);
a = new ExpectedAvp();
a.code = 259;
a.count = 1;
expectedAvps.put(a, a);
a = new ExpectedAvp();
a.code = 293;
a.count = 1;
expectedAvps.put(a, a);
testPresentAvps(request.getGenericData().getAvps(), expectedAvps);
}
use of org.mobicents.slee.resource.diameter.base.events.avp.DiameterAvpImpl in project jain-slee.diameter by RestComm.
the class DiameterMessageImpl method getAvpsInternal.
// ===== AVP Management =====
private DiameterAvp[] getAvpsInternal(AvpSet set) throws Exception {
List<DiameterAvp> avps = new ArrayList<DiameterAvp>();
for (Avp a : set) {
AvpRepresentation avpRep = AvpDictionary.INSTANCE.getAvp(a.getCode(), a.getVendorId());
if (avpRep == null) {
// log.error("Avp with code: " + a.getCode() + " VendorId: " + a.getVendorId() + " is not listed in dictionary, skipping!");
continue;
} else {
if (avpRep.getType().equals("Grouped")) {
// TODO: There's no info about if AVP has mandatory or protected flags set...
GroupedAvpImpl gAVP = new GroupedAvpImpl(a.getCode(), a.getVendorId(), avpRep.getRuleMandatoryAsInt(), avpRep.getRuleProtectedAsInt(), a.getRaw());
gAVP.setExtensionAvps(getAvpsInternal(a.getGrouped()));
// This is a grouped AVP... let's make it like that.
avps.add(gAVP);
} else {
// TODO: There's no info about if AVP has mandatory or protected flags set...
avps.add(new DiameterAvpImpl(a.getCode(), a.getVendorId(), avpRep.getRuleMandatoryAsInt(), avpRep.getRuleProtectedAsInt(), a.getRaw(), DiameterAvpType.fromString(avpRep.getType())));
}
}
}
return avps.toArray(new DiameterAvp[avps.size()]);
}
Aggregations