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