@@ -27,7 +27,7 @@ import { Subscription } from "rxjs/Subscription";
2727
2828import Servient from "../src/servient" ;
2929import ConsumedThing from "../src/consumed-thing" ;
30- import { Form , SecurityScheme } from "../src/thing-description" ;
30+ import { AllOfSecurityScheme , Form , OneOfSecurityScheme , SecurityScheme } from "../src/thing-description" ;
3131import { ProtocolClient , ProtocolClientFactory } from "../src/protocol-interfaces" ;
3232import { Content } from "../src/content" ;
3333import { ContentSerdes } from "../src/content-serdes" ;
@@ -806,7 +806,7 @@ class WoTClientTest {
806806 expect ( WoTClientTest . servient . hasClientFor ( tcf2 . scheme ) ) . to . be . not . true ;
807807 }
808808
809- @test "ensure combo security" ( ) {
809+ @test "ensure combo security - allOf " ( ) {
810810 const ct = new ConsumedThing ( WoTClientTest . servient ) ;
811811 ct . securityDefinitions = {
812812 basic_sc : {
@@ -829,12 +829,69 @@ class WoTClientTest {
829829 href : "https://example.com/" ,
830830 } ;
831831 ct . ensureClientSecurity ( pc , form ) ;
832- expect ( pc . securitySchemes . length ) . equals ( 2 ) ;
833- expect ( pc . securitySchemes [ 0 ] . scheme ) . equals ( "opcua-channel-security" ) ;
834- expect ( pc . securitySchemes [ 1 ] . scheme ) . equals ( "opcua-authentication" ) ;
832+ expect ( pc . securitySchemes . length ) . equals ( 1 ) ;
833+ expect ( pc . securitySchemes [ 0 ] . scheme ) . equals ( "combo" ) ;
834+
835+ const comboScheme = pc . securitySchemes [ 0 ] as AllOfSecurityScheme ;
836+ expect ( comboScheme . allOf ) . instanceOf ( Array ) ;
837+ expect ( comboScheme . allOf . length ) . equal ( 2 ) ;
838+ expect ( comboScheme . allOf [ 0 ] . scheme ) . equals ( "opcua-channel-security" ) ;
839+ expect ( comboScheme . allOf [ 1 ] . scheme ) . equals ( "opcua-authentication" ) ;
840+ }
841+
842+ @test "ensure combo security - oneOf" ( ) {
843+ const ct = new ConsumedThing ( WoTClientTest . servient ) ;
844+ ct . securityDefinitions = {
845+ basic_sc : {
846+ scheme : "basic" ,
847+ } ,
848+ opcua_secure_channel_encrypt_sc : {
849+ scheme : "opcua-channel-security" ,
850+ mode : "encrypt" ,
851+ } ,
852+ opcua_secure_channel_sign_sc : {
853+ scheme : "opcua-channel-security" ,
854+ mode : "sign" ,
855+ } ,
856+ opcua_authetication_sc : {
857+ scheme : "opcua-authentication" ,
858+ } ,
859+ comob_opcua_secure_channel : {
860+ scheme : "combo" ,
861+ oneOf : [ "opcua_secure_channel_encrypt_sc" , "opcua_secure_channel_sign_sc" ] ,
862+ } ,
863+ combo_sc : {
864+ scheme : "combo" ,
865+ allOf : [ "comob_opcua_secure_channel" , "opcua_authetication_sc" ] ,
866+ } ,
867+ } ;
868+ ct . security = [ "combo_sc" ] ;
869+ const pc = new TestProtocolClient ( ) ;
870+ const form : Form = {
871+ href : "https://example.com/" ,
872+ } ;
873+ ct . ensureClientSecurity ( pc , form ) ;
874+ expect ( pc . securitySchemes . length ) . equals ( 1 ) ;
875+ expect ( pc . securitySchemes [ 0 ] . scheme ) . equals ( "combo" ) ;
876+
877+ const comboScheme = pc . securitySchemes [ 0 ] as AllOfSecurityScheme ;
878+
879+ expect ( comboScheme . allOf ) . instanceOf ( Array ) ;
880+ expect ( comboScheme . allOf . length ) . equal ( 2 ) ;
881+ expect ( comboScheme . allOf [ 0 ] . scheme ) . equals ( "combo" ) ;
882+ expect ( comboScheme . allOf [ 1 ] . scheme ) . equals ( "opcua-authentication" ) ;
883+
884+ //
885+ const firstScheme = comboScheme . allOf [ 0 ] as OneOfSecurityScheme ;
886+ expect ( firstScheme . scheme ) . equal ( "combo" ) ;
887+ expect ( firstScheme . oneOf ) . instanceOf ( Array ) ;
888+
889+ expect ( firstScheme . oneOf . length ) . equal ( 2 ) ;
890+ expect ( firstScheme . oneOf [ 0 ] . scheme ) . equal ( "opcua-channel-security" ) ;
891+ expect ( firstScheme . oneOf [ 0 ] . scheme ) . equal ( "opcua-channel-security" ) ;
835892 }
836893
837- @test "ensure combo security in form" ( ) {
894+ @test "ensure combo security in form - allOf " ( ) {
838895 const ct = new ConsumedThing ( WoTClientTest . servient ) ;
839896 ct . securityDefinitions = {
840897 basic_sc : {
@@ -858,9 +915,11 @@ class WoTClientTest {
858915 security : [ "combo_sc" ] ,
859916 } ;
860917 ct . ensureClientSecurity ( pc , form ) ;
861- expect ( pc . securitySchemes . length ) . equals ( 2 ) ;
862- expect ( pc . securitySchemes [ 0 ] . scheme ) . equals ( "opcua-channel-security" ) ;
863- expect ( pc . securitySchemes [ 1 ] . scheme ) . equals ( "opcua-authentication" ) ;
918+ expect ( pc . securitySchemes . length ) . equals ( 1 ) ;
919+ const comboScheme = pc . securitySchemes [ 0 ] as AllOfSecurityScheme ;
920+
921+ expect ( comboScheme . allOf [ 0 ] . scheme ) . equals ( "opcua-channel-security" ) ;
922+ expect ( comboScheme . allOf [ 1 ] . scheme ) . equals ( "opcua-authentication" ) ;
864923 }
865924
866925 @test "ensure no infinite loop with recursive combo security" ( ) {
@@ -879,6 +938,66 @@ class WoTClientTest {
879938 security : [ "combo_sc" ] ,
880939 } ;
881940 ct . ensureClientSecurity ( pc , form ) ;
882- expect ( pc . securitySchemes . length ) . equals ( 0 ) ;
941+ expect ( pc . securitySchemes . length ) . equals ( 1 ) ;
942+ }
943+
944+ @test "complex combo security with repeated elements" ( ) {
945+ const ct = new ConsumedThing ( WoTClientTest . servient ) ;
946+ ct . securityDefinitions = {
947+ // a badly designed combo that goes into infinite loop
948+ a : {
949+ scheme : "a" ,
950+ } ,
951+ b : {
952+ scheme : "b" ,
953+ } ,
954+ c : {
955+ scheme : "c" ,
956+ } ,
957+ combo_a_and_b : {
958+ scheme : "combo" ,
959+ allOf : [ "a" , "b" ] ,
960+ } ,
961+ combo_a_and_c : {
962+ scheme : "combo" ,
963+ allOf : [ "a" , "c" ] ,
964+ } ,
965+ combo_a_or_b : {
966+ scheme : "combo" ,
967+ oneOf : [ "a" , "b" ] ,
968+ } ,
969+ combo_of_combo : {
970+ scheme : "combo" ,
971+ oneOf : [ "combo_a_and_b" , "combo_a_and_c" ] ,
972+ } ,
973+ } ;
974+ ct . security = [ "combo_of_combo" ] ;
975+ const pc = new TestProtocolClient ( ) ;
976+ const form : Form = {
977+ href : "https://example.com/" ,
978+ } ;
979+ ct . ensureClientSecurity ( pc , form ) ;
980+ expect ( pc . securitySchemes . length ) . equals ( 1 ) ;
981+ expect ( pc . securitySchemes [ 0 ] . scheme ) . equal ( "combo" ) ;
982+ const comboOfCombo = pc . securitySchemes [ 0 ] as OneOfSecurityScheme ;
983+ expect ( comboOfCombo . oneOf ) . instanceOf ( Array ) ;
984+ expect ( comboOfCombo . oneOf . length ) . equal ( 2 ) ;
985+ expect ( comboOfCombo . oneOf [ 0 ] . scheme ) . equal ( "combo" ) ;
986+ expect ( comboOfCombo . oneOf [ 1 ] . scheme ) . equal ( "combo" ) ;
987+
988+ const first = comboOfCombo . oneOf [ 0 ] as AllOfSecurityScheme ;
989+ expect ( first . allOf ) . instanceOf ( Array ) ;
990+ expect ( first . allOf [ 0 ] . scheme ) . equal ( "a" ) ;
991+ expect ( first . allOf [ 1 ] . scheme ) . equal ( "b" ) ;
992+
993+ const second = comboOfCombo . oneOf [ 1 ] as AllOfSecurityScheme ;
994+ expect ( second . allOf ) . instanceOf ( Array ) ;
995+ expect ( second . allOf [ 0 ] . scheme ) . equal ( "a" ) ;
996+ expect ( second . allOf [ 1 ] . scheme ) . equal ( "c" ) ;
997+
998+ // Verfy that a has been processed once - with strict equality
999+ const a1 = first . allOf [ 0 ] ;
1000+ const a2 = second . allOf [ 0 ] ;
1001+ expect ( a1 ) . equals ( a2 ) ;
8831002 }
8841003}
0 commit comments