@voxgig/sdkgen 4.48.1 → 4.49.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (119) hide show
  1. package/bin/voxgig-sdkgen +1 -1
  2. package/package.json +1 -1
  3. package/project/.sdk/model/feature/secrets.aontu +3 -0
  4. package/project/.sdk/src/cmp/c/Config_c.ts +6 -1
  5. package/project/.sdk/src/cmp/c/fragment/Entity.fragment.c +0 -10
  6. package/project/.sdk/src/cmp/clojure/fragment/Entity.fragment.clj +0 -9
  7. package/project/.sdk/src/cmp/csharp/fragment/EntityBase.fragment.cs +0 -11
  8. package/project/.sdk/src/cmp/go/fragment/Entity.fragment.go +0 -7
  9. package/project/.sdk/src/cmp/java/fragment/EntityBase.fragment.java +0 -10
  10. package/project/.sdk/src/cmp/js/fragment/EntityBase.fragment.js +2 -11
  11. package/project/.sdk/src/cmp/kotlin/fragment/EntityBase.fragment.kt +0 -10
  12. package/project/.sdk/src/cmp/lua/fragment/Entity.fragment.lua +0 -11
  13. package/project/.sdk/src/cmp/ocaml/Entity_ocaml.ts +2 -10
  14. package/project/.sdk/src/cmp/perl/fragment/Entity.fragment.pm +0 -13
  15. package/project/.sdk/src/cmp/php/fragment/Entity.fragment.php +0 -10
  16. package/project/.sdk/src/cmp/py/fragment/Entity.fragment.py +0 -10
  17. package/project/.sdk/src/cmp/rb/fragment/Entity.fragment.rb +0 -10
  18. package/project/.sdk/src/cmp/rust/fragment/Entity.fragment.rs +0 -12
  19. package/project/.sdk/src/cmp/scala/fragment/EntityBase.fragment.scala +0 -10
  20. package/project/.sdk/src/cmp/swift/fragment/EntityBase.fragment.swift +0 -9
  21. package/project/.sdk/src/cmp/ts/fragment/EntityBase.fragment.ts +0 -7
  22. package/project/.sdk/src/cmp/zig/fragment/Entity.fragment.zig +1 -14
  23. package/project/.sdk/tm/c/feature/cache.c +16 -1
  24. package/project/.sdk/tm/c/feature/secrets.c +149 -18
  25. package/project/.sdk/tm/c/tests/feature/secrets/secrets_test.c +319 -70
  26. package/project/.sdk/tm/c/tests/feature_test.c +31 -0
  27. package/project/.sdk/tm/clojure/feature/secrets/sdk/feature/secrets.clj +74 -29
  28. package/project/.sdk/tm/clojure/src/sdk/core.clj +55 -6
  29. package/project/.sdk/tm/clojure/src/sdk/features.clj +16 -2
  30. package/project/.sdk/tm/clojure/test/sdk/test/feature/secrets.clj +191 -48
  31. package/project/.sdk/tm/clojure/test/sdk/test/feature.clj +16 -0
  32. package/project/.sdk/tm/cpp/core/types.hpp +1 -11
  33. package/project/.sdk/tm/cpp/feature/cache.hpp +14 -1
  34. package/project/.sdk/tm/cpp/feature/secrets.hpp +133 -18
  35. package/project/.sdk/tm/cpp/test/feature/secrets/secrets_test.cpp +175 -8
  36. package/project/.sdk/tm/cpp/test/feature_test.cpp +27 -0
  37. package/project/.sdk/tm/csharp/core/Control.cs +0 -5
  38. package/project/.sdk/tm/csharp/feature/CacheFeature.cs +16 -0
  39. package/project/.sdk/tm/csharp/feature/SecretsFeature.cs +77 -38
  40. package/project/.sdk/tm/csharp/test/FeatureTest.cs +29 -0
  41. package/project/.sdk/tm/csharp/test/feature/secrets/SecretsFeatureTest.cs +315 -68
  42. package/project/.sdk/tm/elixir/lib/projectname/entity_base.ex +0 -13
  43. package/project/.sdk/tm/elixir/lib/projectname/feature/cache.ex +32 -10
  44. package/project/.sdk/tm/elixir/lib/projectname/feature/secrets.ex +78 -22
  45. package/project/.sdk/tm/elixir/test/feature/secrets/secrets_test.exs +178 -19
  46. package/project/.sdk/tm/elixir/test/feature_test.exs +20 -0
  47. package/project/.sdk/tm/go/feature/cache_feature.go +14 -1
  48. package/project/.sdk/tm/go/feature/secrets_feature.go +110 -15
  49. package/project/.sdk/tm/go/test/feature/secrets/secrets_feature_test.go +373 -87
  50. package/project/.sdk/tm/go/test/feature_test.go +32 -0
  51. package/project/.sdk/tm/go/utility/media.go +2 -2
  52. package/project/.sdk/tm/java/core/Control.java +0 -5
  53. package/project/.sdk/tm/java/feature/CacheFeature.java +12 -0
  54. package/project/.sdk/tm/java/feature/SecretsFeature.java +104 -34
  55. package/project/.sdk/tm/java/test/FeatureTest.java +25 -0
  56. package/project/.sdk/tm/java/test/PrimaryUtilityTest.java +39 -0
  57. package/project/.sdk/tm/java/test/feature/secrets/SecretsFeatureTest.java +267 -53
  58. package/project/.sdk/tm/java/utility/Fetcher.java +18 -2
  59. package/project/.sdk/tm/java/utility/Media.java +2 -2
  60. package/project/.sdk/tm/js/src/feature/cache/CacheFeature.js +13 -0
  61. package/project/.sdk/tm/js/src/feature/secrets/SecretsFeature.js +79 -29
  62. package/project/.sdk/tm/js/test/feature/secrets/Secrets.test.js +282 -93
  63. package/project/.sdk/tm/js/test/feature.test.js +17 -0
  64. package/project/.sdk/tm/kotlin/core/Control.kt +0 -5
  65. package/project/.sdk/tm/kotlin/feature/CacheFeature.kt +12 -0
  66. package/project/.sdk/tm/kotlin/feature/SecretsFeature.kt +84 -24
  67. package/project/.sdk/tm/kotlin/test/FeatureTest.kt +24 -0
  68. package/project/.sdk/tm/kotlin/test/PrimaryUtilityTest.kt +35 -0
  69. package/project/.sdk/tm/kotlin/test/feature/secrets/SecretsTest.kt +236 -54
  70. package/project/.sdk/tm/kotlin/utility/Fetcher.kt +13 -1
  71. package/project/.sdk/tm/lua/feature/cache_feature.lua +20 -0
  72. package/project/.sdk/tm/lua/feature/secrets_feature.lua +92 -15
  73. package/project/.sdk/tm/lua/test/feature/secrets/secrets_feature_test.lua +208 -43
  74. package/project/.sdk/tm/lua/test/feature_test.lua +18 -0
  75. package/project/.sdk/tm/lua/utility/clean.lua +1 -0
  76. package/project/.sdk/tm/ocaml/feature/secrets_feature.ml +100 -14
  77. package/project/.sdk/tm/ocaml/sdk_features.ml +7 -1
  78. package/project/.sdk/tm/ocaml/test/feature/secrets/t_secrets.ml +143 -7
  79. package/project/.sdk/tm/ocaml/test/t_feature.ml +15 -0
  80. package/project/.sdk/tm/perl/feature/cache_feature.pm +13 -0
  81. package/project/.sdk/tm/perl/feature/secrets_feature.pm +87 -32
  82. package/project/.sdk/tm/perl/t/feature/secrets/secrets.t +177 -24
  83. package/project/.sdk/tm/perl/t/feature.t +24 -0
  84. package/project/.sdk/tm/php/feature/CacheFeature.php +13 -0
  85. package/project/.sdk/tm/php/feature/SecretsFeature.php +79 -23
  86. package/project/.sdk/tm/php/test/FeatureTest.php +23 -0
  87. package/project/.sdk/tm/php/test/feature/secrets/SecretsTest.php +258 -48
  88. package/project/.sdk/tm/py/pkg/feature/cache_feature.py +7 -1
  89. package/project/.sdk/tm/py/pkg/feature/secrets_feature.py +90 -29
  90. package/project/.sdk/tm/py/test/feature/secrets/test_secrets.py +313 -80
  91. package/project/.sdk/tm/py/test/test_feature.py +18 -0
  92. package/project/.sdk/tm/rb/feature/cache_feature.rb +12 -0
  93. package/project/.sdk/tm/rb/feature/secrets_feature.rb +68 -21
  94. package/project/.sdk/tm/rb/test/feature/secrets/secrets_feature_test.rb +214 -43
  95. package/project/.sdk/tm/rb/test/feature_test.rb +20 -0
  96. package/project/.sdk/tm/rust/feature/cache.rs +15 -1
  97. package/project/.sdk/tm/rust/feature/secrets.rs +103 -23
  98. package/project/.sdk/tm/rust/tests/feature/secrets/main.rs +357 -67
  99. package/project/.sdk/tm/rust/tests/feature_test.rs +41 -0
  100. package/project/.sdk/tm/scala/core/Control.scala +0 -5
  101. package/project/.sdk/tm/scala/feature/CacheFeature.scala +12 -0
  102. package/project/.sdk/tm/scala/feature/SecretsFeature.scala +88 -22
  103. package/project/.sdk/tm/scala/sdktest/SdkTestMain.scala +61 -1
  104. package/project/.sdk/tm/scala/sdktest/feature/secrets/SecretsTestMain.scala +230 -73
  105. package/project/.sdk/tm/scala/utility/Fetcher.scala +13 -1
  106. package/project/.sdk/tm/swift/Sources/ProjectNameSDK/core/Control.swift +0 -5
  107. package/project/.sdk/tm/swift/Sources/ProjectNameSDK/feature/CacheFeature.swift +11 -0
  108. package/project/.sdk/tm/swift/Sources/ProjectNameSDK/feature/SecretsFeature.swift +76 -16
  109. package/project/.sdk/tm/swift/Tests/ProjectNameSDKTests/FeatureTest.swift +21 -0
  110. package/project/.sdk/tm/swift/Tests/ProjectNameSDKTests/feature/secrets/SecretsFeatureTest.swift +257 -73
  111. package/project/.sdk/tm/ts/src/feature/cache/CacheFeature.ts +13 -0
  112. package/project/.sdk/tm/ts/src/feature/secrets/SecretsFeature.ts +77 -17
  113. package/project/.sdk/tm/ts/test/feature/secrets/Secrets.test.ts +282 -87
  114. package/project/.sdk/tm/ts/test/feature.test.ts +17 -0
  115. package/project/.sdk/tm/zig/feature/cache.zig +15 -1
  116. package/project/.sdk/tm/zig/feature/secrets.zig +109 -32
  117. package/project/.sdk/tm/zig/test/feature/secrets/secrets_test.zig +232 -9
  118. package/project/.sdk/tm/zig/test/feature_test.zig +33 -0
  119. package/project/sdkgen-package.json +1 -1
@@ -163,16 +163,40 @@ static const char* call_auth(Rec* r, int i, bool* present) {
163
163
  return get_str(headers, cred_header());
164
164
  }
165
165
 
166
- // The credential header carries the SPEC's credential prefix (if any), which
167
- // a TEMPLATE cannot know (a bearer scheme gives `Bearer <token>`, an apiKey
168
- // scheme the raw token). So assert on the CREDENTIAL and let the prefix be
169
- // whatever this SDK's API declares.
170
- static bool credential_is(const char* header, const char* token) {
166
+ // The header value prepare_auth writes for a token, in the form the DEFAULT
167
+ // client's auth gives: under Basic the token is the key half of the encoded
168
+ // pair, and a prefix is space-joined.
169
+ static bool paired_is(const char* header, const char* token, const char* secret) {
171
170
  if (NULL == header) return false;
172
- size_t hl = strlen(header);
173
- size_t tl = strlen(token);
174
- if (0 == strcmp(header, token)) return true;
175
- return hl > tl + 1 && ' ' == header[hl - tl - 1] && 0 == strcmp(header + hl - tl, token);
171
+ voxgig_value* auth = getp(sdk_options_map(test_sdk(NULL, NULL)), "auth");
172
+ const char* prefix = get_str(auth, "prefix");
173
+ bool basic = false;
174
+ get_bool(auth, "basic", &basic);
175
+
176
+ char* value = NULL;
177
+ if (basic) {
178
+ size_t n = strlen(token) + 1 + strlen(secret) + 1;
179
+ char* pair = (char*)malloc(n);
180
+ snprintf(pair, n, "%s:%s", token, secret);
181
+ value = clean_base64(pair);
182
+ free(pair);
183
+ } else {
184
+ value = strdup(token);
185
+ }
186
+
187
+ bool same;
188
+ if (NULL == prefix || '\0' == prefix[0]) {
189
+ same = 0 == strcmp(header, value);
190
+ } else {
191
+ size_t pl = strlen(prefix);
192
+ same = 0 == strncmp(header, prefix, pl) && ' ' == header[pl] && 0 == strcmp(header + pl + 1, value);
193
+ }
194
+ free(value);
195
+ return same;
196
+ }
197
+
198
+ static bool credential_is(const char* header, const char* token) {
199
+ return paired_is(header, token, "");
176
200
  }
177
201
 
178
202
  // ---- a LIVE client ----------------------------------------------------------
@@ -324,6 +348,15 @@ static void t_empty_apikey_defers(void) {
324
348
  CHECK(credential_is(call_auth(r, 0, NULL), "CHAIN01"), "active: an explicitly EMPTY apikey also defers");
325
349
  }
326
350
 
351
+ static void t_resolved_key_paired(void) {
352
+ Rec* r = rec_new();
353
+ ProjectNameSDK* sdk = live(r, providers1(memory(NULL, "APIKEY", "CHAIN05")),
354
+ cmap(1, "secret", v_str("PASS01")));
355
+ direct(sdk);
356
+ CHECK(paired_is(call_auth(r, 0, NULL), "CHAIN05", "PASS01"),
357
+ "active: a resolved key is placed as prepare_auth places it, the secret kept");
358
+ }
359
+
327
360
  static void t_auth_null_suppresses(void) {
328
361
  // Chain AND explicit apikey both set: suppression beats both.
329
362
  Rec* r = rec_new();
@@ -658,6 +691,19 @@ static void t_exchange_401_rebuys_and_retries(void) {
658
691
  CHECK(credential_is(call_auth(r, 3, NULL), "ACCESS02"), "401: the retry carried the fresh token, rebuilt from auth.prefix");
659
692
  }
660
693
 
694
+ static void t_exchange_retry_paired(void) {
695
+ Rec* r = rec_new();
696
+ r->script[0] = 401;
697
+ ProjectNameSDK* sdk = live(r, exchange_opts(refresh_store(), v_undef()),
698
+ cmap(1, "secret", v_str("PASS01")));
699
+ direct(sdk);
700
+ CHECK_INT_EQ(r->api, 2, "paired 401: the request was tried again once");
701
+ // calls: 0 token, 1 api(401), 2 token, 3 api(200)
702
+ CHECK(paired_is(call_auth(r, 1, NULL), "ACCESS01", "PASS01"), "paired 401: the first attempt kept the secret");
703
+ CHECK(paired_is(call_auth(r, 3, NULL), "ACCESS02", "PASS01"),
704
+ "paired 401: a token bought on a refusal is placed as prepare_auth places it");
705
+ }
706
+
661
707
  static void t_exchange_retry_once_not_loop(void) {
662
708
  Rec* r = rec_new();
663
709
  r->script[0] = 401;
@@ -757,6 +803,7 @@ static void t_exchange_auth_null_suppresses(void) {
757
803
  voxgig_value* res = direct(sdk);
758
804
  CHECK(!res_ok(res), "auth null + exchange: the refusal stands");
759
805
  CHECK_INT_EQ(r->api, 1, "auth null + exchange: a refusal of a deliberately unauthenticated request is not retried");
806
+ CHECK_INT_EQ(r->token, 0, "auth null + exchange: no token is bought");
760
807
  bool present = true;
761
808
  call_auth(r, r->token, &present); // the first API call follows any purchase
762
809
  CHECK(!present, "auth null + exchange: no credential on the wire, refusal or not");
@@ -791,6 +838,57 @@ static void t_exchange_test_mode_buys_nothing(void) {
791
838
  CHECK_INT_EQ(fh_track_int(feats[0].f, "buys"), 1, "test mode: counted as a (fake) purchase");
792
839
  }
793
840
 
841
+ // The bundled transport hands the feature a Location already resolved, so
842
+ // only a supplied system.fetch reaches the feature's own resolver with it raw.
843
+ typedef struct {
844
+ voxgig_value* calls;
845
+ const char* exchange;
846
+ const char* next;
847
+ } QueryFetch;
848
+
849
+ static voxgig_value* query_fetch(void* ud, voxgig_value* args) {
850
+ QueryFetch* q = (QueryFetch*)ud;
851
+ voxgig_value* urlv = voxgig_getelem(args, v_int(0), voxgig_new_undef());
852
+ voxgig_value* fetchdef = voxgig_getelem(args, v_int(1), voxgig_new_undef());
853
+ const char* url = voxgig_is_string(urlv) ? voxgig_as_string(urlv) : "";
854
+ voxgig_list_push(voxgig_as_list(q->calls),
855
+ cmap(2, "url", v_str(url), "fetchdef", voxgig_clone(fetchdef)));
856
+ if (0 == strcmp(url, q->exchange)) {
857
+ voxgig_value* moved = respond(307, v_map());
858
+ setp(moved, "headers", cmap(1, "location", v_str("?step=2")));
859
+ return moved;
860
+ }
861
+ if (0 == strcmp(url, q->next)) return respond(200, cmap(1, "access_token", v_str("MOVED01")));
862
+ return respond(200, cmap(1, "ok", v_bool(true)));
863
+ }
864
+
865
+ static void t_exchange_redirect_query_supplied(void) {
866
+ QueryFetch q = { v_list(), "http://api.test/auth/token", "http://api.test/auth/token?step=2" };
867
+ voxgig_value* exchange = cmap(3, "active", v_bool(true), "refresh", v_str("REFRESH01"),
868
+ "redirect", v_str("same-origin"));
869
+ ProjectNameSDK* sdk = projectname_sdk_new(cmap(3,
870
+ "base", v_str("http://api.test"),
871
+ "system", cmap(1, "fetch", vfn(query_fetch, &q)),
872
+ "feature", cmap(1, "secrets", cmap(3,
873
+ "active", v_bool(true),
874
+ "name", v_str("refresh_token"),
875
+ "exchange", exchange))), NULL);
876
+ voxgig_value* res = direct(sdk);
877
+ voxgig_value* first = voxgig_getelem(q.calls, v_int(0), voxgig_new_undef());
878
+ voxgig_value* second = voxgig_getelem(q.calls, v_int(1), voxgig_new_undef());
879
+ CHECK(res_ok(res), res_err(res));
880
+ CHECK_INT_EQ((int)voxgig_list_len(voxgig_as_list(q.calls)), 3,
881
+ "query-only: the exchange, once again, then the API call");
882
+ CHECK_STR_EQ(get_str(first, "url"), q.exchange, "query-only: the exchange is asked first");
883
+ CHECK_STR_EQ(get_str(second, "url"), q.next, "query-only: asked again at its own path");
884
+ CHECK_STR_EQ(get_str(getp(second, "fetchdef"), "method"), get_str(getp(first, "fetchdef"), "method"),
885
+ "query-only: the same method");
886
+ CHECK_STR_EQ(get_str(getp(second, "fetchdef"), "body"), get_str(getp(first, "fetchdef"), "body"),
887
+ "query-only: the same body");
888
+ CHECK_STR_EQ(get_str(getp(first, "fetchdef"), "redirect"), "manual", "query-only: manual first");
889
+ CHECK_STR_EQ(get_str(getp(second, "fetchdef"), "redirect"), "manual", "query-only: manual again");
890
+ }
891
+
794
892
  #if !defined(_WIN32)
795
893
 
796
894
  static int redirect_listener(int* port) {
@@ -816,9 +914,10 @@ static void redirect_say(int c, const char* text) {
816
914
  }
817
915
  }
818
916
 
819
- // A forked listener. The token path of the origin answers `moved`; every
820
- // other request is written to `log` as one line and answered with a token.
821
- static pid_t redirect_serve(int fd, int log, const char* moved) {
917
+ // A forked listener. The token path of the origin answers `moved`, with a
918
+ // line to `count`; every other request is written to `log` as one line and
919
+ // answered with a token.
920
+ static pid_t redirect_serve(int fd, int log, int count, const char* moved) {
822
921
  fflush(NULL);
823
922
  pid_t pid = fork();
824
923
  if (0 != pid) return pid;
@@ -855,6 +954,7 @@ static pid_t redirect_serve(int fd, int log, const char* moved) {
855
954
  if ((size_t)(buf + n - body) < want) want = (size_t)(buf + n - body);
856
955
  body[want] = '\0';
857
956
  if (NULL != moved && 0 == strcmp(target, "/api/auth/token")) {
957
+ redirect_say(count, "x\n");
858
958
  redirect_say(c, moved);
859
959
  } else {
860
960
  char line[1024];
@@ -879,75 +979,106 @@ static int redirect_lines(int log, char* into, size_t size) {
879
979
  return lines;
880
980
  }
881
981
 
982
+ enum { TO_MOVED, TO_AWAY, TO_MOVED_ABSOLUTE, TO_SELF, TO_QUERY };
983
+
984
+ typedef struct {
985
+ voxgig_value* res;
986
+ char base[64];
987
+ char location[128];
988
+ char away[512];
989
+ char again[512];
990
+ int reached;
991
+ int after;
992
+ int exchanged;
993
+ } RedirectRun;
994
+
995
+ // One exchange, on a fresh client, against a fresh origin answering it with
996
+ // `status` to `where`, and a fresh other origin.
997
+ static void redirect_run(int status, int where, const char* redirect, RedirectRun* r) {
998
+ int port = 0;
999
+ int otherport = 0;
1000
+ int fd = redirect_listener(&port);
1001
+ int otherfd = redirect_listener(&otherport);
1002
+ int elsewhere[2];
1003
+ int beyond[2];
1004
+ int count[2];
1005
+ CHECK(0 <= fd && 0 <= otherfd && 0 == pipe(elsewhere) && 0 == pipe(beyond) &&
1006
+ 0 == pipe(count), "redirect: loopback listeners");
1007
+
1008
+ if (TO_AWAY == where) {
1009
+ snprintf(r->location, sizeof(r->location), "http://127.0.0.1:%d/api/auth/token", otherport);
1010
+ } else if (TO_MOVED_ABSOLUTE == where) {
1011
+ snprintf(r->location, sizeof(r->location), "http://127.0.0.1:%d/api/moved/token", port);
1012
+ } else if (TO_QUERY == where) {
1013
+ snprintf(r->location, sizeof(r->location), "?step=2");
1014
+ } else {
1015
+ snprintf(r->location, sizeof(r->location), TO_SELF == where ? "/api/auth/token" : "/api/moved/token");
1016
+ }
1017
+ char moved[256];
1018
+ snprintf(moved, sizeof(moved), "HTTP/1.1 %d Moved\r\nLocation: %s\r\n"
1019
+ "Content-Length: 0\r\nConnection: close\r\n\r\n", status, r->location);
1020
+
1021
+ pid_t origin = redirect_serve(fd, beyond[1], count[1], moved);
1022
+ pid_t other = redirect_serve(otherfd, elsewhere[1], -1, NULL);
1023
+ close(fd);
1024
+ close(otherfd);
1025
+ close(beyond[1]);
1026
+ close(elsewhere[1]);
1027
+ close(count[1]);
1028
+
1029
+ snprintf(r->base, sizeof(r->base), "http://127.0.0.1:%d/api", port);
1030
+ voxgig_value* exchange = cmap(2, "active", v_bool(true), "refresh", v_str("REFRESH01"));
1031
+ if (NULL != redirect) setp(exchange, "redirect", v_str(redirect));
1032
+ ProjectNameSDK* sdk = projectname_sdk_new(cmap(2,
1033
+ "base", v_str(r->base),
1034
+ "feature", cmap(1, "secrets", cmap(3,
1035
+ "active", v_bool(true),
1036
+ "name", v_str("refresh_token"),
1037
+ "exchange", exchange))), NULL);
1038
+ r->res = direct(sdk);
1039
+
1040
+ kill(origin, SIGTERM);
1041
+ kill(other, SIGTERM);
1042
+ waitpid(origin, NULL, 0);
1043
+ waitpid(other, NULL, 0);
1044
+
1045
+ char counted[64];
1046
+ r->reached = redirect_lines(elsewhere[0], r->away, sizeof(r->away));
1047
+ r->after = redirect_lines(beyond[0], r->again, sizeof(r->again));
1048
+ r->exchanged = redirect_lines(count[0], counted, sizeof(counted));
1049
+ close(elsewhere[0]);
1050
+ close(beyond[0]);
1051
+ close(count[0]);
1052
+ }
1053
+
1054
+ static int redirect_refused_with(RedirectRun* r, int status) {
1055
+ char want[160];
1056
+ snprintf(want, sizeof(want), "token exchange failed: %d from %s/auth/token", status, r->base);
1057
+ return !res_ok(r->res) && NULL != strstr(res_err(r->res), want);
1058
+ }
1059
+
882
1060
  // Real listeners and the SDK's own exchange transport: a stub follows nothing.
883
1061
  static void t_exchange_redirect_refused(void) {
884
- struct { int status; int away; } cases[] = { {307, 1}, {308, 1}, {302, 1}, {307, 0} };
885
- char labels[4][1024];
1062
+ struct { int status; int where; } cases[] =
1063
+ { {307, TO_AWAY}, {308, TO_AWAY}, {302, TO_AWAY}, {307, TO_MOVED} };
1064
+ char labels[4][1400];
886
1065
  int clean[4];
887
1066
  int refused = 0;
888
1067
  int followed = 0;
889
1068
  int ncases = (int)(sizeof(cases) / sizeof(cases[0]));
890
1069
 
891
1070
  for (int i = 0; i < ncases; i++) {
892
- int port = 0;
893
- int otherport = 0;
894
- int fd = redirect_listener(&port);
895
- int otherfd = redirect_listener(&otherport);
896
- int elsewhere[2];
897
- int beyond[2];
898
- CHECK(0 <= fd && 0 <= otherfd && 0 == pipe(elsewhere) && 0 == pipe(beyond),
899
- "redirect: loopback listeners");
900
-
901
- char moved[256];
902
- char location[128];
903
- if (cases[i].away) {
904
- snprintf(location, sizeof(location), "http://127.0.0.1:%d/api/auth/token", otherport);
905
- } else {
906
- snprintf(location, sizeof(location), "/api/moved/token");
907
- }
908
- snprintf(moved, sizeof(moved), "HTTP/1.1 %d Moved\r\nLocation: %s\r\n"
909
- "Content-Length: 0\r\nConnection: close\r\n\r\n", cases[i].status, location);
910
-
911
- pid_t origin = redirect_serve(fd, beyond[1], moved);
912
- pid_t other = redirect_serve(otherfd, elsewhere[1], NULL);
913
- close(fd);
914
- close(otherfd);
915
- close(beyond[1]);
916
- close(elsewhere[1]);
917
-
918
- char base[64];
919
- snprintf(base, sizeof(base), "http://127.0.0.1:%d/api", port);
920
- ProjectNameSDK* sdk = projectname_sdk_new(cmap(2,
921
- "base", v_str(base),
922
- "feature", cmap(1, "secrets", cmap(3,
923
- "active", v_bool(true),
924
- "name", v_str("refresh_token"),
925
- "exchange", cmap(2, "active", v_bool(true), "refresh", v_str("REFRESH01"))))), NULL);
926
- voxgig_value* res = direct(sdk);
927
-
928
- kill(origin, SIGTERM);
929
- kill(other, SIGTERM);
930
- waitpid(origin, NULL, 0);
931
- waitpid(other, NULL, 0);
932
-
933
- char away[256];
934
- char again[256];
935
- int reached = redirect_lines(elsewhere[0], away, sizeof(away));
936
- int after = redirect_lines(beyond[0], again, sizeof(again));
937
- close(elsewhere[0]);
938
- close(beyond[0]);
939
- followed += reached + after;
940
-
941
- char want[160];
942
- snprintf(want, sizeof(want), "token exchange failed: %d from %s/auth/token",
943
- cases[i].status, base);
944
- int failed = !res_ok(res) && NULL != strstr(res_err(res), want);
1071
+ RedirectRun r;
1072
+ redirect_run(cases[i].status, cases[i].where, NULL, &r);
1073
+ followed += r.reached + r.after;
1074
+
1075
+ int failed = redirect_refused_with(&r, cases[i].status);
945
1076
  refused += failed;
946
1077
 
947
1078
  snprintf(labels[i], sizeof(labels[i]),
948
1079
  "redirect %d to %s: other origin got [%s], origin got [%s], ended: %s",
949
- cases[i].status, location, away, again, res_err(res));
950
- clean[i] = failed && 0 == reached && 0 == after;
1080
+ cases[i].status, r.location, r.away, r.again, res_err(r.res));
1081
+ clean[i] = failed && 0 == r.reached && 0 == r.after;
951
1082
  }
952
1083
 
953
1084
  printf("secrets: exchange refused %d redirect(s), %d followed\n", refused, followed);
@@ -956,20 +1087,133 @@ static void t_exchange_redirect_refused(void) {
956
1087
  CHECK_INT_EQ(followed, 0, "redirect: nothing followed a refused exchange");
957
1088
  }
958
1089
 
1090
+ // Each case names the target the exchange is asked again at, or NULL when
1091
+ // refused; a Location of a query alone keeps the path that answered.
1092
+ static void t_exchange_redirect_same_origin(void) {
1093
+ struct { int status; int where; const char* next; } cases[] = {
1094
+ {307, TO_MOVED, "/api/moved/token"}, {308, TO_MOVED_ABSOLUTE, "/api/moved/token"},
1095
+ {307, TO_QUERY, "/api/auth/token?step=2"}, {307, TO_AWAY, NULL},
1096
+ {302, TO_MOVED, NULL}, {307, TO_SELF, NULL} };
1097
+ char labels[6][1400];
1098
+ int clean[6];
1099
+ int followed = 0;
1100
+ int refused = 0;
1101
+ int elsewhere = 0;
1102
+ int looped = 0;
1103
+ int ncases = (int)(sizeof(cases) / sizeof(cases[0]));
1104
+
1105
+ for (int i = 0; i < ncases; i++) {
1106
+ RedirectRun r;
1107
+ redirect_run(cases[i].status, cases[i].where, "same-origin", &r);
1108
+ elsewhere += r.reached;
1109
+
1110
+ if (NULL != cases[i].next) {
1111
+ char want[160];
1112
+ int wn = snprintf(want, sizeof(want), "POST %s {\"refresh_token\":\"REFRESH01\"}\n",
1113
+ cases[i].next);
1114
+ clean[i] = res_ok(r.res) && 2 == r.after && 0 == strncmp(r.again, want, (size_t)wn);
1115
+ followed += clean[i];
1116
+ } else {
1117
+ clean[i] = 0 == r.after && redirect_refused_with(&r, cases[i].status);
1118
+ refused += clean[i];
1119
+ }
1120
+ if (TO_SELF == cases[i].where) looped = r.exchanged;
1121
+
1122
+ snprintf(labels[i], sizeof(labels[i]),
1123
+ "same-origin %d to %s: other origin got [%s], origin got [%s], ended: %s",
1124
+ cases[i].status, r.location, r.away, r.again, res_err(r.res));
1125
+ }
1126
+
1127
+ printf("secrets: exchange opt-in followed %d redirect(s) on its own origin, "
1128
+ "refused %d, %d elsewhere\n", followed, refused, elsewhere);
1129
+ for (int i = 0; i < ncases; i++) CHECK(clean[i], labels[i]);
1130
+ CHECK_INT_EQ(followed, 3, "same-origin: every redirect on the origin followed");
1131
+ CHECK_INT_EQ(refused, 3, "same-origin: every other redirect refused");
1132
+ CHECK_INT_EQ(elsewhere, 0, "same-origin: the other origin got nothing");
1133
+ CHECK_INT_EQ(looped, 6, "same-origin: a redirect to itself is followed five times");
1134
+ }
1135
+
959
1136
  #else
960
1137
 
961
1138
  static void t_exchange_redirect_refused(void) {
962
1139
  printf("secrets: redirect skipped, this platform has no POSIX sockets\n");
963
1140
  }
964
1141
 
1142
+ static void t_exchange_redirect_same_origin(void) {
1143
+ printf("secrets: redirect skipped, this platform has no POSIX sockets\n");
1144
+ }
1145
+
965
1146
  #endif
966
1147
 
1148
+ // ---- auth off ----------------------------------------------------------------
1149
+
1150
+ // Off by the config where it declares no auth, else by `auth: null`: either
1151
+ // way the feature places no credential, buys no token and retries nothing.
1152
+ static bool carries(Rec* r, int i) {
1153
+ static const char* SECRETS[] = {"OFFKEY01", "OFFKEY02", "OFFREFRESH01", "ACCESS01", NULL};
1154
+ bool present = false;
1155
+ call_auth(r, i, &present);
1156
+ if (present) return true;
1157
+ voxgig_value* headers = getp(call_fetchdef(r, i), "headers");
1158
+ if (!voxgig_is_map(headers)) return false;
1159
+ voxgig_map* m = voxgig_as_map(headers);
1160
+ for (size_t k = 0; k < m->len; k++) {
1161
+ voxgig_value* v = m->entries[k].value;
1162
+ if (!voxgig_is_string(v)) continue;
1163
+ for (int s = 0; NULL != SECRETS[s]; s++) {
1164
+ if (NULL != strstr(voxgig_as_string(v), SECRETS[s])) return true;
1165
+ }
1166
+ }
1167
+ return false;
1168
+ }
1169
+
1170
+ static void t_auth_off(void) {
1171
+ bool declared = voxgig_is_map(getpath2(shared_config(), "options", "auth"));
1172
+ int sent = 0;
1173
+ int credentials = 0;
1174
+ int bought = 0;
1175
+
1176
+ for (int scenario = 0; scenario < 3; scenario++) {
1177
+ Rec* r = rec_new();
1178
+ voxgig_value* fopts = v_map();
1179
+ voxgig_value* extra = v_map();
1180
+ if (0 == scenario) {
1181
+ fopts = providers1(memory(NULL, "APIKEY", "OFFKEY01"));
1182
+ } else if (1 == scenario) {
1183
+ setp(extra, "apikey", v_str("OFFKEY02"));
1184
+ } else {
1185
+ r->script[0] = 401;
1186
+ fopts = exchange_opts(clist(1, memory(NULL, "APIKEY", "OFFREFRESH01")), v_undef());
1187
+ }
1188
+ if (declared) setp(extra, "auth", voxgig_new_null());
1189
+
1190
+ ProjectNameSDK* sdk = live(r, fopts, extra);
1191
+ direct(sdk);
1192
+
1193
+ CHECK_INT_EQ(r->api, 1, "auth off: one request, not retried");
1194
+ CHECK_INT_EQ(r->token, 0, "auth off: no token bought");
1195
+ CHECK(!carries(r, r->token), "auth off: no credential sent");
1196
+ CHECK_STR_EQ(feature_secrets_credential(sdk_feature_secrets(sdk)), "", "auth off: nothing resolved");
1197
+
1198
+ for (int i = 0; i < r->api + r->token; i++) {
1199
+ const char* url = get_str(voxgig_getelem(r->calls, v_int(i), voxgig_new_undef()), "url");
1200
+ if (NULL != url && NULL == strstr(url, r->tokenpath) && carries(r, i)) credentials++;
1201
+ }
1202
+ sent += r->api;
1203
+ bought += r->token;
1204
+ }
1205
+
1206
+ printf("secrets: auth off by %s: %d request(s), %d credential(s), %d token(s)\n",
1207
+ declared ? "option" : "config", sent, credentials, bought);
1208
+ }
1209
+
967
1210
  int main(void) {
968
1211
  // Read from the config, never from a probe: an SDK whose config declares no
969
1212
  // auth places no credential, and these tests assert on the one it places.
970
1213
  if (!voxgig_is_map(getpath2(shared_config(), "options", "auth"))) {
971
1214
  printf("SKIP [secrets]: this SDK's config declares no auth, so it places no credential\n");
972
- return 0;
1215
+ RUN(t_auth_off);
1216
+ TEST_SUMMARY("secrets");
973
1217
  }
974
1218
 
975
1219
  RUN(t_inactive_apikey_as_before);
@@ -977,6 +1221,7 @@ int main(void) {
977
1221
  RUN(t_apikey_wins_over_chain);
978
1222
  RUN(t_omitted_apikey_defers);
979
1223
  RUN(t_empty_apikey_defers);
1224
+ RUN(t_resolved_key_paired);
980
1225
  RUN(t_auth_null_suppresses);
981
1226
  RUN(t_custom_provider_verbatim);
982
1227
  RUN(t_miss_everywhere_no_header);
@@ -996,6 +1241,7 @@ int main(void) {
996
1241
  RUN(t_exchange_explicit_refresh_wins);
997
1242
  RUN(t_exchange_one_purchase_many_requests);
998
1243
  RUN(t_exchange_401_rebuys_and_retries);
1244
+ RUN(t_exchange_retry_paired);
999
1245
  RUN(t_exchange_retry_once_not_loop);
1000
1246
  RUN(t_exchange_other_status_not_expiry);
1001
1247
  RUN(t_exchange_statuses_configurable);
@@ -1008,6 +1254,9 @@ int main(void) {
1008
1254
  RUN(t_exchange_off_leaves_feature_as_before);
1009
1255
  RUN(t_exchange_test_mode_buys_nothing);
1010
1256
  RUN(t_exchange_redirect_refused);
1257
+ RUN(t_exchange_redirect_same_origin);
1258
+ RUN(t_exchange_redirect_query_supplied);
1259
+ RUN(t_auth_off);
1011
1260
 
1012
1261
  // The generator's runtime lane reads this line: a suite that RAN fewer
1013
1262
  // cases than it declares was trimmed, not passed.
@@ -432,6 +432,36 @@ static void test_cache_does_not_cache_non_2xx(void) {
432
432
  CHECK_INT_EQ(fh_track_int(f, "bypass"), 2, "cache non-2xx: 2 bypasses");
433
433
  }
434
434
 
435
+ static voxgig_value* rep_audio(void* ud, int64_t n, voxgig_value* fd, Context* ctx, PNError** e) {
436
+ voxgig_value* res = fh_response(200, v_undef(), cmap(1, "content-type", v_str("audio/wav")));
437
+ voxgig_list_push(voxgig_as_list((voxgig_value*)ud), res);
438
+ return res;
439
+ }
440
+
441
+ static Response* handed(FhOpResult r) { return NULL == r.ctx ? NULL : r.ctx->response; }
442
+
443
+ // The response's headers are the transport's own map; a rebuilt copy has another.
444
+ static bool handed_is(FhOpResult r, voxgig_value* sent, size_t i) {
445
+ voxgig_list* l = voxgig_as_list(sent);
446
+ return NULL != handed(r) && i < l->len && handed(r)->headers == getp(l->items[i], "headers");
447
+ }
448
+
449
+ static void test_cache_passes_other_than_json_by_unread(void) {
450
+ if (!fh_present("cache")) return;
451
+ voxgig_value* calls;
452
+ voxgig_value* sent = voxgig_new_list();
453
+ Fetcher* srv = fh_recorder(rep_audio, sent, &calls);
454
+ Feature* f = feature_cache_new();
455
+ FhFeat feats[] = {{f, v_undef()}};
456
+ FhHarness h = fh_make(srv, feats, 1);
457
+ FhOpResult a = fh_op(&h, op_load_path("/w/1/audio"));
458
+ FhOpResult b = fh_op(&h, op_load_path("/w/1/audio"));
459
+ CHECK_INT_EQ(rec_count(calls), 2, "cache audio: the body was stored");
460
+ CHECK(handed_is(a, sent, 0), "cache audio: the cache handed on a copy it had read");
461
+ CHECK(handed_is(b, sent, 1), "cache audio: the cache handed on a copy it had read");
462
+ CHECK_INT_EQ(fh_track_int(f, "bypass"), 2, "cache audio: 2 bypasses");
463
+ }
464
+
435
465
  static void test_cache_refetches_after_ttl(void) {
436
466
  if (!fh_present("cache")) return;
437
467
  FhClock clock = fh_clock_new();
@@ -1146,6 +1176,7 @@ int main(void) {
1146
1176
  RUN(test_cache_serves_repeated_read_from_cache);
1147
1177
  RUN(test_cache_does_not_cache_non_get);
1148
1178
  RUN(test_cache_does_not_cache_non_2xx);
1179
+ RUN(test_cache_passes_other_than_json_by_unread);
1149
1180
  RUN(test_cache_refetches_after_ttl);
1150
1181
  RUN(test_cache_evicts_oldest_past_max);
1151
1182
  RUN(test_cache_inactive_does_not_wrap);