Skip to content

Commit

Permalink
Segment test cases for system metadata (#63)
Browse files Browse the repository at this point in the history
* refactor: continuous outbound MQTT topic structure

* bump: project requirements

* docs: resolve formatting issues with markdownlint

* feat: create experimental dev container

* feat: outline basic application + republisher content testing

* refactor: test payload attributes w/ verification & timestamp

* fix: check on missing topic & test cases for payload contents

* ci: workflow for testing code (coverage)
  • Loading branch information
TechSolomon authored Jul 16, 2024
1 parent ef68314 commit acec3a1
Show file tree
Hide file tree
Showing 15 changed files with 274 additions and 83 deletions.
22 changes: 22 additions & 0 deletions .devcontainer/devcontainer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// For format details, see https://aka.ms/devcontainer.json. For config options, see the
// README at: https://github.com/devcontainers/templates/tree/main/src/go
{
"name": "Go",
// Or use a Dockerfile or Docker Compose file. More info: https://containers.dev/guide/dockerfile
"image": "mcr.microsoft.com/devcontainers/go:1-1.22-bookworm"

// Features to add to the dev container. More info: https://containers.dev/features.
// "features": {},

// Use 'forwardPorts' to make a list of ports inside the container available locally.
// "forwardPorts": [],

// Use 'postCreateCommand' to run commands after the container is created.
// "postCreateCommand": "go version",

// Configure tool-specific properties.
// "customizations": {},

// Uncomment to connect as root instead. More info: https://aka.ms/dev-containers-non-root.
// "remoteUser": "root"
}
12 changes: 12 additions & 0 deletions .github/dependabot.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# To get started with Dependabot version updates, you'll need to specify which
# package ecosystems to update and where the package manifests are located.
# Please see the documentation for more information:
# https://docs.github.com/github/administering-a-repository/configuration-options-for-dependency-updates
# https://containers.dev/guide/dependabot

version: 2
updates:
- package-ecosystem: "devcontainers"
directory: "/"
schedule:
interval: weekly
4 changes: 2 additions & 2 deletions .github/workflows/go.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# This workflow will build a Go project
# For more information see: https://docs.github.com/en/actions/automating-builds-and-tests/building-and-testing-go

name: ARCTIC CAMIO Lab
name: CAMIO Lab

on:
push:
Expand All @@ -27,4 +27,4 @@ jobs:
run: go build -o diode .

- name: Test with the Go CLI
run: go test
run: go test -v ./...
3 changes: 3 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ build:
test:
go test -v ./...

coverage:
go test -cover ./...

run: build
./${BIN_NAME} --help

Expand Down
23 changes: 11 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,19 @@ Scripts for verifying TCP passthrough functionality.

> [!TIP]
> This project utilizes [`go`](https://go.dev/) for module management.
> You can find installation instructions via https://go.dev/doc/install.
> You can find installation instructions via [relevant operating system](https://go.dev/doc/install) documentation.
- Clone repository: `gh repo clone acep-uaf/data-diode`
- Source navigation: `cd data-diode`
- Build binary: `make`
- [`build-essential`](https://packages.ubuntu.com/focal/build-essential)
- Build binary: [`make`](https://www.gnu.org/software/make/)
- CLI: `./diode [options...]`

#### Branch Management
### Branch Management

- `main` → production ready environment.
- `dev` → testing changes to be merged into `main`.

#### Directory Structure
### Directory Structure

```zsh
.
Expand All @@ -39,7 +38,7 @@ Scripts for verifying TCP passthrough functionality.
5 directories, 7 files
```

#### Architecture Diagram
### Architecture Diagram

```mermaid
graph LR
Expand All @@ -52,23 +51,23 @@ graph LR
## User Stories

#### Scenario Planning
### Scenario Planning

1. Power Plant Operator
1. Information Security Auditor
1. Energy Awareness Application Developer
1. Community Member

#### Threat Model[^1]
### Threat Model[^1]

- [ ] Tactics
- [ ] Techniques
- [ ] Procedures

## System Benchmarking
### System Benchmarking

#### Experimental Design
### Experimental Design

###### [Device Configuration](docs/SOP.md)
#### [Device Configuration](docs/SOP.md)

[^1]: https://csrc.nist.gov/glossary/term/tactics_techniques_and_procedures
[^1]: <https://csrc.nist.gov/glossary/term/tactics_techniques_and_procedures>
10 changes: 5 additions & 5 deletions docs/SOP.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,16 @@
## Research Question

#### State Synchronization
### State Synchronization

#### System Replication
### System Replication

## System Analysis

#### Latency
### Latency

#### Bandwidth
### Bandwidth

#### Throughput
### Throughput

## Data Integrity
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ require (

require (
github.com/cpuguy83/go-md2man/v2 v2.0.4 // indirect
github.com/gorilla/websocket v1.5.1 // indirect
github.com/gorilla/websocket v1.5.3 // indirect
github.com/russross/blackfriday/v2 v2.1.0 // indirect
github.com/xrash/smetrics v0.0.0-20240521201337-686a1a2994c1 // indirect
golang.org/x/net v0.25.0 // indirect
golang.org/x/net v0.27.0 // indirect
golang.org/x/sync v0.7.0 // indirect
)
8 changes: 4 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,16 @@ github.com/cpuguy83/go-md2man/v2 v2.0.4 h1:wfIWP927BUkWJb2NmU/kNDYIBTh/ziUX91+lV
github.com/cpuguy83/go-md2man/v2 v2.0.4/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
github.com/eclipse/paho.mqtt.golang v1.4.3 h1:2kwcUGn8seMUfWndX0hGbvH8r7crgcJguQNCyp70xik=
github.com/eclipse/paho.mqtt.golang v1.4.3/go.mod h1:CSYvoAlsMkhYOXh/oKyxa8EcBci6dVkLCbo5tTC1RIE=
github.com/gorilla/websocket v1.5.1 h1:gmztn0JnHVt9JZquRuzLw3g4wouNVzKL15iLr/zn/QY=
github.com/gorilla/websocket v1.5.1/go.mod h1:x3kM2JMyaluk02fnUJpQuwD2dCS5NDG2ZHL0uE0tcaY=
github.com/gorilla/websocket v1.5.3 h1:saDtZ6Pbx/0u+bgYQ3q96pZgCzfhKXGPqt7kZ72aNNg=
github.com/gorilla/websocket v1.5.3/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
github.com/russross/blackfriday/v2 v2.1.0 h1:JIOH55/0cWyOuilr9/qlrm0BSXldqnqwMsf35Ld67mk=
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
github.com/urfave/cli/v2 v2.27.2 h1:6e0H+AkS+zDckwPCUrZkKX38mRaau4nL2uipkJpbkcI=
github.com/urfave/cli/v2 v2.27.2/go.mod h1:g0+79LmHHATl7DAcHO99smiR/T7uGLw84w8Y42x+4eM=
github.com/xrash/smetrics v0.0.0-20240521201337-686a1a2994c1 h1:gEOO8jv9F4OT7lGCjxCBTO/36wtF6j2nSip77qHd4x4=
github.com/xrash/smetrics v0.0.0-20240521201337-686a1a2994c1/go.mod h1:Ohn+xnUBiLI6FVj/9LpzZWtj1/D6lUovWYBkxHVV3aM=
golang.org/x/net v0.25.0 h1:d/OCCoBEUq33pjydKrGQhw7IlUPI2Oylr+8qLx49kac=
golang.org/x/net v0.25.0/go.mod h1:JkAGAh7GEvH74S6FOH42FLoXpXbE/aqXSrIQjXgsiwM=
golang.org/x/net v0.27.0 h1:5K3Njcw06/l2y9vpGCSdcxWOYHOUk3dVNGDXN+FvAys=
golang.org/x/net v0.27.0/go.mod h1:dDi0PyhWNoiUOrAS8uXv/vnScO4wnHQO4mj9fn/RytE=
golang.org/x/sync v0.7.0 h1:YsImfSBoP9QPYL0xyKJPq0gcaJdG3rInoqxTWbfQu9M=
golang.org/x/sync v0.7.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
Expand Down
3 changes: 2 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@ func main() {
Action: func(dCtx *cli.Context) error {
fmt.Println("----- DIAGNOSTICS -----")
fmt.Printf("%+v\n", config)
// TODO: Flag for current data diode ACK response.
return nil
},
},
Expand All @@ -147,7 +148,7 @@ func main() {
{
Name: "mqtt-subscribe",
Aliases: []string{"ms"},
Usage: "Recieve payload, encapsulate message, & stream to diode",
Usage: "Receive payload, encapsulate message, & stream to diode",
Action: func(msCtx *cli.Context) error {
utility.InboundMessageFlow(subBrokerIP, subBrokerPort, subBrokerTopic, clientLocation)
return nil
Expand Down
3 changes: 2 additions & 1 deletion utility/application.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ const (
SAMPLE = 10240
)

// For versions prior to Go 1.21 (built-ins).
func min(a, b int) int {
if a < b {
return a
Expand Down Expand Up @@ -150,7 +151,7 @@ func StartPlaceholderServer(host string, port int) {
}
}

func RecieveMessage(destination string, messages chan<- string) error {
func ReceiveMessage(destination string, messages chan<- string) error {
server, err := net.Listen("tcp", destination)
if err != nil {
fmt.Println(">> [!] Error connecting to diode: ", err)
Expand Down
38 changes: 24 additions & 14 deletions utility/application_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,26 +5,36 @@ import (
"testing"
)

func TestRecieveMessage(t *testing.T) {
serverMock, err := net.Listen("tcp", "localhost:0")
func TestSendMessage(t *testing.T) {
server, err := net.Listen(CONN_TYPE, "localhost:0")
if err != nil {
t.Fatalf("[!] Failed to start the mock server: %v", err)
t.Fatalf(">> [!] Failed to create mock server: %v", err)
}
defer serverMock.Close()
defer server.Close()

serverMockAddress := serverMock.Addr().String()

contents := make(chan string)
go func() {
err := RecieveMessage(serverMockAddress, contents)
conn, err := server.Accept()
if err != nil {
t.Errorf("[?] Returned an error: %v", err)
t.Error(">> [!] Failed to accept connection:", err)
return
}
defer conn.Close()

buffer := make([]byte, CHUNK_SIZE)
_, err = conn.Read(buffer)
if err != nil {
t.Error("[!] Failed to read data:", err)
return
}

_, err = conn.Write([]byte(ACKNOWLEDGEMENT))
if err != nil {
t.Error("[!] Failed to send acknowledgement:", err)
return
}
}()

conn, err := net.Dial("tcp", serverMockAddress)
if err != nil {
t.Fatalf("[!] Failed to connect to the mock server: %v", err)
}
defer conn.Close()
input := "data-diode"
client := server.Addr().String()
SendMessage(input, client)
}
31 changes: 31 additions & 0 deletions utility/metadata.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package utility

import (
"crypto/md5"
"encoding/base64"
"fmt"
"time"
)

func MakeTimestamp() int64 {
return time.Now().UnixMicro()
}

func Verification(data string) string {
hash := md5.New()
hash.Write([]byte(data))
return fmt.Sprintf("%x", hash.Sum(nil))
}

func EncapsulatePayload(message string) string {
encoded := base64.StdEncoding.EncodeToString([]byte(message))
return encoded
}

func UnencapsulatePayload(message string) string {
decoded, err := base64.StdEncoding.DecodeString(message)
if err != nil {
fmt.Println(">> [!] Error decoding the message: ", err)
}
return string(decoded)
}
68 changes: 68 additions & 0 deletions utility/metadata_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package utility

import (
"fmt"
"testing"
)

func TestMakeTimestamp(t *testing.T) {
actual := MakeTimestamp()
expected := 16

length := len(fmt.Sprintf("%d", actual))

if length != expected {
t.Errorf("Expected %d but got %d", expected, length)
}
}

func TestVerification(t *testing.T) {
testcases := []struct {
input string
}{
{"data-diode"},
}

for _, test := range testcases {
actual := Verification(test.input)
expected := "ef217cdf54b0b3ece89a0d7686da550b"

if actual != expected {
t.Errorf("Expected %s but got %s", expected, actual)
}
}
}

func TestEncapsulatePayload(t *testing.T) {
testcases := []struct {
input string
}{
{"data-diode"},
}

for _, test := range testcases {
actual := EncapsulatePayload(test.input)
expected := "ZGF0YS1kaW9kZQ=="

if actual != expected {
t.Errorf("Expected %s but got %s", expected, actual)
}
}
}

func TestUnencapsulatePayload(t *testing.T) {
testcases := []struct {
input string
}{
{"ZGF0YS1kaW9kZQ=="},
}

for _, test := range testcases {
actual := UnencapsulatePayload(test.input)
expected := "data-diode"

if actual != expected {
t.Errorf("Expected %s but got %s", expected, actual)
}
}
}
Loading

0 comments on commit acec3a1

Please sign in to comment.