func TestCache(t *testing.T) { c, err := New("test.db") if err != nil { t.Fatal(err) } for _, test := range []string{ "/A/B", "/A", "/A", "/A/B/C", "/B", } { d := &ndn.Data{ Name: ndn.NewName(test), } c.Add(d) } for _, test := range []struct { in string want string }{ {"/A", "/A/B/C"}, {"/A/B", "/A/B/C"}, {"/C", ""}, } { d := c.Get(&ndn.Interest{ Name: ndn.NewName(test.in), Selectors: ndn.Selectors{ ChildSelector: 1, }, }) var got string if d != nil { got = d.Name.String() } if got != test.want { t.Fatalf("Get(%v) == %v, got %v", test.in, test.want, got) } } }
func main() { var ( identity string keyType string file string ) flag.StringVar(&identity, "identity", "/ndn/guest/alice", "identity") flag.StringVar(&keyType, "type", "rsa", "[ rsa | ecdsa | hmac ]") flag.StringVar(&file, "file", "default", "file name for private key and certificate") flag.Parse() var ( name = ndn.NewName(fmt.Sprintf("%s/%d/KEY/%%00%%00", identity, time.Now().UnixNano()/1000000)) key ndn.Key ) switch keyType { case "rsa": pri, err := rsa.GenerateKey(rand.Reader, 2048) if err != nil { log.Println(err) return } key = &ndn.RSAKey{ Name: name, PrivateKey: pri, } case "ecdsa": pri, err := ecdsa.GenerateKey(elliptic.P224(), rand.Reader) if err != nil { log.Println(err) return } key = &ndn.ECDSAKey{ Name: name, PrivateKey: pri, } case "hmac": // 256 / 8 = 32 pri := make([]byte, 32) rand.Read(pri) key = &ndn.HMACKey{ Name: name, PrivateKey: pri, } default: log.Println("unsupported key type") return } // private key pem, err := os.Create(file + ".pri") if err != nil { log.Println(err) return } defer pem.Close() err = ndn.EncodePrivateKey(key, pem) if err != nil { log.Println(err) return } // certificate cert, err := os.Create(file + ".ndncert") if err != nil { log.Println(err) return } defer cert.Close() err = ndn.EncodeCertificate(key, cert) if err != nil { log.Println(err) return } log.Println(name, "exported") }
func main() { flag.Parse() var data []byte var retry int = 0 // config configFile, err := os.Open(*configPath) if err != nil { log.Fatalln(err) } defer configFile.Close() err = json.NewDecoder(configFile).Decode(&config) if err != nil { log.Fatalln(err) } // connect to nfd conn, err := packet.Dial(config.NFD.Network, config.NFD.Address) if err != nil { log.Fatalln(err) } // start a new face but do not receive new interests face := ndn.NewFace(conn, nil) defer face.Close() // read producer key pem, err := os.Open(config.PrivateKeyPath) if err != nil { log.Fatalln(err) } defer pem.Close() key, _ := ndn.DecodePrivateKey(pem) // create a data fetcher f := mux.NewFetcher() // 0. a data packet comes // 1. verifiy checksum f.Use(mux.ChecksumVerifier) // 2. add the data to the in-memory cache f.Use(mux.Cacher) // 3. logging f.Use(mux.Logger) // see producer // 4. assemble segments if the content has multiple segments // 5. decrypt dec := mux.Decryptor(key.(*ndn.RSAKey)) // 6. unzip fileSplit := strings.Split(*filePrefix, "/") fileName := fileSplit[len(fileSplit)-1] file, err := os.Create(fileName) if err != nil { log.Fatalln(err) } defer file.Close() fmt.Printf("\nFetching file %s from ndn:%s\n\n", fileName, *filePrefix) retry_limit := 10 if config.RetryLimit != 0 { retry_limit = config.RetryLimit } for retry = 0; retry <= retry_limit; retry++ { data = f.Fetch(face, &ndn.Interest{Name: ndn.NewName(*filePrefix)}, mux.Assembler, dec, mux.Gunzipper) if data != nil { break } else { fmt.Println("Empty data!") } } if retry <= retry_limit { databytes, err := io.Copy(file, bytes.NewReader(data)) if err != nil { log.Fatalln(err) } fmt.Printf("wrote %d bytes\n", databytes) } else { fmt.Printf("\nFailed to fetch %s file after %d times retry attempt.\n\n", fileName, retry) err := os.Remove(fileName) if err != nil { log.Fatalln(err) } } }