From 0bf9778d2e4215937bfe981dee2ba98bdd786a9a Mon Sep 17 00:00:00 2001 From: Muzzammil Shahid Date: Tue, 9 Jul 2024 18:22:41 +0500 Subject: [PATCH] Improve tests of acceptor and joiner, add more test cases --- test/joiner_acceptor_test.dart | 169 ++++++++++++++++++++++----------- 1 file changed, 112 insertions(+), 57 deletions(-) diff --git a/test/joiner_acceptor_test.dart b/test/joiner_acceptor_test.dart index d750a8c..1de89d5 100644 --- a/test/joiner_acceptor_test.dart +++ b/test/joiner_acceptor_test.dart @@ -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 methods() { - return ["cryptosign", "ticket", "wampcra", "anonymous"]; - } + List 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()); - 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()); + 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()); 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()); 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); }