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

Test coverage #11

Merged
merged 5 commits into from
May 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,6 @@ If you would like to contribute to the development, submit a pull request with y
### TODO

- [x] Make a fully functional rest api wireguard server
- [ ] Test coverage
- [x] Test coverage
- [ ] Implementation of the ability to update clients
- [ ] Make API points to get statistics on clients
4 changes: 2 additions & 2 deletions lib/wire_guard/config_updater.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class ConfigUpdater

def initialize
@json_config = JSON.parse(File.read(WireGuard::Server::WG_JSON_PATH))
system('wg-quick down wg0') if File.exist?(WG_CONF_PATH)
Kernel.system('wg-quick down wg0') if File.exist?(WG_CONF_PATH)
end

def self.update
Expand All @@ -30,7 +30,7 @@ def update

dump_wireguard_config(new_config_build)

system('wg-quick up wg0')
Kernel.system('wg-quick up wg0')
end

private
Expand Down
30 changes: 30 additions & 0 deletions spec/fixtures/wg0.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# Note: Do not edit this file directly.
# Your changes will be overwritten!

# Server
[Interface]
PrivateKey = 6Mlqg+1Umojm7a4VvgIi+YMp4oPrWNnZ5HLRFu4my2w=
Address = 10.8.0.1/24
ListenPort = 51820
PreUp =
PostUp = iptables -t nat -A POSTROUTING -s 10.8.0.0/24 -o eth0 -j MASQUERADE; iptables -A INPUT -p udp -m udp --dport 51820 -j ACCEPT; iptables -A FORWARD -i wg0 -j ACCEPT; iptables -A FORWARD -o wg0 -j ACCEPT;
PreDown =
PostDown = iptables -t nat -D POSTROUTING -s 10.8.0.0/24 -o eth0 -j MASQUERADE; iptables -D INPUT -p udp -m udp --dport 51820 -j ACCEPT; iptables -D FORWARD -i wg0 -j ACCEPT; iptables -D FORWARD -o wg0 -j ACCEPT;

# Client ID: 1
[Peer]
PublicKey = LiXk4UOfnScgf4UnkcYNcz4wWeqTOW1UrHKRVhZ1OXg=
PresharedKey = 3UzAMA6mLIGjHOImShNb5tWlkwxsha8LZZP7dm49meQ=
AllowedIPs = 10.8.0.2/32

# Client ID: 2
[Peer]
PublicKey = hvIyIW2o8JROVKuY2yYFdUn0oA+43aLuT8KCy0YbORE=
PresharedKey = dVW/5kF8wnsx0zAwR4uPIa06btACxpQ/rHBL1B3qPnk=
AllowedIPs = 10.8.0.3/32

# Client ID: 3
[Peer]
PublicKey = bPKBg66uC1J2hlkE31Of5wnkg+IjowVXgoLcjcLn0js=
PresharedKey = IyVg7fktkSBxJ0uK82j6nlI7Vmo0E53eBmYZ723/45E=
AllowedIPs = 10.8.0.4/32
40 changes: 40 additions & 0 deletions spec/lib/wire_guard/config_builder_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
# frozen_string_literal: true

RSpec.describe WireGuard::ConfigBuilder do
subject(:build) { described_class.new(configs, params).config }

before do
allow(WireGuard::KeyGenerator).to receive_messages(wg_genkey: 'wg_genkey', wg_pubkey: 'wg_pubkey',
wg_genpsk: 'wg_genpsk')
end

let(:params) do
{
lol: 'kek'
}
end

let(:configs) do
{
'last_id' => 23,
'last_address' => '10.8.0.255'
}
end

let(:expected_result) do
{
id: 24,
address: '10.8.1.0',
private_key: 'wg_genkey',
public_key: 'wg_pubkey',
preshared_key: 'wg_genpsk',
data: {
lol: 'kek'
}
}
end

it 'creates the correct config' do
expect(build).to eq(expected_result)
end
end
28 changes: 28 additions & 0 deletions spec/lib/wire_guard/config_updater_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# frozen_string_literal: true

RSpec.describe WireGuard::ConfigUpdater do
subject(:update) { described_class.update }

before do
allow(Kernel).to receive(:system).with('wg-quick down wg0').and_return(true)
allow(Kernel).to receive(:system).with('wg-quick up wg0').and_return(true)
create_conf_file('spec/fixtures/wg0.json')

update
end

after do
FileUtils.rm_rf("#{Settings.wg_path}/wg0.json")
FileUtils.rm_rf("#{Settings.wg_path}/wg0.conf")
end

it 'creates the correct config file for the wireguard server' do
config = File.read("#{Settings.wg_path}/wg0.conf")

expect(config).to eq(File.read('spec/fixtures/wg0.conf'))
end

it 'restarts the wireguard server' do
expect(Kernel).to have_received(:system).with('wg-quick up wg0')
end
end
Loading
Loading