From a4c5c38b53c92d4d9f4af694f707f06485f11da2 Mon Sep 17 00:00:00 2001 From: Samuel Mannehed Date: Thu, 2 Jan 2020 17:29:41 +0100 Subject: [PATCH] Add unit tests for encodeUTF8 and decodeUTF8 --- kasmweb/tests/test.util.js | 43 +++++++++++++++++++++++++++++++++++++- 1 file changed, 42 insertions(+), 1 deletion(-) diff --git a/kasmweb/tests/test.util.js b/kasmweb/tests/test.util.js index 201acc8..1f670bd 100644 --- a/kasmweb/tests/test.util.js +++ b/kasmweb/tests/test.util.js @@ -2,6 +2,7 @@ const expect = chai.expect; import * as Log from '../core/util/logging.js'; +import { encodeUTF8, decodeUTF8 } from '../core/util/strings.js'; describe('Utils', function () { "use strict"; @@ -58,8 +59,48 @@ describe('Utils', function () { }); }); + describe('string functions', function () { + it('should decode UTF-8 to DOMString correctly', function () { + // The capital cyrillic letter 'PE' has the hexcode 'd0 9f' in UTF-8 + const utf8string = String.fromCodePoint(parseInt("d0", 16), + parseInt("9f", 16)); + const domstring = decodeUTF8(utf8string); + expect(domstring).to.equal("П"); + }); + + it('should encode DOMString to UTF-8 correctly', function () { + const domstring = "åäöa"; + const utf8string = encodeUTF8(domstring); + + // The hexcode in UTF-8 for 'å' is 'c3 a5' + expect(utf8string.codePointAt(0).toString(16)).to.equal("c3"); + expect(utf8string.codePointAt(1).toString(16)).to.equal("a5"); + // ä + expect(utf8string.codePointAt(2).toString(16)).to.equal("c3"); + expect(utf8string.codePointAt(3).toString(16)).to.equal("a4"); + // ö + expect(utf8string.codePointAt(4).toString(16)).to.equal("c3"); + expect(utf8string.codePointAt(5).toString(16)).to.equal("b6"); + // a + expect(utf8string.codePointAt(6).toString(16)).to.equal("61"); + }); + + it('should keep the string intact if encoding to UTF-8 and then decoding', function() { + const domstring = "κόσμε"; + const utf8string = encodeUTF8(domstring); + expect(decodeUTF8(utf8string)).to.equal(domstring); + }); + + it('should ignore URIErrors when UTF-8 decoding if allowLatin1 is set', function() { + expect(() => decodeUTF8("�")).to.throw(URIError); + expect(() => decodeUTF8("�", true)).to.not.throw(URIError); + + // Only URIError should be ignored + expect(() => decodeUTF8(undefVar, true)).to.throw(Error); + }); + }); + // TODO(directxman12): test the conf_default and conf_defaults methods - // TODO(directxman12): test decodeUTF8 // TODO(directxman12): test the event methods (addEvent, removeEvent, stopEvent) // TODO(directxman12): figure out a good way to test getPosition and getEventPosition // TODO(directxman12): figure out how to test the browser detection functions properly