Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Exports tests #67

Draft
wants to merge 21 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,14 @@ cli:
go build -o eduvpn-common-cli ./cmd/cli

test:
go test -race ./...
go test -tags=cgotesting -race ./...

clean:
rm -rf lib
go clean

coverage:
go test -v -coverpkg=./... -coverprofile=common.cov ./...
go test -tags=cgotesting -v -coverpkg=./... -coverprofile=common.cov ./...
go tool cover -func common.cov

sloc:
Expand Down
3 changes: 3 additions & 0 deletions client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -528,6 +528,9 @@ func (c *Client) SetProfileID(pID string) error {
if err != nil {
return i18nerr.WrapInternalf(err, "Failed to set the profile ID: '%s'", pID)
}
if _, ok := srv.Profiles.Map[pID]; !ok {
return i18nerr.WrapInternalf(err, "Failed to set the profile ID as it does not exist: '%s'", pID)
}
srv.Profiles.Current = pID
c.TrySave()
return nil
Expand Down
2 changes: 2 additions & 0 deletions client/fsm.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ func newFSM(
Transitions: []FSMTransition{
{To: StateMain, Description: "Authorized"},
{To: StateDisconnected, Description: "Cancel, was disconnected"},
{To: StateGotConfig, Description: "Cancel, was got config"},
},
},
StateGettingConfig: FSMState{
Expand All @@ -140,6 +141,7 @@ func newFSM(
Transitions: []FSMTransition{
{To: StateGettingConfig, Description: "Get a VPN config again"},
{To: StateConnecting, Description: "VPN is connecting"},
{To: StateOAuthStarted, Description: "Renew"},
},
},
StateConnecting: FSMState{
Expand Down
53 changes: 2 additions & 51 deletions cmd/cli/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"github.com/eduvpn/eduvpn-common/internal/version"
"github.com/eduvpn/eduvpn-common/types/cookie"
srvtypes "github.com/eduvpn/eduvpn-common/types/server"
"github.com/eduvpn/eduvpn-common/util"

"github.com/pkg/browser"
)
Expand All @@ -34,56 +35,6 @@ func openBrowser(data interface{}) {
}()
}

// GetLanguageMatched uses a map from language tags to strings to extract the right language given the tag
// It implements it according to https://github.com/eduvpn/documentation/blob/dc4d53c47dd7a69e95d6650eec408e16eaa814a2/SERVER_DISCOVERY.md#language-matching
func GetLanguageMatched(langMap map[string]string, langTag string) string {
// If no map is given, return the empty string
if len(langMap) == 0 {
return ""
}
// Try to find the exact match
if val, ok := langMap[langTag]; ok {
return val
}
// Try to find a key that starts with the OS language setting
for k := range langMap {
if strings.HasPrefix(k, langTag) {
return langMap[k]
}
}
// Try to find a key that starts with the first part of the OS language (e.g. de-)
pts := strings.Split(langTag, "-")
// We have a "-"
if len(pts) > 1 {
for k := range langMap {
if strings.HasPrefix(k, pts[0]+"-") {
return langMap[k]
}
}
}
// search for just the language (e.g. de)
for k := range langMap {
if k == pts[0] {
return langMap[k]
}
}

// Pick one that is deemed best, e.g. en-US or en, but note that not all languages are always available!
// We force an entry that is english exactly or with an english prefix
for k := range langMap {
if k == "en" || strings.HasPrefix(k, "en-") {
return langMap[k]
}
}

// Otherwise just return one
for k := range langMap {
return langMap[k]
}

return ""
}

// Ask for a profile in the command line.
func sendProfile(state *client.Client, data interface{}) {
fmt.Printf("Multiple VPN profiles found. Please select a profile by entering e.g. 1")
Expand All @@ -102,7 +53,7 @@ func sendProfile(state *client.Client, data interface{}) {
var options []string
i := 0
for k, v := range sps.Map {
ps += fmt.Sprintf("\n%d - %s", i+1, GetLanguageMatched(v.DisplayName, "en"))
ps += fmt.Sprintf("\n%d - %s", i+1, util.GetLanguageMatched(v.DisplayName, "en"))
options = append(options, k)
i++
}
Expand Down
42 changes: 1 addition & 41 deletions exports/exports.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,47 +14,7 @@
package main

/*
#include <stdint.h>
#include <stdlib.h>

typedef long long int (*ReadRxBytes)();

typedef int (*StateCB)(int oldstate, int newstate, void* data);

typedef void (*RefreshList)();
typedef void (*TokenGetter)(const char* server_id, int server_type, char* out, size_t len);
typedef void (*TokenSetter)(const char* server_id, int server_type, const char* tokens);
typedef void (*ProxySetup)(int fd, const char* peer_ips);
typedef void (*ProxyReady)();

static long long int get_read_rx_bytes(ReadRxBytes read)
{
return read();
}
static int call_callback(StateCB callback, int oldstate, int newstate, void* data)
{
return callback(oldstate, newstate, data);
}
static void call_refresh_list(RefreshList refresh)
{
refresh();
}
static void call_token_getter(TokenGetter getter, const char* server_id, int server_type, char* out, size_t len)
{
getter(server_id, server_type, out, len);
}
static void call_token_setter(TokenSetter setter, const char* server_id, int server_type, const char* tokens)
{
setter(server_id, server_type, tokens);
}
static void call_proxy_setup(ProxySetup proxysetup, int fd, const char* peer_ips)
{
proxysetup(fd, peer_ips);
}
static void call_proxy_ready(ProxyReady ready)
{
ready();
}
#include "exports.h"
*/
import "C"

Expand Down
46 changes: 46 additions & 0 deletions exports/exports.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
#ifndef EXPORTS_H
#define EXPORTS_H

#include <stdint.h>
#include <stdlib.h>

typedef long long int (*ReadRxBytes)();

typedef int (*StateCB)(int oldstate, int newstate, void* data);

typedef void (*RefreshList)();
typedef void (*TokenGetter)(const char* server_id, int server_type, char* out, size_t len);
typedef void (*TokenSetter)(const char* server_id, int server_type, const char* tokens);
typedef void (*ProxySetup)(int fd, const char* peer_ips);
typedef void (*ProxyReady)();

static long long int get_read_rx_bytes(ReadRxBytes read)
{
return read();
}
static int call_callback(StateCB callback, int oldstate, int newstate, void* data)
{
return callback(oldstate, newstate, data);
}
static void call_refresh_list(RefreshList refresh)
{
refresh();
}
static void call_token_getter(TokenGetter getter, const char* server_id, int server_type, char* out, size_t len)
{
getter(server_id, server_type, out, len);
}
static void call_token_setter(TokenSetter setter, const char* server_id, int server_type, const char* tokens)
{
setter(server_id, server_type, tokens);
}
static void call_proxy_setup(ProxySetup proxysetup, int fd, const char* peer_ips)
{
proxysetup(fd, peer_ips);
}
static void call_proxy_ready(ProxyReady ready)
{
ready();
}

#endif /* EXPORTS_H */
21 changes: 21 additions & 0 deletions exports/exports_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
//go:build cgotesting

package main

import "testing"

func TestRegister(t *testing.T) {
testRegister(t)
}

func TestServerList(t *testing.T) {
testServerList(t)
}

func TestGetConfig(t *testing.T) {
testGetConfig(t)
}

func TestLetsConnectDiscovery(t *testing.T) {
testLetsConnectDiscovery(t)
}
Loading
Loading