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