sig-test.js 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. var sig = require('../lib/sig');
  2. var assert = require('assert-diff');
  3. var fs = require('fs');
  4. var path = require('path');
  5. var nock = require('./nock');
  6. var html5player = require('./html5player.json');
  7. describe('Get tokens', function() {
  8. var key = 'en_US-vfljDEtYP';
  9. var url = '//s.ytimg.com/yts/jsbin/player-en_US-vfljDEtYP/base.js';
  10. it('Returns a set of tokens', function(done) {
  11. var filepath = path.resolve(
  12. __dirname, 'files/html5player/' + key + '.js');
  13. nock.url('http:' + url).replyWithFile(200, filepath);
  14. sig.getTokens(url, true, function(err, tokens) {
  15. if (err) return done(err);
  16. assert.ok(tokens.length);
  17. done();
  18. });
  19. });
  20. });
  21. describe('Signature decypher', function() {
  22. describe('extract decyphering actions', function() {
  23. it('Returns the correct set of actions', function() {
  24. for (var name in html5player) {
  25. var filepath = path.resolve(
  26. __dirname, 'files/html5player/' + name + '.js');
  27. var body = fs.readFileSync(filepath, 'utf8');
  28. var actions = sig.extractActions(body);
  29. assert.deepEqual(actions, html5player[name]);
  30. }
  31. });
  32. });
  33. function testDecipher(tokens, input, expected) {
  34. var result = sig.decipher(tokens, input);
  35. assert.equal(result, expected);
  36. }
  37. describe('properly apply actions based on tokens', function() {
  38. it('reverses', function() {
  39. testDecipher(['r'], 'abcdefg', 'gfedcba');
  40. });
  41. it('swaps head and position', function() {
  42. testDecipher(['w2'], 'abcdefg', 'cbadefg');
  43. testDecipher(['w3'], 'abcdefg', 'dbcaefg');
  44. testDecipher(['w5'], 'abcdefg', 'fbcdeag');
  45. });
  46. it('slices', function() {
  47. testDecipher(['s3'], 'abcdefg', 'defg');
  48. });
  49. it('real set of tokens', function() {
  50. testDecipher(html5player['en_US-vfl0Cbn9e'],
  51. 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ',
  52. 'bbSdefghijklmnoaqrstuvwxyzAZCDEFGHIJKLMNOPQRpTUVWc');
  53. });
  54. });
  55. });