2018-05-03 10:20:13 +01:00
/* eslint no-console: 0 */
/**
* nodeApi.js
*
* Test node api utilities
*
* @author d98762625 [d98762625@gmail.com]
* @copyright Crown Copyright 2018
* @license Apache-2.0
*/
import assert from "assert" ;
import it from "../assertionHandler" ;
import chef from "../../../src/node/index" ;
2018-06-06 17:29:30 +01:00
import OperationError from "../../../src/core/errors/OperationError" ;
import SyncDish from "../../../src/node/SyncDish" ;
import { toBase32 } from "../../../src/node/index" ;
2018-05-03 10:20:13 +01:00
import TestRegister from "../../TestRegister" ;
TestRegister . addApiTests ([
it ( "should have some operations" , () => {
assert ( chef );
assert ( chef . toBase32 );
assert ( chef . setUnion );
assert ( ! chef . randomFunction );
}),
2018-06-06 17:29:30 +01:00
it ( "should export other functions at top level" , () => {
assert ( toBase32 );
}),
it ( "should be synchronous" , () => {
2018-05-03 10:20:13 +01:00
try {
2018-06-06 17:29:30 +01:00
const result = chef . toBase32 ( "input" );
2018-05-03 10:20:13 +01:00
assert . notEqual ( "something" , result );
} catch ( e ) {
// shouldnt reach here
assert ( false );
}
try {
const fail = chef . setUnion ( "1" );
// shouldnt get here
2018-05-03 13:58:15 +01:00
assert ( ! fail || false );
2018-05-03 10:20:13 +01:00
} catch ( e ) {
assert ( true );
}
}),
2018-06-06 17:29:30 +01:00
it ( "should not catch Errors" , () => {
try {
chef . setUnion ( "1" );
2018-05-03 10:20:13 +01:00
assert ( false );
2018-06-06 17:29:30 +01:00
} catch ( e ) {
assert ( e instanceof OperationError );
}
2018-05-11 19:25:14 +01:00
}),
2018-06-06 17:29:30 +01:00
it ( "should accept arguments in object format for operations" , () => {
const result = chef . setUnion ( "1 2 3 4:3 4 5 6" , {
2018-05-11 19:25:14 +01:00
itemDelimiter : " " ,
sampleDelimiter : ":"
});
2018-06-06 17:29:30 +01:00
assert . equal ( result . value , "1 2 3 4 5 6" );
2018-05-11 19:25:14 +01:00
}),
2018-06-06 17:29:30 +01:00
it ( "should accept just some of the optional arguments being overriden" , () => {
const result = chef . setIntersection ( "1 2 3 4 5\\n\\n3 4 5" , {
2018-05-11 19:25:14 +01:00
itemDelimiter : " " ,
});
2018-06-06 17:29:30 +01:00
assert . equal ( result . value , "3 4 5" );
2018-05-11 19:25:14 +01:00
}),
2018-06-06 17:29:30 +01:00
it ( "should accept no override arguments and just use the default values" , () => {
const result = chef . powerSet ( "1,2,3" );
assert . equal ( result . value , "\n3\n2\n1\n2,3\n1,3\n1,2\n1,2,3\n" );
}),
it ( "should return an object with a .to method" , () => {
const result = chef . toBase32 ( "input" );
assert ( result . to );
assert . equal ( result . to ( "string" ), "NFXHA5LU" );
}),
it ( "should return an object with a .get method" , () => {
const result = chef . toBase32 ( "input" );
assert ( result . get );
assert . equal ( result . get ( "string" ), "NFXHA5LU" );
}),
it ( "should return a SyncDish" , () => {
const result = chef . toBase32 ( "input" );
assert ( result instanceof SyncDish );
}),
it ( "should coerce to a string as you expect" , () => {
const result = chef . fromBase32 ( chef . toBase32 ( "something" ));
assert . equal ( String ( result ), "something" );
// This kind of coercion uses toValue
assert . equal ( "" + result , "NaN" );
}),
it ( "should coerce to a number as you expect" , () => {
const result = chef . fromBase32 ( chef . toBase32 ( "32" ));
assert . equal ( 3 + result , 35 );
}),
2018-06-19 16:41:16 +01:00
it ( "chef.help: should exist" , () => {
assert ( chef . help );
}),
it ( "chef.help: should describe a operation" , () => {
const result = chef . help ( "tripleDESDecrypt" );
2018-07-06 11:13:38 +01:00
assert . strictEqual ( result . name , "Triple DES Decrypt" );
2018-06-19 16:41:16 +01:00
assert . strictEqual ( result . module , "Ciphers" );
assert . strictEqual ( result . inputType , "string" );
assert . strictEqual ( result . outputType , "string" );
assert . strictEqual ( result . description , "Triple DES applies DES three times to each block to increase key size.<br><br><b>Key:</b> Triple DES uses a key length of 24 bytes (192 bits).<br>DES uses a key length of 8 bytes (64 bits).<br><br><b>IV:</b> The Initialization Vector should be 8 bytes long. If not entered, it will default to 8 null bytes.<br><br><b>Padding:</b> In CBC and ECB mode, PKCS#7 padding will be used." );
assert . strictEqual ( result . args . length , 5 );
}),
it ( "chef.help: null for invalid operation" , () => {
const result = chef . help ( "some invalid function name" );
assert . strictEqual ( result , null );
}),
2018-06-25 17:58:57 +01:00
it ( "chef.help: takes a wrapped operation as input" , () => {
const result = chef . help ( chef . toBase32 );
2018-07-06 11:13:38 +01:00
assert . strictEqual ( result . name , "To Base32" );
2018-06-25 17:58:57 +01:00
assert . strictEqual ( result . module , "Default" );
})
2018-05-03 10:20:13 +01:00
]);