<commit_before># Copyright (c) 2015 Vault12, Inc.
# MIT License https://opensource.org/licenses/MIT
window.__CRYPTO_DEBUG = true # Expose #theglow objects as window globals

expect  = require('chai').expect

Utils   = require 'utils'
MailBox = require 'mailbox'
Nacl    = require 'nacl'
Relay   = require 'relay'
Config  = require 'config'

# ----- Communication MailBox -----
describe 'MailBox, offline Relay', ->
  return unless window.__globalTest.runTests['mailbox']

  [Alice, Bob] = [null, null]

   # An elegant variable init block
  [msg1, msg2, msg3, msg4, msg5,
   msg6, msg7, pt1, pt2, pt3, pt4] = (null for n in [1..11])

  it 'create mailbox', (done)->
    handle done, MailBox.new('Alice_mbx').then (ret)->
      Alice = ret
      MailBox.new('Bob_mbx').then (ret)->
        Bob = ret
        expect(Alice.keyRing).not.null
        expect(Bob.keyRing).not.null
        done()

  it 'exchange keys', (done)->
    expect(Alice.keyRing.getNumberOfGuests()).equal(0)
    expect(Alice.keyRing.getGuestKey('Bob_mbx')).is.null
    handle done, Alice.keyRing.addGuest('Bob_mbx', Bob.getPubCommKey()).then ->
      expect(Alice.keyRing.getNumberOfGuests()).equal(1)
      expect(Alice.keyRing.getGuestKey('Bob_mbx')).is.not.null

      expect(Bob.keyRing.getNumberOfGuests()).equal(0)
      expect(Bob.keyRing.getGuestKey('Alice_mbx')).is.null
      Bob.keyRing.addGuest('Alice_mbx', Alice.getPubCommKey()).then ->
        expect(Bob.keyRing.getNumberOfGuests()).equal(1)
        expect(Bob.keyRing.getGuestKey('Alice_mbx')).is.not.null
        done()

  it 'nonces with data', (done)->
    # data = 0xCDEF89AB
    data = 0xFFFFFFFF
    handle done, MailBox._makeNonce().then (n1)=>
      # random nonce with timestamp only
      expect(MailBox._nonceData n1).is.not.equal data

      # random nonce with extra data, like message counter
      MailBox._makeNonce(data).then (n2)=>
        expect(MailBox._nonceData n2).is.equal data
        done()

  it 'Mailbox from well known seed', (done)->
    handle done, MailBox.fromSeed('hello').then (m)->
      pk = m.keyRing.getPubCommKey()
      pk.should.equal '<KEY>GXVnzsDh4zv+IlH7sV8llEFoEmg9fG3pRA='
      m.keyRing.hpk.should.deep.equal new Uint8Array([255, 29, 75, 250, 114, 23, 77,
        198, 215, 184, 25, 211, 126, 152, 31, 82, 236, 188, 237, 35, 204, 66,
        209, 107, 162, 211, 241, 170, 1, 60, 236, 221])
      m.selfDestruct(true).then -><commit_msg>warm up NACL library to fix failing test in PhantomJS<commit_after># Copyright (c) 2015 Vault12, Inc.
# MIT License https://opensource.org/licenses/MIT
window.__CRYPTO_DEBUG = true # Expose #theglow objects as window globals

expect  = require('chai').expect

Utils   = require 'utils'
MailBox = require 'mailbox'
Nacl    = require 'nacl'
Relay   = require 'relay'
Config  = require 'config'

# ----- Communication MailBox -----
describe 'MailBox, offline Relay', ->
  return unless window.__globalTest.runTests['mailbox']

  [Alice, Bob] = [null, null]

   # An elegant variable init block
  [msg1, msg2, msg3, msg4, msg5,
   msg6, msg7, pt1, pt2, pt3, pt4] = (null for n in [1..11])

  it 'create mailbox', (done)->
    handle done, MailBox.new('Alice_mbx').then (ret)->
      Alice = ret
      MailBox.new('Bob_mbx').then (ret)->
        Bob = ret
        expect(Alice.keyRing).not.null
        expect(Bob.keyRing).not.null
        done()

  it 'exchange keys', (done)->
    expect(Alice.keyRing.getNumberOfGuests()).equal(0)
    expect(Alice.keyRing.getGuestKey('Bob_mbx')).is.null
    handle done, Alice.keyRing.addGuest('Bob_mbx', Bob.getPubCommKey()).then ->
      expect(Alice.keyRing.getNumberOfGuests()).equal(1)
      expect(Alice.keyRing.getGuestKey('Bob_mbx')).is.not.null

      expect(Bob.keyRing.getNumberOfGuests()).equal(0)
      expect(Bob.keyRing.getGuestKey('Alice_mbx')).is.null
      Bob.keyRing.addGuest('Alice_mbx', Alice.getPubCommKey()).then ->
        expect(Bob.keyRing.getNumberOfGuests()).equal(1)
        expect(Bob.keyRing.getGuestKey('Alice_mbx')).is.not.null
        done()

  it 'nonces with data', (done)->
    # data = 0xCDEF89AB
    data = 0xFFFFFFFF
    handle done, MailBox._makeNonce().then (n1)=>
      # random nonce with timestamp only
      expect(MailBox._nonceData n1).is.not.equal data

      # random nonce with extra data, like message counter
      MailBox._makeNonce(data).then (n2)=>
        expect(MailBox._nonceData n2).is.equal data
        done()

  # first call of Mailbox.fromSeed returns an object with wrong keys in PhantomJS environment,
  # so this line is needed to "warm-up" NACL and generate correct keyRings for tests.
  # TODO: get rid of this
  MailBox.fromSeed('warmup')

  it 'Mailbox from well known seed', (done)->
    handle done, MailBox.fromSeed('hello').then (m)->
      pk = m.keyRing.getPubCommKey()
      pk.should.equal '<KEY>
      m.keyRing.hpk.should.deep.equal new Uint8Array([255, 29, 75, 250, 114, 23, 77,
        198, 215, 184, 25, 211, 126, 152, 31, 82, 236, 188, 237, 35, 204, 66,
        209, 107, 162, 211, 241, 170, 1, 60, 236, 221])
      m.selfDestruct(true).then ->
{'fraction_non_alphanumeric': 0.1463702537871336, 'fraction_numerical': 0.04701947668699587, 'mean_word_length': 3.2402001668056712, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 31, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 2, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '791866', 'n_tokens_mistral': 2034, 'n_tokens_neox': 1764, 'n_words': 420}