From 0ee5ca6ebe3ff8fbb8112cd7e235fdbc98ff32bb Mon Sep 17 00:00:00 2001
From: Pierre Ossman <ossman@cendio.se>
Date: Mon, 13 Feb 2017 15:00:32 +0100
Subject: [PATCH] Fix tests after changing auth negotiation

PhantomJS has a very basic implementation of Uint8Array, so we
need to help it out a bit.
---
 core/rfb.js       | 19 +++++++++++++++----
 tests/test.rfb.js | 14 +++++++++++---
 2 files changed, 26 insertions(+), 7 deletions(-)

diff --git a/core/rfb.js b/core/rfb.js
index b8ff782..e26e7b1 100644
--- a/core/rfb.js
+++ b/core/rfb.js
@@ -795,15 +795,26 @@
                 var types = this._sock.rQshiftBytes(num_types);
                 Util.Debug("Server security types: " + types);
 
+                // Polyfill since IE and PhantomJS doesn't have
+                // TypedArray.includes()
+                function includes(item, array) {
+                    for (var i = 0; i < array.length; i++) {
+                        if (array[i] === item) {
+                            return true;
+                        }
+                    }
+                    return false;
+                }
+
                 // Look for each auth in preferred order
                 this._rfb_auth_scheme = 0;
-                if (types.indexOf(1) !== -1) {
+                if (includes(1, types)) {
                     this._rfb_auth_scheme = 1; // None
-                } else if (types.indexOf(22) !== -1) {
+                } else if (includes(22, types)) {
                     this._rfb_auth_scheme = 22; // XVP
-                } else if (types.indexOf(16) !== -1) {
+                } else if (includes(16, types)) {
                     this._rfb_auth_scheme = 16; // Tight
-                } else if (types.indexOf(2) !== -1) {
+                } else if (includes(2, types)) {
                     this._rfb_auth_scheme = 2; // VNC Auth
                 } else {
                     return this._fail("Unsupported server",
diff --git a/tests/test.rfb.js b/tests/test.rfb.js
index 229cfe5..f903d05 100644
--- a/tests/test.rfb.js
+++ b/tests/test.rfb.js
@@ -712,12 +712,20 @@ describe('Remote Frame Buffer Protocol Client', function() {
                 expect(client._rfb_auth_scheme).to.equal(auth_scheme);
             });
 
+            it('should prefer no authentication is possible', function () {
+                client._rfb_version = 3.7;
+                var auth_schemes = [2, 1, 3];
+                client._sock._websocket._receive_data(auth_schemes);
+                expect(client._rfb_auth_scheme).to.equal(1);
+                expect(client._sock).to.have.sent(new Uint8Array([1, 1]));
+            });
+
             it('should choose for the most prefered scheme possible for versions >= 3.7', function () {
                 client._rfb_version = 3.7;
-                var auth_schemes = [2, 1, 2];
+                var auth_schemes = [2, 22, 16];
                 client._sock._websocket._receive_data(auth_schemes);
-                expect(client._rfb_auth_scheme).to.equal(2);
-                expect(client._sock).to.have.sent(new Uint8Array([2]));
+                expect(client._rfb_auth_scheme).to.equal(22);
+                expect(client._sock).to.have.sent(new Uint8Array([22]));
             });
 
             it('should fail if there are no supported schemes for versions >= 3.7', function () {
-- 
GitLab