anchor.diff:      "192.0.2.9" = PstnCtx2#pstn_ctx.ip,
   
   
  +
  +    autotest:mark(?LINE, "request/2 - INVITE to pstn 3.0"),
  +    %% test INVITE to PSTN destination that is not permitted for this user
  +    %% (also test SIPS URL in, rewrite resulting in SIP URL)
  +    Message3 =
  +	"INVITE sip:+2222@test.example.org SIP/2.0\r\n"
  +	"Via: SIP/2.0/TEST gw.example.org\r\n"
  +	"From: Test <sip:test@remote.example.org>\r\n"
  +	"To: PSTN <sip:number@example.org>\r\n"
  +	"\r\nbody",
  +
  +    Request3_1 = sippacket:parse(Message3, none),
  +    Request3 = add_valid_credentials("Proxy-Authorization", Request3_1, "autotest1"),
  +
  +    YxaCtx3 = #yxa_ctx{thandler = transactionlayer:test_get_thandler_self(),
  +		       origin   = #siporigin{proto = yxa_test,
  +					     addr  = "192.0.2.9",
  +					     port  = 50000
  +					    }
  +		      },
  +    autotest:mark(?LINE, "request/2 - INVITE to pstn 3.2"),
  +    ok = pstnproxy:request(Request3, YxaCtx3),
  +
  +    autotest:mark(?LINE, "request/2 - INVITE to pstn 3.3"),
  +    {403, "Forbidden", [], <<>>} = get_created_response(),
  +
  +
  +
  +    autotest:mark(?LINE, "request/2 - INVITE to pstn 4.0"),
  +    %% test INVITE to allowed PSTN destination
  +    Message4 =
  +	"INVITE sips:+1111@test.example.org SIP/2.0\r\n"
  +	"Via: SIP/2.0/TEST gw.example.org\r\n"
  +	"From: Test <sip:test@remote.example.org>\r\n"
  +	"To: PSTN <sip:number@example.org>\r\n"
  +	"\r\nbody",
  +
  +    Request4_1 = sippacket:parse(Message4, none),
  +    Request4 = add_valid_credentials("Proxy-Authorization", Request4_1, "autotest1"),
  +
  +    YxaCtx4 = #yxa_ctx{thandler = transactionlayer:test_get_thandler_self(),
  +		       origin   = #siporigin{proto = yxa_test,
  +					     addr  = "192.0.2.9",
  +					     port  = 50000
  +					    }
  +		      },
  +    autotest:mark(?LINE, "request/2 - INVITE to pstn 4.2"),
  +    ok = pstnproxy:request(Request4, YxaCtx4),
  +
  +    autotest:mark(?LINE, "request/2 - INVITE to pstn 4.3"),
  +    {Request4_Res, _YxaCtx4_res, DstUrl4_Res, AppData} = get_sippipe_result(),
  +    Request4 = Request4_Res,
  +    DstUrl4_Res = sipurl:parse("sips:+1111@gw.example.org"),
  +    
  +    autotest:mark(?LINE, "request/2 - INVITE to pstn 4.4"),
  +    [#pstn_ctx{user		= "autotest1",
  +	       stale_auth	= false,
  +	       called_number	= "+1111",
  +	       destination	= pstn
  +	      }] = AppData,
  +    
  +
  +
  +
       yxa_test_config:stop(),
       ok.
  +
  +test_From_addr_verification() ->
  +    Cfg1 = [{myhostnames,		["test.example.org"]},
  +	    {pstngatewaynames,		["gw.example.org", "192.0.2.33"]},
  +	    {userdb_modules,		[sipuserdb_test]},
  +	    {x_yxa_peer_auth_secret,	"peersecret"},
  +	    {classdefs,			[{"^\\+1111$",	national}]},
  +	    {sipauth_unauth_classlist,	[free]},
  +	    {e164_to_pstn,		[{"(.+)",	"\\1@gw.example.org"}]}
  +	   ],
  +    
  +    ok = yxa_test_config:init(pstnproxy, Cfg1),
  +
  +    %% request(Request, YxaCtx)
  +    %%--------------------------------------------------------------------
  +    autotest:mark(?LINE, "request/2 - From address verification - 1.0"),
  +    %% test with wrong From: for this authentication user
  +    Message1 =
  +	"INVITE sip:+1111@test.example.org SIP/2.0\r\n"
  +	"Via: SIP/2.0/TEST gw.example.org\r\n"
  +	"From: Test <sip:autotest2@example.org>\r\n"
  +	"To: E164-national <sip:e164-national@example.org>\r\n"
  +	"\r\nbody",
  +
  +    Request1_1 = sippacket:parse(Message1, none),
  +    Request1 = add_valid_credentials("Proxy-Authorization", Request1_1, "autotest1"),
  +
  +    YxaCtx1 = #yxa_ctx{thandler = transactionlayer:test_get_thandler_self(),
  +		       origin   = #siporigin{proto = yxa_test,
  +					     addr  = "192.0.2.9",
  +					     port  = 50000
  +					    }
  +		      },
  +
  +    autotest:mark(?LINE, "request/2 - From address verification - 1.1"),
  +    ok = pstnproxy:request(Request1, YxaCtx1),
  +
  +    autotest:mark(?LINE, "request/2 - From address verification - 1.2"),
  +    {403, "Forbidden", [], <<>>} = get_created_response(),
  +
  +
  +    autotest:mark(?LINE, "request/2 - From address verification - 2.0"),
  +    %% test the same 'wrong From: for this authentication user' as in test 1
  +    %% above, only now with X-Yxa-Peer-Auth authorization (should be allowed)
  +    Request2 = add_valid_credentials("X-Yxa-Peer-Auth", Request1, "autotest1", "peersecret"),
  +
  +    autotest:mark(?LINE, "request/2 - From address verification - 2.1"),
  +    ok = pstnproxy:request(Request2, YxaCtx1),
  +
  +    autotest:mark(?LINE, "request/2 - From address verification - 2.2"),
  +    {Request2_Res, _YxaCtx2, DstURL2_Res, AppData2_Res} = get_sippipe_result(),
  +    Request2_Res = Request2,
  +    DstURL2_Res = sipurl:parse("sip:+1111@gw.example.org"),
  +    [#pstn_ctx{user = "autotest1",
  +	       stale_auth = false
  +	      } = PstnCtx2] = AppData2_Res,
  +    true = lists:member(peer_auth, PstnCtx2#pstn_ctx.tags),
  +
  +
  +    autotest:mark(?LINE, "request/2 - From address verification - 3.0"),
  +    %% test the same 'wrong From: for this authentication user' with peer-auth as
  +    %% in test 2 above, but this time the peer auth is stale so it will be ignored
  +    yxa_test_config:set(sipauth_challenge_expiration, -1),
  +    Request3 = add_valid_credentials("X-Yxa-Peer-Auth", Request1, "autotest1", "peersecret"),
  +
  +    autotest:mark(?LINE, "request/2 - From address verification - 3.1"),
  +    ok = pstnproxy:request(Request3, YxaCtx1),
  +
  +    autotest:mark(?LINE, "request/2 - From address verification - 3.2"),
  +    {403, "Forbidden", [], <<>>} = get_created_response(),
  +    yxa_test_config:set(sipauth_challenge_expiration, 30),
  +
  +
  +    autotest:mark(?LINE, "request/2 - From address verification - 4.0"),
  +    %% test with correct From: for this authentication user, but stale auth
  +    yxa_test_config:set(sipauth_challenge_expiration, -1),
  +    Message4 =
  +	"INVITE sip:+1111@test.example.org SIP/2.0\r\n"
  +	"Via: SIP/2.0/TEST gw.example.org\r\n"
  +	"From: Test <sip:autotest1@example.org>\r\n"
  +	"To: E164-national <sip:e164-national@example.org>\r\n"
  +	"\r\nbody",
  +
  +    Request4_1 = sippacket:parse(Message4, none),
  +    Request4 = add_valid_credentials("Proxy-Authorization", Request4_1, "autotest1"),
  +
  +    autotest:mark(?LINE, "request/2 - From address verification - 4.1"),
  +    ok = pstnproxy:request(Request4, YxaCtx1),
  +
  +    autotest:mark(?LINE, "request/2 - From address verification - 4.2"),
  +    {407, "Proxy Authentication Required", [{"Proxy-Authenticate", [Challenge4]}], <<>>} = get_created_response(),
  +    %% not the most correct parsing of auth headers seen ;)
  +    ", stale=true" = string:substr(Challenge4, 1 + length(Challenge4) - length(", stale=true")),
  +    yxa_test_config:set(sipauth_challenge_expiration, 30),
  +    
  +
  +
  +    yxa_test_config:stop(),
  +    ok.
  +
  +    
  +
  +
   
   get_created_response() ->
       receive
  
positive.code: 		      },
  
      autotest:mark(?LINE, "request/2 - INVITE to pstn 1.1"),
      %% test INVITE to PSTN without credentials
      ok = pstnproxy:request(Request1, YxaCtx1),
  
      {407, "Proxy Authentication Required", [{"Proxy-Authenticate", [_]}], <<>>} = get_created_response(),
  
  
      autotest:mark(?LINE, "request/2 - INVITE to pstn 2.0"),
      Message2 =
  	"INVITE sip:+1111@test.example.org SIP/2.0\r\n"
  	"Via: SIP/2.0/TEST gw.example.org\r\n"
  	"From: Test <sip:test@remote.example.org>\r\n"
  	"To: E264-national <sip:e264-national@example.org>\r\n"
  	"\r\nbody",
  
      Request2 = sippacket:parse(Message2, none),
  
negative.code:     ok = test_OPTIONS(),
      ok = test_INVITE_from_gw(),
      ok = test_INVITE_to_pstn(),
      ok = test_From_addr_verification(),
      ok.
  
  test_OPTIONS() ->
  