tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

encoding.test.ts (2210B)


      1 /**
      2 * @license
      3 * Copyright 2018 Google Inc.
      4 * SPDX-License-Identifier: Apache-2.0
      5 */
      6 
      7 import {describe, it} from 'node:test';
      8 
      9 import expect from 'expect';
     10 
     11 import {mergeUint8Arrays, stringToTypedArray} from './encoding.js';
     12 
     13 describe('Typed Array helpers', function () {
     14  describe('stringToTypedArray', function () {
     15    it('should get body length from empty string', async () => {
     16      const result = stringToTypedArray('');
     17 
     18      expect(Buffer.from('').compare(Buffer.from(result))).toBe(0);
     19    });
     20    it('should get body length from latin string', async () => {
     21      const body = 'Lorem ipsum dolor sit amet';
     22      const result = stringToTypedArray(body);
     23 
     24      expect(Buffer.from(body).compare(Buffer.from(result))).toBe(0);
     25    });
     26    it('should get body length from string with emoji', async () => {
     27      const body = 'How Long is this string in bytes 📏?';
     28      const result = stringToTypedArray(body);
     29 
     30      expect(Buffer.from(body).compare(Buffer.from(result))).toBe(0);
     31    });
     32 
     33    it('should get body length from base64', async () => {
     34      const body = btoa('Lorem ipsum dolor sit amet');
     35      const result = stringToTypedArray(body, true);
     36 
     37      expect(Buffer.from(body, 'base64').compare(Buffer.from(result))).toBe(0);
     38    });
     39 
     40    it('should get body length from base64 containing emoji', async () => {
     41      // 'How Long is this string in bytes 📏?';
     42      const base64 = 'SG93IExvbmcgaXMgdGhpcyBzdHJpbmcgaW4gYnl0ZXMg8J+Tjz8=';
     43      const result = stringToTypedArray(base64, true);
     44 
     45      expect(Buffer.from(base64, 'base64').compare(Buffer.from(result))).toBe(
     46        0,
     47      );
     48    });
     49  });
     50 
     51  describe('mergeUint8Arrays', () => {
     52    it('should work', () => {
     53      const one = new Uint8Array([1]);
     54      const two = new Uint8Array([12]);
     55      const three = new Uint8Array([123]);
     56 
     57      expect(mergeUint8Arrays([one, two, three])).toEqual(
     58        new Uint8Array([1, 12, 123]),
     59      );
     60    });
     61 
     62    it('should work with empty arrays', () => {
     63      const one = new Uint8Array([1]);
     64      const two = new Uint8Array([]);
     65      const three = new Uint8Array([]);
     66 
     67      expect(mergeUint8Arrays([one, two, three])).toEqual(new Uint8Array([1]));
     68    });
     69  });
     70 });