Parcourir la source

Merge branch 'feat/show-user-ip-cli-16' into dev

Mustafa Arici il y a 8 ans
Parent
commit
3c48230a1f
9 fichiers modifiés avec 105 ajouts et 165 suppressions
  1. 1 11
      api/rpc.go
  2. 2 23
      cmd/ovpm/main.go
  3. 32 25
      pb/user.pb.go
  4. 1 0
      pb/user.proto
  5. 21 74
      pb/vpn.pb.go
  6. 0 3
      pb/vpn.proto
  7. 21 0
      user.go
  8. 10 12
      vpn.go
  9. 17 17
      vpn_test.go

+ 1 - 11
api/rpc.go

@@ -1,5 +1,3 @@
-//go:generate protoc -I pb/ pb/user.proto pb/vpn.proto --go_out=plugins=grpc:pb
-
 package api
 
 import (
@@ -29,6 +27,7 @@ func (s *UserService) List(ctx context.Context, req *pb.UserListRequest) (*pb.Us
 			ServerSerialNumber: user.GetServerSerialNumber(),
 			Username:           user.GetUsername(),
 			CreatedAt:          user.GetCreatedAt(),
+			IPNet:              user.GetIPNet(),
 		})
 	}
 
@@ -140,12 +139,3 @@ func (s *VPNService) Init(ctx context.Context, req *pb.VPNInitRequest) (*pb.VPNI
 	}
 	return &pb.VPNInitResponse{}, nil
 }
-
-func (s *VPNService) Apply(ctx context.Context, req *pb.VPNApplyRequest) (*pb.VPNApplyResponse, error) {
-	logrus.Debugf("rpc call: vpn apply")
-	if err := ovpm.Emit(); err != nil {
-		logrus.Errorf("can not apply configuration: %v", err)
-		return nil, err
-	}
-	return &pb.VPNApplyResponse{}, nil
-}

+ 2 - 23
cmd/ovpm/main.go

@@ -1,5 +1,3 @@
-//go:generate go-bindata template/
-
 package main
 
 import (
@@ -68,10 +66,10 @@ func main() {
 							return err
 						}
 						table := tablewriter.NewWriter(os.Stdout)
-						table.SetHeader([]string{"#", "username", "created at", "valid crt"})
+						table.SetHeader([]string{"#", "username", "ip", "created at", "valid crt"})
 						//table.SetBorder(false)
 						for i, user := range resp.Users {
-							data := []string{fmt.Sprintf("%v", i+1), user.Username, user.CreatedAt, fmt.Sprintf("%t", user.ServerSerialNumber == server.SerialNumber)}
+							data := []string{fmt.Sprintf("%v", i+1), user.Username, user.IPNet, user.CreatedAt, fmt.Sprintf("%t", user.ServerSerialNumber == server.SerialNumber)}
 							table.Append(data)
 						}
 						table.Render()
@@ -322,25 +320,6 @@ func main() {
 						return nil
 					},
 				},
-				// {
-				// 	Name:  "apply",
-				// 	Usage: "Apply pending changes.",
-				// 	Action: func(c *cli.Context) error {
-				// 		action = "apply"
-
-				// 		conn := getConn(c.GlobalString("daemon-port"))
-				// 		defer conn.Close()
-				// 		vpnSvc := pb.NewVPNServiceClient(conn)
-
-				// 		if _, err := vpnSvc.Apply(context.Background(), &pb.VPNApplyRequest{}); err != nil {
-				// 			logrus.Errorf("can not apply configuration: %v", err)
-				// 			os.Exit(1)
-				// 			return err
-				// 		}
-				// 		logrus.Info("changes applied; OpenVPN restarted")
-				// 		return nil
-				// 	},
-				// },
 			},
 		},
 	}

+ 32 - 25
pb/user.pb.go

@@ -18,10 +18,8 @@ It has these top-level messages:
 	UserGenConfigResponse
 	VPNStatusRequest
 	VPNInitRequest
-	VPNApplyRequest
 	VPNStatusResponse
 	VPNInitResponse
-	VPNApplyResponse
 */
 package pb
 
@@ -146,6 +144,7 @@ type UserResponse_User struct {
 	ServerSerialNumber string `protobuf:"bytes,2,opt,name=ServerSerialNumber" json:"ServerSerialNumber,omitempty"`
 	Cert               string `protobuf:"bytes,3,opt,name=Cert" json:"Cert,omitempty"`
 	CreatedAt          string `protobuf:"bytes,4,opt,name=CreatedAt" json:"CreatedAt,omitempty"`
+	IPNet              string `protobuf:"bytes,5,opt,name=IPNet" json:"IPNet,omitempty"`
 }
 
 func (m *UserResponse_User) Reset()                    { *m = UserResponse_User{} }
@@ -181,6 +180,13 @@ func (m *UserResponse_User) GetCreatedAt() string {
 	return ""
 }
 
+func (m *UserResponse_User) GetIPNet() string {
+	if m != nil {
+		return m.IPNet
+	}
+	return ""
+}
+
 type UserGenConfigResponse struct {
 	ClientConfig string `protobuf:"bytes,1,opt,name=ClientConfig" json:"ClientConfig,omitempty"`
 }
@@ -415,27 +421,28 @@ var _UserService_serviceDesc = grpc.ServiceDesc{
 func init() { proto.RegisterFile("user.proto", fileDescriptor0) }
 
 var fileDescriptor0 = []byte{
-	// 351 bytes of a gzipped FileDescriptorProto
-	0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0x8c, 0x53, 0xd1, 0x4e, 0xea, 0x40,
-	0x10, 0xbd, 0x85, 0x42, 0x2e, 0x03, 0x89, 0x30, 0x42, 0xb2, 0x36, 0x3e, 0x90, 0x7d, 0x22, 0x31,
-	0x29, 0x11, 0x1e, 0x7d, 0xd2, 0x9a, 0xf8, 0xa0, 0x31, 0x06, 0xe2, 0x07, 0xb4, 0x32, 0x9a, 0x26,
-	0xb0, 0xad, 0xbb, 0x8b, 0xfc, 0x80, 0xff, 0xe1, 0xbf, 0xf8, 0x65, 0x66, 0xbb, 0x2d, 0x05, 0x52,
-	0x0d, 0x6f, 0x3b, 0xe7, 0xcc, 0x19, 0x66, 0x0e, 0xa7, 0x00, 0x6b, 0x45, 0xd2, 0x4f, 0x65, 0xa2,
-	0x13, 0xac, 0xa5, 0x11, 0xef, 0xc1, 0xc9, 0xb3, 0x22, 0xf9, 0x10, 0x2b, 0x3d, 0xa3, 0xf7, 0x35,
-	0x29, 0xcd, 0xef, 0xa1, 0x67, 0xa0, 0x40, 0x52, 0xa8, 0x29, 0x07, 0xd1, 0x83, 0xff, 0x06, 0x14,
-	0xe1, 0x8a, 0x98, 0x33, 0x74, 0x46, 0xad, 0xd9, 0xb6, 0x36, 0xdc, 0x53, 0xa8, 0xd4, 0x26, 0x91,
-	0x0b, 0x56, 0xb3, 0x5c, 0x51, 0xf3, 0xb1, 0x1d, 0x76, 0x4b, 0x4b, 0x3a, 0x6a, 0x18, 0xf7, 0xa1,
-	0x6b, 0xde, 0x33, 0x12, 0xb4, 0x39, 0xa6, 0x7f, 0x02, 0x7d, 0xf3, 0xbe, 0x23, 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02, 0x00, 0x00, 0xff, 0xff, 0x31, 0xff, 0x3d, 0x2c, 0x31, 0x03, 0x00, 0x00,
+	// 362 bytes of a gzipped FileDescriptorProto
+	0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0x8c, 0x53, 0xd1, 0x6a, 0xea, 0x40,
+	0x10, 0xbd, 0xd1, 0x44, 0xae, 0xa3, 0x70, 0x75, 0xae, 0xc2, 0x36, 0xf4, 0x41, 0xf6, 0x49, 0x28,
+	0x44, 0xaa, 0x8f, 0x7d, 0x6a, 0x53, 0x28, 0xa5, 0x45, 0x44, 0xe9, 0x07, 0x24, 0x75, 0x5a, 0x02,
+	0x9a, 0xa4, 0xbb, 0x6b, 0xfd, 0x97, 0xbe, 0xf4, 0xb7, 0xfa, 0x39, 0x65, 0xb3, 0x89, 0x51, 0x49,
+	0x8b, 0x6f, 0x33, 0xe7, 0xcc, 0x99, 0x4c, 0x4e, 0x4e, 0x00, 0x36, 0x92, 0x84, 0x97, 0x8a, 0x44,
+	0x25, 0x58, 0x4b, 0x43, 0xde, 0x85, 0x7f, 0x4f, 0x92, 0xc4, 0x63, 0x24, 0xd5, 0x9c, 0xde, 0x36,
+	0x24, 0x15, 0x7f, 0x80, 0xae, 0x86, 0x7c, 0x41, 0x81, 0xa2, 0x1c, 0x44, 0x17, 0xfe, 0x6a, 0x30,
+	0x0e, 0xd6, 0xc4, 0xac, 0x81, 0x35, 0x6c, 0xce, 0x77, 0xbd, 0xe6, 0x66, 0x81, 0x94, 0xdb, 0x44,
+	0x2c, 0x59, 0xcd, 0x70, 0x45, 0xcf, 0x47, 0x66, 0xd9, 0x2d, 0xad, 0xe8, 0xa4, 0x65, 0xdc, 0x83,
+	0x8e, 0xae, 0xe7, 0x14, 0xd3, 0xf6, 0x94, 0xf9, 0x31, 0xf4, 0x74, 0x7d, 0x47, 0xb1, 0x9f, 0xc4,
+	0x2f, 0xd1, 0xeb, 0x29, 0x9a, 0x2f, 0x0b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03, 0x00, 0x00,
+	0xff, 0xff, 0xf5, 0x56, 0x02, 0x91, 0x47, 0x03, 0x00, 0x00,
 }

+ 1 - 0
pb/user.proto

@@ -37,6 +37,7 @@ message UserResponse {
     string ServerSerialNumber = 2;
     string Cert = 3;
     string CreatedAt = 4;
+    string IPNet = 5;
   }
 
   repeated User users = 1;

+ 21 - 74
pb/vpn.pb.go

@@ -49,14 +49,6 @@ func (m *VPNInitRequest) GetPort() string {
 	return ""
 }
 
-type VPNApplyRequest struct {
-}
-
-func (m *VPNApplyRequest) Reset()                    { *m = VPNApplyRequest{} }
-func (m *VPNApplyRequest) String() string            { return proto.CompactTextString(m) }
-func (*VPNApplyRequest) ProtoMessage()               {}
-func (*VPNApplyRequest) Descriptor() ([]byte, []int) { return fileDescriptor1, []int{2} }
-
 type VPNStatusResponse struct {
 	Name         string `protobuf:"bytes,1,opt,name=Name" json:"Name,omitempty"`
 	SerialNumber string `protobuf:"bytes,2,opt,name=SerialNumber" json:"SerialNumber,omitempty"`
@@ -72,7 +64,7 @@ type VPNStatusResponse struct {
 func (m *VPNStatusResponse) Reset()                    { *m = VPNStatusResponse{} }
 func (m *VPNStatusResponse) String() string            { return proto.CompactTextString(m) }
 func (*VPNStatusResponse) ProtoMessage()               {}
-func (*VPNStatusResponse) Descriptor() ([]byte, []int) { return fileDescriptor1, []int{3} }
+func (*VPNStatusResponse) Descriptor() ([]byte, []int) { return fileDescriptor1, []int{2} }
 
 func (m *VPNStatusResponse) GetName() string {
 	if m != nil {
@@ -143,23 +135,13 @@ type VPNInitResponse struct {
 func (m *VPNInitResponse) Reset()                    { *m = VPNInitResponse{} }
 func (m *VPNInitResponse) String() string            { return proto.CompactTextString(m) }
 func (*VPNInitResponse) ProtoMessage()               {}
-func (*VPNInitResponse) Descriptor() ([]byte, []int) { return fileDescriptor1, []int{4} }
-
-type VPNApplyResponse struct {
-}
-
-func (m *VPNApplyResponse) Reset()                    { *m = VPNApplyResponse{} }
-func (m *VPNApplyResponse) String() string            { return proto.CompactTextString(m) }
-func (*VPNApplyResponse) ProtoMessage()               {}
-func (*VPNApplyResponse) Descriptor() ([]byte, []int) { return fileDescriptor1, []int{5} }
+func (*VPNInitResponse) Descriptor() ([]byte, []int) { return fileDescriptor1, []int{3} }
 
 func init() {
 	proto.RegisterType((*VPNStatusRequest)(nil), "pb.VPNStatusRequest")
 	proto.RegisterType((*VPNInitRequest)(nil), "pb.VPNInitRequest")
-	proto.RegisterType((*VPNApplyRequest)(nil), "pb.VPNApplyRequest")
 	proto.RegisterType((*VPNStatusResponse)(nil), "pb.VPNStatusResponse")
 	proto.RegisterType((*VPNInitResponse)(nil), "pb.VPNInitResponse")
-	proto.RegisterType((*VPNApplyResponse)(nil), "pb.VPNApplyResponse")
 }
 
 // Reference imports to suppress errors if they are not otherwise used.
@@ -175,7 +157,6 @@ const _ = grpc.SupportPackageIsVersion4
 type VPNServiceClient interface {
 	Status(ctx context.Context, in *VPNStatusRequest, opts ...grpc.CallOption) (*VPNStatusResponse, error)
 	Init(ctx context.Context, in *VPNInitRequest, opts ...grpc.CallOption) (*VPNInitResponse, error)
-	Apply(ctx context.Context, in *VPNApplyRequest, opts ...grpc.CallOption) (*VPNApplyResponse, error)
 }
 
 type vPNServiceClient struct {
@@ -204,21 +185,11 @@ func (c *vPNServiceClient) Init(ctx context.Context, in *VPNInitRequest, opts ..
 	return out, nil
 }
 
-func (c *vPNServiceClient) Apply(ctx context.Context, in *VPNApplyRequest, opts ...grpc.CallOption) (*VPNApplyResponse, error) {
-	out := new(VPNApplyResponse)
-	err := grpc.Invoke(ctx, "/pb.VPNService/Apply", in, out, c.cc, opts...)
-	if err != nil {
-		return nil, err
-	}
-	return out, nil
-}
-
 // Server API for VPNService service
 
 type VPNServiceServer interface {
 	Status(context.Context, *VPNStatusRequest) (*VPNStatusResponse, error)
 	Init(context.Context, *VPNInitRequest) (*VPNInitResponse, error)
-	Apply(context.Context, *VPNApplyRequest) (*VPNApplyResponse, error)
 }
 
 func RegisterVPNServiceServer(s *grpc.Server, srv VPNServiceServer) {
@@ -261,24 +232,6 @@ func _VPNService_Init_Handler(srv interface{}, ctx context.Context, dec func(int
 	return interceptor(ctx, in, info, handler)
 }
 
-func _VPNService_Apply_Handler(srv interface{}, ctx context.Context, dec func(interface{}) error, interceptor grpc.UnaryServerInterceptor) (interface{}, error) {
-	in := new(VPNApplyRequest)
-	if err := dec(in); err != nil {
-		return nil, err
-	}
-	if interceptor == nil {
-		return srv.(VPNServiceServer).Apply(ctx, in)
-	}
-	info := &grpc.UnaryServerInfo{
-		Server:     srv,
-		FullMethod: "/pb.VPNService/Apply",
-	}
-	handler := func(ctx context.Context, req interface{}) (interface{}, error) {
-		return srv.(VPNServiceServer).Apply(ctx, req.(*VPNApplyRequest))
-	}
-	return interceptor(ctx, in, info, handler)
-}
-
 var _VPNService_serviceDesc = grpc.ServiceDesc{
 	ServiceName: "pb.VPNService",
 	HandlerType: (*VPNServiceServer)(nil),
@@ -291,10 +244,6 @@ var _VPNService_serviceDesc = grpc.ServiceDesc{
 			MethodName: "Init",
 			Handler:    _VPNService_Init_Handler,
 		},
-		{
-			MethodName: "Apply",
-			Handler:    _VPNService_Apply_Handler,
-		},
 	},
 	Streams:  []grpc.StreamDesc{},
 	Metadata: "vpn.proto",
@@ -303,25 +252,23 @@ var _VPNService_serviceDesc = grpc.ServiceDesc{
 func init() { proto.RegisterFile("vpn.proto", fileDescriptor1) }
 
 var fileDescriptor1 = []byte{
-	// 312 bytes of a gzipped FileDescriptorProto
-	0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0x74, 0x92, 0xcb, 0x4e, 0xf3, 0x30,
-	0x10, 0x85, 0xff, 0xf4, 0x92, 0xbf, 0x19, 0x21, 0x68, 0x87, 0x82, 0xac, 0x88, 0x05, 0xca, 0x8a,
-	0x55, 0x25, 0x2e, 0x12, 0x5b, 0xa2, 0x6c, 0x60, 0x81, 0x15, 0xb5, 0x52, 0xf6, 0x09, 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 0x80, 0x8d, 0xd0, 0xfe, 0xaa, 0x4b, 0xda, 0x65, 0x62, 0xd9, 0xdd, 0xb7,
-	0x07, 0xa0, 0x43, 0x61, 0x62, 0x5f, 0xbe, 0x31, 0x7c, 0x04, 0xdf, 0xe6, 0x83, 0xcb, 0x15, 0x2f,
-	0x56, 0xc7, 0x8b, 0x09, 0x2f, 0x8e, 0xa8, 0xab, 0xfc, 0x0f, 0x6f, 0x61, 0xa2, 0x7b, 0x21, 0xba,
-	0x0f, 0x7a, 0xbb, 0x0b, 0xcf, 0x07, 0xec, 0x60, 0x79, 0x80, 0xa9, 0x99, 0x05, 0xdb, 0xfb, 0xfe,
-	0xb6, 0xc2, 0xe5, 0x10, 0xb6, 0xae, 0xc2, 0x37, 0x2f, 0xe7, 0xfe, 0x37, 0x00, 0x00, 0xff, 0xff,
-	0x10, 0x88, 0xf5, 0x13, 0x46, 0x02, 0x00, 0x00,
+	// 278 bytes of a gzipped FileDescriptorProto
+	0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0x74, 0x91, 0x4f, 0x4f, 0x83, 0x40,
+	0x10, 0xc5, 0xa5, 0x45, 0x2c, 0x13, 0xa3, 0xed, 0xf8, 0x27, 0x1b, 0xe2, 0xc1, 0xec, 0xc9, 0x13,
+	0x89, 0x7a, 0xf0, 0x6a, 0xc3, 0x45, 0x0f, 0x12, 0xd2, 0x26, 0xbd, 0x83, 0xce, 0x81, 0x68, 0x01,
+	0x77, 0x87, 0xc6, 0xef, 0xec, 0x97, 0x30, 0xbb, 0x8b, 0xa5, 0x34, 0xe9, 0xed, 0xed, 0x6f, 0x67,
+	0xde, 0x64, 0xde, 0x40, 0xb8, 0x69, 0xaa, 0xb8, 0x51, 0x35, 0xd7, 0x38, 0x6a, 0x0a, 0x89, 0x30,
+	0x5d, 0x65, 0xe9, 0x92, 0x73, 0x6e, 0xf5, 0x82, 0xbe, 0x5b, 0xd2, 0x2c, 0x9f, 0xe1, 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7e, 0xe8, 0x01, 0x00, 0x00,
 }

+ 0 - 3
pb/vpn.proto

@@ -7,12 +7,10 @@ message VPNInitRequest {
   string Hostname = 1;
   string Port = 2;
 }
-message VPNApplyRequest {}
 
 service VPNService {
   rpc Status (VPNStatusRequest) returns (VPNStatusResponse) {}
   rpc Init (VPNInitRequest) returns (VPNInitResponse) {}
-  rpc Apply (VPNApplyRequest) returns (VPNApplyResponse) {}
 }
 
 message VPNStatusResponse {
@@ -27,4 +25,3 @@ message VPNStatusResponse {
   string CreatedAt = 9;
 }
 message VPNInitResponse {}
-message VPNApplyResponse {}

+ 21 - 0
user.go

@@ -2,6 +2,7 @@ package ovpm
 
 import (
 	"fmt"
+	"net"
 	"time"
 
 	"github.com/Sirupsen/logrus"
@@ -214,3 +215,23 @@ func (u *DBUser) GetServerSerialNumber() string {
 func (u *DBUser) GetCreatedAt() string {
 	return u.CreatedAt.Format(time.UnixDate)
 }
+
+// getIP returns user's vpn ip addr.
+func (u *DBUser) getIP() net.IP {
+	clientsNetMask := net.IPMask(net.ParseIP(_DefaultServerNetMask))
+	clientsNetPrefix := net.ParseIP(_DefaultServerNetwork)
+	clientNet := clientsNetPrefix.Mask(clientsNetMask).To4()
+	clientNet[3] = byte(u.ID)
+	return clientNet
+}
+
+// GetIPNet returns user's vpn ip network. (e.g. 192.168.0.1/24)
+func (u *DBUser) GetIPNet() string {
+	mask := net.IPMask(net.ParseIP(_DefaultServerNetMask))
+
+	ipn := net.IPNet{
+		IP:   u.getIP(),
+		Mask: mask,
+	}
+	return ipn.String()
+}

+ 10 - 12
vpn.go

@@ -1,4 +1,5 @@
 //go:generate go-bindata -pkg bindata -o bindata/bindata.go template/
+//go:generate protoc -I pb/ pb/user.proto pb/vpn.proto --go_out=plugins=grpc:pb
 
 package ovpm
 
@@ -6,7 +7,6 @@ import (
 	"bytes"
 	"fmt"
 	"math/big"
-	"net"
 	"os"
 	"os/exec"
 	"strings"
@@ -323,13 +323,15 @@ func Emit() error {
 
 	logrus.Info("configurations emitted to the filesystem")
 
-	for {
-		if vpnProc.Status() == supervisor.RUNNING || vpnProc.Status() == supervisor.STOPPED {
-			logrus.Info("OpenVPN process is restarting")
-			RestartVPNProc()
-			break
+	if IsInitialized() {
+		for {
+			if vpnProc.Status() == supervisor.RUNNING || vpnProc.Status() == supervisor.STOPPED {
+				logrus.Info("OpenVPN process is restarting")
+				RestartVPNProc()
+				break
+			}
+			time.Sleep(1 * time.Second)
 		}
-		time.Sleep(1 * time.Second)
 	}
 
 	return nil
@@ -485,18 +487,14 @@ func emitCCD() error {
 
 	// Create and write rendered ccd data.
 	os.Mkdir(_DefaultVPNCCDPath, 0755)
-	clientsNetMask := net.IPMask(net.ParseIP(_DefaultServerNetMask))
-	clientsNetPrefix := net.ParseIP(_DefaultServerNetwork)
-	clientNet := clientsNetPrefix.Mask(clientsNetMask).To4()
 
 	counter := 2
 	for _, user := range users {
 		var result bytes.Buffer
-		clientNet[3] = byte(counter)
 		params := struct {
 			IP      string
 			NetMask string
-		}{IP: clientNet.String(), NetMask: _DefaultServerNetMask}
+		}{IP: user.getIP().String(), NetMask: _DefaultServerNetMask}
 
 		data, err := bindata.Asset("template/ccd.file.tmpl")
 		if err != nil {

+ 17 - 17
vpn_test.go

@@ -9,9 +9,15 @@ import (
 
 var fs map[string]string
 
+func setupTestCase() {
+	// Initialize.
+	fs = make(map[string]string)
+	vpnProc.Stop()
+}
+
 func TestVPNInit(t *testing.T) {
 	// Init:
-	Testing = true
+	setupTestCase()
 	SetupDB("sqlite3", ":memory:")
 	defer CeaseDB()
 	// Prepare:
@@ -41,7 +47,7 @@ func TestVPNInit(t *testing.T) {
 
 func TestVPNDeinit(t *testing.T) {
 	// Init:
-	Testing = true
+	setupTestCase()
 	SetupDB("sqlite3", ":memory:")
 	defer CeaseDB()
 
@@ -92,7 +98,7 @@ func TestVPNDeinit(t *testing.T) {
 
 func TestVPNIsInitialized(t *testing.T) {
 	// Init:
-	Testing = true
+	setupTestCase()
 	SetupDB("sqlite3", ":memory:")
 	defer CeaseDB()
 
@@ -115,7 +121,7 @@ func TestVPNIsInitialized(t *testing.T) {
 
 func TestVPNGetServerInstance(t *testing.T) {
 	// Init:
-	Testing = true
+	setupTestCase()
 	SetupDB("sqlite3", ":memory:")
 	defer CeaseDB()
 
@@ -152,7 +158,7 @@ func TestVPNGetServerInstance(t *testing.T) {
 
 func TestVPNDumpsClientConfig(t *testing.T) {
 	// Init:
-	Testing = true
+	setupTestCase()
 	SetupDB("sqlite3", ":memory:")
 	defer CeaseDB()
 	Init("localhost", "")
@@ -174,7 +180,7 @@ func TestVPNDumpsClientConfig(t *testing.T) {
 
 func TestVPNDumpClientConfig(t *testing.T) {
 	// Init:
-	Testing = true
+	setupTestCase()
 	SetupDB("sqlite3", ":memory:")
 	defer CeaseDB()
 	Init("localhost", "")
@@ -199,7 +205,7 @@ func TestVPNDumpClientConfig(t *testing.T) {
 
 func TestVPNGetSystemCA(t *testing.T) {
 	// Init:
-	Testing = true
+	setupTestCase()
 	SetupDB("sqlite3", ":memory:")
 	defer CeaseDB()
 
@@ -231,7 +237,7 @@ func TestVPNGetSystemCA(t *testing.T) {
 
 func TestVPNStartVPNProc(t *testing.T) {
 	// Init:
-	Testing = true
+	setupTestCase()
 	SetupDB("sqlite3", ":memory:")
 	defer CeaseDB()
 
@@ -265,7 +271,7 @@ func TestVPNStartVPNProc(t *testing.T) {
 
 func TestVPNStopVPNProc(t *testing.T) {
 	// Init:
-	Testing = true
+	setupTestCase()
 	SetupDB("sqlite3", ":memory:")
 	defer CeaseDB()
 	Init("localhost", "")
@@ -300,10 +306,6 @@ func TestVPNRestartVPNProc(t *testing.T) {
 	// Test:
 
 	// Call restart.
-	// Isn't it stopped?
-	if vpnProc.Status() != supervisor.STOPPED {
-		t.Fatalf("expected state is STOPPED, got %s instead", vpnProc.Status())
-	}
 
 	RestartVPNProc()
 
@@ -323,7 +325,7 @@ func TestVPNRestartVPNProc(t *testing.T) {
 
 func TestVPNEmit(t *testing.T) {
 	// Init:
-	Testing = true
+	setupTestCase()
 	SetupDB("sqlite3", ":memory:")
 	defer CeaseDB()
 	Init("localhost", "")
@@ -373,9 +375,7 @@ func (f *fakeProcess) Status() supervisor.State {
 }
 
 func init() {
-	// Initialize.
-	fs = make(map[string]string)
-
+	Testing = true
 	// Monkeypatch emitToFile()
 	monkey.Patch(emitToFile, func(path, content string, mode uint) error {
 		fs[path] = content