@@ -77,6 +77,44 @@ describe('trust-on-first-use', function() {
7777
7878 var driver ;
7979
80+ it ( 'should not throw an error if the host file contains two host duplicates' , function ( done ) {
81+ 'use strict' ;
82+ // Assuming we only run this test on NodeJS with TOFU support
83+ if ( ! hasFeature ( "trust_on_first_use" ) ) {
84+ done ( ) ;
85+ return ;
86+ }
87+
88+ // Given
89+ var knownHostsPath = "build/known_hosts" ;
90+ if ( fs . existsSync ( knownHostsPath ) ) {
91+ fs . unlinkSync ( knownHostsPath ) ;
92+ }
93+
94+ driver = neo4j . driver ( "bolt://localhost" , neo4j . auth . basic ( "neo4j" , "neo4j" ) , {
95+ encrypted : true ,
96+ trust : "TRUST_ON_FIRST_USE" ,
97+ knownHosts : knownHostsPath
98+ } ) ;
99+
100+ driver . session ( ) ; // write into the knownHost file
101+
102+ // duplicate the same serverId twice
103+ setTimeout ( function ( ) {
104+ var text = fs . readFileSync ( knownHostsPath , 'utf8' ) ;
105+ fs . writeFileSync ( knownHostsPath , text + text ) ;
106+ } , 1000 ) ;
107+
108+ // When
109+ setTimeout ( function ( ) {
110+ driver . session ( ) . run ( "RETURN true AS a" ) . then ( function ( data ) {
111+ // Then we get to here.
112+ expect ( data . records [ 0 ] . get ( 'a' ) ) . toBe ( true ) ;
113+ done ( ) ;
114+ } ) ;
115+ } , 2000 ) ;
116+ } ) ;
117+
80118 it ( 'should accept previously un-seen hosts' , function ( done ) {
81119 // Assuming we only run this test on NodeJS with TOFU support
82120 if ( ! hasFeature ( "trust_on_first_use" ) ) {
@@ -105,6 +143,59 @@ describe('trust-on-first-use', function() {
105143 } ) ;
106144 } ) ;
107145
146+ it ( 'should not duplicate fingerprint entries' , function ( done ) {
147+ // Assuming we only run this test on NodeJS with TOFU support
148+ if ( ! hasFeature ( "trust_on_first_use" ) ) {
149+ done ( ) ;
150+ return ;
151+ }
152+
153+ // Given
154+ var knownHostsPath = "build/known_hosts" ;
155+ if ( fs . existsSync ( knownHostsPath ) ) {
156+ fs . unlinkSync ( knownHostsPath ) ;
157+ }
158+ fs . writeFileSync ( knownHostsPath , '' ) ;
159+
160+ driver = neo4j . driver ( "bolt://localhost" , neo4j . auth . basic ( "neo4j" , "neo4j" ) , {
161+ encrypted : true ,
162+ trust : "TRUST_ON_FIRST_USE" ,
163+ knownHosts : knownHostsPath
164+ } ) ;
165+
166+ // When
167+ driver . session ( ) ;
168+ driver . session ( ) ;
169+
170+ setTimeout ( function ( ) {
171+ var lines = { } ;
172+ fs . readFileSync ( knownHostsPath , 'utf8' )
173+ . split ( '\n' )
174+ . filter ( function ( line ) {
175+ return ! ! ( line . trim ( ) ) ;
176+ } )
177+ . forEach ( function ( line ) {
178+ if ( ! lines [ line ] ) {
179+ lines [ line ] = 0 ;
180+ }
181+ lines [ line ] ++ ;
182+ } ) ;
183+
184+ var duplicatedLines = Object
185+ . keys ( lines )
186+ . map ( function ( line ) {
187+ return lines [ line ] ;
188+ } )
189+ . filter ( function ( count ) {
190+ return count > 1 ;
191+ } )
192+ . length ;
193+
194+ expect ( duplicatedLines ) . toBe ( 0 ) ;
195+ done ( ) ;
196+ } , 1000 ) ;
197+ } ) ;
198+
108199 it ( 'should should give helpful error if database cert does not match stored certificate' , function ( done ) {
109200 // Assuming we only run this test on NodeJS with TOFU support
110201 if ( ! hasFeature ( "trust_on_first_use" ) ) {
0 commit comments