---
title: "17.9 Exercises"
url: https://una-lang.org/en/learn/information-flow/exercises/
release: v0.4.0
edition: "0.2"
---

# 17.9 Exercises

Each exercise starts from `exercises/N/start`: the day-to-day program with the exercise's examples added, and a stub when there is one. `una test .` fails there until the exercise is done. Work in the start package: edit the files it names, run `una fmt .` to put the code in canonical form, then `una check .` until it passes, then `una test .`.

## 17.9.1 Exercise 1: release the domain

::::exercise
The operations team counts notices by mail provider, so it needs the domain of each address, and nothing else. Add a policy `email_domain` to `privacy.una`, from `secret[PII] Text` to `public Text`, that gives the part after the `@`, or `(none)` when there is no single `@`. The start package already imports it, so `una check .` reports U1001 until it exists, and it ends with:

```una include="exercises/1/start/src/notices.una#L160-L166" title="exercises/1/start/src/notices.una"
example "the domain of an address"
  do email_domain("rosa@example.invalid")
  expect result == "example.invalid"

example "an address with no domain has none"
  do email_domain("no address")
  expect result == "(none)"
```

:::hint
*Hint:* write it in two parts, as `masked_email` is. First the policy, with its five lines: `from`, `to`, `via`, a `purpose` and an `owner`. Then a private `via` function with the same types apart from labels, which splits the address on `@`, as `mask_email` does, and gives the second part when there are exactly two.
:::

Check your answer with `una test .`.

:::answer Answer (*check, run, test*; `exercises/1/answer`).
Add the policy and its `via` function to `privacy.una`:

```diff include="exercises/1/start..exercises/1/answer:src/privacy.una" context="1" title="src/privacy.una"
--- a/src/privacy.una
+++ b/src/privacy.una
@@ -19 +19,18 @@
     "{address.slice(start: 0, end: 1)}***@{domain}"
+
+--- Releases the domain of an email address, and nothing of the mailbox.
+pub policy email_domain
+  from secret[PII] Text
+  to public Text
+  via domain_of
+  purpose "count notices by mail provider"
+  owner support
+
+domain_of(address: secret[PII] Text) -> public Text
+=
+  let parts = address.split(on: "@")
+  if parts.len != 2
+    "(none)"
+  else
+    parts.at(index: 1)
+      if none use "(none)"
```

`una test .` reports 11 examples passing. `una report .` now lists the calls of `email_domain` under `information`, with their purpose.
:::
::::

## 17.9.2 Exercise 2: priority notices

::::exercise
Some customers have a priority flag, which the store keeps `confidential`. Their notice's subject is `Priority: order A-1001 has shipped` instead of `Order A-1001 has shipped`. The start package adds the field `vip: confidential Bool` to `Customer`, uses `subject` in `notify`, and has a stub of it that ignores the flag:

```una include="exercises/2/start/src/notices.una#subject" title="exercises/2/start/src/notices.una"
--- The subject of a notice. Exercise 2: mark it `Priority:` for a priority customer.
pub subject(customer: Customer, shipment: Shipment) -> confidential Text = "Order {shipment.order} has shipped"
```

and these examples:

```una include="exercises/2/start/src/notices.una#L164-L170" title="exercises/2/start/src/notices.una"
example "a priority customer's subject says so"
  do subject(Customer(id: "C-17", name: "Rosa", email: "rosa@example.invalid", wants_notices: true, vip: true), shipment: Shipment(order: "A-1001", customer: "C-17", carrier: "Parcelo"))
  expect result == "Priority: order A-1001 has shipped"

example "another customer's subject is plain"
  do subject(Customer(id: "C-20", name: "Tomas", email: "tomas@example.invalid", wants_notices: true, vip: false), shipment: Shipment(order: "A-1002", customer: "C-20", carrier: "Parcelo"))
  expect result == "Order A-1002 has shipped"
```

:::hint
*Hint:* an `if … then … else …` on `customer.vip`, with an interpolated subject in each branch ([How the program counter rises](https://una-lang.org/en/learn/information-flow/branches-and-failures-implicit-flows/#how-the-program-counter-rises)). Keep the result type: try `-> Text` once, and read what the checker says.
:::

Check your answer with `una test .`. On the start package it reports:

```text include="exercises/2/start/expected/commands/test.txt#L11-L13,L16"
example notices "a priority customer's subject says so" ... FAILED
  src/notices.una:166: expect result == "Priority: order A-1001 has shipped"
  it is false, with `result` = `"Order A-1001 has shipped"`
11 examples: 10 passed, 1 failed
```

:::answer Answer (*check, run, test*; `exercises/2/answer`).
Branch on the flag:

```diff include="exercises/2/start..exercises/2/answer:src/notices.una" context="1" title="src/notices.una"
--- a/src/notices.una
+++ b/src/notices.una
@@ -38,4 +38,4 @@
 
---- The subject of a notice. Exercise 2: mark it `Priority:` for a priority customer.
-pub subject(customer: Customer, shipment: Shipment) -> confidential Text = "Order {shipment.order} has shipped"
+--- The subject of a notice. It is decided by the confidential `vip` flag, so it is confidential.
+pub subject(customer: Customer, shipment: Shipment) -> confidential Text = if customer.vip then "Priority: order {shipment.order} has shipped" else "Order {shipment.order} has shipped"
 
```

The subject is `confidential` because a confidential flag chose it. The mail service accepts `secret[PII]`, which is above `confidential`, so the subject may go there, and nowhere public.
:::
::::

## 17.9.3 Exercise 3: a blocked domain

::::exercise
A partner sends its own notices to the addresses at `blocked.example.invalid`, so the store must not mail them. The start package adds the variant `Blocked(id: Text)`, a customer at that domain, and this example:

```una include="exercises/3/start/src/notices.una#L162-L166" title="exercises/3/start/src/notices.una"
example "an address at a blocked domain is rejected before any mail is sent"
  given @customers = directory(sample_customers())
  given @mailer = mail_down()
  do notify(Shipment(order: "A-1006", customer: "C-30", carrier: "Parcelo"))
  expect fails Blocked
```

Like the failure paths of the program, it installs `mail_down`, so it passes only if `notify` fails with `Blocked` before any mail goes out.

The direct way, a branch on `customer.email.ends_with(suffix: "@blocked.example.invalid")` in `notify`, does not check. Among the diagnostics una reports:

```text include="exercises/3/direct/expected/rendered.txt#L10"
[U4002] Error: this `fail` depends on `secret[PII]` data, so its failure would reveal it; return a labeled value instead of failing
```

:::hint
*Hint:* `notify` needs one public fact about the address: is it at the blocked domain? A policy can release exactly that bit ([What to release](https://una-lang.org/en/learn/information-flow/releasing-data-policies-and-trusted-modules/#what-to-release)), and `notify` can branch on a public `Bool` and fail. Add the policy to `privacy.una`, import it in `notices.una` next to `masked_email`, and add an `else if` branch to `notify`.
:::

Check your answer with `una test .`.

:::answer Answer (*check, run, test*; `exercises/3/answer`).
Add a policy to `privacy.una` that releases one `Bool`:

```diff include="exercises/3/start..exercises/3/answer:src/privacy.una" context="1" title="src/privacy.una"
--- a/src/privacy.una
+++ b/src/privacy.una
@@ -19 +19,11 @@
     "{address.slice(start: 0, end: 1)}***@{domain}"
+
+--- Releases one fact about an address: whether it is at the blocked domain.
+pub policy blocked_domain
+  from secret[PII] Text
+  to public Bool
+  via is_blocked
+  purpose "skip the domain whose notices a partner sends"
+  owner support
+
+is_blocked(address: secret[PII] Text) -> public Bool = address.ends_with(suffix: "@blocked.example.invalid")
```

Import it in `notices.una`, and branch on it in `notify`:

```diff include="exercises/3/start..exercises/3/answer:src/notices.una" context="1" title="src/notices.una"
--- a/src/notices.una
+++ b/src/notices.una
@@ -2,3 +2,3 @@
 
-import privacy (masked_email)
+import privacy (blocked_domain, masked_email)
 
@@ -49,2 +49,4 @@
     fail OptedOut(id: customer.id)
+  else if blocked_domain(customer.email)
+    fail Blocked(id: customer.id)
   else
```

The policy releases one bit of personal data, on purpose, and the report lists the call. Without it, the `fail` would reveal that same bit to every caller with nothing to show for it in review.
:::
::::
