From 1a3c9466b919a7e80c06f827dd12eceb7fde63e3 Mon Sep 17 00:00:00 2001 From: lukasmittag Date: Fri, 8 Dec 2023 16:24:25 +0100 Subject: [PATCH] Address findings: change to grpc codes; fix naming --- kuksa_databroker/databroker/Cargo.toml | 2 +- .../tests/features/read_write_values.feature | 50 +++++++++---------- .../databroker/tests/read_write_values.rs | 34 ++++++------- .../databroker/tests/world/mod.rs | 4 +- 4 files changed, 42 insertions(+), 48 deletions(-) diff --git a/kuksa_databroker/databroker/Cargo.toml b/kuksa_databroker/databroker/Cargo.toml index 152727f6..cde54975 100644 --- a/kuksa_databroker/databroker/Cargo.toml +++ b/kuksa_databroker/databroker/Cargo.toml @@ -80,8 +80,8 @@ vergen = { version = "8", features = [ [dev-dependencies] anyhow = "1.0" +chrono = "^0.4" cucumber = { version = "0.20", default-features = false, features = ["libtest", "macros"] } -chrono = { version = "0.4.31" } [[test]] name = "read_write_values" diff --git a/kuksa_databroker/databroker/tests/features/read_write_values.feature b/kuksa_databroker/databroker/tests/features/read_write_values.feature index 05fbe055..a49a8581 100644 --- a/kuksa_databroker/databroker/tests/features/read_write_values.feature +++ b/kuksa_databroker/databroker/tests/features/read_write_values.feature @@ -3,63 +3,63 @@ Feature: Reading and writing values of a VSS Data Entry Rule: Access with right permissions succeeds and fails with wrong/no permissions Background: - Given a running Databroker server with authorization true with the following Data Entries registered + Given a running Databroker server with authorization enabled with the following Data Entries registered | path | data type | change type | type | | Vehicle.Speed | float | Static | Sensor | | Vehicle.ADAS.ABS.IsEnabled | bool | Static | Actuator | - Scenario: Write the current value of an unset Data Entry without authenticating fails + Scenario: Writing the current value of an unset Data Entry without authenticating fails When a client sets the current value of Vehicle.Width of type float to 13.4 - Then the current value for Vehicle.Width can not be accessed because we are unauthorized + Then the operation fails with status code 16 Scenario: Read the current value of an unset Data Entry without authenticating fails When a client gets the current value of Vehicle.Width - Then the current value for Vehicle.Width can not be accessed because we are unauthorized + Then the operation fails with status code 16 - Scenario: Write the current value of a Data Entry without right permissions fails - When a client uses a token with auhtorization read + Scenario: Writing the current value of a Data Entry without right permissions fails + When a client uses a token with scope read And a client sets the current value of Vehicle.Speed of type float to 13.4 Then setting the value for Vehicle.Speed fails with error code 403 - Scenario: Write the current value of a Data Entry without right permissions fails - When a client uses a token with auhtorization actuate + Scenario: Writing the current value of a Data Entry without right permissions fails + When a client uses a token with scope actuate And a client sets the current value of Vehicle.Speed of type float to 13.4 Then setting the value for Vehicle.Speed fails with error code 403 - Scenario: Write the current value of a Data Entry without right permissions fails - When a client uses a token with auhtorization provide:Vehicle.ADAS.ABS.IsEnabled + Scenario: Writing the current value of a Data Entry without right permissions fails + When a client uses a token with scope provide:Vehicle.ADAS.ABS.IsEnabled And a client sets the current value of Vehicle.Speed of type float to 13.4 Then setting the value for Vehicle.Speed fails with error code 403 - Scenario: Write the current value of a Data Entry with right permissions succeeds - When a client uses a token with auhtorization provide:Vehicle.Speed + Scenario: Writing the current value of a Data Entry with right permissions succeeds + When a client uses a token with scope provide:Vehicle.Speed And a client sets the current value of Vehicle.Speed of type float to 13.4 - Then the set operation succeeds without an error + Then the set operation succeeds - Scenario: Write the target value of a Data Entry without right permissions fails - When a client uses a token with auhtorization read + Scenario: Writing the target value of a Data Entry without right permissions fails + When a client uses a token with scope read And a client sets the target value of Vehicle.ADAS.ABS.IsEnabled of type bool to true Then setting the value for Vehicle.Speed fails with error code 403 - Scenario: Write the target value of a Data Entry without right permissions fails - When a client uses a token with auhtorization provide + Scenario: Writing the target value of a Data Entry without right permissions fails + When a client uses a token with scope provide And a client sets the target value of Vehicle.ADAS.ABS.IsEnabled of type bool to true Then setting the value for Vehicle.Speed fails with error code 403 - Scenario: Write the target value of a Data Entry without right permissions fails - When a client uses a token with auhtorization actuate:Vehicle.Speed + Scenario: Writing the target value of a Data Entry without right permissions fails + When a client uses a token with scope actuate:Vehicle.Speed And a client sets the target value of Vehicle.ADAS.ABS.IsEnabled of type bool to true Then setting the value for Vehicle.Speed fails with error code 403 - Scenario: Write the target value of a Data Entry with right permissions succeeds - When a client uses a token with auhtorization actuate:Vehicle.ADAS.ABS.IsEnabled + Scenario: Writing the target value of a Data Entry with right permissions succeeds + When a client uses a token with scope actuate:Vehicle.ADAS.ABS.IsEnabled And a client sets the target value of Vehicle.ADAS.ABS.IsEnabled of type bool to true - Then the set operation succeeds without an error + Then the set operation succeeds Rule: Accessing unregistered Data Entries fails Background: - Given a running Databroker server with authorization false + Given a running Databroker server with authorization disabled Scenario: Setting the current value of an unregistered Data Entry fails When a client sets the current value of No.Such.Path of type float to 13.4 @@ -80,7 +80,7 @@ Feature: Reading and writing values of a VSS Data Entry Rule: Target values can only be set on Actuators Background: - Given a running Databroker server with authorization false with the following Data Entries registered + Given a running Databroker server with authorization disabled with the following Data Entries registered | path | data type | change type | type | | Vehicle.Powertrain.Range | uint32 | Continuous | Sensor | | Vehicle.Width | uint16 | Static | Attribute | @@ -96,7 +96,7 @@ Feature: Reading and writing values of a VSS Data Entry Rule: Accessing registered Data Entries works Background: - Given a running Databroker server with authorization false with the following Data Entries registered + Given a running Databroker server with authorization disabled with the following Data Entries registered | path | data type | change type | type | | Vehicle.Cabin.Lights.AmbientLight | uint8 | OnChange | Actuator | | Vehicle.Cabin.Sunroof.Position | int8 | OnChange | Actuator | diff --git a/kuksa_databroker/databroker/tests/read_write_values.rs b/kuksa_databroker/databroker/tests/read_write_values.rs index 6b9a230b..b127d673 100644 --- a/kuksa_databroker/databroker/tests/read_write_values.rs +++ b/kuksa_databroker/databroker/tests/read_write_values.rs @@ -14,11 +14,9 @@ use core::panic; use std::{collections::HashMap, future, time::SystemTime, vec}; -use common::ClientError; use cucumber::{cli, gherkin::Step, given, then, when, writer, World as _}; use databroker::broker; use databroker_proto::kuksa::val::v1::{datapoint::Value, DataType, Datapoint}; -use tonic::Code; use tracing::debug; use world::{DataBrokerWorld, ValueType}; @@ -74,9 +72,18 @@ fn get_data_entries_from_table( data_entries } -#[given(regex = "^a running Databroker server with authorization (true|false).*$")] -async fn start_databroker_server(w: &mut DataBrokerWorld, auth: bool, step: &Step) { - w.start_databroker(get_data_entries_from_table(step), auth) +#[given(regex = "^a running Databroker server with authorization (enabled|disabled).*$")] +async fn start_databroker_server(w: &mut DataBrokerWorld, auth: String, step: &Step) { + let authorization_enabled: bool; + if auth == "enabled" { + authorization_enabled = true; + } else if auth == "disabled" { + authorization_enabled = false; + } else { + panic!("Not a known authorization keyword use enabled/disabled!") + } + + w.start_databroker(get_data_entries_from_table(step), authorization_enabled) .await; assert!(w.broker_client.is_some()) } @@ -93,7 +100,7 @@ async fn a_known_data_entry_has_value( w.assert_set_succeeded() } -#[when(expr = "a client uses a token with auhtorization {word}")] +#[when(expr = "a client uses a token with scope {word}")] async fn authorize_client(w: &mut DataBrokerWorld, scope: String) { let token = w.create_token(scope); w.broker_client @@ -244,20 +251,7 @@ fn assert_request_failure(w: &mut DataBrokerWorld, expected_status_code: i32) { w.assert_status_has_code(expected_status_code) } -#[then(expr = "the current value for {word} can not be accessed because we are unauthorized")] -fn assert_current_value_unauthenticated(w: &mut DataBrokerWorld) { - if let Some(error) = w.current_client_error.clone() { - match error { - ClientError::Connection(e) => { - panic!("No connection error {:?} should occcur", e) - } - ClientError::Function(e) => panic!("No function error {:?} should occur", e), - ClientError::Status(status) => assert_eq!(status.code(), Code::Unauthenticated), - } - } -} - -#[then(expr = "the set operation succeeds without an error")] +#[then(expr = "the set operation succeeds")] fn assert_set_succeeds(w: &mut DataBrokerWorld) { w.assert_set_succeeded() } diff --git a/kuksa_databroker/databroker/tests/world/mod.rs b/kuksa_databroker/databroker/tests/world/mod.rs index 821b184b..18fbe067 100644 --- a/kuksa_databroker/databroker/tests/world/mod.rs +++ b/kuksa_databroker/databroker/tests/world/mod.rs @@ -371,7 +371,7 @@ impl DataBrokerWorld { } } - pub fn create_token(&self, _scope: String) -> String { + pub fn create_token(&self, scope: String) -> String { let datetime = Utc::now(); let timestamp = datetime.timestamp(); let timestamp_exp = (match datetime.checked_add_months(chrono::Months::new(24)) { @@ -386,7 +386,7 @@ impl DataBrokerWorld { aud: vec!["kuksa.val".to_string()], iat: timestamp, exp: timestamp_exp, - scope: _scope, + scope, }; // Create an encoding key from the private key