Ejemplo n.º 1
0
func TestClientManagerGetMaxSizeExceeded(t *testing.T) {
	manager := apns2.NewClientManager()
	manager.MaxSize = 1
	cert1 := mockCert()
	_ = manager.Get(cert1)
	cert2, _ := certificate.FromP12File("certificate/_fixtures/certificate-valid.p12", "")
	_ = manager.Get(cert2)
	cert3, _ := certificate.FromP12File("certificate/_fixtures/certificate-valid-encrypted.p12", "password")
	c := manager.Get(cert3)
	assert.True(t, bytes.Equal(cert3.Certificate[0], c.Certificate.Certificate[0]))
	assert.Equal(t, 1, manager.Len())
}
Ejemplo n.º 2
0
func main() {

	cert, err := certificate.FromP12File("../cert.p12", "")
	if err != nil {
		log.Fatal("Cert Error:", err)
	}

	notification := &apns2.Notification{}
	notification.DeviceToken = "11aa01229f15f0f0c52029d8cf8cd0aeaf2365fe4cebc4af26cd6d76b7919ef7"
	notification.Topic = "com.sideshow.Apns2"
	notification.Payload = []byte(`{
			"aps" : {
				"alert" : "Hello!"
			}
		}
	`)

	client := apns2.NewClient(cert).Production()
	res, err := client.Push(notification)

	if err != nil {
		log.Fatal("Error:", err)
	}

	fmt.Printf("%v %v %v\n", res.StatusCode, res.ApnsID, res.Reason)
}
Ejemplo n.º 3
0
func TestClientNameToCertificate(t *testing.T) {
	certificate, _ := certificate.FromP12File("certificate/_fixtures/certificate-valid.p12", "")
	client := apns.NewClient(certificate)
	name := client.HTTPClient.Transport.(*http2.Transport).TLSClientConfig.NameToCertificate
	assert.Len(t, name, 1)

	certificate2 := tls.Certificate{}
	client2 := apns.NewClient(certificate2)
	name2 := client2.HTTPClient.Transport.(*http2.Transport).TLSClientConfig.NameToCertificate
	assert.Len(t, name2, 0)
}
Ejemplo n.º 4
0
func TestDialTLSTimeout(t *testing.T) {
	apns.TLSDialTimeout = 1 * time.Millisecond
	certificate, _ := certificate.FromP12File("certificate/_fixtures/certificate-valid.p12", "")
	client := apns.NewClient(certificate)
	dialTLS := client.HTTPClient.Transport.(*http2.Transport).DialTLS
	listener, err := net.Listen("tcp", "127.0.0.1:0")
	if err != nil {
		t.Fatal(err)
	}
	address := listener.Addr().String()
	defer listener.Close()
	var e error
	if _, e = dialTLS("tcp", address, nil); e == nil {
		t.Fatal("Dial completed successfully")
	}
	if !strings.Contains(e.Error(), "timed out") {
		t.Errorf("resulting error not a timeout: %s", e)
	}
}
Ejemplo n.º 5
0
func newPusher(c Config) (Pusher, error) {
	var (
		cert    tls.Certificate
		errCert error
	)
	if c.CertificateFileName != nil && *c.CertificateFileName != "" {
		cert, errCert = certificate.FromP12File(*c.CertificateFileName, *c.CertificatePassword)
	} else {
		cert, errCert = certificate.FromP12Bytes(*c.CertificateBytes, *c.CertificatePassword)
	}
	if errCert != nil {
		return nil, errCert
	}
	if *c.Production {
		logger.Debug("APNS Pusher in Production mode")
		return apns2.NewClient(cert).Production(), nil
	}
	logger.Debug("APNS Pusher in Development mode")
	return apns2.NewClient(cert).Development(), nil
}
Ejemplo n.º 6
0
func TestBadPasswordP12File(t *testing.T) {
	cer, err := certificate.FromP12File("_fixtures/certificate-valid-encrypted.p12", "")
	assert.Equal(t, tls.Certificate{}, cer)
	assert.Equal(t, errors.New("pkcs12: decryption password incorrect").Error(), err.Error())
}
Ejemplo n.º 7
0
func TestNoSuchFileP12File(t *testing.T) {
	cer, err := certificate.FromP12File("", "")
	assert.Equal(t, errors.New("open : no such file or directory").Error(), err.Error())
	assert.Equal(t, tls.Certificate{}, cer)
}
Ejemplo n.º 8
0
func TestEncryptedValidCertificateFromP12File(t *testing.T) {
	cer, err := certificate.FromP12File("_fixtures/certificate-valid-encrypted.p12", "password")
	assert.NoError(t, err)
	assert.Nil(t, verifyHostname(cer))
}
Ejemplo n.º 9
0
func TestValidCertificateFromP12File(t *testing.T) {
	cer, err := certificate.FromP12File("_fixtures/certificate-valid.p12", "")
	assert.Nil(t, err)
	assert.Nil(t, verifyHostname(cer))
}