2
2
/* eslint-env mocha */
3
3
'use strict'
4
4
5
- const hat = require ( 'hat' )
6
-
7
- const { fixture } = require ( './utils' )
8
5
const { spawnNodeWithId } = require ( '../utils/spawn' )
9
6
const { getDescribe, getIt, expect } = require ( '../utils/mocha' )
7
+ const delay = require ( '../utils/delay' )
10
8
11
9
module . exports = ( createCommon , options ) => {
12
10
const describe = getDescribe ( options )
13
11
const it = getIt ( options )
14
- const common = createCommon ( )
15
12
16
13
describe ( '.name.resolve offline' , function ( ) {
17
- const keyName = hat ( )
14
+ const common = createCommon ( )
18
15
let ipfs
19
16
let nodeId
20
- let keyId
21
17
22
18
before ( function ( done ) {
23
19
common . setup ( ( err , factory ) => {
@@ -28,136 +24,143 @@ module.exports = (createCommon, options) => {
28
24
29
25
ipfs = node
30
26
nodeId = node . peerId . id
31
-
32
- ipfs . add ( fixture . data , { pin : false } , done )
27
+ done ( )
33
28
} )
34
29
} )
35
30
} )
36
31
37
32
after ( ( done ) => common . teardown ( done ) )
38
33
39
- it ( 'should resolve a record with the default params after a publish' , function ( done ) {
40
- const value = fixture . cid
34
+ // This test should be changed to the recursive logic when recursive === true is the default
35
+ it ( 'should resolve a record default options' , async ( ) => {
36
+ const [ { path } ] = await ipfs . add ( Buffer . from ( 'should resolve a record default options' ) )
37
+ await ipfs . name . publish ( path , { 'allow-offline' : true } )
38
+ return expect ( ipfs . name . resolve ( `/ipns/${ nodeId } ` ) )
39
+ . to . become ( `/ipfs/${ path } ` )
40
+ } )
41
41
42
- ipfs . name . publish ( value , { 'allow-offline' : true } , ( err , res ) => {
43
- expect ( err ) . to . not . exist ( )
44
- expect ( res ) . to . exist ( )
42
+ it ( 'should resolve a record recursive === false' , async ( ) => {
43
+ const [ { path } ] = await ipfs . add ( Buffer . from ( 'should resolve a record recursive === false' ) )
44
+ await ipfs . name . publish ( path , { 'allow-offline' : true } )
45
+ return expect ( ipfs . name . resolve ( `/ipns/${ nodeId } ` , { recursive : false } ) )
46
+ . to . become ( `/ipfs/${ path } ` )
47
+ } )
45
48
46
- ipfs . name . resolve ( nodeId , ( err , res ) => {
47
- expect ( err ) . to . not . exist ( )
48
- expect ( res ) . to . equal ( `/ipfs/${ value } ` )
49
+ it ( 'should resolve a record recursive === true' , async ( ) => {
50
+ const [ { path } ] = await ipfs . add ( Buffer . from ( 'should resolve a record recursive === true' ) )
49
51
50
- done ( )
51
- } )
52
- } )
52
+ const { id : keyId } = await ipfs . key . gen ( 'key-name' , { type : 'rsa' , size : 2048 } )
53
+
54
+ await ipfs . name . publish ( path , { 'allow-offline' : true } )
55
+ await ipfs . name . publish ( `/ipns/${ nodeId } ` , { 'allow-offline' : true , key : 'key-name' } )
56
+
57
+ return expect ( ipfs . name . resolve ( `/ipns/${ keyId } ` , { recursive : true } ) )
58
+ . to . become ( `/ipfs/${ path } ` )
53
59
} )
54
60
55
- it ( 'should not get the entry if its validity time expired' , function ( done ) {
61
+ // This test should be changed to the recursive logic when recursive === true is the default
62
+ it ( 'should resolve a record default options with remainder' , async ( ) => {
63
+ const [ { path } ] = await ipfs . add ( Buffer . from ( 'should resolve a record with the default params with remainder' ) )
64
+ await ipfs . name . publish ( path , { 'allow-offline' : true } )
65
+ return expect ( ipfs . name . resolve ( `/ipns/${ nodeId } /remainder/file.txt` ) )
66
+ . to . become ( `/ipfs/${ path } /remainder/file.txt` )
67
+ } )
68
+
69
+ it ( 'should resolve a record recursive === false with remainder' , async ( ) => {
70
+ const [ { path } ] = await ipfs . add ( Buffer . from ( 'should resolve a record recursive = false with remainder' ) )
71
+ await ipfs . name . publish ( path , { 'allow-offline' : true } )
72
+ return expect ( ipfs . name . resolve ( `/ipns/${ nodeId } /remainder/file.txt` , { recursive : false } ) )
73
+ . to . become ( `/ipfs/${ path } /remainder/file.txt` )
74
+ } )
75
+
76
+ it ( 'should resolve a record recursive === true with remainder' , async ( ) => {
77
+ const [ { path } ] = await ipfs . add ( Buffer . from ( 'should resolve a record recursive = true with remainder' ) )
78
+
79
+ const { id : keyId } = await ipfs . key . gen ( 'key-name-remainder' , { type : 'rsa' , size : 2048 } )
80
+
81
+ await ipfs . name . publish ( path , { 'allow-offline' : true } )
82
+ await ipfs . name . publish ( `/ipns/${ nodeId } ` , { 'allow-offline' : true , key : 'key-name-remainder' } )
83
+
84
+ return expect ( ipfs . name . resolve ( `/ipns/${ keyId } /remainder/file.txt` , { recursive : true } ) )
85
+ . to . be . eventually . equal ( `/ipfs/${ path } /remainder/file.txt` )
86
+ } )
87
+
88
+ it ( 'should not get the entry if its validity time expired' , async ( ) => {
56
89
const publishOptions = {
57
- resolve : true ,
58
90
lifetime : '100ms' ,
59
91
ttl : '10s' ,
60
- key : 'self' ,
61
92
'allow-offline' : true
62
93
}
63
94
64
- ipfs . add ( Buffer . from ( 'should not get the entry if its validity time expired' ) , ( err , r ) => {
65
- expect ( err ) . to . not . exist ( )
66
- ipfs . name . publish ( r [ 0 ] . path , publishOptions , ( err , res ) => {
67
- expect ( err ) . to . not . exist ( )
68
- expect ( res ) . to . exist ( )
69
-
70
- // guarantee that the record has an expired validity.
71
- setTimeout ( function ( ) {
72
- ipfs . name . resolve ( nodeId , ( err , res ) => {
73
- // go only has 1 possible error https://github.com/ipfs/go-ipfs/blob/master/namesys/interface.go#L51
74
- // so here we just expect an Error and don't match the error type to expiration
75
- expect ( err ) . to . exist ( )
76
-
77
- done ( )
78
- } )
79
- } , 500 )
80
- } )
81
- } )
95
+ // we add new data instead of re-using fixture to make sure lifetime handling doesn't break
96
+ const [ { path } ] = await ipfs . add ( Buffer . from ( 'should not get the entry if its validity time expired' ) )
97
+ await ipfs . name . publish ( path , publishOptions )
98
+ await delay ( 500 )
99
+ // go only has 1 possible error https://github.com/ipfs/go-ipfs/blob/master/namesys/interface.go#L51
100
+ // so here we just expect an Error and don't match the error type to expiration
101
+ return expect ( ipfs . name . resolve ( nodeId ) ) . to . be . rejected ( )
82
102
} )
103
+ } )
83
104
84
- it ( 'should recursively resolve to an IPFS hash' , function ( done ) {
85
- const value = fixture . cid
86
- const publishOptions = {
87
- resolve : false ,
88
- lifetime : '24h' ,
89
- ttl : '10s' ,
90
- key : 'self' ,
91
- 'allow-offline' : true
92
- }
105
+ describe ( '.name.resolve dns' , function ( ) {
106
+ const common = createCommon ( )
107
+ let ipfs
108
+ this . retries ( 3 )
93
109
94
- // Generate new key
95
- ipfs . key . gen ( keyName , { type : 'rsa' , size : 2048 } , ( err , key ) => {
110
+ before ( function ( done ) {
111
+ common . setup ( ( err , factory ) => {
96
112
expect ( err ) . to . not . exist ( )
97
113
98
- keyId = key . id
99
-
100
- // publish ipfs
101
- ipfs . name . publish ( value , publishOptions , ( err , res ) => {
114
+ spawnNodeWithId ( factory , ( err , node ) => {
102
115
expect ( err ) . to . not . exist ( )
103
- expect ( res ) . to . exist ( )
104
-
105
- publishOptions . key = keyName
106
-
107
- // publish ipns with the generated key
108
- ipfs . name . publish ( `/ipns/${ nodeId } ` , publishOptions , ( err , res ) => {
109
- expect ( err ) . to . not . exist ( )
110
- expect ( res ) . to . exist ( )
111
116
112
- const resolveOptions = {
113
- nocache : false ,
114
- recursive : true
115
- }
116
-
117
- // recursive resolve (will get ipns first, and will resolve again to find the ipfs)
118
- ipfs . name . resolve ( keyId , resolveOptions , ( err , res ) => {
119
- expect ( err ) . to . not . exist ( )
120
- expect ( res ) . to . exist ( )
121
- expect ( res ) . to . equal ( `/ipfs/${ value } ` )
122
-
123
- done ( )
124
- } )
125
- } )
117
+ ipfs = node
118
+ done ( )
126
119
} )
127
120
} )
128
121
} )
129
122
130
- it ( 'should resolve /ipns/<hash>/remainder/file.txt' , async ( ) => {
131
- const add = await ipfs . add ( Buffer . from ( 'fake remainder' ) )
132
- await ipfs . name . publish ( add [ 0 ] . path , { 'allow-offline' : true } )
123
+ after ( ( done ) => common . teardown ( done ) )
133
124
134
- const resolve = await ipfs . name . resolve ( `/ipns/${ nodeId } /remainder/file.txt` )
125
+ // This test should be changed to the recursive logic when recursive === true is the default
126
+ it ( 'should resolve /ipns/ipfs.io' , async ( ) => {
127
+ return expect ( ipfs . name . resolve ( '/ipns/ipfs.io' ) )
128
+ . to . eventually . match ( / \/ i p n s \/ .+ $ / )
129
+ } )
135
130
136
- return expect ( resolve ) . to . match ( / \/ i p f s \/ .+ \/ r e m a i n d e r \/ f i l e .t x t $ / )
131
+ it ( 'should resolve /ipns/ipfs.io recursive === false' , async ( ) => {
132
+ return expect ( ipfs . name . resolve ( '/ipns/ipfs.io' , { recursive : false } ) )
133
+ . to . eventually . match ( / \/ i p n s \/ .+ $ / )
137
134
} )
138
135
139
- it ( 'should resolve ipfs.io' , async ( ) => {
140
- const r = await ipfs . name . resolve ( 'ipfs.io' , { recursive : false } )
141
- return expect ( r ) . to . match ( / \/ i p n s \/ .+ $ / )
136
+ it ( 'should resolve /ipns/ ipfs.io recursive === true ' , async ( ) => {
137
+ return expect ( ipfs . name . resolve ( '/ipns/ ipfs.io' , { recursive : true } ) )
138
+ . to . eventually . match ( / \/ i p f s \/ .+ $ / )
142
139
} )
143
140
144
- it ( 'should resolve ipfs.io with remainder' , async ( ) => {
145
- const r = await ipfs . name . resolve ( '/ipns/ipfs.io/images/ipfs-logo.svg' , { recursive : false } )
146
- return expect ( r ) . to . match ( / \/ i p n s \/ .+ \/ i m a g e s \/ i p f s - l o g o .s v g $ / )
141
+ // This test should be changed to the recursive logic when recursive === true is the default
142
+ it ( 'should resolve /ipns/ipfs.io with remainder' , async ( ) => {
143
+ return expect ( ipfs . name . resolve ( '/ipns/ipfs.io/images/ipfs-logo.svg' ) )
144
+ . to . eventually . match ( / \/ i p n s \/ .+ \/ i m a g e s \/ i p f s - l o g o .s v g $ / )
147
145
} )
148
146
149
- it ( 'should resolve /ipns/ipfs.io recursive' , async ( ) => {
150
- const r = await ipfs . name . resolve ( 'ipfs.io' , { recursive : true } )
151
- return expect ( r ) . to . match ( / \/ i p f s \/ .+ $ / )
147
+ it ( 'should resolve /ipns/ipfs.io with remainder recursive === false ' , async ( ) => {
148
+ return expect ( ipfs . name . resolve ( '/ipns/ ipfs.io/images/ipfs-logo.svg ' , { recursive : false } ) )
149
+ . to . eventually . match ( / \/ i p n s \/ .+ \/ i m a g e s \/ i p f s - l o g o . s v g $ / )
152
150
} )
153
151
154
- it ( 'should resolve /ipns/ipfs.io recursive with remainder' , async ( ) => {
155
- const r = await ipfs . name . resolve ( '/ipns/ipfs.io/images/ipfs-logo.svg' , { recursive : true } )
156
- return expect ( r ) . to . match ( / \/ i p f s \/ .+ \/ i m a g e s \/ i p f s - l o g o .s v g $ / )
152
+ it ( 'should resolve /ipns/ipfs.io with remainder recursive === true ' , async ( ) => {
153
+ return expect ( ipfs . name . resolve ( '/ipns/ipfs.io/images/ipfs-logo.svg' , { recursive : true } ) )
154
+ . to . eventually . match ( / \/ i p f s \/ .+ \/ i m a g e s \/ i p f s - l o g o .s v g $ / )
157
155
} )
158
156
159
157
it ( 'should fail to resolve /ipns/ipfs.a' , async ( ) => {
160
158
return expect ( ipfs . name . resolve ( 'ipfs.a' ) ) . to . be . rejected ( )
161
159
} )
160
+
161
+ it ( 'should resolve ipns path with hamt-shard recursive === true' , async ( ) => {
162
+ return expect ( ipfs . name . resolve ( '/ipns/tr.wikipedia-on-ipfs.org/wiki/Anasayfa.html' , { recursive : true } ) )
163
+ . to . eventually . match ( / \/ i p f s \/ .+ $ / )
164
+ } )
162
165
} )
163
166
}
0 commit comments