Compare commits
	
		
			4 Commits
		
	
	
		
			master
			...
			keyFromArg
		
	
	| Author | SHA1 | Date | 
|---|---|---|
|  | 77794be7db | |
|  | 08433db912 | |
|  | a6ef43c637 | |
|  | c079f0dace | 
|  | @ -36,6 +36,6 @@ func (ebs *EchoBotService) OnChatMessage(oc *goricochet.OpenConnection, channelI | |||
| 
 | ||||
| func main() { | ||||
| 	ricochetService := new(EchoBotService) | ||||
| 	ricochetService.Init("./private_key") | ||||
| 	ricochetService.InitFromKeyFile("./private_key") | ||||
| 	ricochetService.Listen(ricochetService, 12345) | ||||
| } | ||||
|  |  | |||
|  | @ -22,21 +22,26 @@ type StandardRicochetService struct { | |||
| 
 | ||||
| // Init initializes a StandardRicochetService with the cryptographic key given
 | ||||
| // by filename.
 | ||||
| func (srs *StandardRicochetService) Init(filename string) error { | ||||
| 	srs.ricochet = new(Ricochet) | ||||
| 	srs.ricochet.Init() | ||||
| 
 | ||||
| func (srs *StandardRicochetService) InitFromKeyFile(filename string) error { | ||||
| 	pemData, err := ioutil.ReadFile(filename) | ||||
| 
 | ||||
| 	if err != nil { | ||||
| 		return errors.New("Could not setup ricochet service: could not read private key") | ||||
| 	} | ||||
| 	return srs.InitFromKey(pemData) | ||||
| } | ||||
| 
 | ||||
| // Init initializes a StandardRicochetService with the cryptographic key
 | ||||
| func (srs *StandardRicochetService) InitFromKey(pemData []byte) error { | ||||
| 	srs.ricochet = new(Ricochet) | ||||
| 	srs.ricochet.Init() | ||||
| 
 | ||||
| 	block, _ := pem.Decode(pemData) | ||||
| 	if block == nil || block.Type != "RSA PRIVATE KEY" { | ||||
| 		return errors.New("Could not setup ricochet service: no valid PEM data found") | ||||
| 	} | ||||
| 
 | ||||
| 	var err error | ||||
| 	srs.privateKey, err = x509.ParsePKCS1PrivateKey(block.Bytes) | ||||
| 	if err != nil { | ||||
| 		return errors.New("Could not setup ricochet service: could not parse private key") | ||||
|  | @ -180,3 +185,8 @@ func (srs *StandardRicochetService) OnBadUsageError(oc *OpenConnection, channelI | |||
| func (srs *StandardRicochetService) OnFailedError(oc *OpenConnection, channelID int32) { | ||||
| 	oc.RejectOpenChannel(channelID, "FailedError") | ||||
| } | ||||
| 
 | ||||
| // RGetServerHostname returns the generated tor hostname from the private key
 | ||||
| func (srs *StandardRicochetService) GetServerHostname() string { | ||||
| 	return srs.serverHostname | ||||
| } | ||||
|  | @ -77,7 +77,7 @@ func (ts *TestBadUsageService) IsKnownContact(hostname string) bool { | |||
| 
 | ||||
| func TestBadUsageServer(t *testing.T) { | ||||
| 	ricochetService := new(TestBadUsageService) | ||||
| 	err := ricochetService.Init("./private_key") | ||||
| 	err := ricochetService.InitFromKeyFile("./private_key") | ||||
| 
 | ||||
| 	if err != nil { | ||||
| 		t.Errorf("Could not initate ricochet service: %v", err) | ||||
|  | @ -88,7 +88,7 @@ func TestBadUsageServer(t *testing.T) { | |||
| 	time.Sleep(time.Second * 2) | ||||
| 
 | ||||
| 	ricochetService2 := new(TestBadUsageService) | ||||
| 	err = ricochetService2.Init("./private_key") | ||||
| 	err = ricochetService2.InitFromKeyFile("./private_key") | ||||
| 
 | ||||
| 	if err != nil { | ||||
| 		t.Errorf("Could not initate ricochet service: %v", err) | ||||
|  |  | |||
|  | @ -55,7 +55,7 @@ func (ts *TestService) IsKnownContact(hostname string) bool { | |||
| 
 | ||||
| func TestServer(t *testing.T) { | ||||
| 	ricochetService := new(TestService) | ||||
| 	err := ricochetService.Init("./private_key") | ||||
| 	err := ricochetService.InitFromKeyFile("./private_key") | ||||
| 
 | ||||
| 	if err != nil { | ||||
| 		t.Errorf("Could not initate ricochet service: %v", err) | ||||
|  | @ -66,7 +66,7 @@ func TestServer(t *testing.T) { | |||
| 	time.Sleep(time.Second * 2) | ||||
| 
 | ||||
| 	ricochetService2 := new(TestService) | ||||
| 	err = ricochetService2.Init("./private_key") | ||||
| 	err = ricochetService2.InitFromKeyFile("./private_key") | ||||
| 
 | ||||
| 	if err != nil { | ||||
| 		t.Errorf("Could not initate ricochet service: %v", err) | ||||
|  | @ -87,7 +87,7 @@ func TestServer(t *testing.T) { | |||
| 
 | ||||
| func TestServerInvalidKey(t *testing.T) { | ||||
| 	ricochetService := new(TestService) | ||||
| 	err := ricochetService.Init("./private_key.does.not.exist") | ||||
| 	err := ricochetService.InitFromKeyFile("./private_key.does.not.exist") | ||||
| 
 | ||||
| 	if err == nil { | ||||
| 		t.Errorf("Should not have initate ricochet service, private key should not exist") | ||||
|  | @ -96,7 +96,7 @@ func TestServerInvalidKey(t *testing.T) { | |||
| 
 | ||||
| func TestServerCouldNotConnect(t *testing.T) { | ||||
| 	ricochetService := new(TestService) | ||||
| 	err := ricochetService.Init("./private_key") | ||||
| 	err := ricochetService.InitFromKeyFile("./private_key") | ||||
| 	if err != nil { | ||||
| 		t.Errorf("Could not initate ricochet service: %v", err) | ||||
| 	} | ||||
|  | @ -105,3 +105,14 @@ func TestServerCouldNotConnect(t *testing.T) { | |||
| 		t.Errorf("Should not have been been able to connect to 127.0.0.1:65535|kwke2hntvyfqm7dr") | ||||
| 	} | ||||
| } | ||||
| 
 | ||||
| func TestGetServerHostname(t *testing.T) { | ||||
| 	ricochetService := new(TestService) | ||||
| 	err := ricochetService.InitFromKeyFile("./private_key") | ||||
| 	if err != nil { | ||||
| 		t.Errorf("Could not initate ricochet service: %v", err) | ||||
| 	} | ||||
| 	if ricochetService.GetServerHostname() != "kwke2hntvyfqm7dr" { | ||||
| 		t.Errorf("GetServerHostname did not return expected 'kwke2hntvyfqm7dr'") | ||||
| 	} | ||||
| } | ||||
|  | @ -32,7 +32,7 @@ func (ts *TestUnauthorizedService) OnFailedChannelOpen(oc *OpenConnection, chann | |||
| 
 | ||||
| func TestUnauthorizedClientReject(t *testing.T) { | ||||
| 	ricochetService := new(TestService) | ||||
| 	err := ricochetService.Init("./private_key") | ||||
| 	err := ricochetService.InitFromKeyFile("./private_key") | ||||
| 
 | ||||
| 	if err != nil { | ||||
| 		t.Errorf("Could not initate ricochet service: %v", err) | ||||
|  | @ -43,7 +43,7 @@ func TestUnauthorizedClientReject(t *testing.T) { | |||
| 	time.Sleep(time.Second * 2) | ||||
| 
 | ||||
| 	ricochetService2 := new(TestUnauthorizedService) | ||||
| 	err = ricochetService2.Init("./private_key") | ||||
| 	err = ricochetService2.InitFromKeyFile("./private_key") | ||||
| 
 | ||||
| 	if err != nil { | ||||
| 		t.Errorf("Could not initate ricochet service: %v", err) | ||||
|  |  | |||
|  | @ -29,7 +29,7 @@ func (ts *TestUnknownContactService) IsKnownContact(hostname string) bool { | |||
| 
 | ||||
| func TestUnknownContactServer(t *testing.T) { | ||||
| 	ricochetService := new(StandardRicochetService) | ||||
| 	err := ricochetService.Init("./private_key") | ||||
| 	err := ricochetService.InitFromKeyFile("./private_key") | ||||
| 
 | ||||
| 	if err != nil { | ||||
| 		t.Errorf("Could not initate ricochet service: %v", err) | ||||
|  | @ -40,7 +40,7 @@ func TestUnknownContactServer(t *testing.T) { | |||
| 	time.Sleep(time.Second * 2) | ||||
| 
 | ||||
| 	ricochetService2 := new(TestUnknownContactService) | ||||
| 	err = ricochetService2.Init("./private_key") | ||||
| 	err = ricochetService2.InitFromKeyFile("./private_key") | ||||
| 
 | ||||
| 	if err != nil { | ||||
| 		t.Errorf("Could not initate ricochet service: %v", err) | ||||
|  |  | |||
		Loading…
	
		Reference in New Issue