Skip to content

Commit

Permalink
Improve tests of acceptor and joiner, add more test cases
Browse files Browse the repository at this point in the history
  • Loading branch information
muzzammilshahid committed Jul 9, 2024
1 parent 3e399a8 commit 0bf9778
Showing 1 changed file with 112 additions and 57 deletions.
169 changes: 112 additions & 57 deletions test/joiner_acceptor_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,90 +6,145 @@ import "package:wampproto/joiner.dart";
import "package:wampproto/messages.dart";
import "package:wampproto/serializers.dart";

const realm = "realm1";
const authID = "foo";
const ticket = "fooTicket";
const secret = "barSecret";
const privateKey = "175604dcce3944595dad640da1676d5e1e1a3950f872f177b1269981140f1c5d";
const publicKey = "8096cadfd3af87662d4c6589605801c1e2841c4e2cf3d6c30fb187c09c76c5ac";
final authenticator = Authenticator();

// Custom authenticator implementation
class Authenticator extends IServerAuthenticator {
@override
Response authenticate(Request request) {
switch (request.method) {
case "anonymous":
if (request is AnonymousRequest) {
if (request.realm == realm && request.authID == authID) {
return Response(request.authID, "anonymous");
case "ticket":
}

throw Exception("invalid realm");
} else if (request is TicketRequest) {
if (request.ticket == ticket) {
return Response(request.authID, "anonymous");
case "wampcra":
return WAMPCRAResponse(request.authID, "anonymous", "password");
case "cryptosign":
}

throw Exception("invalid ticket");
} else if (request is WAMPCRARequest) {
if (request.realm == realm && request.authID == authID) {
return WAMPCRAResponse(request.authID, "anonymous", secret);
}

throw Exception("invalid authID");
} else if (request is CryptoSignRequest) {
if (request.publicKey == publicKey) {
return Response(request.authID, "anonymous");
}

default:
throw ArgumentError("invalid auth method");
throw Exception("unknown publikey");
}
throw Exception("invalid auth method");
}

@override
List<String> methods() {
return ["cryptosign", "ticket", "wampcra", "anonymous"];
}
List<String> methods() => ["cryptosign", "ticket", "wampcra", "anonymous"];
}

const realm = "realm1";

void main() {
var authenticator = Authenticator();
test("AnonymousAuth", () {
var serializer = JSONSerializer();
var joiner = Joiner(realm, serializer: serializer, authenticator: AnonymousAuthenticator(""));
var acceptor = Acceptor(serializer: serializer, authenticator: authenticator);

var hello = joiner.sendHello();

var welcomeMap = acceptor.receive(hello);
var welcome = serializer.deserialize(welcomeMap.key);
expect(welcome, isA<Welcome>());
expect(welcomeMap.value, true);

var welcomeJoiner = joiner.receive(welcomeMap.key);
expect(welcomeJoiner, null);

var sessionDetails = joiner.getSessionDetails();
expect(sessionDetails, isNotNull);
group("Authentication Tests", () {
test("AnonymousAuth", () {
// Setup for anonymous authentication test
var anonymousAuthenticator = AnonymousAuthenticator(authID);
final serializer = JSONSerializer();
final joiner = Joiner(realm, serializer: serializer, authenticator: anonymousAuthenticator);
final acceptor = Acceptor(serializer: serializer, authenticator: authenticator);

final hello = joiner.sendHello();

// Process and verify the HELLO message
final welcomeMap = acceptor.receive(hello);
final welcome = serializer.deserialize(welcomeMap.key);

expect(welcome, isA<Welcome>());
expect(welcomeMap.value, true);

// Ensure no additional messages are received
final payload = joiner.receive(welcomeMap.key);
expect(payload, null);

// Verify session details are available
final sessionDetails = joiner.getSessionDetails();
expect(sessionDetails, isNotNull);
});

test("TicketAuth", () {
var ticketAuthenticator = TicketAuthenticator(ticket, authID);
testAuth(ticketAuthenticator);
});

test("TicketAuthInvalidTicket", () {
var ticketAuthenticator = TicketAuthenticator("invalid", authID);
expect(() => testAuth(ticketAuthenticator), throwsException);
});

test("CRAAuth", () {
var craAuthenticator = WAMPCRAAuthenticator(secret, authID, {"challenge": "test"});
testAuth(craAuthenticator);
});

test("CRAAuthInvalidSecret", () {
var craAuthenticator = WAMPCRAAuthenticator("invalid", authID, {"challenge": "test"});
expect(() => testAuth(craAuthenticator), throwsException);
});

test("CRAAuthInvalidAuthID", () {
var craAuthenticator = WAMPCRAAuthenticator(secret, "invalid", {"challenge": "test"});
expect(() => testAuth(craAuthenticator), throwsException);
});

test("CryptoSignAuth", () {
var cryptoSignAuthenticator = CryptoSignAuthenticator(authID, privateKey);
testAuth(cryptoSignAuthenticator);
});

test("CryptoSignAuthInvalidKey", () {
var cryptoSignAuthenticator =
CryptoSignAuthenticator(authID, "2e9bef98114241d2226996cf09faf87dad892643a7c5fde186783470bce21df3");
expect(() => testAuth(cryptoSignAuthenticator), throwsException);
});
});

test("TicketAuth", () => testAuth(authenticator, TicketAuthenticator("", "test")));

test("CRAAuth", () => testAuth(authenticator, WAMPCRAAuthenticator("password", "test", {"challenge": "test"})));

test(
"CryptoSignAuth",
() => testAuth(
authenticator,
CryptoSignAuthenticator("authID", "6d9b906ad60d1f4dd796dbadcc2e2252310565ccdc6fe10b289df5684faf2a46"),
),
);
}

void testAuth(Authenticator authenticator, IClientAuthenticator clientAuthenticator) {
var serializer = JSONSerializer();
var joiner = Joiner(realm, serializer: serializer, authenticator: clientAuthenticator);
var acceptor = Acceptor(serializer: serializer, authenticator: authenticator);
void testAuth(IClientAuthenticator clientAuthenticator) {
final serializer = JSONSerializer();
final joiner = Joiner(realm, serializer: serializer, authenticator: clientAuthenticator);
final acceptor = Acceptor(serializer: serializer, authenticator: authenticator);

var hello = joiner.sendHello();
final hello = joiner.sendHello();

// Process and verify the CHALLENGE message
final challengeMap = acceptor.receive(hello);
final challenge = serializer.deserialize(challengeMap.key);

var challengeMap = acceptor.receive(hello);
var challenge = serializer.deserialize(challengeMap.key);
expect(challenge, isA<Challenge>());
expect(challengeMap.value, false);

var authenticated = joiner.receive(challengeMap.key);
// Authenticate and verify the response
final authenticated = joiner.receive(challengeMap.key);
expect(authenticated, isNotNull);

var welcomeMap = acceptor.receive(authenticated!);
var welcome = serializer.deserialize(welcomeMap.key);
// Process and verify the WELCOME message
final welcomeMap = acceptor.receive(authenticated!);
final welcome = serializer.deserialize(welcomeMap.key);

expect(welcome, isA<Welcome>());
expect(welcomeMap.value, true);

var welcomeJoiner = joiner.receive(welcomeMap.key);
expect(welcomeJoiner, null);
expect(joiner.getSessionDetails(), isNotNull);
// Ensure no additional messages are received
final payload = joiner.receive(welcomeMap.key);
expect(payload, null);

var sessionDetails = joiner.getSessionDetails();
// Verify session details are available
final sessionDetails = joiner.getSessionDetails();
expect(sessionDetails, isNotNull);
}

0 comments on commit 0bf9778

Please sign in to comment.