|
| 1 | +package config |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/json" |
| 5 | + "testing" |
| 6 | +) |
| 7 | + |
| 8 | +func TestMigrationDecode(t *testing.T) { |
| 9 | + str := ` |
| 10 | + { |
| 11 | + "DownloadSources": ["IPFS", "HTTP", "127.0.0.1"], |
| 12 | + "Keep": "cache", |
| 13 | + "Peers": [ |
| 14 | + { |
| 15 | + "ID": "12D3KooWGC6TvWhfapngX6wvJHMYvKpDMXPb3ZnCZ6dMoaMtimQ5", |
| 16 | + "Addrs": ["/ip4/127.0.0.1/tcp/4001", "/ip4/127.0.0.1/udp/4001/quic"] |
| 17 | + }, |
| 18 | + { |
| 19 | + "ID": "12D3KooWGC6TvWhfajpgX6wvJHMYvKpDMXPb3ZnCZ6dMoaMtimQ7", |
| 20 | + "Addrs": ["/ip4/10.0.0.2/tcp/4001"] |
| 21 | + } |
| 22 | + ] |
| 23 | + } |
| 24 | + ` |
| 25 | + |
| 26 | + var cfg Migration |
| 27 | + if err := json.Unmarshal([]byte(str), &cfg); err != nil { |
| 28 | + t.Errorf("failed while unmarshalling migration struct: %s", err) |
| 29 | + } |
| 30 | + |
| 31 | + if len(cfg.DownloadSources) != 3 { |
| 32 | + t.Fatal("wrong number of DownloadSources") |
| 33 | + } |
| 34 | + expect := []string{"IPFS", "HTTP", "127.0.0.1"} |
| 35 | + for i := range expect { |
| 36 | + if cfg.DownloadSources[i] != expect[i] { |
| 37 | + t.Errorf("wrong DownloadSource at %d", i) |
| 38 | + } |
| 39 | + } |
| 40 | + |
| 41 | + if cfg.Keep != "cache" { |
| 42 | + t.Error("wrong value for Keep") |
| 43 | + } |
| 44 | + |
| 45 | + if len(cfg.Peers) != 2 { |
| 46 | + t.Fatal("wrong number of peers") |
| 47 | + } |
| 48 | + |
| 49 | + peer := cfg.Peers[0] |
| 50 | + if peer.ID.String() != "12D3KooWGC6TvWhfapngX6wvJHMYvKpDMXPb3ZnCZ6dMoaMtimQ5" { |
| 51 | + t.Errorf("wrong ID for first peer") |
| 52 | + } |
| 53 | + if len(peer.Addrs) != 2 { |
| 54 | + t.Error("wrong number of addrs for first peer") |
| 55 | + } |
| 56 | + if peer.Addrs[0].String() != "/ip4/127.0.0.1/tcp/4001" { |
| 57 | + t.Error("wrong first addr for first peer") |
| 58 | + } |
| 59 | + if peer.Addrs[1].String() != "/ip4/127.0.0.1/udp/4001/quic" { |
| 60 | + t.Error("wrong second addr for first peer") |
| 61 | + } |
| 62 | + |
| 63 | + peer = cfg.Peers[1] |
| 64 | + if len(peer.Addrs) != 1 { |
| 65 | + t.Fatal("wrong number of addrs for second peer") |
| 66 | + } |
| 67 | + if peer.Addrs[0].String() != "/ip4/10.0.0.2/tcp/4001" { |
| 68 | + t.Error("wrong first addr for second peer") |
| 69 | + } |
| 70 | +} |
0 commit comments